diff options
author | 2020-04-28 00:29:00 -0400 | |
---|---|---|
committer | 2020-04-28 00:29:00 -0400 | |
commit | 68a0667e862f0714410aafbb499a95a03f5bee23 (patch) | |
tree | 34b7d690dae5b9dd8f188fc44802a02f8dd9a64a /Tests/MODELS2020-CaseStudies/case.study.pledge.run/src/run | |
parent | rename Pledge Case Studies and add familyTree case study (diff) | |
download | VIATRA-Generator-68a0667e862f0714410aafbb499a95a03f5bee23.tar.gz VIATRA-Generator-68a0667e862f0714410aafbb499a95a03f5bee23.tar.zst VIATRA-Generator-68a0667e862f0714410aafbb499a95a03f5bee23.zip |
Able to run Pledge Case Study
Diffstat (limited to 'Tests/MODELS2020-CaseStudies/case.study.pledge.run/src/run')
-rw-r--r-- | Tests/MODELS2020-CaseStudies/case.study.pledge.run/src/run/GenerateScenarios.xtend | 243 |
1 files changed, 243 insertions, 0 deletions
diff --git a/Tests/MODELS2020-CaseStudies/case.study.pledge.run/src/run/GenerateScenarios.xtend b/Tests/MODELS2020-CaseStudies/case.study.pledge.run/src/run/GenerateScenarios.xtend new file mode 100644 index 00000000..8445130a --- /dev/null +++ b/Tests/MODELS2020-CaseStudies/case.study.pledge.run/src/run/GenerateScenarios.xtend | |||
@@ -0,0 +1,243 @@ | |||
1 | package run | ||
2 | |||
3 | import Taxation.TaxationPackage | ||
4 | import hu.bme.mit.inf.dslreasoner.ecore2logic.Ecore2Logic | ||
5 | import hu.bme.mit.inf.dslreasoner.ecore2logic.Ecore2LogicConfiguration | ||
6 | import hu.bme.mit.inf.dslreasoner.ecore2logic.Ecore2Logic_Trace | ||
7 | import hu.bme.mit.inf.dslreasoner.ecore2logic.EcoreMetamodelDescriptor | ||
8 | import hu.bme.mit.inf.dslreasoner.logic.model.builder.TracedOutput | ||
9 | import hu.bme.mit.inf.dslreasoner.logic.model.logiclanguage.Type | ||
10 | import hu.bme.mit.inf.dslreasoner.logic.model.logicproblem.LogicProblem | ||
11 | import hu.bme.mit.inf.dslreasoner.logic.model.logicresult.LogicResult | ||
12 | import hu.bme.mit.inf.dslreasoner.logic.model.logicresult.ModelResult | ||
13 | import hu.bme.mit.inf.dslreasoner.logic2ecore.Logic2Ecore | ||
14 | import hu.bme.mit.inf.dslreasoner.viatra2logic.Viatra2Logic | ||
15 | import hu.bme.mit.inf.dslreasoner.viatra2logic.Viatra2LogicConfiguration | ||
16 | import hu.bme.mit.inf.dslreasoner.viatra2logic.ViatraQuerySetDescriptor | ||
17 | import hu.bme.mit.inf.dslreasoner.viatrasolver.partialinterpretation2logic.InstanceModel2Logic | ||
18 | import hu.bme.mit.inf.dslreasoner.viatrasolver.partialinterpretationlanguage.partialinterpretation.PartialInterpretation | ||
19 | import hu.bme.mit.inf.dslreasoner.viatrasolver.partialinterpretationlanguage.visualisation.PartialInterpretation2Gml | ||
20 | import hu.bme.mit.inf.dslreasoner.viatrasolver.reasoner.ViatraReasoner | ||
21 | import hu.bme.mit.inf.dslreasoner.viatrasolver.reasoner.ViatraReasonerConfiguration | ||
22 | import hu.bme.mit.inf.dslreasoner.visualisation.pi2graphviz.GraphvizVisualiser | ||
23 | import hu.bme.mit.inf.dslreasoner.workspace.FileSystemWorkspace | ||
24 | import hu.bme.mit.inf.dslreasoner.workspace.ReasonerWorkspace | ||
25 | import java.math.BigDecimal | ||
26 | import java.text.SimpleDateFormat | ||
27 | import java.util.Date | ||
28 | import java.util.HashMap | ||
29 | import java.util.List | ||
30 | import java.util.Map | ||
31 | import java.util.TreeSet | ||
32 | import org.eclipse.emf.ecore.EAttribute | ||
33 | import org.eclipse.emf.ecore.EClass | ||
34 | import org.eclipse.emf.ecore.EEnum | ||
35 | import org.eclipse.emf.ecore.EEnumLiteral | ||
36 | import org.eclipse.emf.ecore.EObject | ||
37 | import org.eclipse.emf.ecore.EPackage | ||
38 | import org.eclipse.emf.ecore.EReference | ||
39 | import org.eclipse.emf.ecore.resource.Resource | ||
40 | import org.eclipse.emf.ecore.xmi.impl.XMIResourceFactoryImpl | ||
41 | import org.eclipse.viatra.query.patternlanguage.emf.EMFPatternLanguageStandaloneSetup | ||
42 | import org.eclipse.viatra.query.runtime.api.IQueryGroup | ||
43 | import org.eclipse.viatra.query.runtime.rete.matcher.ReteEngine | ||
44 | import queries.Case_study_A | ||
45 | |||
46 | class GenerateScenarios { | ||
47 | def static void main(String[] args) { | ||
48 | val x = (new EMFPatternLanguageStandaloneSetup).createInjectorAndDoEMFRegistration | ||
49 | // //PatternLanguageWithRSSetup.createInjectorAndDoEMFRegistration | ||
50 | // ApplicationConfigurationStandaloneSetup.doSetup | ||
51 | Resource.Factory.Registry.INSTANCE.extensionToFactoryMap.put("xmi", new XMIResourceFactoryImpl) | ||
52 | Resource.Factory.Registry.INSTANCE.extensionToFactoryMap.put("logicproblem", new XMIResourceFactoryImpl) | ||
53 | Resource.Factory.Registry.INSTANCE.extensionToFactoryMap.put("partialmodel", new XMIResourceFactoryImpl) | ||
54 | ReteEngine.getClass | ||
55 | |||
56 | val Ecore2Logic ecore2Logic = new Ecore2Logic | ||
57 | val Logic2Ecore logic2Ecore = new Logic2Ecore(ecore2Logic) | ||
58 | val Viatra2Logic viatra2Logic = new Viatra2Logic(ecore2Logic) | ||
59 | val InstanceModel2Logic instanceModel2Logic = new InstanceModel2Logic | ||
60 | |||
61 | // Workspace setup | ||
62 | val Date date = new Date(System.currentTimeMillis) | ||
63 | val SimpleDateFormat format = new SimpleDateFormat("dd-HHmm"); | ||
64 | val formattedDate = format.format(date) | ||
65 | |||
66 | val inputs = new FileSystemWorkspace('''inputs/''', "") | ||
67 | val workspace = new FileSystemWorkspace('''output/''' + formattedDate + '''/''', "") | ||
68 | workspace.initAndClear | ||
69 | |||
70 | println("Input and output workspaces are created") | ||
71 | |||
72 | // print(TaxationPackage.eINSTANCE.getEClassifiers.filter(EClass).toList.get(0)) | ||
73 | |||
74 | //* | ||
75 | val metamodel = loadMetamodel(TaxationPackage.eINSTANCE) | ||
76 | val partialModel = loadPartialModel(inputs, "Household.xmi") | ||
77 | val queries = loadQueries(metamodel, Case_study_A.instance) | ||
78 | /*/ | ||
79 | val metamodel = loadMetamodel(FamilytreePackage.eINSTANCE) | ||
80 | // val partialModel = loadPartialModel(inputs, "yakindu/Yakindu.xmi") | ||
81 | val queries = loadQueries(metamodel, FamilyTreeConstraints.instance) | ||
82 | //*/ | ||
83 | println("DSL loaded") | ||
84 | |||
85 | val SIZE = 10 | ||
86 | var REPS = 1 | ||
87 | val RUNTIME = 600 | ||
88 | |||
89 | // ///////////////////////// | ||
90 | // Prepare Problem | ||
91 | val modelGenerationProblem = ecore2Logic.transformMetamodel(metamodel, new Ecore2LogicConfiguration()) | ||
92 | var problem = modelGenerationProblem.output | ||
93 | |||
94 | // val modelExtensionProblem = instanceModel2Logic.transform(modelGenerationProblem, partialModel) | ||
95 | // problem = modelExtensionProblem.output | ||
96 | |||
97 | val validModelExtensionProblem = viatra2Logic.transformQueries(queries, modelGenerationProblem, | ||
98 | new Viatra2LogicConfiguration) | ||
99 | problem = validModelExtensionProblem.output | ||
100 | |||
101 | workspace.writeModel(problem, "problem.logicproblem") | ||
102 | println("Problem created") | ||
103 | |||
104 | for (var i = 0; i < REPS; i++) { | ||
105 | |||
106 | println("Run #" + i + ":") | ||
107 | |||
108 | var ViatraReasoner reasoner = new ViatraReasoner | ||
109 | |||
110 | // ///////////////////////////////////////////////////// | ||
111 | // Define Config File | ||
112 | val knownIntegers = new TreeSet<Integer> | ||
113 | knownIntegers.addAll(0) | ||
114 | |||
115 | val knownReals = new TreeSet<BigDecimal> | ||
116 | knownReals.addAll(new BigDecimal("0.0")) | ||
117 | |||
118 | val knownStrings = new TreeSet<String> | ||
119 | knownStrings.addAll("r0", "r1", "r2") | ||
120 | |||
121 | |||
122 | val solverConfig = new ViatraReasonerConfiguration => [ | ||
123 | it.runtimeLimit = RUNTIME | ||
124 | it.typeScopes.maxNewElements = SIZE | ||
125 | // it.typeScopes.minNewElements = SIZE | ||
126 | if(!knownIntegers.isEmpty) it.typeScopes.knownIntegers = knownIntegers | ||
127 | if(!knownReals.isEmpty) it.typeScopes.knownReals = knownReals | ||
128 | // if(!knownStrings.isEmpty) it.typeScopes.knownStrings = knownStrings | ||
129 | ] | ||
130 | |||
131 | val startTime = System.currentTimeMillis | ||
132 | var solution = reasoner.solve(problem, solverConfig, workspace) | ||
133 | val totalTime = System.currentTimeMillis-startTime | ||
134 | |||
135 | println(" Problem Solved") | ||
136 | solution.writeStats(totalTime) | ||
137 | |||
138 | if (solution instanceof ModelResult) { | ||
139 | solution.writeRepresentation(workspace, i) | ||
140 | // solution.writeInterpretation(logic2Ecore, workspace, i, reasoner, modelGenerationProblem) | ||
141 | println(" Solution saved and visualised") | ||
142 | } else | ||
143 | println(" Returned: " + solution.class.simpleName.toString) | ||
144 | |||
145 | |||
146 | // println("Stats Created") | ||
147 | // Run Garbage Collector | ||
148 | val Runtime r = Runtime.getRuntime(); | ||
149 | r.gc(); | ||
150 | r.gc(); | ||
151 | r.gc(); | ||
152 | Thread.sleep(3000) | ||
153 | } | ||
154 | println("---End---") | ||
155 | |||
156 | } | ||
157 | |||
158 | def static Map<Type, Integer> getTypeMap(Map<Class, Integer> classMap, EcoreMetamodelDescriptor metamodel, | ||
159 | Ecore2Logic e2l, Ecore2Logic_Trace trace) { | ||
160 | val typeMap = new HashMap<Type, Integer> | ||
161 | val listMap = metamodel.classes.toMap[s|s.name] | ||
162 | |||
163 | for (Class elem : classMap.keySet) { | ||
164 | typeMap.put(e2l.TypeofEClass( | ||
165 | trace, | ||
166 | listMap.get(elem.simpleName) | ||
167 | ), classMap.get(elem)) | ||
168 | } | ||
169 | return typeMap | ||
170 | } | ||
171 | |||
172 | def static loadMetamodel(EPackage pckg) { | ||
173 | val List<EClass> classes = pckg.getEClassifiers.filter(EClass).toList | ||
174 | val List<EEnum> enums = pckg.getEClassifiers.filter(EEnum).toList | ||
175 | val List<EEnumLiteral> literals = enums.map[getELiterals].flatten.toList | ||
176 | val List<EReference> references = classes.map[getEReferences].flatten.toList | ||
177 | val List<EAttribute> attributes = classes.map[getEAttributes].flatten.toList | ||
178 | return new EcoreMetamodelDescriptor(classes, #{}, false, enums, literals, references, attributes) | ||
179 | } | ||
180 | |||
181 | def static loadPartialModel(ReasonerWorkspace inputs, String path) { | ||
182 | Resource.Factory.Registry.INSTANCE.getExtensionToFactoryMap().put("*", new XMIResourceFactoryImpl()) | ||
183 | inputs.readModel(EObject, path).eResource.contents | ||
184 | // inputs.readModel(EObject,"FamInstance.xmi").eResource.allContents.toList | ||
185 | } | ||
186 | |||
187 | def static loadQueries(EcoreMetamodelDescriptor metamodel, IQueryGroup i) { | ||
188 | val patterns = i.specifications.toList | ||
189 | val wfPatterns = patterns.filter[it.allAnnotations.exists[it.name == "Constraint"]].toSet | ||
190 | val derivedFeatures = emptyMap | ||
191 | // NO DERIVED FEATURES | ||
192 | // val derivedFeatures = new LinkedHashMap | ||
193 | // derivedFeatures.put(i.type,metamodel.attributes.filter[it.name == "type"].head) | ||
194 | // derivedFeatures.put(i.model,metamodel.references.filter[it.name == "model"].head) | ||
195 | val res = new ViatraQuerySetDescriptor( | ||
196 | patterns, | ||
197 | wfPatterns, | ||
198 | derivedFeatures | ||
199 | ) | ||
200 | return res | ||
201 | } | ||
202 | |||
203 | def static writeInterpretation(LogicResult solution, Logic2Ecore logic2Ecore, ReasonerWorkspace workspace, int i, | ||
204 | ViatraReasoner reasoner, TracedOutput<LogicProblem, Ecore2Logic_Trace> mgProb) { | ||
205 | val interpretations = reasoner.getInterpretations(solution as ModelResult) | ||
206 | for (interpIndex : 0 ..< interpretations.size) { | ||
207 | // val extension b = new LogicStructureBuilder | ||
208 | // val extension a = new LogicProblemBuilder | ||
209 | val interpretation = interpretations.get(interpIndex) | ||
210 | val model = logic2Ecore.transformInterpretation(interpretation, mgProb.trace) | ||
211 | workspace.writeModel(model, '''solution«i»_«interpIndex».xmi''') | ||
212 | } | ||
213 | } | ||
214 | |||
215 | def static writeRepresentation(LogicResult solution, ReasonerWorkspace workspace, int i) { | ||
216 | val representations = solution.representation | ||
217 | for (representationIndex : 0 ..< representations.size) { | ||
218 | val representation = representations.get(representationIndex) | ||
219 | if (representation instanceof PartialInterpretation) { | ||
220 | val gml = (new PartialInterpretation2Gml).transform(representation) | ||
221 | workspace.writeText('''solution«i»_«representationIndex».gml''', gml) | ||
222 | |||
223 | val png = (new GraphvizVisualiser).visualiseConcretization(representation) | ||
224 | png.writeToFile(workspace, '''solution«i»_«representationIndex».png''') | ||
225 | |||
226 | // workspace.writeModel(representation, '''solution«representationIndex».partialintrpretation''') | ||
227 | } else { | ||
228 | workspace.writeText('''solution«representationIndex».txt''', representation.toString) | ||
229 | } | ||
230 | } | ||
231 | } | ||
232 | |||
233 | def static writeStats(LogicResult solution, long time) { | ||
234 | val stats = solution.statistics | ||
235 | println(" Statistics:") | ||
236 | // for (e : stats.entries.filter[name.substring(0, 9) == "_Solution"]) { | ||
237 | // println(" " + e.name + ": " + (e as IntStatisticEntry).value + " ms") | ||
238 | // } | ||
239 | println(" \"solve\" time: " + time as double/1000 + " s") | ||
240 | |||
241 | // println("<<End Statistics>>") | ||
242 | } | ||
243 | } | ||