Skip to content

Commit 20bf5e4

Browse files
committed
Improved EOL interpreter (fixes #215)
1 parent 55d01bf commit 20bf5e4

File tree

2 files changed

+86
-66
lines changed

2 files changed

+86
-66
lines changed

plugins/org.eclipse.epsilon.emc.emf/src/org/eclipse/epsilon/emc/emf/InMemoryEmfModel.java

Lines changed: 31 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,16 @@
1414
import java.util.Collection;
1515
import java.util.Collections;
1616
import java.util.List;
17+
1718
import org.eclipse.emf.ecore.EPackage;
19+
import org.eclipse.emf.ecore.EPackage.Registry;
1820
import org.eclipse.emf.ecore.impl.EPackageRegistryImpl;
1921
import org.eclipse.emf.ecore.resource.Resource;
20-
import org.eclipse.emf.ecore.resource.ResourceSet;
2122

2223
public class InMemoryEmfModel extends EmfModel {
2324

25+
protected Collection<EPackage> ePackages;
26+
2427
public InMemoryEmfModel(String name, Resource modelImpl, EPackage... ePackages) {
2528
init(name, modelImpl, Arrays.asList(ePackages), true);
2629
}
@@ -65,72 +68,41 @@ public InMemoryEmfModel(String name, Resource modelImpl, Collection<EPackage> eP
6568
protected void init(String name, Resource modelImpl, Collection<EPackage> ePackages, boolean isContainerListenerEnabled) {
6669
setName(name);
6770
this.modelImpl = modelImpl;
71+
this.ePackages = ePackages;
6872

69-
// If some packages are provided, create a registry and put them in it
70-
if (!(ePackages == null || ePackages.isEmpty())) {
71-
this.registry = new EPackageRegistryImpl();
72-
for (EPackage ePackage : ePackages) {
73-
registry.put(ePackage.getNsURI(), ePackage);
74-
List<EPackage> dependencies = new ArrayList<>();
75-
EmfUtil.collectDependencies(ePackage, dependencies);
76-
for (EPackage dependency : dependencies) {
77-
registry.put(dependency.getNsURI(), dependency);
78-
}
79-
}
80-
}
81-
else {
82-
// If no packages are provided and registry is null,
83-
// fall back to the global package registry
84-
if (this.registry == null && modelImpl != null && modelImpl.getResourceSet() != null && modelImpl.getResourceSet().getPackageRegistry().isEmpty()) {
85-
this.registry = EPackage.Registry.INSTANCE;
86-
}
87-
}
88-
89-
// // If there is no ResourceSet we cannot register or call the resource creation factory
90-
// // @see https://bugs.eclipse.org/bugs/show_bug.cgi?id=540424
91-
// final ResourceSet resourceSet = modelImpl.getResourceSet();
92-
// if (resourceSet != null) {
93-
// Resource.Factory.Registry rfReg = resourceSet.getResourceFactoryRegistry();
94-
// if (rfReg == null) {
95-
// resourceSet.setResourceFactoryRegistry(Resource.Factory.Registry.INSTANCE);
96-
// }
97-
// }
98-
//
99-
// if (ePackages == null || ePackages.isEmpty()) {
100-
//
101-
// registry = EPackage.Registry.INSTANCE;
102-
//
103-
// // No additional packages are provided, so if the package registry of
104-
// // the local resource registry is empty, use the global registry instead
105-
//
106-
// // If there is no ResourceSet available, AbstractEmfModel#getPackageRegistry()
107-
// // already returns the global registry, so no need to worry about this
108-
// //if (resourceSet != null && resourceSet.getPackageRegistry().isEmpty()) {
109-
// // resourceSet.setPackageRegistry(EPackage.Registry.INSTANCE);
110-
// //}
111-
// }
112-
// else {
113-
// final EPackage.Registry epReg = getResource().getResourceSet().getPackageRegistry();
114-
// for (EPackage ePackage : ePackages) {
115-
// epReg.put(ePackage.getNsURI(), ePackage);
116-
//
117-
// //Added : Collect dependencies
118-
//
119-
// List<EPackage> dependencies = new ArrayList<>();
120-
// EmfUtil.collectDependencies(ePackage, dependencies);
121-
// for (EPackage dependency : dependencies) {
122-
// epReg.put(dependency.getNsURI(), dependency);
123-
// }
124-
// }
125-
// }
126-
12773
// Since 1.6, having CachedContentsAdapter implies cached=true, otherwise it's inconsistent.
12874
setCachingEnabled(true);
12975

13076
if (isContainerListenerEnabled) {
13177
this.setupContainmentChangeListeners();
13278
}
13379
}
80+
81+
@Override
82+
protected Registry getPackageRegistry() {
83+
if (compositeRegistry == null) {
84+
// If some packages are provided, create a registry and put them in it
85+
if (!(ePackages == null || ePackages.isEmpty())) {
86+
this.registry = new EPackageRegistryImpl();
87+
for (EPackage ePackage : ePackages) {
88+
registry.put(ePackage.getNsURI(), ePackage);
89+
List<EPackage> dependencies = new ArrayList<>();
90+
EmfUtil.collectDependencies(ePackage, dependencies);
91+
for (EPackage dependency : dependencies) {
92+
registry.put(dependency.getNsURI(), dependency);
93+
}
94+
}
95+
}
96+
else {
97+
// If no packages are provided and registry is null,
98+
// fall back to the global package registry
99+
if (this.registry == null && modelImpl != null && modelImpl.getResourceSet() != null && modelImpl.getResourceSet().getPackageRegistry().isEmpty()) {
100+
this.registry = EPackage.Registry.INSTANCE;
101+
}
102+
}
103+
}
104+
return super.getPackageRegistry();
105+
}
134106

135107
@Override
136108
public void loadModel() {

plugins/org.eclipse.epsilon.eol.dt.interpreter/src/org/eclipse/epsilon/eol/dt/interpreter/EolInterpreter.java

Lines changed: 55 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@
99
**********************************************************************/
1010
package org.eclipse.epsilon.eol.dt.interpreter;
1111

12+
import java.util.ArrayList;
13+
import java.util.Collection;
14+
import java.util.LinkedHashSet;
1215
import java.util.List;
1316
import java.util.concurrent.Callable;
1417

@@ -19,7 +22,11 @@
1922
import org.eclipse.acceleo.ui.interpreter.language.InterpreterContext;
2023
import org.eclipse.core.runtime.IStatus;
2124
import org.eclipse.core.runtime.Status;
25+
import org.eclipse.emf.common.notify.Notifier;
2226
import org.eclipse.emf.ecore.EObject;
27+
import org.eclipse.emf.ecore.EPackage;
28+
import org.eclipse.emf.ecore.resource.Resource;
29+
import org.eclipse.emf.ecore.resource.ResourceSet;
2330
import org.eclipse.epsilon.common.dt.console.EpsilonConsole;
2431
import org.eclipse.epsilon.common.dt.editor.AbstractModuleEditorSourceViewerConfiguration;
2532
import org.eclipse.epsilon.emc.emf.InMemoryEmfModel;
@@ -67,18 +74,41 @@ public Callable<EvaluationResult> getEvaluationTask(EvaluationContext context) {
6774
module.parse(context.getExpression());
6875
FrameStack frameStack = module.getContext().getFrameStack();
6976

70-
List<EObject> eObjects = context.getTargetEObjects();
71-
if (eObjects.isEmpty()) {
77+
LinkedHashSet<Resource> resources = new LinkedHashSet<>();
78+
79+
for (Notifier notifier : context.getTargetNotifiers()) {
80+
Resource resource = getResource(notifier);
81+
if (resource != null) resources.add(resource);
82+
}
83+
84+
List<InMemoryEmfModel> models = new ArrayList<InMemoryEmfModel>();
85+
for (Resource resource : resources) {
86+
InMemoryEmfModel model = new InMemoryEmfModel(resource) {
87+
@Override
88+
protected void init(String name, Resource modelImpl, Collection<EPackage> ePackages,
89+
boolean isContainerListenerEnabled) {
90+
// Models in the interpreter should have access to the global EPackage.Registry
91+
// See https://github.com/eclipse-epsilon/epsilon/discussions/213
92+
this.registry = EPackage.Registry.INSTANCE;
93+
super.init(name, modelImpl, ePackages, isContainerListenerEnabled);
94+
}
95+
};
96+
97+
model.setName(models.isEmpty() ? "M" : "M" + models.size());
98+
module.getContext().getModelRepository().addModel(model);
99+
}
100+
101+
List<Notifier> notifiers = context.getTargetNotifiers();
102+
103+
if (notifiers.isEmpty()) {
72104
frameStack.put("self", null);
73105
}
74106
else {
75-
module.getContext().getModelRepository().addModel(new InMemoryEmfModel(eObjects.get(0).eResource()));
76-
77-
if (eObjects.size() == 1) {
78-
frameStack.put("self", eObjects.get(0));
107+
if (notifiers.size() == 1) {
108+
frameStack.put("self", notifiers.get(0));
79109
}
80110
else {
81-
frameStack.put("self", eObjects);
111+
frameStack.put("self", notifiers);
82112
}
83113
}
84114

@@ -96,5 +126,23 @@ public Callable<EvaluationResult> getEvaluationTask(EvaluationContext context) {
96126
return new EvaluationResult(result);
97127
};
98128
}
129+
130+
protected Resource getResource(Notifier notifier) {
131+
Resource resource = null;
132+
133+
if (notifier instanceof ResourceSet) {
134+
ResourceSet resourceSet = (ResourceSet) notifier;
135+
if (!resourceSet.getResources().isEmpty()) {
136+
resource = resourceSet.getResources().get(0);
137+
}
138+
}
139+
else if (notifier instanceof Resource) {
140+
resource = (Resource) notifier;
141+
}
142+
else if (notifier instanceof EObject) {
143+
resource = ((EObject) notifier).eResource();
144+
}
145+
return resource;
146+
}
99147

100148
}

0 commit comments

Comments
 (0)