Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAdolfo SBH2016-09-02 17:45:29 +0000
committerEd Willink2016-10-22 14:16:09 +0000
commit1aa1d8e5d46780bc1ea9fcbca81233c20e98660f (patch)
treebdcbaff6ba643ed16b7d7753bc021efe199e623e
parent2ab6aed778254efe1115be3075ad80799c169e28 (diff)
downloadorg.eclipse.qvtd-1aa1d8e5d46780bc1ea9fcbca81233c20e98660f.tar.gz
org.eclipse.qvtd-1aa1d8e5d46780bc1ea9fcbca81233c20e98660f.tar.xz
org.eclipse.qvtd-1aa1d8e5d46780bc1ea9fcbca81233c20e98660f.zip
[cs2as] - Also considering nested packages
- Creating a Visitable Metaclass injector
-rw-r--r--plugins/org.eclipse.qvtd.cs2as.build/src/org/eclipse/qvtd/cs2as/build/mwe/CSTracePropertyInjector.java76
-rw-r--r--plugins/org.eclipse.qvtd.cs2as.build/src/org/eclipse/qvtd/cs2as/build/mwe/VisitableInjector.java191
2 files changed, 236 insertions, 31 deletions
diff --git a/plugins/org.eclipse.qvtd.cs2as.build/src/org/eclipse/qvtd/cs2as/build/mwe/CSTracePropertyInjector.java b/plugins/org.eclipse.qvtd.cs2as.build/src/org/eclipse/qvtd/cs2as/build/mwe/CSTracePropertyInjector.java
index 000700b9d..77ca84da8 100644
--- a/plugins/org.eclipse.qvtd.cs2as.build/src/org/eclipse/qvtd/cs2as/build/mwe/CSTracePropertyInjector.java
+++ b/plugins/org.eclipse.qvtd.cs2as.build/src/org/eclipse/qvtd/cs2as/build/mwe/CSTracePropertyInjector.java
@@ -26,48 +26,48 @@ public class CSTracePropertyInjector extends AbstractWorkflowComponent2 {
public static class ASTraceInfo {
private String genModelURI;
private String traceClassName;
-
+
@Mandatory
public void setTargetGenModel(String genModelURI) {
this.genModelURI = genModelURI;
}
-
+
@Mandatory
public void setTargetTypeName(String className) {
this.traceClassName = className;
}
}
-
+
public static class CSTraceInfo {
private String baseGenModelURI;
private String targetGenModelURI;
private String traceClassName = "CSTrace";
private String tracePropName = "ast";
-
+
@Mandatory
public void setTargetGenModel(String genModelURI) {
this.targetGenModelURI = genModelURI;
}
-
+
public void setBaseGenModel(String genModelURI) {
this.baseGenModelURI = genModelURI;
}
-
+
public void setTracePropertyName(String tracePropName) {
this.tracePropName = tracePropName;
}
-
+
public void setTraceClassName(String traceClassName) {
this.traceClassName = traceClassName;
}
}
-
+
private ResourceSet rSet;
-
+
private CSTraceInfo csTraceInfo;
private ASTraceInfo asTraceType;
-
-
+
+
@Override
public void preInvoke() {
super.preInvoke();
@@ -75,7 +75,7 @@ public class CSTracePropertyInjector extends AbstractWorkflowComponent2 {
csTraceInfo.baseGenModelURI = csTraceInfo.targetGenModelURI;
}
}
-
+
@Override
protected void invokeInternal(WorkflowContext ctx, ProgressMonitor monitor, Issues issues) {
@@ -85,19 +85,19 @@ public class CSTracePropertyInjector extends AbstractWorkflowComponent2 {
GenModel targetGenModel = (GenModel) targetResource.getContents().get(0);
GenPackage targetGenPackage = targetGenModel.getGenPackages().get(0); // FIXME what if we have many ?
EPackage targetEPackage = targetGenPackage.getEcorePackage();
-
+
boolean targetChanged = false;
for (EClassifier eClassifier : targetEPackage.getEClassifiers()) {
if (eClassifier instanceof EClass) {
EClass eClass = (EClass) eClassifier;
if (eClass.getESuperTypes().size() == 0
- && eClass != traceClass) {
+ && eClass != traceClass) {
eClass.getESuperTypes().add(traceClass);
targetChanged = true;
}
}
}
-
+
if (targetChanged) {
targetEPackage.eResource().save(null);
targetGenModel.reconcile();
@@ -107,16 +107,16 @@ public class CSTracePropertyInjector extends AbstractWorkflowComponent2 {
issues.addError(e.getMessage());
}
}
-
+
private EClass getCSTraceClass(String genModelURI) throws IOException {
-
+
Resource resource = rSet.getResource(URI.createURI(genModelURI), true);
GenModel genModel = (GenModel) resource.getContents().get(0);
GenPackage baseGenPackage = genModel.getGenPackages().get(0); // FIXME what if we have many ?
EPackage ePackage = baseGenPackage.getEcorePackage();
-
+
String traceClassName = csTraceInfo.traceClassName;
- String tracePropName = csTraceInfo.tracePropName;
+ String tracePropName = csTraceInfo.tracePropName;
for (EClassifier eClassifier : ePackage.getEClassifiers()) {
if (eClassifier instanceof EClass) {
EClass eClass = (EClass) eClassifier;
@@ -129,7 +129,7 @@ public class CSTracePropertyInjector extends AbstractWorkflowComponent2 {
}
}
}
-
+
// If not found we create it
// FIXME solve the situation of having an already "traceClassName" class with no ast property
EClass traceClass = EcoreFactory.eINSTANCE.createEClass();
@@ -139,7 +139,7 @@ public class CSTracePropertyInjector extends AbstractWorkflowComponent2 {
traceProp.setName(tracePropName);
traceProp.setTransient(true);
if (asTraceType == null) {
- traceProp.setEType(EcorePackage.Literals.EOBJECT);
+ traceProp.setEType(EcorePackage.Literals.EOBJECT);
} else {
Resource asResource = rSet.getResource(URI.createURI(asTraceType.genModelURI), true);
GenModel asGenModel = (GenModel) asResource.getContents().get(0);
@@ -151,19 +151,19 @@ public class CSTracePropertyInjector extends AbstractWorkflowComponent2 {
usedGenPackages.add(asGenPackage);
}
}
-
-
+
+
traceClass.getEStructuralFeatures().add(traceProp);
ePackage.getEClassifiers().add(traceClass);
-
-
+
+
ePackage.eResource().save(null);
genModel.reconcile();
resource.save(null);
return traceClass;
}
-
- private EClass getASTraceClass(EPackage ePackage) {
+
+ private EClass getASTraceClass(EPackage ePackage, boolean isNestedPackage) {
for (EClassifier eClassifier : ePackage.getEClassifiers()) {
if (eClassifier instanceof EClass) {
EClass eClass = (EClass) eClassifier;
@@ -172,19 +172,33 @@ public class CSTracePropertyInjector extends AbstractWorkflowComponent2 {
}
}
}
- throw new IllegalStateException(MessageFormat.format("AS class with name {0} not found in the EPackage of {1}", asTraceType.traceClassName,asTraceType.genModelURI));
+ for (EPackage nestedPackage : ePackage.getESubpackages()) {
+ EClass asTraceClass = getASTraceClass(nestedPackage, true);
+ if (asTraceClass != null) {
+ return asTraceClass;
+ }
+ }
+ if (isNestedPackage) {
+ return null;
+ } else {
+ throw new IllegalStateException(MessageFormat.format("AS class with name {0} not found in the EPackage of {1}", asTraceType.traceClassName,asTraceType.genModelURI));
+ }
+ }
+
+ private EClass getASTraceClass(EPackage ePackage) {
+ return getASTraceClass(ePackage, false);
}
-
+
@Mandatory
public void setResourceSet(ResourceSet rSet) {
this.rSet = rSet;
}
-
+
@Mandatory
public void setCSTraceInfo(CSTraceInfo csTraceInfo) {
this.csTraceInfo = csTraceInfo;
}
-
+
public void setASTraceType(ASTraceInfo asTraceInfo) {
this.asTraceType = asTraceInfo;
}
diff --git a/plugins/org.eclipse.qvtd.cs2as.build/src/org/eclipse/qvtd/cs2as/build/mwe/VisitableInjector.java b/plugins/org.eclipse.qvtd.cs2as.build/src/org/eclipse/qvtd/cs2as/build/mwe/VisitableInjector.java
new file mode 100644
index 000000000..c8bdc83b2
--- /dev/null
+++ b/plugins/org.eclipse.qvtd.cs2as.build/src/org/eclipse/qvtd/cs2as/build/mwe/VisitableInjector.java
@@ -0,0 +1,191 @@
+package org.eclipse.qvtd.cs2as.build.mwe;
+
+import java.io.IOException;
+import java.text.MessageFormat;
+
+import org.eclipse.emf.codegen.ecore.genmodel.GenModel;
+import org.eclipse.emf.codegen.ecore.genmodel.GenPackage;
+import org.eclipse.emf.common.util.URI;
+import org.eclipse.emf.ecore.EClass;
+import org.eclipse.emf.ecore.EClassifier;
+import org.eclipse.emf.ecore.EPackage;
+import org.eclipse.emf.ecore.EcoreFactory;
+import org.eclipse.emf.ecore.resource.Resource;
+import org.eclipse.emf.ecore.resource.ResourceSet;
+import org.eclipse.emf.mwe.core.WorkflowContext;
+import org.eclipse.emf.mwe.core.issues.Issues;
+import org.eclipse.emf.mwe.core.lib.AbstractWorkflowComponent2;
+import org.eclipse.emf.mwe.core.monitor.ProgressMonitor;
+import org.eclipse.emf.mwe2.runtime.Mandatory;
+
+public class VisitableInjector extends AbstractWorkflowComponent2 {
+
+ public static class VisitableInfo {
+ private String baseGenModelURI;
+ private String targetGenModelURI;
+ private String className = "Visitable";
+ private String instanceTypeName = "";
+ private String mainPackageName;
+
+ @Mandatory
+ public void setTargetGenModel(String genModelURI) {
+ this.targetGenModelURI = genModelURI;
+ }
+
+ public void setBaseGenModel(String genModelURI) {
+ this.baseGenModelURI = genModelURI;
+ }
+
+ public void setVisitableClassName(String className) {
+ this.className = className;
+ }
+
+ public void setMainPackageName(String mainPackage) {
+ this.mainPackageName = mainPackage;
+ }
+
+ @Mandatory
+ public void setVisitableInstanceTypeName(String instanceTypeName) {
+ this.instanceTypeName = instanceTypeName;
+ }
+ }
+
+ private ResourceSet rSet;
+
+ private VisitableInfo visitableInfo;
+
+
+
+ @Override
+ public void preInvoke() {
+ super.preInvoke();
+ if (visitableInfo.baseGenModelURI == null) {
+ visitableInfo.baseGenModelURI = visitableInfo.targetGenModelURI;
+ }
+ }
+
+ @Override
+ protected void invokeInternal(WorkflowContext ctx, ProgressMonitor monitor, Issues issues) {
+
+ try {
+ EClass visitableClass = getVisitableClass(visitableInfo.baseGenModelURI);
+ injectInterface(visitableInfo.targetGenModelURI, visitableClass);
+
+ } catch(IOException e) {
+ issues.addError(e.getMessage());
+ }
+ }
+
+ private EPackage getMainPackage(GenModel targetGenModel, String mainPackageName) {
+
+ if (mainPackageName == null) {
+ GenPackage targetGenPackage = targetGenModel.getGenPackages().get(0); // FIXME what if we have many ?
+ return targetGenPackage.getEcorePackage();
+ } else {
+ for (GenPackage gPackage : targetGenModel.getGenPackages()) {
+ EPackage mainPackage = getMainPackage(gPackage, mainPackageName);
+ if (mainPackage != null) {
+ return mainPackage;
+ }
+ }
+ }
+ throw new IllegalStateException(MessageFormat.format("EPackage with name {0} not found in GenModel {1}", mainPackageName,targetGenModel.eResource().getURI()));
+ }
+
+ private EPackage getMainPackage(GenPackage targetGenPackage, String mainPackageName) {
+ EPackage ePackage = targetGenPackage.getEcorePackage();
+ if (mainPackageName.equals(ePackage.getName())) {
+ return ePackage;
+ } else {
+ for (GenPackage nestedGPackage : targetGenPackage.getNestedGenPackages()) {
+ EPackage mainPackage = getMainPackage(nestedGPackage, mainPackageName);
+ if (mainPackage != null) {
+ return mainPackage;
+ }
+ }
+ return null;
+ }
+ }
+
+ private void injectInterface(String genModelURI, EClass visitableClass) throws IOException {
+
+ Resource targetResource = rSet.getResource(URI.createURI(genModelURI), true);
+ GenModel targetGenModel = (GenModel) targetResource.getContents().get(0);
+ boolean targetChanged = false;
+ for (GenPackage gPackage : targetGenModel.getGenPackages()) {
+ targetChanged |= injectInterface(gPackage, visitableClass);
+ }
+ if (targetChanged) {
+ targetGenModel.reconcile();
+ targetResource.save(null);
+ }
+ }
+ private boolean injectInterface(GenPackage targetGenPackage, EClass visitableClass) throws IOException {
+
+ EPackage targetEPackage = targetGenPackage.getEcorePackage();
+ boolean targetChanged = false;
+ for (EClassifier eClassifier : targetEPackage.getEClassifiers()) {
+ if (eClassifier instanceof EClass) {
+ EClass eClass = (EClass) eClassifier;
+ if (eClass.getESuperTypes().size() == 0
+ && eClass != visitableClass) {
+ eClass.getESuperTypes().add(visitableClass);
+ targetChanged = true;
+ }
+ }
+ }
+ if (targetChanged) {
+ targetEPackage.eResource().save(null);
+ }
+ return targetChanged;
+ }
+ private EClass getVisitableClass(String genModelURI) throws IOException {
+
+ Resource resource = rSet.getResource(URI.createURI(genModelURI), true);
+ GenModel genModel = (GenModel) resource.getContents().get(0);
+ EPackage ePackage = getMainPackage(genModel, visitableInfo.mainPackageName);
+
+ String className = visitableInfo.className;
+ String instanceTypeName = visitableInfo.instanceTypeName;
+
+ for (EClassifier eClassifier : ePackage.getEClassifiers()) {
+ if (eClassifier instanceof EClass) {
+ EClass eClass = (EClass) eClassifier;
+ if (className.equals(eClass.getName())
+ && instanceTypeName.equals(eClass.getInstanceTypeName())) {
+ return eClass;
+ }
+ }
+ }
+
+ // If not found we create it
+ EClass visitableClass = EcoreFactory.eINSTANCE.createEClass();
+ visitableClass.setName(className);
+ visitableClass.setInstanceTypeName(instanceTypeName);
+ visitableClass.setAbstract(true);
+ visitableClass.setInterface(true);
+ ePackage.getEClassifiers().add(visitableClass);
+
+
+ ePackage.eResource().save(null);
+ genModel.reconcile();
+ resource.save(null);
+ return visitableClass;
+ }
+
+
+
+ @Mandatory
+ public void setResourceSet(ResourceSet rSet) {
+ this.rSet = rSet;
+ }
+
+ @Mandatory
+ public void setVisitableInfo(VisitableInfo csTraceInfo) {
+ this.visitableInfo = csTraceInfo;
+ }
+
+
+
+}
+

Back to the top