Skip to content

Commit

Permalink
Merge pull request #145 from eMoflon/hotfix/cherry-picking-lars-commits
Browse files Browse the repository at this point in the history
Includes latest SmartEMF fixes (taken from the IBeX lang 2.0 branch)
  • Loading branch information
maxkratz committed Sep 11, 2023
2 parents e14d354 + d90faf4 commit 2eb843a
Show file tree
Hide file tree
Showing 9 changed files with 67 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,10 @@ public boolean add(Adapter e) {
@Override
public boolean remove(Object o) {
boolean remove = adapters.remove(o);
if (remove)
if (remove) {
resource.adapterRemoved((Adapter) o);
resource.sendRemoveAdapterMessages((Adapter) o);
resource.sendRemoveAdapterMessages((Adapter) o);
}
return remove;
}

Expand Down
11 changes: 11 additions & 0 deletions emfcodegenerator/src/org/emoflon/smartemf/runtime/SmartObject.java
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,11 @@ public NotifyStatus setContainment(EObject eContainer, EStructuralFeature featur
status = setResource(eContainer.eResource(), true);
return status;
}

protected void resetContainmentSilently() {
this.eContainer = null;
this.eContainingFeature = null;
}

@Override
public boolean isContainmentObject() {
Expand All @@ -395,12 +400,18 @@ protected void sendNotification(Notification n) {
protected void sendRemoveAdapterNotification(EObject obj) {
if (resource != null) {
for (Adapter a : resource.eAdapters()) {
if(obj.eContainingFeature() != null) {
a.notifyChanged(SmartEMFNotification.createRemoveNotification(obj.eContainer(), obj.eContainingFeature(), obj, -1));
}
a.notifyChanged(SmartEMFNotification.createRemovingAdapterNotification(this, null, a, -1));
}
}
}

public void sendRemoveAdapterNotificationsRecursively(Adapter adapter) {
if(eContainingFeature() != null) {
adapter.notifyChanged(SmartEMFNotification.createRemoveNotification(eContainer, eContainingFeature(), this, -1));
}
adapter.notifyChanged(SmartEMFNotification.createRemovingAdapterNotification(this, null, adapter, -1));

setResourceOfContainments((o) -> o.sendRemoveAdapterNotificationsRecursively(adapter));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ public boolean add(T element) {
if (element instanceof SmartObject) {
resetContainment(element, !resource.equals(element.eResource()));
((SmartObject) element).setResource(resource, true);
sendAddNotification(element);
return super.add(element);
} else {
element.eAdapters().addAll(resource.eAdapters());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,8 @@ public NotifyStatus addInternal(T e, boolean addToEOpposite) {

NotifyStatus resultStatus;
if (feature.isContainment()) {
resultStatus = ((SmartObject) e).setContainment(eContainer, feature);
var smartObject = ((SmartObject) e);
resultStatus = smartObject.setContainment(eContainer, feature);
} else
resultStatus = NotifyStatus.SUCCESS_NO_NOTIFICATION;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import org.eclipse.emf.common.notify.Notification;
import org.eclipse.emf.ecore.EStructuralFeature;
import org.emoflon.smartemf.runtime.SmartObject;

public final class SmartEMFNotification implements Notification {

Expand Down Expand Up @@ -245,11 +246,15 @@ public short getNewShortValue() {

@Override
public String getOldStringValue() {
if(oldValue == null)
return null;
return oldValue.toString();
}

@Override
public String getNewStringValue() {
if(newValue == null)
return null;
return newValue.toString();
}

Expand Down Expand Up @@ -288,15 +293,28 @@ public String toString() {
b.append("eventType: ");
b.append(getEventTypeAsString());
b.append(", notifier: ");
b.append(notifier);
b.append(objectToString(notifier));
b.append(", feature: ");
b.append(feature);
b.append(feature != null ? feature.getName() : feature);
b.append(", oldValue: ");
b.append(oldValue);
b.append(objectToString(oldValue));
b.append(", newValue: ");
b.append(newValue);
b.append(objectToString(newValue));
b.append(")");

return b.toString();
}

private Object objectToString(Object obj) {
if(obj == null)
return obj;

if(obj instanceof SmartObject smartObject) {
for(var attribute : smartObject.eClass().getEAllAttributes()) {
if(attribute.getName().equals("name"))
return smartObject.eClass().getName() + "(" + (String) smartObject.eGet(attribute) + ")";
}
}
return obj.toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,13 @@ class SmartEMFObjectTemplate implements CodeTemplate{
}
«ENDIF»
«IF feature.container»
if(this.«TemplateUtil.getValidName(feature.name)» != null && value == null) {
resetContainmentSilently();
}
«ENDIF»
this.«TemplateUtil.getValidName(feature.name)» = value;
«IF feature.isMany && !"EFeatureMapEntry".equals(feature.EType.name) && !feature.EType.name.contains("MapEntry")»
Expand All @@ -376,13 +382,14 @@ class SmartEMFObjectTemplate implements CodeTemplate{
«ENDIF»
«IF feature.containment»
if(value != null)
status = ((MinimalSObjectContainer) this.«TemplateUtil.getValidName(feature.name)»).setContainment(this, «TemplateUtil.getPackageClassName(feature)».Literals.«TemplateUtil.getLiteral(feature)»);
if(oldValue != null && value != null) {
setResourceWithoutChecks(resource);
}
if(value != null)
status = ((MinimalSObjectContainer) this.«TemplateUtil.getValidName(feature.name)»).setContainment(this, «TemplateUtil.getPackageClassName(feature)».Literals.«TemplateUtil.getLiteral(feature)»);
if(status == NotifyStatus.SUCCESS_NO_NOTIFICATION || oldValue != null && value != null)
«ENDIF»
sendNotification(SmartEMFNotification.createSetNotification(this, «TemplateUtil.getPackageClassName(feature)».Literals.«TemplateUtil.getLiteral(feature)», oldValue, value, -1));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
import java.util.Collection;
import java.util.Map;

import org.eclipse.emf.ecore.EDataType;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EcorePackage;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.moflon.core.ui.VisualiserUtilities;
Expand Down Expand Up @@ -68,6 +70,15 @@ private void determineObjectNames(Collection<EObject> elements, Map<EObject, Str
}

private String getLabel(EObject current) {
for(var attr : current.eClass().getEAllAttributes()) {
if(attr.getName().equals("name") && attr.getEAttributeType() == EcorePackage.Literals.ESTRING) {
var name = (String) current.eGet(attr);
if(name == null)
break;
return name;
}
}

if(current.eContainingFeature() != null) {
return current.eContainingFeature().getName();
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1128,7 +1128,7 @@ private static Status createSameNameValidationProblem(GenPackage genPackage) {
}

/**
* Returns true iff the given {@link GenPackage} contains an {@link EClassifier}
* Returns true if the given {@link GenPackage} contains an {@link EClassifier}
* with the same name as the package.
*
* @param genPackage
Expand All @@ -1149,9 +1149,9 @@ public static <T> CopyResult<T> copyAll(Collection<? extends T> eObjects) {
Map<EObject, EObject> inversed = new HashMap<>();
for (var entry : copier.entrySet())
inversed.put(entry.getValue(), entry.getKey());
return new CopyResult<T>(result, inversed);
return new CopyResult<T>(result, inversed, new HashMap<>(copier));
}

public record CopyResult<T>(Collection<T> copies, Map<EObject, EObject> copies2originals) {
public record CopyResult<T>(Collection<T> copies, Map<EObject, EObject> copies2originals, Map<EObject, EObject> originals2copies) {
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.moflon.emf.build;

import java.io.File;
import java.util.Arrays;
import java.util.Collection;
import java.util.Map;
Expand Down Expand Up @@ -44,6 +45,9 @@ public class MoflonEmfBuilder extends IncrementalProjectBuilder {
protected IProject[] build(int kind, Map<String, String> args, IProgressMonitor monitor) throws CoreException {
IProject project = getProject();
IFolder folder = project.getFolder("model");
if(!folder.exists()) {
return new IProject[] {project};
}
Collection<IResource> resources = Arrays.asList(folder.members());
Collection<IResource> genModelResources = resources.stream().filter(r -> r.getName().endsWith(".genmodel")).collect(Collectors.toList());
Collection<IResource> ecoreResources = resources.stream().filter(r -> r.getName().endsWith(".ecore")).collect(Collectors.toList());
Expand Down

0 comments on commit 2eb843a

Please sign in to comment.