diff options
author | 2020-05-17 01:58:13 -0400 | |
---|---|---|
committer | 2020-05-17 01:58:13 -0400 | |
commit | 48871e1be75169e4e3768a7c0c47791e02c7e634 (patch) | |
tree | cfcd4e4fdc0dea4f4b0102a610459ed7509f47f8 /Tests/MODELS2020-CaseStudies/case.study.pledge.run/src/run/GeneratePledgeModels.xtend | |
parent | Adjust Taxation model. (diff) | |
download | VIATRA-Generator-48871e1be75169e4e3768a7c0c47791e02c7e634.tar.gz VIATRA-Generator-48871e1be75169e4e3768a7c0c47791e02c7e634.tar.zst VIATRA-Generator-48871e1be75169e4e3768a7c0c47791e02c7e634.zip |
update measurements setup. make archives.
Diffstat (limited to 'Tests/MODELS2020-CaseStudies/case.study.pledge.run/src/run/GeneratePledgeModels.xtend')
-rw-r--r-- | Tests/MODELS2020-CaseStudies/case.study.pledge.run/src/run/GeneratePledgeModels.xtend | 360 |
1 files changed, 0 insertions, 360 deletions
diff --git a/Tests/MODELS2020-CaseStudies/case.study.pledge.run/src/run/GeneratePledgeModels.xtend b/Tests/MODELS2020-CaseStudies/case.study.pledge.run/src/run/GeneratePledgeModels.xtend deleted file mode 100644 index 23a34bf1..00000000 --- a/Tests/MODELS2020-CaseStudies/case.study.pledge.run/src/run/GeneratePledgeModels.xtend +++ /dev/null | |||
@@ -1,360 +0,0 @@ | |||
1 | package run | ||
2 | |||
3 | import Taxation.TaxationPackage | ||
4 | import familytree.FamilytreePackage | ||
5 | import hu.bme.mit.inf.dslreasoner.ecore2logic.Ecore2Logic | ||
6 | import hu.bme.mit.inf.dslreasoner.ecore2logic.Ecore2LogicConfiguration | ||
7 | import hu.bme.mit.inf.dslreasoner.ecore2logic.Ecore2Logic_Trace | ||
8 | import hu.bme.mit.inf.dslreasoner.ecore2logic.EcoreMetamodelDescriptor | ||
9 | import hu.bme.mit.inf.dslreasoner.logic.model.builder.DocumentationLevel | ||
10 | import hu.bme.mit.inf.dslreasoner.logic.model.builder.TracedOutput | ||
11 | import hu.bme.mit.inf.dslreasoner.logic.model.logiclanguage.Type | ||
12 | import hu.bme.mit.inf.dslreasoner.logic.model.logicproblem.LogicProblem | ||
13 | import hu.bme.mit.inf.dslreasoner.logic.model.logicresult.LogicResult | ||
14 | import hu.bme.mit.inf.dslreasoner.logic.model.logicresult.ModelResult | ||
15 | import hu.bme.mit.inf.dslreasoner.logic.model.logicresult.impl.ModelResultImpl | ||
16 | import hu.bme.mit.inf.dslreasoner.logic2ecore.Logic2Ecore | ||
17 | import hu.bme.mit.inf.dslreasoner.viatra2logic.Viatra2Logic | ||
18 | import hu.bme.mit.inf.dslreasoner.viatra2logic.Viatra2LogicConfiguration | ||
19 | import hu.bme.mit.inf.dslreasoner.viatra2logic.ViatraQuerySetDescriptor | ||
20 | import hu.bme.mit.inf.dslreasoner.viatrasolver.partialinterpretation2logic.InstanceModel2Logic | ||
21 | import hu.bme.mit.inf.dslreasoner.viatrasolver.partialinterpretationlanguage.partialinterpretation.PartialInterpretation | ||
22 | import hu.bme.mit.inf.dslreasoner.viatrasolver.partialinterpretationlanguage.visualisation.PartialInterpretation2Gml | ||
23 | import hu.bme.mit.inf.dslreasoner.viatrasolver.reasoner.ViatraReasoner | ||
24 | import hu.bme.mit.inf.dslreasoner.viatrasolver.reasoner.ViatraReasonerConfiguration | ||
25 | import hu.bme.mit.inf.dslreasoner.visualisation.pi2graphviz.GraphvizVisualiser | ||
26 | import hu.bme.mit.inf.dslreasoner.workspace.FileSystemWorkspace | ||
27 | import hu.bme.mit.inf.dslreasoner.workspace.ReasonerWorkspace | ||
28 | import java.io.PrintWriter | ||
29 | import java.math.BigDecimal | ||
30 | import java.text.SimpleDateFormat | ||
31 | import java.util.Date | ||
32 | import java.util.HashMap | ||
33 | import java.util.List | ||
34 | import java.util.Map | ||
35 | import java.util.TreeSet | ||
36 | import org.eclipse.emf.common.util.EList | ||
37 | import org.eclipse.emf.ecore.EAttribute | ||
38 | import org.eclipse.emf.ecore.EClass | ||
39 | import org.eclipse.emf.ecore.EEnum | ||
40 | import org.eclipse.emf.ecore.EEnumLiteral | ||
41 | import org.eclipse.emf.ecore.EObject | ||
42 | import org.eclipse.emf.ecore.EPackage | ||
43 | import org.eclipse.emf.ecore.EReference | ||
44 | import org.eclipse.emf.ecore.resource.Resource | ||
45 | import org.eclipse.emf.ecore.xmi.impl.XMIResourceFactoryImpl | ||
46 | import org.eclipse.viatra.query.patternlanguage.emf.EMFPatternLanguageStandaloneSetup | ||
47 | import org.eclipse.viatra.query.runtime.api.IQueryGroup | ||
48 | import org.eclipse.viatra.query.runtime.rete.matcher.ReteEngine | ||
49 | import queries.Case_study_A | ||
50 | import queries.FamilyTreeConstraints | ||
51 | |||
52 | class GeneratePledgeModels { | ||
53 | static val SIZE_LB = 20 | ||
54 | static val SIZE_UB = 20 | ||
55 | static val SIZE_MUL = 1 | ||
56 | static val SIZE_INC = 5 | ||
57 | |||
58 | static var REPS = 1 | ||
59 | static val RUNTIME = 600 | ||
60 | |||
61 | static val DOMAIN = "FamilyTree" // "FamilyTree", "Taxation" | ||
62 | static val QUERIES = false | ||
63 | static val INDIV_WRT = true | ||
64 | static val GLOBAL_WRT = true | ||
65 | static val q2t = if(QUERIES) "Y" else "N" | ||
66 | |||
67 | def static void main(String[] args) { | ||
68 | (new EMFPatternLanguageStandaloneSetup).createInjectorAndDoEMFRegistration | ||
69 | Resource.Factory.Registry.INSTANCE.extensionToFactoryMap.put("xmi", new XMIResourceFactoryImpl) | ||
70 | Resource.Factory.Registry.INSTANCE.extensionToFactoryMap.put("logicproblem", new XMIResourceFactoryImpl) | ||
71 | Resource.Factory.Registry.INSTANCE.extensionToFactoryMap.put("partialmodel", new XMIResourceFactoryImpl) | ||
72 | ReteEngine.getClass | ||
73 | val Ecore2Logic ecore2Logic = new Ecore2Logic | ||
74 | val Logic2Ecore logic2Ecore = new Logic2Ecore(ecore2Logic) | ||
75 | val Viatra2Logic viatra2Logic = new Viatra2Logic(ecore2Logic) | ||
76 | val InstanceModel2Logic instanceModel2Logic = new InstanceModel2Logic | ||
77 | |||
78 | // Workspace setup | ||
79 | val Date date = new Date(System.currentTimeMillis) | ||
80 | val SimpleDateFormat format = new SimpleDateFormat("dd-HHmm"); | ||
81 | val formattedDate = format.format(date) | ||
82 | |||
83 | val inputs = new FileSystemWorkspace('''inputs/''', "") | ||
84 | val dataws = new FileSystemWorkspace('''output/''', "") | ||
85 | val modelsws = new FileSystemWorkspace('''output/''' + formattedDate + '''/models/''', "") | ||
86 | val workspace = new FileSystemWorkspace('''output/''' + formattedDate + '''/''', "") | ||
87 | val debug = new FileSystemWorkspace('''output/''' + DOMAIN + "-" + formattedDate + '''/debug/''', "") | ||
88 | workspace.initAndClear | ||
89 | modelsws.initAndClear | ||
90 | |||
91 | println("Input and output workspaces are created") | ||
92 | |||
93 | // ////////////////////////// | ||
94 | // DOMAIN | ||
95 | var EcoreMetamodelDescriptor metamodel = null | ||
96 | var EList<EObject> partialModel = null | ||
97 | var ViatraQuerySetDescriptor queries = null | ||
98 | switch DOMAIN { | ||
99 | case "Taxation": { | ||
100 | metamodel = loadMetamodel(TaxationPackage.eINSTANCE) | ||
101 | partialModel = loadPartialModel(inputs, "Household.xmi") | ||
102 | // queries = loadQueries(metamodel, Case_study_A.instance) | ||
103 | } | ||
104 | case "FamilyTree": { | ||
105 | metamodel = loadMetamodel(FamilytreePackage.eINSTANCE) | ||
106 | partialModel = loadPartialModel(inputs, "FamilyTree.xmi") | ||
107 | // queries = loadQueries(metamodel, FamilyTreeConstraints.instance) | ||
108 | } | ||
109 | default: { | ||
110 | System.err.println("Unsupported Domain") | ||
111 | throw new Exception | ||
112 | } | ||
113 | } | ||
114 | println("DSL loaded") | ||
115 | |||
116 | // ///////////////////////// | ||
117 | // Prepare Problem | ||
118 | val modelGenerationProblem = ecore2Logic.transformMetamodel(metamodel, new Ecore2LogicConfiguration()) | ||
119 | var problem = modelGenerationProblem.output | ||
120 | val modelExtensionProblem = instanceModel2Logic.transform(modelGenerationProblem, partialModel) | ||
121 | problem = modelExtensionProblem.output | ||
122 | if (QUERIES) { | ||
123 | val validModelExtensionProblem = viatra2Logic.transformQueries(queries, modelExtensionProblem, | ||
124 | new Viatra2LogicConfiguration) | ||
125 | problem = validModelExtensionProblem.output | ||
126 | } | ||
127 | // debug.writeModel(problem, "problem.logicproblem") | ||
128 | println("Problem created") | ||
129 | // ///////////////////////// | ||
130 | // BEGIN RUN | ||
131 | var PrintWriter global_writer = null | ||
132 | val header = "domain, queries?,size,model?,timeout,measuredTime,TransformatonTime,SolverTime\n" | ||
133 | if (GLOBAL_WRT) { | ||
134 | global_writer = new PrintWriter( | ||
135 | workspace.workspaceURI + "//_" + DOMAIN + SIZE_LB + "to" + SIZE_UB + "x" + REPS + "q" + q2t + "-" + | ||
136 | formattedDate + ".csv") | ||
137 | global_writer.append(header) | ||
138 | } | ||
139 | |||
140 | var NEXT_INC = SIZE_INC | ||
141 | for (var size = SIZE_LB; size <= SIZE_UB; size += NEXT_INC) { | ||
142 | NEXT_INC *= SIZE_MUL | ||
143 | |||
144 | var PrintWriter indiv_writer = null | ||
145 | if (INDIV_WRT) { | ||
146 | indiv_writer = new PrintWriter( // TODO | ||
147 | workspace.workspaceURI + "//__" + DOMAIN + "Sz" + size + "x" + REPS + "q" + q2t + "-" + formattedDate + | ||
148 | ".csv") | ||
149 | indiv_writer.append(header) | ||
150 | } | ||
151 | |||
152 | println() | ||
153 | println("DOMAIN: " + DOMAIN + ", SIZE=" + size) | ||
154 | |||
155 | for (var i = 0; i < REPS; i++) { | ||
156 | |||
157 | print("<<Run number " + i + ">> : ") | ||
158 | |||
159 | var ViatraReasoner reasoner = new ViatraReasoner | ||
160 | |||
161 | // ///////////////////////////////////////////////////// | ||
162 | // Define Config File | ||
163 | val knownIntegers = new TreeSet<Integer> | ||
164 | knownIntegers.addAll(0, 10, 20, 30, 40, 50) | ||
165 | |||
166 | val knownReals = new TreeSet<BigDecimal> | ||
167 | knownReals.addAll(new BigDecimal("0.0")) | ||
168 | |||
169 | val knownStrings = new TreeSet<String> | ||
170 | knownStrings.addAll("r0", "r1", "r2", "r3", "r4", "r5", "r6") | ||
171 | |||
172 | val curSize = size | ||
173 | val solverConfig = new ViatraReasonerConfiguration => [ | ||
174 | it.documentationLevel = DocumentationLevel::NONE | ||
175 | // it.debugCongiguration = new DebugConfiguration => [logging = true] | ||
176 | it.runtimeLimit = RUNTIME | ||
177 | // it.typeScopes.maxNewElements = SIZE | ||
178 | it.typeScopes.minNewElements = curSize | ||
179 | |||
180 | // if(!knownIntegers.isEmpty) it.typeScopes.knownIntegers = knownIntegers | ||
181 | if(!knownReals.isEmpty) it.typeScopes.knownReals = knownReals | ||
182 | // if(!knownStrings.isEmpty) it.typeScopes.knownStrings = knownStrings | ||
183 | ] | ||
184 | |||
185 | val startTime = System.currentTimeMillis | ||
186 | var solution = reasoner.solve(problem, solverConfig, debug) | ||
187 | val measuredTime = System.currentTimeMillis - startTime | ||
188 | |||
189 | print("<<END") | ||
190 | |||
191 | val toAddtoCSV = DOMAIN + "," + QUERIES + "," + size + "," + (solution.class == ModelResultImpl).toString + | ||
192 | "," + RUNTIME + "," + measuredTime / 1000.0 + "," + solution.statistics.transformationTime / 1000.0 + | ||
193 | "," + solution.statistics.solverTime / 1000.0 + "\n" | ||
194 | |||
195 | if (GLOBAL_WRT) global_writer.append(toAddtoCSV) | ||
196 | // { | ||
197 | // global_writer.append(DOMAIN + ",") | ||
198 | // global_writer.append(QUERIES + ",") | ||
199 | // global_writer.append(size + ",") | ||
200 | // global_writer.append((solution.class == ModelResultImpl).toString + ",") | ||
201 | // global_writer.append(RUNTIME + ",") | ||
202 | // global_writer.append(measuredTime / 1000.0 + ",") | ||
203 | // | ||
204 | // global_writer.append(solution.statistics.transformationTime / 1000.0 + ",") | ||
205 | // global_writer.append(solution.statistics.solverTime / 1000.0 + "") | ||
206 | // global_writer.append("\n") | ||
207 | // } | ||
208 | if (INDIV_WRT) indiv_writer.append(toAddtoCSV) | ||
209 | // { | ||
210 | // indiv_writer.append(DOMAIN + ",") | ||
211 | // indiv_writer.append(QUERIES + ",") | ||
212 | // indiv_writer.append(size + ",") | ||
213 | // indiv_writer.append((solution.class == ModelResultImpl).toString + ",") | ||
214 | // global_writer.append(RUNTIME + ",") | ||
215 | // indiv_writer.append(measuredTime / 1000.0 + ",") | ||
216 | // | ||
217 | // indiv_writer.append(solution.statistics.transformationTime / 1000.0 + ",") | ||
218 | // indiv_writer.append(solution.statistics.solverTime / 1000.0 + "") | ||
219 | // indiv_writer.append("\n") | ||
220 | // } | ||
221 | |||
222 | println("->" + measuredTime / 1000.0 + "s") | ||
223 | |||
224 | // solution.writeStats(totalTime, solverConfig) | ||
225 | try { | ||
226 | if (solution instanceof ModelResult) { | ||
227 | solution.writeInterpretation(logic2Ecore, modelsws, size + "_" + i, reasoner, modelGenerationProblem) | ||
228 | println(" (XMI Success)") | ||
229 | } | ||
230 | } catch (Exception e) { | ||
231 | System.err.println(" (XMI Error)" + e.message) | ||
232 | } | ||
233 | |||
234 | try { | ||
235 | if (solution instanceof ModelResult) { | ||
236 | solution.writeRepresentation(modelsws, size + "_" + i) | ||
237 | println(" (VIZ Success) >>") | ||
238 | } | ||
239 | } catch (Exception e) { | ||
240 | System.err.println(" (VIZ Error)" + e.message + ">>") | ||
241 | } | ||
242 | |||
243 | println() | ||
244 | // Run Garbage Collector | ||
245 | val Runtime r = Runtime.getRuntime(); | ||
246 | r.gc(); | ||
247 | r.gc(); | ||
248 | r.gc(); | ||
249 | Thread.sleep(3000) | ||
250 | } | ||
251 | if(INDIV_WRT) indiv_writer.close | ||
252 | } | ||
253 | if(GLOBAL_WRT) global_writer.close | ||
254 | } | ||
255 | |||
256 | def | ||
257 | |||
258 | static Map<Type, Integer> getTypeMap(Map<Class, Integer> classMap, EcoreMetamodelDescriptor metamodel, | ||
259 | Ecore2Logic e2l, Ecore2Logic_Trace trace) { | ||
260 | val typeMap = new HashMap<Type, Integer> | ||
261 | val listMap = metamodel.classes.toMap[s|s.name] | ||
262 | |||
263 | for (Class elem : classMap.keySet) { | ||
264 | typeMap.put(e2l.TypeofEClass( | ||
265 | trace, | ||
266 | listMap.get(elem.simpleName) | ||
267 | ), classMap.get(elem)) | ||
268 | } | ||
269 | return typeMap | ||
270 | } | ||
271 | |||
272 | def | ||
273 | |||
274 | static loadMetamodel(EPackage pckg) { | ||
275 | val List<EClass> classes = pckg.getEClassifiers.filter(EClass).toList | ||
276 | val List<EEnum> enums = pckg.getEClassifiers.filter(EEnum).toList | ||
277 | val List<EEnumLiteral> literals = enums.map[getELiterals].flatten.toList | ||
278 | val List<EReference> references = classes.map[getEReferences].flatten.toList | ||
279 | val List<EAttribute> attributes = classes.map[getEAttributes].flatten.toList | ||
280 | return new EcoreMetamodelDescriptor(classes, #{}, false, enums, literals, references, attributes) | ||
281 | } | ||
282 | |||
283 | def | ||
284 | |||
285 | static loadPartialModel(ReasonerWorkspace inputs, String path) { | ||
286 | Resource.Factory.Registry.INSTANCE.getExtensionToFactoryMap().put("*", new XMIResourceFactoryImpl()) | ||
287 | inputs.readModel(EObject, path).eResource.contents | ||
288 | // inputs.readModel(EObject,"FamInstance.xmi").eResource.allContents.toList | ||
289 | } | ||
290 | |||
291 | def | ||
292 | |||
293 | static loadQueries(EcoreMetamodelDescriptor metamodel, IQueryGroup i) { | ||
294 | val patterns = i.specifications.toList | ||
295 | val wfPatterns = patterns.filter[it.allAnnotations.exists[it.name == "Constraint"]].toSet | ||
296 | val derivedFeatures = emptyMap | ||
297 | // NO DERIVED FEATURES | ||
298 | // val derivedFeatures = new LinkedHashMap | ||
299 | // derivedFeatures.put(i.type,metamodel.attributes.filter[it.name == "type"].head) | ||
300 | // derivedFeatures.put(i.model,metamodel.references.filter[it.name == "model"].head) | ||
301 | val res = new ViatraQuerySetDescriptor( | ||
302 | patterns, | ||
303 | wfPatterns, | ||
304 | derivedFeatures | ||
305 | ) | ||
306 | return res | ||
307 | } | ||
308 | |||
309 | def | ||
310 | |||
311 | static writeInterpretation(LogicResult solution, Logic2Ecore logic2Ecore, ReasonerWorkspace workspace, | ||
312 | String id, ViatraReasoner reasoner, TracedOutput<LogicProblem, Ecore2Logic_Trace> mgProb) { | ||
313 | val interpretations = reasoner.getInterpretations(solution as ModelResult) | ||
314 | for (interpIndex : 0 ..< interpretations.size) { | ||
315 | // val extension b = new LogicStructureBuilder | ||
316 | // val extension a = new LogicProblemBuilder | ||
317 | val interpretation = interpretations.get(interpIndex) | ||
318 | val model = logic2Ecore.transformInterpretation(interpretation, mgProb.trace) | ||
319 | // println(model) | ||
320 | workspace.writeModel(model, '''sol-«id»_«interpIndex».xmi''') | ||
321 | } | ||
322 | } | ||
323 | |||
324 | def | ||
325 | |||
326 | static writeRepresentation(LogicResult solution, ReasonerWorkspace workspace, String id) { | ||
327 | val representations = solution.representation | ||
328 | for (representationIndex : 0 ..< representations.size) { | ||
329 | val representation = representations.get(representationIndex) | ||
330 | if (representation instanceof PartialInterpretation) { | ||
331 | val gml = (new PartialInterpretation2Gml).transform(representation) | ||
332 | workspace.writeText('''sol-«id»_«representationIndex».gml''', gml) | ||
333 | |||
334 | val png = (new GraphvizVisualiser).visualiseConcretization(representation) | ||
335 | // println(png) | ||
336 | png.writeToFile(workspace, '''sol-«id»_«representationIndex».png''') | ||
337 | |||
338 | // workspace.writeModel(representation, '''solution«representationIndex».partialintrpretation''') | ||
339 | } else { | ||
340 | workspace.writeText('''sol-«representationIndex».txt''', representation.toString) | ||
341 | } | ||
342 | } | ||
343 | } | ||
344 | |||
345 | def | ||
346 | |||
347 | static writeStats(LogicResult solution, long time, ViatraReasonerConfiguration config) { | ||
348 | val stats = solution.statistics | ||
349 | println(" Statistics:") | ||
350 | // for (e : stats.entries.filter[name.substring(0, 9) == "_Solution"]) { | ||
351 | // println(" " + e.name + ": " + (e as IntStatisticEntry).value + " ms") | ||
352 | // } | ||
353 | println( | ||
354 | " #new nodes : [" + config.typeScopes.minNewElements + ".." + | ||
355 | (if(config.typeScopes.maxNewElements == 2147483647) "*" else config.typeScopes.maxNewElements) + "]") | ||
356 | println(" \"solve\" time: " + time as double / 1000 + " s") | ||
357 | |||
358 | // println("<<End Statistics>>") | ||
359 | } | ||
360 | } | ||