Skip to content

Commit

Permalink
add support for models that reference other models
Browse files Browse the repository at this point in the history
contributes to #59

Signed-off-by: Didier Vojtisek <[email protected]>
  • Loading branch information
dvojtise committed Sep 24, 2021
1 parent ab5bceb commit 71e65ff
Show file tree
Hide file tree
Showing 6 changed files with 182 additions and 127 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -172,9 +172,16 @@ public class BuilderTemplates {
"package ${package.name};\n"+
"import java.io.Serializable;\n" +
"import java.lang.reflect.Method;\n" +
"import java.util.ArrayList;\n" +
"import java.util.Collection;\n" +
"import java.util.HashSet;\n" +
"import java.util.Map;" +
"import java.lang.reflect.InvocationTargetException;\n" +
"import org.eclipse.emf.common.util.TreeIterator;\n" +
"import org.eclipse.emf.ecore.EObject;\n" +
"import org.eclipse.emf.ecore.EStructuralFeature.Setting;\n" +
"import org.eclipse.emf.ecore.resource.Resource;\n" +
"import org.eclipse.emf.ecore.util.EcoreUtil;\n" +
"import org.eclipse.gemoc.execution.concurrent.ccsljavaengine.extensions.k3.rtd.modelstate.k3ModelState.ElementState;\n" +
"import org.eclipse.gemoc.execution.concurrent.ccsljavaengine.extensions.k3.rtd.modelstate.k3ModelState.K3ModelState;\n" +
"import org.eclipse.gemoc.execution.concurrent.ccsljavaengine.extensions.k3.rtd.modelstate.k3ModelState.K3ModelStateFactory;\n"+
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -241,26 +241,36 @@ private void createLanguageSpecificModelStateHelper(String value, IProject proje

sbContent.append("\tpublic K3ModelState getK3ModelState(EObject model) {\n"
+ "\t\tK3ModelState res = theFactory.createK3ModelState();\n" + "\n"
+ "\t\tTreeIterator<EObject> allContentIt = model.eAllContents();\n"
+ "\t\twhile (allContentIt.hasNext()) {\n" + "\t\t\tEObject elem = allContentIt.next();\n" + "\n");

sbContent.append("\t\t\tClass<?> clazz =null;\n");
+ "\t\t // consider indirectly referenced models (ugly and probably not efficient)\n"
+ " ArrayList<EObject> allElements = new ArrayList<EObject>();\n"
+ " model.eAllContents().forEachRemaining(x -> allElements.add(x));\n"
+ " Map<EObject, Collection<Setting>> f = EcoreUtil.CrossReferencer.find(allElements);\n"
+ " HashSet<Resource> consideredResources = new HashSet<Resource>();\n"
+ " consideredResources.add(model.eResource());\n"
+ " f.keySet().forEach(eo -> consideredResources.add(eo.eResource()));\n"
+ " \n"
+ " for(Resource resource : consideredResources) {\n"
+ " TreeIterator<EObject> allContentIt = resource.getAllContents();\n"
+ " while (allContentIt.hasNext()) {\n"
+ " EObject elem = allContentIt.next();\n" + "\n");

sbContent.append(" Class<?> clazz =null;\n");
for (String aspect : setAspectsWithRTDs) {
sbContent.append("\t\t\tclazz = K3DslHelper.getTarget(" + aspect + ".class);\n"
+ "\t\t\tif (clazz.isInstance(elem)) {\n"
+ "\t\t\t\tElementState elemState = theFactory.createElementState();\n"
+ "\t\t\t\telemState.setModelElement(elem);\n"
+ "\t\t\t\tres.getOwnedElementstates().add(elemState);\n");
sbContent.append(" clazz = K3DslHelper.getTarget(" + aspect + ".class);\n"
+ " if (clazz.isInstance(elem)) {\n"
+ " ElementState elemState = theFactory.createElementState();\n"
+ " elemState.setModelElement(elem);\n"
+ " res.getOwnedElementstates().add(elemState);\n");
int i = 0;
for (String property : mapAspectProperties.get(aspect)) {
sbContent.append("\t\t\t\tAttributeNameToValue n2v" + i + " = new AttributeNameToValue(\"" + property
sbContent.append(" AttributeNameToValue n2v" + i + " = new AttributeNameToValue(\"" + property
+ "\", " + languageToUpperFirst + "RTDAccessor.get" + toUpperFirst(property) + "(("+ mapAspectizedClass.get(aspect)+")elem));\n"
+ "\t\t\t\telemState.getSavedRTDs().add(n2v" + i + ");\n");
+ " elemState.getSavedRTDs().add(n2v" + i + ");\n");
i++;
}
sbContent.append("\t\t\t}\n");
sbContent.append(" }\n");
}
sbContent.append("\t\t}\n\t\treturn res;\n\t\t}");
sbContent.append("\t\t\t}\n\t\t}\n\t\treturn res;\n\t}\n");

fileContent = fileContent.replaceAll(Pattern.quote("${saveAndRestoreMethod}"), sbContent.toString());
fileContent = fileContent.replaceAll(Pattern.quote("${extraImports}"), sbExtraImport.toString());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ Automatic-Module-Name: org.eclipse.gemoc.example.moccmlsigpml
Bundle-SymbolicName: org.eclipse.gemoc.example.moccmlsigpml;singleton:=true
Export-Package: sigpml.xdsml.api.impl
Bundle-Version: 2.3.0.qualifier
Bundle-Name: moccmlsigpml Xdsml
Bundle-Name: moccmlsigpml Xdsml
Bundle-ClassPath: .
Require-Bundle: org.eclipse.emf.ecore.xmi;bundle-version="2.8.0";visibility:="reexport",
org.eclipse.gemoc.ultimateplotter;bundle-version="1.0.0";visibility:="reexport",
Expand Down Expand Up @@ -38,4 +38,4 @@ Bundle-ActivationPolicy: lazy
Bundle-ManifestVersion: 2
Bundle-Activator: org.eclipse.gemoc.example.moccmlsigpml.Activator
Bundle-RequiredExecutionEnvironment: JavaSE-1.8
Bundle-Vendor: Eclipse GEMOC Project
Bundle-Vendor: Eclipse GEMOC Project
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,15 @@
package sigpml.xdsml.api.impl;
import java.io.Serializable;
import java.lang.reflect.Method;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.Map;import java.lang.reflect.InvocationTargetException;
import org.eclipse.emf.common.util.TreeIterator;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EStructuralFeature.Setting;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.util.EcoreUtil;
import org.eclipse.gemoc.execution.concurrent.ccsljavaengine.extensions.k3.rtd.modelstate.k3ModelState.ElementState;
import org.eclipse.gemoc.execution.concurrent.ccsljavaengine.extensions.k3.rtd.modelstate.k3ModelState.K3ModelState;
import org.eclipse.gemoc.execution.concurrent.ccsljavaengine.extensions.k3.rtd.modelstate.k3ModelState.K3ModelStateFactory;
Expand Down Expand Up @@ -39,102 +45,116 @@ public boolean equals(Object obj) {
return false;
}
return true;
} }
K3ModelStateFactory theFactory = K3ModelStateFactory.eINSTANCE;
}

}
K3ModelStateFactory theFactory = K3ModelStateFactory.eINSTANCE;
public K3ModelState getK3ModelState(EObject model) {
K3ModelState res = theFactory.createK3ModelState();

TreeIterator<EObject> allContentIt = model.eAllContents();
while (allContentIt.hasNext()) {
EObject elem = allContentIt.next();
// consider indirectly referenced models (ugly and probably not efficient)
ArrayList<EObject> allElements = new ArrayList<EObject>();
model.eAllContents().forEachRemaining(x -> allElements.add(x));
Map<EObject, Collection<Setting>> f = EcoreUtil.CrossReferencer.find(allElements);
HashSet<Resource> consideredResources = new HashSet<Resource>();
consideredResources.add(model.eResource());
f.keySet().forEach(eo -> consideredResources.add(eo.eResource()));

for(Resource resource : consideredResources) {
TreeIterator<EObject> allContentIt = resource.getAllContents();
while (allContentIt.hasNext()) {
EObject elem = allContentIt.next();

Class<?> clazz =null;
clazz = K3DslHelper.getTarget(org.eclipse.gemoc.example.moccmlsigpml.k3dsa.PlaceAspect.class);
if (clazz.isInstance(elem)) {
ElementState elemState = theFactory.createElementState();
elemState.setModelElement(elem);
res.getOwnedElementstates().add(elemState);
AttributeNameToValue n2v0 = new AttributeNameToValue("fifo", SigPMLRTDAccessor.getFifo((org.eclipse.gemoc.example.moccmlsigpml.model.sigpml.Place)elem));
elemState.getSavedRTDs().add(n2v0);
AttributeNameToValue n2v1 = new AttributeNameToValue("currentSize", SigPMLRTDAccessor.getCurrentSize((org.eclipse.gemoc.example.moccmlsigpml.model.sigpml.Place)elem));
elemState.getSavedRTDs().add(n2v1);
AttributeNameToValue n2v2 = new AttributeNameToValue("isInitialized", SigPMLRTDAccessor.getIsInitialized((org.eclipse.gemoc.example.moccmlsigpml.model.sigpml.Place)elem));
elemState.getSavedRTDs().add(n2v2);
}
clazz = K3DslHelper.getTarget(org.eclipse.gemoc.example.moccmlsigpml.k3dsa.SystemAspect.class);
if (clazz.isInstance(elem)) {
ElementState elemState = theFactory.createElementState();
elemState.setModelElement(elem);
res.getOwnedElementstates().add(elemState);
AttributeNameToValue n2v0 = new AttributeNameToValue("sharedMemory", SigPMLRTDAccessor.getSharedMemory((org.eclipse.gemoc.example.moccmlsigpml.model.sigpml.System)elem));
elemState.getSavedRTDs().add(n2v0);
}
clazz = K3DslHelper.getTarget(org.eclipse.gemoc.example.moccmlsigpml.k3dsa.OutputPortAspect.class);
if (clazz.isInstance(elem)) {
ElementState elemState = theFactory.createElementState();
elemState.setModelElement(elem);
res.getOwnedElementstates().add(elemState);
AttributeNameToValue n2v0 = new AttributeNameToValue("sizeWritten", SigPMLRTDAccessor.getSizeWritten((org.eclipse.gemoc.example.moccmlsigpml.model.sigpml.OutputPort)elem));
elemState.getSavedRTDs().add(n2v0);
}
clazz = K3DslHelper.getTarget(org.eclipse.gemoc.example.moccmlsigpml.k3dsa.AgentAspect.class);
if (clazz.isInstance(elem)) {
ElementState elemState = theFactory.createElementState();
elemState.setModelElement(elem);
res.getOwnedElementstates().add(elemState);
AttributeNameToValue n2v0 = new AttributeNameToValue("plotter", SigPMLRTDAccessor.getPlotter((org.eclipse.gemoc.example.moccmlsigpml.model.sigpml.Agent)elem));
elemState.getSavedRTDs().add(n2v0);
AttributeNameToValue n2v1 = new AttributeNameToValue("frame", SigPMLRTDAccessor.getFrame((org.eclipse.gemoc.example.moccmlsigpml.model.sigpml.Agent)elem));
elemState.getSavedRTDs().add(n2v1);
AttributeNameToValue n2v2 = new AttributeNameToValue("figure", SigPMLRTDAccessor.getFigure((org.eclipse.gemoc.example.moccmlsigpml.model.sigpml.Agent)elem));
elemState.getSavedRTDs().add(n2v2);
AttributeNameToValue n2v3 = new AttributeNameToValue("hasBeenStopped", SigPMLRTDAccessor.getHasBeenStopped((org.eclipse.gemoc.example.moccmlsigpml.model.sigpml.Agent)elem));
elemState.getSavedRTDs().add(n2v3);
AttributeNameToValue n2v4 = new AttributeNameToValue("currentExecCycle", SigPMLRTDAccessor.getCurrentExecCycle((org.eclipse.gemoc.example.moccmlsigpml.model.sigpml.Agent)elem));
elemState.getSavedRTDs().add(n2v4);
AttributeNameToValue n2v5 = new AttributeNameToValue("isCurrentlyExecuting", SigPMLRTDAccessor.getIsCurrentlyExecuting((org.eclipse.gemoc.example.moccmlsigpml.model.sigpml.Agent)elem));
elemState.getSavedRTDs().add(n2v5);
}
clazz = K3DslHelper.getTarget(org.eclipse.gemoc.example.moccmlsigpml.k3dsa.InputPortAspect.class);
if (clazz.isInstance(elem)) {
ElementState elemState = theFactory.createElementState();
elemState.setModelElement(elem);
res.getOwnedElementstates().add(elemState);
AttributeNameToValue n2v0 = new AttributeNameToValue("sizeToread", SigPMLRTDAccessor.getSizeToread((org.eclipse.gemoc.example.moccmlsigpml.model.sigpml.InputPort)elem));
elemState.getSavedRTDs().add(n2v0);
}
clazz = K3DslHelper.getTarget(org.eclipse.gemoc.example.moccmlsigpml.k3dsa.HWComputationalResourceAspect.class);
if (clazz.isInstance(elem)) {
ElementState elemState = theFactory.createElementState();
elemState.setModelElement(elem);
res.getOwnedElementstates().add(elemState);
AttributeNameToValue n2v0 = new AttributeNameToValue("executionCycle", SigPMLRTDAccessor.getExecutionCycle((org.eclipse.gemoc.example.moccmlsigpml.model.sigpml.HWComputationalResource)elem));
elemState.getSavedRTDs().add(n2v0);
Class<?> clazz =null;
clazz = K3DslHelper.getTarget(org.eclipse.gemoc.example.moccmlsigpml.k3dsa.PlaceAspect.class);
if (clazz.isInstance(elem)) {
ElementState elemState = theFactory.createElementState();
elemState.setModelElement(elem);
res.getOwnedElementstates().add(elemState);
AttributeNameToValue n2v0 = new AttributeNameToValue("fifo", SigPMLRTDAccessor.getFifo((org.eclipse.gemoc.example.moccmlsigpml.model.sigpml.Place)elem));
elemState.getSavedRTDs().add(n2v0);
AttributeNameToValue n2v1 = new AttributeNameToValue("currentSize", SigPMLRTDAccessor.getCurrentSize((org.eclipse.gemoc.example.moccmlsigpml.model.sigpml.Place)elem));
elemState.getSavedRTDs().add(n2v1);
AttributeNameToValue n2v2 = new AttributeNameToValue("isInitialized", SigPMLRTDAccessor.getIsInitialized((org.eclipse.gemoc.example.moccmlsigpml.model.sigpml.Place)elem));
elemState.getSavedRTDs().add(n2v2);
}
clazz = K3DslHelper.getTarget(org.eclipse.gemoc.example.moccmlsigpml.k3dsa.SystemAspect.class);
if (clazz.isInstance(elem)) {
ElementState elemState = theFactory.createElementState();
elemState.setModelElement(elem);
res.getOwnedElementstates().add(elemState);
AttributeNameToValue n2v0 = new AttributeNameToValue("sharedMemory", SigPMLRTDAccessor.getSharedMemory((org.eclipse.gemoc.example.moccmlsigpml.model.sigpml.System)elem));
elemState.getSavedRTDs().add(n2v0);
}
clazz = K3DslHelper.getTarget(org.eclipse.gemoc.example.moccmlsigpml.k3dsa.OutputPortAspect.class);
if (clazz.isInstance(elem)) {
ElementState elemState = theFactory.createElementState();
elemState.setModelElement(elem);
res.getOwnedElementstates().add(elemState);
AttributeNameToValue n2v0 = new AttributeNameToValue("sizeWritten", SigPMLRTDAccessor.getSizeWritten((org.eclipse.gemoc.example.moccmlsigpml.model.sigpml.OutputPort)elem));
elemState.getSavedRTDs().add(n2v0);
}
clazz = K3DslHelper.getTarget(org.eclipse.gemoc.example.moccmlsigpml.k3dsa.AgentAspect.class);
if (clazz.isInstance(elem)) {
ElementState elemState = theFactory.createElementState();
elemState.setModelElement(elem);
res.getOwnedElementstates().add(elemState);
AttributeNameToValue n2v0 = new AttributeNameToValue("plotter", SigPMLRTDAccessor.getPlotter((org.eclipse.gemoc.example.moccmlsigpml.model.sigpml.Agent)elem));
elemState.getSavedRTDs().add(n2v0);
AttributeNameToValue n2v1 = new AttributeNameToValue("frame", SigPMLRTDAccessor.getFrame((org.eclipse.gemoc.example.moccmlsigpml.model.sigpml.Agent)elem));
elemState.getSavedRTDs().add(n2v1);
AttributeNameToValue n2v2 = new AttributeNameToValue("figure", SigPMLRTDAccessor.getFigure((org.eclipse.gemoc.example.moccmlsigpml.model.sigpml.Agent)elem));
elemState.getSavedRTDs().add(n2v2);
AttributeNameToValue n2v3 = new AttributeNameToValue("hasBeenStopped", SigPMLRTDAccessor.getHasBeenStopped((org.eclipse.gemoc.example.moccmlsigpml.model.sigpml.Agent)elem));
elemState.getSavedRTDs().add(n2v3);
AttributeNameToValue n2v4 = new AttributeNameToValue("currentExecCycle", SigPMLRTDAccessor.getCurrentExecCycle((org.eclipse.gemoc.example.moccmlsigpml.model.sigpml.Agent)elem));
elemState.getSavedRTDs().add(n2v4);
AttributeNameToValue n2v5 = new AttributeNameToValue("isCurrentlyExecuting", SigPMLRTDAccessor.getIsCurrentlyExecuting((org.eclipse.gemoc.example.moccmlsigpml.model.sigpml.Agent)elem));
elemState.getSavedRTDs().add(n2v5);
}
clazz = K3DslHelper.getTarget(org.eclipse.gemoc.example.moccmlsigpml.k3dsa.InputPortAspect.class);
if (clazz.isInstance(elem)) {
ElementState elemState = theFactory.createElementState();
elemState.setModelElement(elem);
res.getOwnedElementstates().add(elemState);
AttributeNameToValue n2v0 = new AttributeNameToValue("sizeToread", SigPMLRTDAccessor.getSizeToread((org.eclipse.gemoc.example.moccmlsigpml.model.sigpml.InputPort)elem));
elemState.getSavedRTDs().add(n2v0);
}
clazz = K3DslHelper.getTarget(org.eclipse.gemoc.example.moccmlsigpml.k3dsa.HWComputationalResourceAspect.class);
if (clazz.isInstance(elem)) {
ElementState elemState = theFactory.createElementState();
elemState.setModelElement(elem);
res.getOwnedElementstates().add(elemState);
AttributeNameToValue n2v0 = new AttributeNameToValue("executionCycle", SigPMLRTDAccessor.getExecutionCycle((org.eclipse.gemoc.example.moccmlsigpml.model.sigpml.HWComputationalResource)elem));
elemState.getSavedRTDs().add(n2v0);
}
}
}
return res;
}
}


public void restoreModelState(K3ModelState state) {
for(ElementState elemState : state.getOwnedElementstates()) {
for(Object o : elemState.getSavedRTDs()) {
AttributeNameToValue n2v = (AttributeNameToValue)o;
String n2vOpName = n2v.name.substring(0,1).toUpperCase() + n2v.name.substring(1);
try {
if (n2v.value != null) {
Method m = SigPMLRTDAccessor.class.getMethod("set"+n2v.name, EObject.class, n2v.value.getClass());
Method m = SigPMLRTDAccessor.class.getMethod("set"+n2vOpName, elemState.getModelElement().getClass().getInterfaces()[0], n2v.value.getClass());
m.invoke(null, elemState.getModelElement(), n2v.value);
}
} catch (NoSuchMethodException | SecurityException | IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
Method m = null;
for(Class<?> c : n2v.value.getClass().getInterfaces()) {

try {
m = SigPMLRTDAccessor.class.getMethod("set"+n2v.name, EObject.class, n2v.value.getClass().getInterfaces()[0]);
m = SigPMLRTDAccessor.class.getMethod("set"+n2vOpName, elemState.getModelElement().getClass().getInterfaces()[0], c);
m.invoke(null, elemState.getModelElement(), n2v.value);
} catch (NoSuchMethodException | SecurityException | IllegalAccessException | IllegalArgumentException | InvocationTargetException e1) {
}
if (m == null) {
throw new RuntimeException("not method found for "+n2v.value.getClass().getName()+"::set"+n2v.name);
throw new RuntimeException("not method found for "+n2v.value.getClass().getName()+"::set"+n2vOpName);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,15 +136,16 @@ public static boolean setAspectProperty(EObject eObject, String languageName, St
if (aspect == null) {
return false;
}
final Class<?> targetClass = ((fr.inria.diverse.k3.al.annotationprocessor.Aspect)aspect.getAnnotations()[0]).className();
try {
aspect.getMethod(propertyName, ((fr.inria.diverse.k3.al.annotationprocessor.Aspect)aspect.getAnnotations()[0]).className(), newValue.getClass()).invoke(eObject, eObject, newValue);
aspect.getMethod(propertyName, targetClass, newValue.getClass()).invoke(eObject, eObject, newValue);
return true;
} catch (NoSuchMethodException | SecurityException | IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
Method m = null;
for(Class<?> c : ((fr.inria.diverse.k3.al.annotationprocessor.Aspect)aspect.getAnnotations()[0]).getClass().getInterfaces()) {
for(Class<?> c : newValue.getClass().getInterfaces()) {

try {
aspect.getMethod(propertyName, c, newValue.getClass()).invoke(eObject, eObject, newValue);
aspect.getMethod(propertyName, targetClass, c).invoke(eObject, eObject, newValue);
return true;
} catch (NoSuchMethodException | SecurityException | IllegalAccessException | IllegalArgumentException | InvocationTargetException e1) {
}
Expand Down
Loading

0 comments on commit 71e65ff

Please sign in to comment.