From 79ae20115e73597fbaf18d5429d7b2788dd4efb6 Mon Sep 17 00:00:00 2001 From: ArenBabikian Date: Sat, 9 May 2020 19:09:06 -0400 Subject: Adjust PLEDEG Java Case Study file --- .../src/run/GeneratePledgeModels.xtend | 331 ++++++++++++++------- 1 file changed, 223 insertions(+), 108 deletions(-) (limited to 'Tests/MODELS2020-CaseStudies/case.study.pledge.run/src/run/GeneratePledgeModels.xtend') 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 index ff0e1d03..3f5855d8 100644 --- a/Tests/MODELS2020-CaseStudies/case.study.pledge.run/src/run/GeneratePledgeModels.xtend +++ b/Tests/MODELS2020-CaseStudies/case.study.pledge.run/src/run/GeneratePledgeModels.xtend @@ -1,6 +1,7 @@ package run import Taxation.TaxationPackage +import familytree.FamilytreePackage import hu.bme.mit.inf.dslreasoner.ecore2logic.Ecore2Logic import hu.bme.mit.inf.dslreasoner.ecore2logic.Ecore2LogicConfiguration import hu.bme.mit.inf.dslreasoner.ecore2logic.Ecore2Logic_Trace @@ -11,6 +12,7 @@ import hu.bme.mit.inf.dslreasoner.logic.model.logiclanguage.Type import hu.bme.mit.inf.dslreasoner.logic.model.logicproblem.LogicProblem import hu.bme.mit.inf.dslreasoner.logic.model.logicresult.LogicResult import hu.bme.mit.inf.dslreasoner.logic.model.logicresult.ModelResult +import hu.bme.mit.inf.dslreasoner.logic.model.logicresult.impl.ModelResultImpl import hu.bme.mit.inf.dslreasoner.logic2ecore.Logic2Ecore import hu.bme.mit.inf.dslreasoner.viatra2logic.Viatra2Logic import hu.bme.mit.inf.dslreasoner.viatra2logic.Viatra2LogicConfiguration @@ -18,12 +20,12 @@ import hu.bme.mit.inf.dslreasoner.viatra2logic.ViatraQuerySetDescriptor import hu.bme.mit.inf.dslreasoner.viatrasolver.partialinterpretation2logic.InstanceModel2Logic import hu.bme.mit.inf.dslreasoner.viatrasolver.partialinterpretationlanguage.partialinterpretation.PartialInterpretation import hu.bme.mit.inf.dslreasoner.viatrasolver.partialinterpretationlanguage.visualisation.PartialInterpretation2Gml -import hu.bme.mit.inf.dslreasoner.viatrasolver.reasoner.DebugConfiguration import hu.bme.mit.inf.dslreasoner.viatrasolver.reasoner.ViatraReasoner import hu.bme.mit.inf.dslreasoner.viatrasolver.reasoner.ViatraReasonerConfiguration import hu.bme.mit.inf.dslreasoner.visualisation.pi2graphviz.GraphvizVisualiser import hu.bme.mit.inf.dslreasoner.workspace.FileSystemWorkspace import hu.bme.mit.inf.dslreasoner.workspace.ReasonerWorkspace +import java.io.PrintWriter import java.math.BigDecimal import java.text.SimpleDateFormat import java.util.Date @@ -31,6 +33,7 @@ import java.util.HashMap import java.util.List import java.util.Map import java.util.TreeSet +import org.eclipse.emf.common.util.EList import org.eclipse.emf.ecore.EAttribute import org.eclipse.emf.ecore.EClass import org.eclipse.emf.ecore.EEnum @@ -43,118 +46,216 @@ import org.eclipse.emf.ecore.xmi.impl.XMIResourceFactoryImpl import org.eclipse.viatra.query.patternlanguage.emf.EMFPatternLanguageStandaloneSetup import org.eclipse.viatra.query.runtime.api.IQueryGroup import org.eclipse.viatra.query.runtime.rete.matcher.ReteEngine +import queries.Case_study_A +import queries.FamilyTreeConstraints class GeneratePledgeModels { - def static void main(String[] args) { - val x = (new EMFPatternLanguageStandaloneSetup).createInjectorAndDoEMFRegistration -// //PatternLanguageWithRSSetup.createInjectorAndDoEMFRegistration -// ApplicationConfigurationStandaloneSetup.doSetup - Resource.Factory.Registry.INSTANCE.extensionToFactoryMap.put("xmi", new XMIResourceFactoryImpl) - Resource.Factory.Registry.INSTANCE.extensionToFactoryMap.put("logicproblem", new XMIResourceFactoryImpl) - Resource.Factory.Registry.INSTANCE.extensionToFactoryMap.put("partialmodel", new XMIResourceFactoryImpl) - ReteEngine.getClass - - val Ecore2Logic ecore2Logic = new Ecore2Logic - val Logic2Ecore logic2Ecore = new Logic2Ecore(ecore2Logic) - val Viatra2Logic viatra2Logic = new Viatra2Logic(ecore2Logic) - val InstanceModel2Logic instanceModel2Logic = new InstanceModel2Logic - - // Workspace setup - val Date date = new Date(System.currentTimeMillis) - val SimpleDateFormat format = new SimpleDateFormat("dd-HHmm"); - val formattedDate = format.format(date) - - val inputs = new FileSystemWorkspace('''inputs/''', "") - val workspace = new FileSystemWorkspace('''output/''' + formattedDate + '''/''', "") - val debug = new FileSystemWorkspace('''output/''' + formattedDate + '''/debug/''', "") - workspace.initAndClear - - println("Input and output workspaces are created") - -// print(TaxationPackage.eINSTANCE.getEClassifiers.filter(EClass).toList.get(0)) - //* - val metamodel = loadMetamodel(TaxationPackage.eINSTANCE) - val partialModel = loadPartialModel(inputs, "Household.xmi") - // val queries = loadQueries(metamodel, Case_study_A.instance) - /*/ - val metamodel = loadMetamodel(FamilytreePackage.eINSTANCE) -// val partialModel = loadPartialModel(inputs, "yakindu/Yakindu.xmi") - val queries = loadQueries(metamodel, FamilyTreeConstraints.instance) - // */ - println("DSL loaded") - - val SIZE = 2 - var REPS = 1 - val RUNTIME = 600 - - // ///////////////////////// - // Prepare Problem - val modelGenerationProblem = ecore2Logic.transformMetamodel(metamodel, new Ecore2LogicConfiguration()) - var problem = modelGenerationProblem.output - -// val modelExtensionProblem = instanceModel2Logic.transform(modelGenerationProblem, partialModel) -// problem = modelExtensionProblem.output -// val validModelExtensionProblem = viatra2Logic.transformQueries(queries, modelGenerationProblem, -// new Viatra2LogicConfiguration) -// problem = validModelExtensionProblem.output - debug.writeModel(problem, "problem.logicproblem") - println("Problem created") - // End Prepare Problem - // ///////////////////////// +static val SIZE_LB = 20 +static val SIZE_UB = 20 +static val SIZE_MUL = 1 +static val SIZE_INC = 5 + +static var REPS = 1 +static val RUNTIME = 600 + +static val DOMAIN = "FamilyTree" // "FamilyTree", "Taxation" +static val QUERIES = false +static val INDIV_WRT = true +static val GLOBAL_WRT = true +static val q2t = if(QUERIES) "Y" else "N" + +def static void main(String[] args) { + (new EMFPatternLanguageStandaloneSetup).createInjectorAndDoEMFRegistration + Resource.Factory.Registry.INSTANCE.extensionToFactoryMap.put("xmi", new XMIResourceFactoryImpl) + Resource.Factory.Registry.INSTANCE.extensionToFactoryMap.put("logicproblem", new XMIResourceFactoryImpl) + Resource.Factory.Registry.INSTANCE.extensionToFactoryMap.put("partialmodel", new XMIResourceFactoryImpl) + ReteEngine.getClass + val Ecore2Logic ecore2Logic = new Ecore2Logic + val Logic2Ecore logic2Ecore = new Logic2Ecore(ecore2Logic) + val Viatra2Logic viatra2Logic = new Viatra2Logic(ecore2Logic) + val InstanceModel2Logic instanceModel2Logic = new InstanceModel2Logic + + // Workspace setup + val Date date = new Date(System.currentTimeMillis) + val SimpleDateFormat format = new SimpleDateFormat("dd-HHmm"); + val formattedDate = format.format(date) + + val inputs = new FileSystemWorkspace('''inputs/''', "") + val dataws = new FileSystemWorkspace('''output/''', "") + val modelsws = new FileSystemWorkspace('''output/''' + formattedDate + '''/models/''', "") + val workspace = new FileSystemWorkspace('''output/''' + formattedDate + '''/''', "") + val debug = new FileSystemWorkspace('''output/''' + DOMAIN + "-" + formattedDate + '''/debug/''', "") + workspace.initAndClear + modelsws.initAndClear + + println("Input and output workspaces are created") + + // ////////////////////////// + // DOMAIN + var EcoreMetamodelDescriptor metamodel = null + var EList partialModel = null + var ViatraQuerySetDescriptor queries = null + switch DOMAIN { + case "Taxation": { + metamodel = loadMetamodel(TaxationPackage.eINSTANCE) + partialModel = loadPartialModel(inputs, "Household.xmi") + queries = loadQueries(metamodel, Case_study_A.instance) + } + case "FamilyTree": { + metamodel = loadMetamodel(FamilytreePackage.eINSTANCE) + partialModel = loadPartialModel(inputs, "FamilyTree.xmi") + queries = loadQueries(metamodel, FamilyTreeConstraints.instance) + } + default: { + System.err.println("Unsupported Domain") + throw new Exception + } + } + println("DSL loaded") + + // ///////////////////////// + // Prepare Problem + val modelGenerationProblem = ecore2Logic.transformMetamodel(metamodel, new Ecore2LogicConfiguration()) + var problem = modelGenerationProblem.output + val modelExtensionProblem = instanceModel2Logic.transform(modelGenerationProblem, partialModel) + problem = modelExtensionProblem.output + if (QUERIES) { + val validModelExtensionProblem = viatra2Logic.transformQueries(queries, modelExtensionProblem, + new Viatra2LogicConfiguration) + problem = validModelExtensionProblem.output + } + // debug.writeModel(problem, "problem.logicproblem") + println("Problem created") + // ///////////////////////// + // BEGIN RUN + var PrintWriter global_writer = null + val header = "domain, queries?,size,model?,timeout,measuredTime,TransformatonTime,SolverTime\n" + if (GLOBAL_WRT) { + global_writer = new PrintWriter( + workspace.workspaceURI + "//_" + DOMAIN + SIZE_LB + "to" + SIZE_UB + "x" + REPS + "q" + q2t + "-" + + formattedDate + ".csv") + global_writer.append(header) + } + + var NEXT_INC = SIZE_INC + for (var size = SIZE_LB; size <= SIZE_UB; size += NEXT_INC) { + NEXT_INC *= SIZE_MUL + + var PrintWriter indiv_writer = null + if (INDIV_WRT) { + indiv_writer = new PrintWriter( // TODO + workspace.workspaceURI + "//__" + DOMAIN + "Sz" + size + "x" + REPS + "q" + q2t + "-" + formattedDate + + ".csv") + indiv_writer.append(header) + } + + println() + println("DOMAIN: " + DOMAIN + ", SIZE=" + size) + for (var i = 0; i < REPS; i++) { - var ViatraReasoner reasoner = new ViatraReasoner + print("<> : ") + + var ViatraReasoner reasoner = new ViatraReasoner - // ///////////////////////////////////////////////////// - // Define Config File - val knownIntegers = new TreeSet - knownIntegers.addAll(0) + // ///////////////////////////////////////////////////// + // Define Config File + val knownIntegers = new TreeSet + knownIntegers.addAll(0, 10, 20, 30, 40, 50) - val knownReals = new TreeSet - knownReals.addAll(new BigDecimal("0.0")) + val knownReals = new TreeSet + knownReals.addAll(new BigDecimal("0.0")) - val knownStrings = new TreeSet - knownStrings.addAll("r0", "r1", "r2") + val knownStrings = new TreeSet + knownStrings.addAll("r0", "r1", "r2", "r3", "r4", "r5", "r6") - val solverConfig = new ViatraReasonerConfiguration => [ - it.documentationLevel = DocumentationLevel::FULL - it.debugCongiguration = new DebugConfiguration => [logging = true] - it.runtimeLimit = RUNTIME -// it.typeScopes.maxNewElements = SIZE -// it.typeScopes.minNewElements = 3 - if(!knownIntegers.isEmpty) it.typeScopes.knownIntegers = knownIntegers - if(!knownReals.isEmpty) it.typeScopes.knownReals = knownReals -// if(!knownStrings.isEmpty) it.typeScopes.knownStrings = knownStrings - ] + val curSize = size + val solverConfig = new ViatraReasonerConfiguration => [ + it.documentationLevel = DocumentationLevel::NONE +// it.debugCongiguration = new DebugConfiguration => [logging = true] + it.runtimeLimit = RUNTIME + // it.typeScopes.maxNewElements = SIZE + it.typeScopes.minNewElements = curSize - println("Run #" + i + ":") - val startTime = System.currentTimeMillis - var solution = reasoner.solve(problem, solverConfig, debug) - val totalTime = System.currentTimeMillis - startTime +// if(!knownIntegers.isEmpty) it.typeScopes.knownIntegers = knownIntegers + if(!knownReals.isEmpty) it.typeScopes.knownReals = knownReals + // if(!knownStrings.isEmpty) it.typeScopes.knownStrings = knownStrings + ] - println(" Problem Solved") - solution.writeStats(totalTime, solverConfig) + val startTime = System.currentTimeMillis + var solution = reasoner.solve(problem, solverConfig, debug) + val measuredTime = System.currentTimeMillis - startTime + print("<" + measuredTime / 1000.0 + "s") + +// solution.writeStats(totalTime, solverConfig) + try { if (solution instanceof ModelResult) { - solution.writeRepresentation(workspace, i) - solution.writeInterpretation(logic2Ecore, workspace, i, reasoner, modelGenerationProblem) - println(" Solution saved and visualised") - } else - println(" Returned: " + solution.class.simpleName.toString) - -// println("Stats Created") - // Run Garbage Collector - val Runtime r = Runtime.getRuntime(); - r.gc(); - r.gc(); - r.gc(); - Thread.sleep(3000) + solution.writeInterpretation(logic2Ecore, modelsws, size + "_" + i, reasoner, modelGenerationProblem) + println(" (XMI Success)") + } + } catch (Exception e) { + System.err.println(" (XMI Error)" + e.message) + } + + try { + if (solution instanceof ModelResult) { + solution.writeRepresentation(modelsws, size + "_" + i) + println(" (VIZ Success) >>") + } + } catch (Exception e) { + System.err.println(" (VIZ Error)" + e.message + ">>") } - println("---End---") + println() + // Run Garbage Collector + val Runtime r = Runtime.getRuntime(); + r.gc(); + r.gc(); + r.gc(); + Thread.sleep(3000) } + if(INDIV_WRT) indiv_writer.close +} +if(GLOBAL_WRT) global_writer.close + } + + def - def static Map getTypeMap(Map classMap, EcoreMetamodelDescriptor metamodel, +static Map getTypeMap(Map classMap, EcoreMetamodelDescriptor metamodel, Ecore2Logic e2l, Ecore2Logic_Trace trace) { val typeMap = new HashMap val listMap = metamodel.classes.toMap[s|s.name] @@ -168,7 +269,9 @@ class GeneratePledgeModels { return typeMap } - def static loadMetamodel(EPackage pckg) { + def + +static loadMetamodel(EPackage pckg) { val List classes = pckg.getEClassifiers.filter(EClass).toList val List enums = pckg.getEClassifiers.filter(EEnum).toList val List literals = enums.map[getELiterals].flatten.toList @@ -177,13 +280,17 @@ class GeneratePledgeModels { return new EcoreMetamodelDescriptor(classes, #{}, false, enums, literals, references, attributes) } - def static loadPartialModel(ReasonerWorkspace inputs, String path) { + def + +static loadPartialModel(ReasonerWorkspace inputs, String path) { Resource.Factory.Registry.INSTANCE.getExtensionToFactoryMap().put("*", new XMIResourceFactoryImpl()) inputs.readModel(EObject, path).eResource.contents // inputs.readModel(EObject,"FamInstance.xmi").eResource.allContents.toList } - def static loadQueries(EcoreMetamodelDescriptor metamodel, IQueryGroup i) { + def + +static loadQueries(EcoreMetamodelDescriptor metamodel, IQueryGroup i) { val patterns = i.specifications.toList val wfPatterns = patterns.filter[it.allAnnotations.exists[it.name == "Constraint"]].toSet val derivedFeatures = emptyMap @@ -199,37 +306,45 @@ class GeneratePledgeModels { return res } - def static writeInterpretation(LogicResult solution, Logic2Ecore logic2Ecore, ReasonerWorkspace workspace, int i, - ViatraReasoner reasoner, TracedOutput mgProb) { + def + +static writeInterpretation(LogicResult solution, Logic2Ecore logic2Ecore, ReasonerWorkspace workspace, + String id, ViatraReasoner reasoner, TracedOutput mgProb) { val interpretations = reasoner.getInterpretations(solution as ModelResult) for (interpIndex : 0 ..< interpretations.size) { // val extension b = new LogicStructureBuilder // val extension a = new LogicProblemBuilder val interpretation = interpretations.get(interpIndex) val model = logic2Ecore.transformInterpretation(interpretation, mgProb.trace) - workspace.writeModel(model, '''solution«i»_«interpIndex».xmi''') +// println(model) + workspace.writeModel(model, '''sol-«id»_«interpIndex».xmi''') } } - def static writeRepresentation(LogicResult solution, ReasonerWorkspace workspace, int i) { + def + +static writeRepresentation(LogicResult solution, ReasonerWorkspace workspace, String id) { val representations = solution.representation for (representationIndex : 0 ..< representations.size) { val representation = representations.get(representationIndex) if (representation instanceof PartialInterpretation) { val gml = (new PartialInterpretation2Gml).transform(representation) - workspace.writeText('''solution«i»_«representationIndex».gml''', gml) + workspace.writeText('''sol-«id»_«representationIndex».gml''', gml) val png = (new GraphvizVisualiser).visualiseConcretization(representation) - png.writeToFile(workspace, '''solution«i»_«representationIndex».png''') +// println(png) + png.writeToFile(workspace, '''sol-«id»_«representationIndex».png''') // workspace.writeModel(representation, '''solution«representationIndex».partialintrpretation''') } else { - workspace.writeText('''solution«representationIndex».txt''', representation.toString) + workspace.writeText('''sol-«representationIndex».txt''', representation.toString) } } } - def static writeStats(LogicResult solution, long time, ViatraReasonerConfiguration config) { + def + +static writeStats(LogicResult solution, long time, ViatraReasonerConfiguration config) { val stats = solution.statistics println(" Statistics:") // for (e : stats.entries.filter[name.substring(0, 9) == "_Solution"]) { -- cgit v1.2.3-70-g09d2