/
Mwe2Runner.java
150 lines (130 loc) · 5.21 KB
/
Mwe2Runner.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
/*******************************************************************************
* Copyright (c) 2008,2010 itemis AG (http://www.itemis.eu) and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
*******************************************************************************/
package org.eclipse.emf.mwe2.launch.runtime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.emf.ecore.util.EcoreUtil;
import org.eclipse.emf.mwe2.language.factory.Mwe2ExecutionEngine;
import org.eclipse.emf.mwe2.language.mwe2.Module;
import org.eclipse.emf.mwe2.language.mwe2.Mwe2Package;
import org.eclipse.emf.mwe2.runtime.workflow.IWorkflow;
import org.eclipse.emf.mwe2.runtime.workflow.IWorkflowContext;
import org.eclipse.xtext.mwe.RuntimeResourceSetInitializer;
import org.eclipse.xtext.mwe.UriFilter;
import org.eclipse.xtext.naming.IQualifiedNameConverter;
import org.eclipse.xtext.naming.QualifiedName;
import org.eclipse.xtext.resource.IEObjectDescription;
import org.eclipse.xtext.resource.IResourceDescription;
import org.eclipse.xtext.resource.IResourceDescriptions;
import com.google.common.collect.Maps;
import com.google.inject.Inject;
import com.google.inject.Provider;
public class Mwe2Runner {
@Inject
private RuntimeResourceSetInitializer initializer;
@Inject
private Mwe2ExecutionEngine engine;
@Inject
private Provider<IWorkflowContext> ctxProvider;
@Inject
private IQualifiedNameConverter qualifiedNameConverter;
public void run(URI createURI, Map<String, String> params) {
run(createURI, params, ctxProvider.get());
}
public void run(URI createURI, Map<String, String> params, IWorkflowContext ctx) {
ResourceSet resourceSet = getConfiguredResourceSet();
Resource resource = resourceSet.getResource(createURI, true);
if (resource != null) {
if (!resource.getContents().isEmpty()) {
EObject eObject = resource.getContents().get(0);
if (eObject instanceof Module) {
run(((Module) eObject), params, ctx);
return;
}
}
}
throw new IllegalArgumentException("Couldn't load module from URI " + createURI);
}
public void run(String moduleName, Map<String, String> params) {
run(moduleName, params, ctxProvider.get());
}
public void run(String moduleName, Map<String, String> params, IWorkflowContext ctx) {
Module module = findModule(moduleName);
if (module == null) {
throw new IllegalStateException("Couldn't find module "+moduleName);
}
run(module, params, ctx);
}
public void run(Module module, Map<String, String> params, IWorkflowContext ctx) {
if (module == null) {
throw new IllegalArgumentException("No module provided ");
}
EcoreUtil.resolveAll(module);
if (!module.eResource().getErrors().isEmpty()) {
throw new IllegalStateException(module.eResource().getErrors().toString());
}
Map<QualifiedName, Object> actualParams = getRealParams(params);
Object object = null;
try {
object = engine.create(module, actualParams);
} catch (RuntimeException e) {
throw new RuntimeException("Problems instantiating module " + module.getCanonicalName() + ": " + e.getMessage(), e);
}
if (!(object instanceof IWorkflow)) {
throw new IllegalArgumentException("The root element must be of type IWorkflow but was '"
+ object.getClass() + "'.");
}
try {
((IWorkflow) object).run(ctx);
} catch (RuntimeException e) {
throw new RuntimeException("Problems running workflow " + module.getCanonicalName() + ": " + e.getMessage(), e);
}
}
protected Map<QualifiedName, Object> getRealParams(Map<String, String> params) {
HashMap<QualifiedName, Object> map = Maps.newHashMap();
for (Map.Entry<String, String> param : params.entrySet()) {
map.put(qualifiedNameConverter.toQualifiedName(param.getKey()), param.getValue());
}
return map;
}
protected Module findModule(String moduleName) {
ResourceSet resourceSet = getConfiguredResourceSet();
IResourceDescriptions descriptions = initializer.getDescriptions(resourceSet);
for (IResourceDescription desc : descriptions.getAllResourceDescriptions()) {
Iterable<IEObjectDescription> iterable = desc.getExportedObjects(Mwe2Package.Literals.MODULE,
qualifiedNameConverter.toQualifiedName(moduleName), false);
for (IEObjectDescription objDesc : iterable) {
return (Module) resourceSet.getEObject(objDesc.getEObjectURI(), true);
}
}
return null;
}
protected ResourceSet getConfiguredResourceSet() {
ResourceSet resourceSet = initializer.getInitializedResourceSet(getPathes(), new UriFilter() {
public boolean matches(URI uri) {
return "mwe2".equalsIgnoreCase(uri.fileExtension());
}
});
return resourceSet;
}
protected List<String> getPathes() {
return initializer.getClassPathEntries();
}
public void setEngine(Mwe2ExecutionEngine engine) {
this.engine = engine;
}
public void setInitializer(RuntimeResourceSetInitializer initializer) {
this.initializer = initializer;
}
}