diff options
Diffstat (limited to 'Domains/ca.mcgill.rtgmrt.example.modes3/src/modes3/run/Modes3ModelGenerator.xtend')
-rw-r--r-- | Domains/ca.mcgill.rtgmrt.example.modes3/src/modes3/run/Modes3ModelGenerator.xtend | 222 |
1 files changed, 222 insertions, 0 deletions
diff --git a/Domains/ca.mcgill.rtgmrt.example.modes3/src/modes3/run/Modes3ModelGenerator.xtend b/Domains/ca.mcgill.rtgmrt.example.modes3/src/modes3/run/Modes3ModelGenerator.xtend new file mode 100644 index 00000000..71d1798f --- /dev/null +++ b/Domains/ca.mcgill.rtgmrt.example.modes3/src/modes3/run/Modes3ModelGenerator.xtend | |||
@@ -0,0 +1,222 @@ | |||
1 | package modes3.run | ||
2 | |||
3 | import com.google.common.collect.ImmutableList | ||
4 | import com.google.common.collect.ImmutableSet | ||
5 | import hu.bme.mit.inf.dslreasoner.ecore2logic.EReferenceMapper_RelationsOverTypes_Trace | ||
6 | import hu.bme.mit.inf.dslreasoner.ecore2logic.Ecore2Logic | ||
7 | import hu.bme.mit.inf.dslreasoner.ecore2logic.Ecore2LogicConfiguration | ||
8 | import hu.bme.mit.inf.dslreasoner.ecore2logic.EcoreMetamodelDescriptor | ||
9 | import hu.bme.mit.inf.dslreasoner.ecore2logic.ecore2logicannotations.Ecore2logicannotationsFactory | ||
10 | import hu.bme.mit.inf.dslreasoner.ecore2logic.ecore2logicannotations.Ecore2logicannotationsPackage | ||
11 | import hu.bme.mit.inf.dslreasoner.logic.model.builder.LogicProblemBuilder | ||
12 | import hu.bme.mit.inf.dslreasoner.logic.model.logiclanguage.LogiclanguagePackage | ||
13 | import hu.bme.mit.inf.dslreasoner.logic.model.logiclanguage.TypeDefinition | ||
14 | import hu.bme.mit.inf.dslreasoner.logic.model.logicproblem.LogicproblemPackage | ||
15 | import hu.bme.mit.inf.dslreasoner.logic.model.logicresult.ModelResult | ||
16 | import hu.bme.mit.inf.dslreasoner.viatra2logic.Viatra2Logic | ||
17 | import hu.bme.mit.inf.dslreasoner.viatra2logic.Viatra2LogicConfiguration | ||
18 | import hu.bme.mit.inf.dslreasoner.viatra2logic.ViatraQuerySetDescriptor | ||
19 | import hu.bme.mit.inf.dslreasoner.viatra2logic.viatra2logicannotations.Viatra2LogicAnnotationsPackage | ||
20 | import hu.bme.mit.inf.dslreasoner.viatrasolver.logic2viatra.TypeInferenceMethod | ||
21 | import hu.bme.mit.inf.dslreasoner.viatrasolver.logic2viatra.cardinality.PolyhedralScopePropagatorConstraints | ||
22 | import hu.bme.mit.inf.dslreasoner.viatrasolver.logic2viatra.cardinality.PolyhedralScopePropagatorSolver | ||
23 | import hu.bme.mit.inf.dslreasoner.viatrasolver.logic2viatra.cardinality.ScopePropagatorStrategy | ||
24 | import hu.bme.mit.inf.dslreasoner.viatrasolver.partialinterpretation2logic.InstanceModel2Logic | ||
25 | import hu.bme.mit.inf.dslreasoner.viatrasolver.partialinterpretationlanguage.partialinterpretation.PartialComplexTypeInterpretation | ||
26 | import hu.bme.mit.inf.dslreasoner.viatrasolver.partialinterpretationlanguage.partialinterpretation.PartialInterpretation | ||
27 | import hu.bme.mit.inf.dslreasoner.viatrasolver.partialinterpretationlanguage.partialinterpretation.PartialinterpretationPackage | ||
28 | import hu.bme.mit.inf.dslreasoner.viatrasolver.partialinterpretationlanguage.visualisation.PartialInterpretation2Gml | ||
29 | import hu.bme.mit.inf.dslreasoner.viatrasolver.reasoner.StateCoderStrategy | ||
30 | import hu.bme.mit.inf.dslreasoner.viatrasolver.reasoner.ViatraReasoner | ||
31 | import hu.bme.mit.inf.dslreasoner.viatrasolver.reasoner.ViatraReasonerConfiguration | ||
32 | import hu.bme.mit.inf.dslreasoner.visualisation.pi2graphviz.GraphvizVisualiser | ||
33 | import hu.bme.mit.inf.dslreasoner.workspace.FileSystemWorkspace | ||
34 | import java.util.List | ||
35 | import modes3.Modes3Factory | ||
36 | import modes3.Modes3Package | ||
37 | import modes3.queries.Modes3Queries | ||
38 | import org.eclipse.emf.ecore.EClass | ||
39 | import org.eclipse.emf.ecore.EObject | ||
40 | import org.eclipse.emf.ecore.resource.Resource | ||
41 | import org.eclipse.emf.ecore.xmi.impl.XMIResourceFactoryImpl | ||
42 | import org.eclipse.viatra.query.patternlanguage.emf.EMFPatternLanguageStandaloneSetup | ||
43 | import org.eclipse.viatra.query.runtime.api.ViatraQueryEngineOptions | ||
44 | import org.eclipse.viatra.query.runtime.localsearch.matcher.integration.LocalSearchEMFBackendFactory | ||
45 | import org.eclipse.viatra.query.runtime.rete.matcher.ReteBackendFactory | ||
46 | import org.eclipse.xtend.lib.annotations.FinalFieldsConstructor | ||
47 | import hu.bme.mit.inf.dslreasoner.logic.model.logiclanguage.RelationDefinition | ||
48 | import hu.bme.mit.inf.dslreasoner.viatrasolver.partialinterpretationlanguage.partialinterpretation.BinaryElementRelationLink | ||
49 | |||
50 | @FinalFieldsConstructor | ||
51 | class Modes3ModelGenerator { | ||
52 | val MonitoringQuery monitoringQuery | ||
53 | val int modelSize | ||
54 | |||
55 | val ecore2Logic = new Ecore2Logic | ||
56 | val instanceModel2Logic = new InstanceModel2Logic | ||
57 | val viatra2Logic = new Viatra2Logic(ecore2Logic) | ||
58 | val solver = new ViatraReasoner | ||
59 | extension val LogicProblemBuilder = new LogicProblemBuilder | ||
60 | |||
61 | def generate() { | ||
62 | val metamodel = createMetamodelDescriptor() | ||
63 | val metamodelLogic = ecore2Logic.transformMetamodel(metamodel, new Ecore2LogicConfiguration) | ||
64 | val segment = ecore2Logic.TypeofEClass(metamodelLogic.trace, Modes3Package.eINSTANCE.segment) | ||
65 | val connectedTo = ecore2Logic.relationOfReference(metamodelLogic.trace, | ||
66 | Modes3Package.eINSTANCE.segment_ConnectedTo) | ||
67 | val connectedToIndicator = (metamodelLogic.trace. | ||
68 | referenceMapperTrace as EReferenceMapper_RelationsOverTypes_Trace).indicators.get( | ||
69 | Modes3Package.eINSTANCE.segment_ConnectedTo) | ||
70 | val inverseAssertion = Assertion( | ||
71 | '''oppositeReference «connectedTo.name» «connectedTo.name»''', | ||
72 | Forall[ | ||
73 | val src = addVar('''src''', segment) | ||
74 | val trg = addVar('''trg''', segment) | ||
75 | connectedToIndicator.call(src, trg) <=> connectedToIndicator.call(trg, src) | ||
76 | ] | ||
77 | ) | ||
78 | metamodelLogic.output.assertions += inverseAssertion | ||
79 | val inverseAnnotation = Ecore2logicannotationsFactory.eINSTANCE.createInverseRelationAssertion => [ | ||
80 | target = inverseAssertion | ||
81 | inverseA = connectedTo | ||
82 | inverseB = connectedTo | ||
83 | ] | ||
84 | metamodelLogic.output.annotations += inverseAnnotation | ||
85 | val initialModel = loadInitialModel() | ||
86 | val initialModelLogic = instanceModel2Logic.transform(metamodelLogic, initialModel) | ||
87 | val queries = loadQueries | ||
88 | val logic = viatra2Logic.transformQueries(queries, initialModelLogic, new Viatra2LogicConfiguration) | ||
89 | val config = new ViatraReasonerConfiguration => [ | ||
90 | runtimeLimit = 3600 | ||
91 | typeScopes => [ | ||
92 | minNewElements = modelSize | ||
93 | maxNewElements = modelSize | ||
94 | minNewElementsByType => [ | ||
95 | put(ecore2Logic.TypeofEClass(metamodelLogic.trace, Modes3Package.eINSTANCE.turnout), 1) | ||
96 | ] | ||
97 | maxNewElementsByType => [ | ||
98 | put(ecore2Logic.TypeofEClass(metamodelLogic.trace, Modes3Package.eINSTANCE.train), 5) | ||
99 | ] | ||
100 | ] | ||
101 | solutionScope.numberOfRequiredSolutions = 1 | ||
102 | nameNewElements = false | ||
103 | typeInferenceMethod = TypeInferenceMethod.PreliminaryAnalysis | ||
104 | stateCoderStrategy = StateCoderStrategy::Neighbourhood | ||
105 | scopePropagatorStrategy = new ScopePropagatorStrategy.Polyhedral( | ||
106 | PolyhedralScopePropagatorConstraints.Relational, PolyhedralScopePropagatorSolver.Clp) | ||
107 | // unitPropagationPatternGenerators += new Modes3UnitPropagationGenerator(ecore2Logic, metamodelLogic.trace) | ||
108 | debugConfiguration.partialInterpretatioVisualiser = null | ||
109 | ] | ||
110 | val workspace = new FileSystemWorkspace("output/", "") | ||
111 | val solution = solver.solve(logic.output, config, workspace) | ||
112 | if (solution instanceof ModelResult) { | ||
113 | println("Saving generated solutions") | ||
114 | val representations = solution.representation | ||
115 | for (representationIndex : 0 ..< representations.size) { | ||
116 | val representation = representations.get(representationIndex) | ||
117 | val representationNumber = representationIndex + 1 | ||
118 | if (representation instanceof PartialInterpretation) { | ||
119 | workspace.writeModel(representation, '''solution«representationNumber».partialinterpretation''') | ||
120 | val partialInterpretation2GML = new PartialInterpretation2Gml | ||
121 | val gml = partialInterpretation2GML.transform(representation) | ||
122 | workspace.writeText('''solution«representationNumber».gml''', gml) | ||
123 | if (representation.newElements.size < 160) { | ||
124 | if (representation instanceof PartialInterpretation) { | ||
125 | representation.problem.types.forEach[println(name)] | ||
126 | val rootType = (representation.problem.types.findFirst [ | ||
127 | name == "Modes3ModelRoot class DefinedPart" | ||
128 | ] as TypeDefinition) | ||
129 | val rootIntepretation = representation.partialtypeinterpratation.filter( | ||
130 | PartialComplexTypeInterpretation).findFirst [ | ||
131 | interpretationOf.name == "Modes3ModelRoot class" | ||
132 | ] | ||
133 | rootIntepretation.elements.removeAll(rootType.elements) | ||
134 | representation.problem.elements.removeAll(rootType.elements) | ||
135 | for (relationInterpretation : representation.partialrelationinterpretation) { | ||
136 | relationInterpretation.relationlinks.removeIf [ link | | ||
137 | if (link instanceof BinaryElementRelationLink) { | ||
138 | rootType.elements.contains(link.param1) || rootType.elements.contains(link.param2) | ||
139 | } else { | ||
140 | false | ||
141 | } | ||
142 | ] | ||
143 | } | ||
144 | rootType.elements.clear | ||
145 | } | ||
146 | val visualiser = new GraphvizVisualiser | ||
147 | val visualisation = visualiser.visualiseConcretization(representation) | ||
148 | visualisation.writeToFile(workspace, '''solution«representationNumber».png''') | ||
149 | } | ||
150 | } else { | ||
151 | workspace.writeText('''solution«representationNumber».txt''', representation.toString) | ||
152 | } | ||
153 | } | ||
154 | } else { | ||
155 | println("Failed to solver problem") | ||
156 | val partial = logic.output | ||
157 | workspace.writeModel(partial, "solution.partialinterpretation") | ||
158 | } | ||
159 | } | ||
160 | |||
161 | static def createMetamodelDescriptor() { | ||
162 | val eClasses = ImmutableList.copyOf(Modes3Package.eINSTANCE.EClassifiers.filter(EClass)) | ||
163 | new EcoreMetamodelDescriptor( | ||
164 | eClasses, | ||
165 | emptySet, | ||
166 | false, | ||
167 | emptyList, | ||
168 | emptyList, | ||
169 | ImmutableList.copyOf(eClasses.flatMap[EReferences]), | ||
170 | emptyList | ||
171 | ) | ||
172 | } | ||
173 | |||
174 | static def List<EObject> loadInitialModel() { | ||
175 | #[Modes3Factory.eINSTANCE.createModes3ModelRoot] | ||
176 | } | ||
177 | |||
178 | def loadQueries() { | ||
179 | val patternsBuilder = ImmutableList.builder | ||
180 | patternsBuilder.addAll(Modes3Queries.instance.specifications) | ||
181 | val patterns = patternsBuilder.build | ||
182 | val validationPatterns = ImmutableSet.copyOf(patterns.filter [ pattern | | ||
183 | pattern.allAnnotations.exists[name == "Constraint"] | ||
184 | ]) | ||
185 | new ViatraQuerySetDescriptor( | ||
186 | patterns, | ||
187 | validationPatterns, | ||
188 | emptyMap | ||
189 | ) | ||
190 | } | ||
191 | |||
192 | def static init() { | ||
193 | EMFPatternLanguageStandaloneSetup.doSetup | ||
194 | ViatraQueryEngineOptions.setSystemDefaultBackends(ReteBackendFactory.INSTANCE, ReteBackendFactory.INSTANCE, | ||
195 | LocalSearchEMFBackendFactory.INSTANCE) | ||
196 | LogiclanguagePackage.eINSTANCE.class | ||
197 | LogicproblemPackage.eINSTANCE.class | ||
198 | PartialinterpretationPackage.eINSTANCE.class | ||
199 | Ecore2logicannotationsPackage.eINSTANCE.class | ||
200 | Viatra2LogicAnnotationsPackage.eINSTANCE.class | ||
201 | Resource.Factory.Registry.INSTANCE.extensionToFactoryMap.put("ecore", new XMIResourceFactoryImpl) | ||
202 | Resource.Factory.Registry.INSTANCE.extensionToFactoryMap.put("logicproblem", new XMIResourceFactoryImpl) | ||
203 | Resource.Factory.Registry.INSTANCE.extensionToFactoryMap.put("partialinterpretation", | ||
204 | new XMIResourceFactoryImpl) | ||
205 | } | ||
206 | |||
207 | def static void main(String[] args) { | ||
208 | if (args.length != 2) { | ||
209 | System.err.println("Usage: <query> <model size>") | ||
210 | } | ||
211 | val monitoringQuery = MonitoringQuery.valueOf(args.get(0)) | ||
212 | val modelSize = Integer.parseInt(args.get(1)) | ||
213 | init() | ||
214 | val generator = new Modes3ModelGenerator(monitoringQuery, modelSize) | ||
215 | generator.generate() | ||
216 | } | ||
217 | |||
218 | private static enum MonitoringQuery { | ||
219 | closeTrains, | ||
220 | misalignedTurnout | ||
221 | } | ||
222 | } | ||