From fc84d3fe670331bc89fb1e4c44104bc1fc811438 Mon Sep 17 00:00:00 2001 From: Kristóf Marussy Date: Wed, 14 Aug 2019 18:26:33 +0200 Subject: Measurements WIP --- .../META-INF/MANIFEST.MF | 8 +- ..._ViatraSolver_polyhedral_typeHierarchy_Clp.json | 13 + ..._ViatraSolver_polyhedral_typeHierarchy_Clp.json | 16 + ..._ViatraSolver_polyhedral_typeHierarchy_Clp.json | 15 + ..._ViatraSolver_polyhedral_typeHierarchy_Clp.json | 15 + ..._ViatraSolver_polyhedral_typeHierarchy_Clp.json | 17 + .../initialModels/satellite.xmi | 14 + .../bme/mit/inf/dslreasoner/run/CountMatches.xtend | 176 ----------- .../run/Ecore2LogicTraceBasedHint.xtend | 56 ++++ .../mit/inf/dslreasoner/run/MetamodelLoader.xtend | 302 +++++++++++++----- .../bme/mit/inf/dslreasoner/run/SGraphHint.xtend | 46 +++ .../mit/inf/dslreasoner/run/SatelliteHint.xtend | 86 +++++ .../run/TypeDistributionCalculator.xtend | 35 ++ .../dslreasoner/run/script/MeasurementScript.xtend | 70 ++++ .../run/script/MeasurementScriptRunner.xtend | 351 +++++++++++++++++++++ 15 files changed, 956 insertions(+), 264 deletions(-) create mode 100644 Tests/hu.bme.mit.inf.dslreasoner.run/configs/FAM_useful_ViatraSolver_polyhedral_typeHierarchy_Clp.json create mode 100644 Tests/hu.bme.mit.inf.dslreasoner.run/configs/Yakindu_useful_ViatraSolver_polyhedral_typeHierarchy_Clp.json create mode 100644 Tests/hu.bme.mit.inf.dslreasoner.run/configs/ecore_useful_ViatraSolver_polyhedral_typeHierarchy_Clp.json create mode 100644 Tests/hu.bme.mit.inf.dslreasoner.run/configs/fs_useful_ViatraSolver_polyhedral_typeHierarchy_Clp.json create mode 100644 Tests/hu.bme.mit.inf.dslreasoner.run/configs/satellite_useful_ViatraSolver_polyhedral_typeHierarchy_Clp.json create mode 100644 Tests/hu.bme.mit.inf.dslreasoner.run/initialModels/satellite.xmi delete mode 100644 Tests/hu.bme.mit.inf.dslreasoner.run/src/hu/bme/mit/inf/dslreasoner/run/CountMatches.xtend create mode 100644 Tests/hu.bme.mit.inf.dslreasoner.run/src/hu/bme/mit/inf/dslreasoner/run/Ecore2LogicTraceBasedHint.xtend create mode 100644 Tests/hu.bme.mit.inf.dslreasoner.run/src/hu/bme/mit/inf/dslreasoner/run/SGraphHint.xtend create mode 100644 Tests/hu.bme.mit.inf.dslreasoner.run/src/hu/bme/mit/inf/dslreasoner/run/SatelliteHint.xtend create mode 100644 Tests/hu.bme.mit.inf.dslreasoner.run/src/hu/bme/mit/inf/dslreasoner/run/TypeDistributionCalculator.xtend create mode 100644 Tests/hu.bme.mit.inf.dslreasoner.run/src/hu/bme/mit/inf/dslreasoner/run/script/MeasurementScript.xtend create mode 100644 Tests/hu.bme.mit.inf.dslreasoner.run/src/hu/bme/mit/inf/dslreasoner/run/script/MeasurementScriptRunner.xtend (limited to 'Tests/hu.bme.mit.inf.dslreasoner.run') diff --git a/Tests/hu.bme.mit.inf.dslreasoner.run/META-INF/MANIFEST.MF b/Tests/hu.bme.mit.inf.dslreasoner.run/META-INF/MANIFEST.MF index cc274c7c..fe223d4a 100644 --- a/Tests/hu.bme.mit.inf.dslreasoner.run/META-INF/MANIFEST.MF +++ b/Tests/hu.bme.mit.inf.dslreasoner.run/META-INF/MANIFEST.MF @@ -28,8 +28,10 @@ Require-Bundle: hu.bme.mit.inf.dslreasoner.viatrasolver.partialinterpretationlan hu.bme.mit.inf.dslreasoner.visualisation;bundle-version="1.0.0", hu.bme.mit.inf.dslreasoner.domains.alloyexamples;bundle-version="1.0.0", org.eclipse.collections;bundle-version="9.2.0", - org.eclipse.viatra.query.patternlanguage.emf;bundle-version="2.2.0", - org.eclipse.viatra.query.runtime.rete;bundle-version="2.2.0", - org.objectweb.asm;bundle-version="7.0.0" + org.eclipse.viatra.query.patternlanguage.emf;bundle-version="2.0.0", + org.eclipse.viatra.query.runtime.rete;bundle-version="2.0.0", + org.objectweb.asm;bundle-version="7.0.0", + com.google.gson;bundle-version="2.8.2", + hu.bme.mit.inf.dslreasoner.domains.satellite;bundle-version="0.1.0" Import-Package: org.apache.log4j Bundle-RequiredExecutionEnvironment: JavaSE-1.8 diff --git a/Tests/hu.bme.mit.inf.dslreasoner.run/configs/FAM_useful_ViatraSolver_polyhedral_typeHierarchy_Clp.json b/Tests/hu.bme.mit.inf.dslreasoner.run/configs/FAM_useful_ViatraSolver_polyhedral_typeHierarchy_Clp.json new file mode 100644 index 00000000..26df3c74 --- /dev/null +++ b/Tests/hu.bme.mit.inf.dslreasoner.run/configs/FAM_useful_ViatraSolver_polyhedral_typeHierarchy_Clp.json @@ -0,0 +1,13 @@ +{ + "inputPath": "initialModels", + "outputPath": "outputModels", + "timeout": 1200, + "saveModels": true, + "warmupIterations": 0, + "iterations": 1, + "domain": "FAM", + "scope": "none", + "sizes": [500], + "solver": "ViatraSolver", + "scopePropagator": "basic" +} diff --git a/Tests/hu.bme.mit.inf.dslreasoner.run/configs/Yakindu_useful_ViatraSolver_polyhedral_typeHierarchy_Clp.json b/Tests/hu.bme.mit.inf.dslreasoner.run/configs/Yakindu_useful_ViatraSolver_polyhedral_typeHierarchy_Clp.json new file mode 100644 index 00000000..5f8a01b1 --- /dev/null +++ b/Tests/hu.bme.mit.inf.dslreasoner.run/configs/Yakindu_useful_ViatraSolver_polyhedral_typeHierarchy_Clp.json @@ -0,0 +1,16 @@ +{ + "inputPath": "initialModels", + "outputPath": "outputModels", + "timeout": 1200, + "saveModels": false, + "warmupIterations": 0, + "iterations": 5, + "domain": "Yakindu", + "scope": "quantiles", + "sizes": [100], + "solver": "ViatraSolver", + "scopePropagator": "polyhedral", + "propagatedConstraints": "hints", + "polyhedronSolver": "Clp", + "scopeHeuristic": "basic" +} diff --git a/Tests/hu.bme.mit.inf.dslreasoner.run/configs/ecore_useful_ViatraSolver_polyhedral_typeHierarchy_Clp.json b/Tests/hu.bme.mit.inf.dslreasoner.run/configs/ecore_useful_ViatraSolver_polyhedral_typeHierarchy_Clp.json new file mode 100644 index 00000000..42073422 --- /dev/null +++ b/Tests/hu.bme.mit.inf.dslreasoner.run/configs/ecore_useful_ViatraSolver_polyhedral_typeHierarchy_Clp.json @@ -0,0 +1,15 @@ +{ + "inputPath": "initialModels", + "outputPath": "outputModels", + "timeout": 1200, + "saveModels": true, + "warmupIterations": 0, + "iterations": 5, + "domain": "ecore", + "scope": "quantiles", + "sizes": [100], + "solver": "ViatraSolver", + "scopePropagator": "polyhedral", + "propagatedConstraints": "relations", + "polyhedronSolver": "Clp" +} diff --git a/Tests/hu.bme.mit.inf.dslreasoner.run/configs/fs_useful_ViatraSolver_polyhedral_typeHierarchy_Clp.json b/Tests/hu.bme.mit.inf.dslreasoner.run/configs/fs_useful_ViatraSolver_polyhedral_typeHierarchy_Clp.json new file mode 100644 index 00000000..d7955ddd --- /dev/null +++ b/Tests/hu.bme.mit.inf.dslreasoner.run/configs/fs_useful_ViatraSolver_polyhedral_typeHierarchy_Clp.json @@ -0,0 +1,15 @@ +{ + "inputPath": "initialModels", + "outputPath": "outputModels", + "timeout": 1200, + "saveModels": true, + "warmupIterations": 1, + "iterations": 1, + "domain": "fs", + "scope": "useful", + "sizes": [50, 100, 150, 200, 250, 300, 350, 400, 450, 500], + "solver": "ViatraSolver", + "scopePropagator": "polyhedral", + "propagatedConstraints": "relations", + "polyhedronSolver": "Clp" +} diff --git a/Tests/hu.bme.mit.inf.dslreasoner.run/configs/satellite_useful_ViatraSolver_polyhedral_typeHierarchy_Clp.json b/Tests/hu.bme.mit.inf.dslreasoner.run/configs/satellite_useful_ViatraSolver_polyhedral_typeHierarchy_Clp.json new file mode 100644 index 00000000..474962e7 --- /dev/null +++ b/Tests/hu.bme.mit.inf.dslreasoner.run/configs/satellite_useful_ViatraSolver_polyhedral_typeHierarchy_Clp.json @@ -0,0 +1,17 @@ +{ + "inputPath": "initialModels", + "outputPath": "outputModels", + "timeout": 1200, + "saveModels": true, + "saveTemporaryFiles": true, + "warmupIterations": 0, + "iterations": 1, + "domain": "satellite", + "scope": "quantiles", + "sizes": [50], + "solver": "ViatraSolver", + "scopePropagator": "polyhedral", + "propagatedConstraints": "hints", + "polyhedronSolver": "Clp", + "scopeHeuristic": "polyhedral" +} diff --git a/Tests/hu.bme.mit.inf.dslreasoner.run/initialModels/satellite.xmi b/Tests/hu.bme.mit.inf.dslreasoner.run/initialModels/satellite.xmi new file mode 100644 index 00000000..77f6ecfd --- /dev/null +++ b/Tests/hu.bme.mit.inf.dslreasoner.run/initialModels/satellite.xmi @@ -0,0 +1,14 @@ + + + + + + + diff --git a/Tests/hu.bme.mit.inf.dslreasoner.run/src/hu/bme/mit/inf/dslreasoner/run/CountMatches.xtend b/Tests/hu.bme.mit.inf.dslreasoner.run/src/hu/bme/mit/inf/dslreasoner/run/CountMatches.xtend deleted file mode 100644 index 02caf9dd..00000000 --- a/Tests/hu.bme.mit.inf.dslreasoner.run/src/hu/bme/mit/inf/dslreasoner/run/CountMatches.xtend +++ /dev/null @@ -1,176 +0,0 @@ -package hu.bme.mit.inf.dslreasoner.run - -import hu.bme.mit.inf.dslreasoner.domains.yakindu.sgraph.yakindumm.YakindummPackage -import hu.bme.mit.inf.dslreasoner.partialsnapshot_mavo.yakindu.mutated.Mutated -import hu.bme.mit.inf.dslreasoner.workspace.FileSystemWorkspace -import java.io.File -import java.util.ArrayList -import java.util.Collection -import java.util.Comparator -import java.util.HashMap -import java.util.List -import java.util.Map -import java.util.TreeSet -import org.eclipse.emf.ecore.EObject -import org.eclipse.emf.ecore.resource.Resource -import org.eclipse.emf.ecore.xmi.impl.XMIResourceFactoryImpl -import org.eclipse.viatra.query.runtime.api.IPatternMatch -import org.eclipse.viatra.query.runtime.api.IQuerySpecification -import org.eclipse.viatra.query.runtime.api.ViatraQueryEngine -import org.eclipse.viatra.query.runtime.emf.EMFScope - -class QueryComparator implements Comparator>{ - - override compare(IQuerySpecification arg0, IQuerySpecification arg1) { - arg0.fullyQualifiedName.compareTo(arg1.fullyQualifiedName) - } -} - -class CountMatches { - var static List> wfPatterns; - var static Map,IQuerySpecification> query2Reference - - def static void main(String[] args) { - YakindummPackage.eINSTANCE.eClass - Resource.Factory.Registry.INSTANCE.extensionToFactoryMap.put("*",new XMIResourceFactoryImpl) - - wfPatterns = Mutated.instance.specifications.toList; - //wfPatterns = wfPatterns.filter[it.allAnnotations.exists[it.name == "Constraint"]].toList - wfPatterns.sort(new QueryComparator) - - val groupName2Representant = new HashMap - query2Reference = new HashMap - for(wfPattern : wfPatterns) { - val groupName = wfPattern.groupName - if(groupName2Representant.containsKey(groupName)) { - val representant = groupName2Representant.get(groupName) - query2Reference.put(wfPattern,representant) - } else { - groupName2Representant.put(groupName,wfPattern) - } - } - - - println('''modelpath;run;model;« - FOR wfPattern:wfPatterns SEPARATOR ";"»#(« - wfPattern.fullyQualifiedName.split("\\.").last»);hash(« - wfPattern.fullyQualifiedName.split("\\.").last»)«ENDFOR»;« - FOR mutant : wfPatterns.filter[query2Reference.keySet.contains(it)] SEPARATOR ';'»diff(« - mutant.fullyQualifiedName.split("\\.").last»)«ENDFOR»''' - ) - countMatches('''D:/FASE18Meas/RemoHF''') - } - - def private static simpleName(IQuerySpecification wfPattern) { - wfPattern.fullyQualifiedName.split("\\.").last - } - def private static groupName(IQuerySpecification wfPattern) { - wfPattern.simpleName.split('_').head - } - - def static void countMatches(String path) { - val file = new File(path) - if(file.isDirectory) { - for(subFileName : file.list) { - (path + "/" + subFileName).countMatches - } - } else if(file.isFile) { - if(path.endsWith("xmi")) { - countMatches(file,path) - } - } - } - - def static void countMatches(File file, String path) { - - - val pathSegments = path.split("/") - val groupName = pathSegments.get(pathSegments.size-2).split("\\.").last.split("_").get(0) - print(groupName +";") - val nameExtension = pathSegments.get(pathSegments.size-1).split("\\.").get(0).split("_") - try{ - val runNumber = nameExtension.get(1) - val modelNumber = nameExtension.get(2) - print('''«runNumber»;«modelNumber»''') - } catch(Exception e) { - print('''«file.name»;0''') - } - - val parent = file.parent - val workspace = new FileSystemWorkspace(parent,"") - val model = workspace.readModel(EObject,file.name) - - val engine = ViatraQueryEngine.on(new EMFScope(model)) - val objectCode = model.eResource.calculateObjectCode - - val pattern2Hash = new HashMap - for(pattern : wfPatterns) { - val matcher = pattern.getMatcher(engine) - val matches = matcher.allMatches - val hash = matches.getMatchSetDescriptor(objectCode) - pattern2Hash.put(pattern,hash) - print(''';«matcher.countMatches»;«hash»''') - } - var mutantsKilled = 0 - for(mutant : wfPatterns.filter[query2Reference.keySet.contains(it)]) { - val equals = pattern2Hash.get(mutant) == pattern2Hash.get(query2Reference.get(mutant)) - print(''';''') - if(equals) { - print('0') - } else { - print('1') - mutantsKilled++ - } - } - //print(''';«mutantsKilled»''') - println() - } - - def static Map calculateObjectCode(Resource resource) { - val res = new HashMap - val iterator = resource.allContents - var index = 1 - while(iterator.hasNext) { - res.put(iterator.next,index++) - } - return res - } - - def static getMatchSetDescriptor(Collection matchSet, Map objectCode) { - val set = new TreeSet(new ArrayComparator) - for(match: matchSet) { - val size = match.parameterNames.size - val idArray = new ArrayList(size) - for(i:0..> { - - override compare(List arg0, List arg1) { - if(arg0.size === arg1.size) { - for(i : 0.. nameToType + val Map> nameToRelation + + protected new(Ecore2Logic ecore2Logic, Ecore2Logic_Trace trace) { + nameToType = ImmutableMap.copyOf(ecore2Logic.allClassesInScope(trace).toMap[name].mapValues [ eClass | + ecore2Logic.TypeofEClass(trace, eClass) + ]) + nameToRelation = ImmutableMap.copyOf(ecore2Logic.allReferencesInScope(trace).groupBy[EContainingClass.name]. + mapValues [ references | + ImmutableMap.copyOf(references.toMap[name].mapValues [ reference | + ecore2Logic.relationOfReference(trace, reference) + ]) + ]) + } + + protected def getType(String name) { + nameToType.get(name) + } + + protected def relation(String typeName, String relationName) { + nameToRelation.get(typeName).get(relationName) + } + + protected static def int countMatches(ViatraQueryMatcher matcher, PartialInterpretation p) { + val match = matcher.newEmptyMatch + match.set(0, p.problem) + match.set(1, p) + matcher.countMatches(match) + } + + protected static def int getCount(ViatraQueryMatcher matcher, PartialInterpretation p) { + val match = matcher.newEmptyMatch + match.set(0, p.problem) + match.set(1, p) + val realMatch = matcher.getOneArbitraryMatch(match) + if (realMatch.present) { + realMatch.get.get(2) as Integer + } else { + 0 + } + } +} diff --git a/Tests/hu.bme.mit.inf.dslreasoner.run/src/hu/bme/mit/inf/dslreasoner/run/MetamodelLoader.xtend b/Tests/hu.bme.mit.inf.dslreasoner.run/src/hu/bme/mit/inf/dslreasoner/run/MetamodelLoader.xtend index 34f3c267..54724226 100644 --- a/Tests/hu.bme.mit.inf.dslreasoner.run/src/hu/bme/mit/inf/dslreasoner/run/MetamodelLoader.xtend +++ b/Tests/hu.bme.mit.inf.dslreasoner.run/src/hu/bme/mit/inf/dslreasoner/run/MetamodelLoader.xtend @@ -4,18 +4,23 @@ import functionalarchitecture.FunctionalarchitecturePackage import hu.bme.mit.inf.dslreasoner.domains.alloyexamples.Ecore import hu.bme.mit.inf.dslreasoner.domains.alloyexamples.FileSystem import hu.bme.mit.inf.dslreasoner.domains.alloyexamples.Filesystem.FilesystemPackage +import hu.bme.mit.inf.dslreasoner.domains.satellite.queries.internal.SatelliteQueriesAll import hu.bme.mit.inf.dslreasoner.domains.transima.fam.FamPatterns import hu.bme.mit.inf.dslreasoner.domains.yakindu.sgraph.yakindumm.YakindummPackage +import hu.bme.mit.inf.dslreasoner.ecore2logic.Ecore2Logic +import hu.bme.mit.inf.dslreasoner.ecore2logic.Ecore2Logic_Trace import hu.bme.mit.inf.dslreasoner.ecore2logic.EcoreMetamodelDescriptor import hu.bme.mit.inf.dslreasoner.partialsnapshot_mavo.yakindu.Patterns import hu.bme.mit.inf.dslreasoner.viatra2logic.ViatraQuerySetDescriptor import hu.bme.mit.inf.dslreasoner.viatrasolver.logic2viatra.ModelGenerationMethod +import hu.bme.mit.inf.dslreasoner.viatrasolver.logic2viatra.cardinality.LinearTypeConstraintHint import hu.bme.mit.inf.dslreasoner.viatrasolver.reasoner.ModelGenerationMethodBasedGlobalConstraint import hu.bme.mit.inf.dslreasoner.workspace.ReasonerWorkspace import java.util.Collection import java.util.HashMap import java.util.LinkedHashMap import java.util.List +import java.util.Map import java.util.Set import org.eclipse.emf.ecore.EAttribute import org.eclipse.emf.ecore.EClass @@ -24,60 +29,83 @@ import org.eclipse.emf.ecore.EEnumLiteral import org.eclipse.emf.ecore.EObject import org.eclipse.emf.ecore.EReference import org.eclipse.emf.ecore.EcorePackage +import org.eclipse.xtend.lib.annotations.Data import org.eclipse.xtext.xbase.lib.Functions.Function1 -import hu.bme.mit.inf.dslreasoner.domains.transima.fam.Type -import hu.bme.mit.inf.dslreasoner.domains.transima.fam.Model +import satellite.SatellitePackage + +@Data +class TypeQuantiles { + double low + double high +} abstract class MetamodelLoader { protected val ReasonerWorkspace workspace + new(ReasonerWorkspace workspace) { this.workspace = workspace - } + } + def EcoreMetamodelDescriptor loadMetamodel() + def Set getRelevantTypes(EcoreMetamodelDescriptor descriptor) + def Set getRelevantReferences(EcoreMetamodelDescriptor descriptor) + def ViatraQuerySetDescriptor loadQueries(EcoreMetamodelDescriptor metamodel) + def List loadPartialModel() - - def List> additionalConstraints() - - def filterByNames(Iterable collection, Function1 nameExtractor, Collection requiredNames) { + + def List> additionalConstraints() + + def Map getTypeQuantiles() { + emptyMap + } + + def List getHints(Ecore2Logic ecore2Logic, Ecore2Logic_Trace trace) { + emptyList + } + + def filterByNames(Iterable collection, Function1 nameExtractor, + Collection requiredNames) { val res = collection.filter[requiredNames.contains(nameExtractor.apply(it))] if(res.size != requiredNames.size) throw new IllegalArgumentException return res.toSet } } -class FAMLoader extends MetamodelLoader{ - +class FAMLoader extends MetamodelLoader { + new(ReasonerWorkspace workspace) { super(workspace) } - + override loadMetamodel() { val package = FunctionalarchitecturePackage.eINSTANCE val List classes = package.EClassifiers.filter(EClass).toList val List enums = package.EClassifiers.filter(EEnum).toList val List literals = enums.map[ELiterals].flatten.toList - val List references = classes.map[EReferences].flatten.toList + val List references = classes.map[EReferences].flatten.filter[name != "type" && name != "model"]. + toList val List attributes = classes.map[EAttributes].flatten.toList - return new EcoreMetamodelDescriptor(classes,#{},false,enums,literals,references,attributes) + return new EcoreMetamodelDescriptor(classes, #{}, false, enums, literals, references, attributes) } - - override getRelevantTypes(EcoreMetamodelDescriptor descriptor) { - return descriptor.classes.filterByNames([it.name],#["FunctionalElement"]) + + override getRelevantTypes(EcoreMetamodelDescriptor descriptor) { + return descriptor.classes.filterByNames([it.name], #["FunctionalElement"]) } + override getRelevantReferences(EcoreMetamodelDescriptor descriptor) { - return descriptor.references.filterByNames([it.name],#["subElements"]) + return descriptor.references.filterByNames([it.name], #["subElements"]) } - + override loadQueries(EcoreMetamodelDescriptor metamodel) { val i = FamPatterns.instance val patterns = i.specifications.toList - val wfPatterns = patterns.filter[it.allAnnotations.exists[it.name== "Constraint"]].toSet + val wfPatterns = patterns.filter[it.allAnnotations.exists[it.name == "Constraint"]].toSet val derivedFeatures = new LinkedHashMap - derivedFeatures.put(Type.instance,metamodel.attributes.filter[it.name == "type"].head) - derivedFeatures.put(Model.instance,metamodel.references.filter[it.name == "model"].head) +// derivedFeatures.put(Type.instance,metamodel.attributes.filter[it.name == "type"].head) +// derivedFeatures.put(Model.instance,metamodel.references.filter[it.name == "model"].head) val res = new ViatraQuerySetDescriptor( patterns, wfPatterns, @@ -85,65 +113,67 @@ class FAMLoader extends MetamodelLoader{ ) return res } + override loadPartialModel() { - this.workspace.readModel(EObject,"FAM.xmi").eResource.allContents.toList + this.workspace.readModel(EObject, "FAM.xmi").eResource.allContents.toList } - + override additionalConstraints() { #[] } } -class YakinduLoader extends MetamodelLoader{ - +class YakinduLoader extends MetamodelLoader { + var useSynchronization = true; - var useComplexStates = false; - public static val patternsWithSynchronization = #[ - "synchHasNoOutgoing", "synchHasNoIncoming", "SynchronizedIncomingInSameRegion", "notSynchronizingStates", - "hasMultipleOutgoingTrainsition", "hasMultipleIncomingTrainsition", "SynchronizedRegionsAreNotSiblings", - "SynchronizedRegionDoesNotHaveMultipleRegions", "synchThree", "twoSynch","noSynch2","synch","noSynch4","noSynch3","noSynch"] - public static val patternsWithComplexStates =#["outgoingFromExit","outgoingFromFinal","choiceHasNoOutgoing","choiceHasNoIncoming"] + var useComplexStates = false; + public static val patternsWithSynchronization = #["synchHasNoOutgoing", "synchHasNoIncoming", + "SynchronizedIncomingInSameRegion", "SynchronizedIncomingInSameRegionHelper1", + "SynchronizedIncomingInSameRegionHelper2", "notSynchronizingStates", "hasMultipleOutgoingTrainsition", + "hasMultipleIncomingTrainsition", "SynchronizedRegionsAreNotSiblings", + "SynchronizedRegionsAreNotSiblingsHelper1", "SynchronizedRegionsAreNotSiblingsHelper2", + "SynchronizedRegionDoesNotHaveMultipleRegions", "synchThree", "twoSynch", "noSynch2", "synch", "noSynch4", + "noSynch3", "noSynch"] + public static val patternsWithComplexStates = #["outgoingFromExit", "outgoingFromFinal", "choiceHasNoOutgoing", + "choiceHasNoIncoming"] + new(ReasonerWorkspace workspace) { super(workspace) YakindummPackage.eINSTANCE.eClass } - + def setUseSynchronization(boolean useSynchronization) { this.useSynchronization = useSynchronization } + def setUseComplexStates(boolean useComplexStates) { this.useComplexStates = useComplexStates } - + override loadMetamodel() { val useSynchInThisLoad = this.useSynchronization val useComplexStates = this.useComplexStates - + val package = YakindummPackage.eINSTANCE - val List classes = package.EClassifiers.filter(EClass) - .filter[useSynchInThisLoad || (it.name != "Synchronization")] - .filter[useComplexStates || (it.name != "Choice" && it.name != "Exit" && it.name != "FinalState")] - .toList + val List classes = package.EClassifiers.filter(EClass).filter [ + useSynchInThisLoad || (it.name != "Synchronization") + ].filter[useComplexStates || (it.name != "Choice" && it.name != "Exit" && it.name != "FinalState")].toList val List enums = package.EClassifiers.filter(EEnum).toList val List literals = enums.map[ELiterals].flatten.toList val List references = classes.map[EReferences].flatten.toList val List attributes = classes.map[EAttributes].flatten.toList - return new EcoreMetamodelDescriptor(classes,#{},false,enums,literals,references,attributes) + return new EcoreMetamodelDescriptor(classes, #{}, false, enums, literals, references, attributes) } + override loadQueries(EcoreMetamodelDescriptor metamodel) { val useSynchInThisLoad = this.useSynchronization - + val i = Patterns.instance - val patterns = i.specifications - .filter[spec | - useSynchInThisLoad || - !patternsWithSynchronization.exists[spec.fullyQualifiedName.endsWith(it)] - ] - .filter[spec | - useComplexStates || - !patternsWithComplexStates.exists[spec.fullyQualifiedName.endsWith(it)] - ] - .toList - val wfPatterns = patterns.filter[it.allAnnotations.exists[it.name== "Constraint"]].toSet + val patterns = i.specifications.filter [ spec | + useSynchInThisLoad || !patternsWithSynchronization.exists[spec.fullyQualifiedName.endsWith(it)] + ].filter [ spec | + useComplexStates || !patternsWithComplexStates.exists[spec.fullyQualifiedName.endsWith(it)] + ].toList + val wfPatterns = patterns.filter[it.allAnnotations.exists[it.name == "Constraint"]].toSet val derivedFeatures = new LinkedHashMap val res = new ViatraQuerySetDescriptor( patterns, @@ -152,53 +182,71 @@ class YakinduLoader extends MetamodelLoader{ ) return res } + override getRelevantTypes(EcoreMetamodelDescriptor descriptor) { - descriptor.classes.filterByNames([it.name],#["Vertex","Transition","Synchronization"]) + descriptor.classes.filterByNames([it.name], #["Vertex", "Transition", "Synchronization"]) } - + override getRelevantReferences(EcoreMetamodelDescriptor descriptor) { - descriptor.references.filterByNames([it.name],#["source","target"]) + descriptor.references.filterByNames([it.name], #["source", "target"]) } - + override loadPartialModel() { - this.workspace.readModel(EObject,"Yakindu.xmi").eResource.allContents.toList + this.workspace.readModel(EObject, "Yakindu.xmi").eResource.allContents.toList } - - override additionalConstraints() { //#[] - #[[method | new SGraphInconsistencyDetector(method)]] + + override additionalConstraints() { // #[] + #[[method|new SGraphInconsistencyDetector(method)]] + } + + override getTypeQuantiles() { + #{ + "Choice" -> new TypeQuantiles(0.118279569892473, 0.154020979020979), + "Entry" -> new TypeQuantiles(0.0283018867924528, 0.0620167525773196), + "Exit" -> new TypeQuantiles(0, 0), + "FinalState" -> new TypeQuantiles(0, 0), + "Region" -> new TypeQuantiles(0.0294117647058824, 0.0633258678611422), + "State" -> new TypeQuantiles(0.132023636740618, 0.175925925925926), +// "Statechart" -> new TypeQuantiles(0.00961538461538462, 0.010752688172043), + "Transition" -> new TypeQuantiles(0.581632653061224, 0.645161290322581) + } + } + + override getHints(Ecore2Logic ecore2Logic, Ecore2Logic_Trace trace) { + #[new SGraphHint(ecore2Logic, trace)] } } -class FileSystemLoader extends MetamodelLoader{ - +class FileSystemLoader extends MetamodelLoader { + new(ReasonerWorkspace workspace) { super(workspace) } - + override loadMetamodel() { val package = FilesystemPackage.eINSTANCE val List classes = package.EClassifiers.filter(EClass).toList val List enums = package.EClassifiers.filter(EEnum).toList val List literals = enums.map[ELiterals].flatten.toList - val List references = classes.map[EReferences].flatten.toList + val List references = classes.map[EReferences].flatten.filter[name != "live"].toList val List attributes = classes.map[EAttributes].flatten.toList - return new EcoreMetamodelDescriptor(classes,#{},false,enums,literals,references,attributes) + return new EcoreMetamodelDescriptor(classes, #{}, false, enums, literals, references, attributes) } - + override getRelevantTypes(EcoreMetamodelDescriptor descriptor) { return null } - + override getRelevantReferences(EcoreMetamodelDescriptor descriptor) { null } - + override loadQueries(EcoreMetamodelDescriptor metamodel) { val patternGroup = FileSystem.instance val patterns = patternGroup.specifications.toList val wfPatterns = patterns.filter[it.allAnnotations.exists[it.name == "Constraint"]].toSet val derivedFeatures = new HashMap - derivedFeatures.put(patternGroup.live,metamodel.references.filter[it.name == "live"].head) +// derivedFeatures.put(patternGroup.live,metamodel.references.filter[it.name == "live"].head) return new ViatraQuerySetDescriptor( patterns, wfPatterns, @@ -206,41 +254,46 @@ class FileSystemLoader extends MetamodelLoader{ ) } - + override loadPartialModel() { - this.workspace.readModel(EObject,"fs.xmi").eResource.allContents.toList + this.workspace.readModel(EObject, "fs.xmi").eResource.allContents.toList } - + override additionalConstraints() { - #[[method | new FileSystemInconsistencyDetector(method)]] + #[[method|new FileSystemInconsistencyDetector(method)]] } - + } class EcoreLoader extends MetamodelLoader { - + new(ReasonerWorkspace workspace) { super(workspace) } - + override loadMetamodel() { val package = EcorePackage.eINSTANCE - val List classes = package.EClassifiers.filter(EClass).filter[it.name!="EFactory"].toList + val List classes = package.EClassifiers.filter(EClass).filter [ + it.name != "EFactory" && it.name != "EObject" && it.name != "EResource" + ].toList val List enums = package.EClassifiers.filter(EEnum).toList val List literals = enums.map[ELiterals].flatten.toList - val List references = classes.map[EReferences].flatten.filter[it.name!="eFactoryInstance"].filter[!it.derived].toList - val List attributes = #[] //classes.map[EAttributes].flatten.toList - return new EcoreMetamodelDescriptor(classes,#{},false,enums,literals,references,attributes) + val List references = classes.map[EReferences].flatten.filter [ + it.name != "eFactoryInstance" && it.name != "contents" && it.name != "references" && + it.name != "eGenericType" && it.name != "eGenericSuperTypes" + ].filter[!it.derived].toList + val List attributes = #[] // classes.map[EAttributes].flatten.toList + return new EcoreMetamodelDescriptor(classes, #{}, false, enums, literals, references, attributes) } - + override getRelevantTypes(EcoreMetamodelDescriptor descriptor) { return null } - + override getRelevantReferences(EcoreMetamodelDescriptor descriptor) { null } - + override loadQueries(EcoreMetamodelDescriptor metamodel) { val patternGroup = Ecore.instance val patterns = patternGroup.specifications.toList @@ -253,13 +306,92 @@ class EcoreLoader extends MetamodelLoader { ) } - + override loadPartialModel() { - this.workspace.readModel(EObject,"ecore.xmi").eResource.allContents.toList + this.workspace.readModel(EObject, "ecore.xmi").eResource.allContents.toList } - + override additionalConstraints() { #[] } + + override getTypeQuantiles() { + #{ + "EAnnotation" -> new TypeQuantiles(0, 0), + "EAttribute" -> new TypeQuantiles(0.14, 0.300943396226415), + "EClass" -> new TypeQuantiles(0.224014336917563, 0.372881355932203), + "EDataType" -> new TypeQuantiles(0, 0), + "EEnum" -> new TypeQuantiles(0, 0.0275208638045255), + "EEnumLiteral" -> new TypeQuantiles(0, 0.105204907665065), + "EGenericType" -> new TypeQuantiles(0, 0), + "EOperation" -> new TypeQuantiles(0, 0), + "EPackage" -> new TypeQuantiles(0.0119047619047619, 0.0192307692307692), + "EParameter" -> new TypeQuantiles(0, 0), + "EReference" -> new TypeQuantiles(0.217599234815878, 0.406779661016949), + "EStringToStringMapEntry" -> new TypeQuantiles(0, 0), + "ETypeParameter" -> new TypeQuantiles(0, 0) + } + } + +} + +class SatelliteLoader extends MetamodelLoader { + + new(ReasonerWorkspace workspace) { + super(workspace) + } + + override loadMetamodel() { + val package = SatellitePackage.eINSTANCE + val List classes = package.EClassifiers.filter(EClass).toList + val List enums = package.EClassifiers.filter(EEnum).toList + val List literals = enums.map[ELiterals].flatten.toList + val List references = classes.map[EReferences].flatten.toList + val List attributes = classes.map[EAttributes].flatten.toList + return new EcoreMetamodelDescriptor(classes, #{}, false, enums, literals, references, attributes) + } + + override getRelevantTypes(EcoreMetamodelDescriptor descriptor) { + null + } + + override getRelevantReferences(EcoreMetamodelDescriptor descriptor) { + null + } + + override loadQueries(EcoreMetamodelDescriptor metamodel) { + val i = SatelliteQueriesAll.instance + val patterns = i.specifications.toList + val wfPatterns = patterns.filter[it.allAnnotations.exists[it.name == "Constraint"]].toSet + val derivedFeatures = new LinkedHashMap + val res = new ViatraQuerySetDescriptor( + patterns, + wfPatterns, + derivedFeatures + ) + return res + } + + override loadPartialModel() { + this.workspace.readModel(EObject, "satellite.xmi").eResource.allContents.toList + } + + override additionalConstraints() { #[] } + + override getHints(Ecore2Logic ecore2Logic, Ecore2Logic_Trace trace) { + #[new SatelliteHint(ecore2Logic, trace)] + } + + override getTypeQuantiles() { + #{ + "CubeSat3U" -> new TypeQuantiles(0.1, 0.25), + "CubeSat6U" -> new TypeQuantiles(0, 0.25), + "SmallSat" -> new TypeQuantiles(0, 0.15), + "UHFCommSubsystem" -> new TypeQuantiles(0.08, 0.25), + "XCommSubsystem" -> new TypeQuantiles(0.08, 0.25), + "KaCommSubsystem" -> new TypeQuantiles(0, 0.1), + "InterferometryPayload" -> new TypeQuantiles(0.15, 0.25) + } + } -} \ No newline at end of file +} diff --git a/Tests/hu.bme.mit.inf.dslreasoner.run/src/hu/bme/mit/inf/dslreasoner/run/SGraphHint.xtend b/Tests/hu.bme.mit.inf.dslreasoner.run/src/hu/bme/mit/inf/dslreasoner/run/SGraphHint.xtend new file mode 100644 index 00000000..97ce4ee6 --- /dev/null +++ b/Tests/hu.bme.mit.inf.dslreasoner.run/src/hu/bme/mit/inf/dslreasoner/run/SGraphHint.xtend @@ -0,0 +1,46 @@ +package hu.bme.mit.inf.dslreasoner.run + +import hu.bme.mit.inf.dslreasoner.ecore2logic.Ecore2Logic +import hu.bme.mit.inf.dslreasoner.ecore2logic.Ecore2Logic_Trace +import hu.bme.mit.inf.dslreasoner.viatrasolver.logic2viatra.cardinality.LinearTypeExpressionBuilderFactory +import hu.bme.mit.inf.dslreasoner.viatrasolver.logic2viatra.patterns.PatternGenerator + +class SGraphHint extends Ecore2LogicTraceBasedHint { + new(Ecore2Logic ecore2Logic, Ecore2Logic_Trace trace) { + super(ecore2Logic, trace) + } + + override getAdditionalPatterns(extension PatternGenerator patternGenerator) { + "" + } + + override createConstraintUpdater(LinearTypeExpressionBuilderFactory it) { + val newEntriesWithoutRegionCount = createBuilder.add(1, "Entry".type).add(-1, "Region".type).build + val newStatesWithoutRegionCount = createBuilder.add(1, "State".type).add(-1, "Region".type).build + val newTransitionWithoutNeedsOutgoingCount = createBuilder.add(1, "Transition".type).add(-1, "Entry".type). + add(-1, "Choice".type).build + val newTransitionWithoutNeedsIncomingCount = createBuilder.add(1, "Transition".type).add(-1, "Choice".type). + build + + val regionsWithoutEntryMatcher = createMatcher( + "unfinishedBy_pattern_hu_bme_mit_inf_dslreasoner_partialsnapshot_mavo_yakindu_noEntryInRegion") + val regionsWithoutStateMatcher = createMatcher( + "unfinishedBy_pattern_hu_bme_mit_inf_dslreasoner_partialsnapshot_mavo_yakindu_noStateInRegion") + val entryHasNoOutgoingMatcher = createMatcher( + "unfinishedBy_pattern_hu_bme_mit_inf_dslreasoner_partialsnapshot_mavo_yakindu_noOutgoingTransitionFromEntry") + val choiceHasNoOutgoingMatcher = createMatcher( + "unfinishedBy_pattern_hu_bme_mit_inf_dslreasoner_partialsnapshot_mavo_yakindu_choiceHasNoOutgoing") + val choiceHasNoIncomingMatcher = createMatcher( + "unfinishedBy_pattern_hu_bme_mit_inf_dslreasoner_partialsnapshot_mavo_yakindu_choiceHasNoIncoming") + val transitionWithoutTargetMatcher = createMatcher("unfinishedLowerMultiplicity_target_reference_Transition") + + return [ p | + newEntriesWithoutRegionCount.assertEqualsTo(regionsWithoutEntryMatcher.countMatches(p)) + newStatesWithoutRegionCount.tightenLowerBound(regionsWithoutStateMatcher.countMatches(p)) + newTransitionWithoutNeedsOutgoingCount.tightenLowerBound( + entryHasNoOutgoingMatcher.countMatches(p) + choiceHasNoOutgoingMatcher.countMatches(p)) + newTransitionWithoutNeedsIncomingCount.tightenLowerBound( + choiceHasNoIncomingMatcher.countMatches(p) - transitionWithoutTargetMatcher.getCount(p)) + ] + } +} diff --git a/Tests/hu.bme.mit.inf.dslreasoner.run/src/hu/bme/mit/inf/dslreasoner/run/SatelliteHint.xtend b/Tests/hu.bme.mit.inf.dslreasoner.run/src/hu/bme/mit/inf/dslreasoner/run/SatelliteHint.xtend new file mode 100644 index 00000000..e95c0c64 --- /dev/null +++ b/Tests/hu.bme.mit.inf.dslreasoner.run/src/hu/bme/mit/inf/dslreasoner/run/SatelliteHint.xtend @@ -0,0 +1,86 @@ +package hu.bme.mit.inf.dslreasoner.run + +import hu.bme.mit.inf.dslreasoner.ecore2logic.Ecore2Logic +import hu.bme.mit.inf.dslreasoner.ecore2logic.Ecore2Logic_Trace +import hu.bme.mit.inf.dslreasoner.viatrasolver.logic2viatra.Modality +import hu.bme.mit.inf.dslreasoner.viatrasolver.logic2viatra.cardinality.LinearTypeExpressionBuilderFactory +import hu.bme.mit.inf.dslreasoner.viatrasolver.logic2viatra.patterns.PatternGenerator + +class SatelliteHint extends Ecore2LogicTraceBasedHint { + static val INTERFEROMETY_PAYLOAD = "hint_interferometryPayload" + static val REMAINING_CONTENTS_KA_COMM_SUBSYSTEM = "hint_kaCommSubsystem" + static val HINT_SPACECRAFT_UHF_POSSIBLE_LINK = "hint_spacecraftWithUhfPossibleLink" + static val HINT_SPACECRAFT_UHF_ONLY_NO_LINK = "hint_spacecraftUhfOnlyNoLink" + + new(Ecore2Logic ecore2Logic, Ecore2Logic_Trace trace) { + super(ecore2Logic, trace) + } + + override getAdditionalPatterns(PatternGenerator it) ''' + pattern «INTERFEROMETY_PAYLOAD»(problem:LogicProblem, interpretation:PartialInterpretation, object:DefinedElement) { + find interpretation(problem, interpretation); + find mustExist(problem, interpretation, object); + «typeIndexer.referInstanceOf("InterferometryPayload".type, Modality.MUST, "object")» + } + + private pattern «REMAINING_CONTENTS_KA_COMM_SUBSYSTEM»_helper(problem:LogicProblem, interpretation:PartialInterpretation, object:DefinedElement, remainingContents:java Integer) { + find remainingContents_commSubsystem_reference_CommunicatingElement_helper(problem, interpretation, object, remainingContents); + «typeIndexer.referInstanceOf("SmallSat".type, Modality.MUST, "object")» + } + + pattern «REMAINING_CONTENTS_KA_COMM_SUBSYSTEM»(problem:LogicProblem, interpretation:PartialInterpretation, remainingContents:java Integer) { + find interpretation(problem, interpretation); + remainingContents == sum find «REMAINING_CONTENTS_KA_COMM_SUBSYSTEM»_helper(problem, interpretation, _, #_); + } + + private pattern hint_spacecraftNotUhfOnly(problem:LogicProblem, interpretation:PartialInterpretation, spacecraft:DefinedElement) { + find interpretation(problem, interpretation); + find mustExist(problem, interpretation, spacecraft); + «typeIndexer.referInstanceOf("Spacecraft".type, Modality.MUST, "spacecraft")» + «relationDeclarationIndexer.referRelation("CommunicatingElement".relation("commSubsystem"), "spacecraft", "comm", Modality.MAY)» + neg «typeIndexer.referInstanceOf("UHFCommSubsystem".type, Modality.MUST, "comm")» + } + + private pattern hint_spacecraftWithUhf(problem:LogicProblem, interpretation:PartialInterpretation, spacecraft:DefinedElement) { + find interpretation(problem, interpretation); + find mustExist(problem, interpretation, spacecraft); + «typeIndexer.referInstanceOf("Spacecraft".type, Modality.MUST, "spacecraft")» + «relationDeclarationIndexer.referRelation("CommunicatingElement".relation("commSubsystem"), "spacecraft", "comm", Modality.MUST)» + «typeIndexer.referInstanceOf("UHFCommSubsystem".type, Modality.MUST, "comm")» + } + + pattern «HINT_SPACECRAFT_UHF_POSSIBLE_LINK»(problem:LogicProblem, interpretation:PartialInterpretation) { + find hint_spacecraftWithUhf(problem, interpretation, spacecraft); + find hint_spacecraftNotUhfOnly(problem, interpretation, spacecraft); + } + + pattern «HINT_SPACECRAFT_UHF_ONLY_NO_LINK»(problem:LogicProblem, interpretation:PartialInterpretation) { + find interpretation(problem, interpretation); + find mustExist(problem, interpretation, spacecraft); + «typeIndexer.referInstanceOf("Spacecraft".type, Modality.MUST, "spacecraft")» + neg find hint_spacecraftNotUhfOnly(problem, interpretation, spacecraft); + find currentInRelation_pattern_hu_bme_mit_inf_dslreasoner_domains_satellite_queries_noLinkToGroundStation(problem, interpretation, spacecraft); + } + ''' + + override createConstraintUpdater(LinearTypeExpressionBuilderFactory it) { + val interferometryPayloadCount = createBuilder.add(1, "InterferometryPayload".type).build + val kaCommSubsystemWithoutSmallSatCount = createBuilder.add(1, "KaCommSubsystem".type).add(-2, "SmallSat".type). + build + val uhfCommSubsystemCount = createBuilder.add(1, "UHFCommSubsystem".type).build + + val interferometryPayloadMatcher = createMatcher(INTERFEROMETY_PAYLOAD) + val kaCommSubsystemRemainingContentsMatcher = createMatcher(REMAINING_CONTENTS_KA_COMM_SUBSYSTEM) + val uhfPossibleLinkMatcher = createMatcher(HINT_SPACECRAFT_UHF_POSSIBLE_LINK) + val uhfNoLinkMatcher = createMatcher(HINT_SPACECRAFT_UHF_ONLY_NO_LINK) + + return [ p | + interferometryPayloadCount.tightenLowerBound(2 - interferometryPayloadMatcher.countMatches(p)) + kaCommSubsystemWithoutSmallSatCount.tightenUpperBound(kaCommSubsystemRemainingContentsMatcher.getCount(p)) + if (uhfPossibleLinkMatcher.countMatches(p) == 0 && uhfNoLinkMatcher.countMatches(p) >= 1) { + uhfCommSubsystemCount.tightenLowerBound(1) + } + ] + } + +} diff --git a/Tests/hu.bme.mit.inf.dslreasoner.run/src/hu/bme/mit/inf/dslreasoner/run/TypeDistributionCalculator.xtend b/Tests/hu.bme.mit.inf.dslreasoner.run/src/hu/bme/mit/inf/dslreasoner/run/TypeDistributionCalculator.xtend new file mode 100644 index 00000000..e2d6e6ca --- /dev/null +++ b/Tests/hu.bme.mit.inf.dslreasoner.run/src/hu/bme/mit/inf/dslreasoner/run/TypeDistributionCalculator.xtend @@ -0,0 +1,35 @@ +package hu.bme.mit.inf.dslreasoner.run + +import hu.bme.mit.inf.dslreasoner.domains.yakindu.sgraph.yakindumm.YakindummPackage +import java.io.File +import org.eclipse.emf.common.util.URI +import org.eclipse.emf.ecore.EPackage +import org.eclipse.emf.ecore.EcorePackage +import org.eclipse.emf.ecore.resource.Resource +import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl +import org.eclipse.emf.ecore.xmi.impl.XMIResourceFactoryImpl + +class TypeDistributionCalculator { + public static def void main(String[] args) { + Resource.Factory.Registry.INSTANCE.getExtensionToFactoryMap().put("*", new XMIResourceFactoryImpl) + EPackage.Registry.INSTANCE.put(EcorePackage.eNS_URI, EcorePackage.eINSTANCE) + EPackage.Registry.INSTANCE.put(YakindummPackage.eNS_URI, YakindummPackage.eINSTANCE) + + println("model,className,count") + val directory = new File(args.get(0)) + for (file : directory.listFiles) { + val modelName = file.name + val resourceSet = new ResourceSetImpl + val resource = resourceSet.getResource(URI.createFileURI(file.absolutePath), true) + val objectsByTypeName = resource.allContents.filter [ obj | + val featureName = obj.eContainingFeature?.name + // Filter out "derived containment" references in Ecore. + // See https://stackoverflow.com/a/46340165 + featureName != "eGenericType" && featureName != "eGenericSuperTypes" + ].groupBy[eClass.name] + for (pair : objectsByTypeName.entrySet) { + println('''«modelName»,«pair.key»,«pair.value.size»''') + } + } + } +} diff --git a/Tests/hu.bme.mit.inf.dslreasoner.run/src/hu/bme/mit/inf/dslreasoner/run/script/MeasurementScript.xtend b/Tests/hu.bme.mit.inf.dslreasoner.run/src/hu/bme/mit/inf/dslreasoner/run/script/MeasurementScript.xtend new file mode 100644 index 00000000..5abff962 --- /dev/null +++ b/Tests/hu.bme.mit.inf.dslreasoner.run/src/hu/bme/mit/inf/dslreasoner/run/script/MeasurementScript.xtend @@ -0,0 +1,70 @@ +package hu.bme.mit.inf.dslreasoner.run.script + +import java.util.List +import org.eclipse.xtend.lib.annotations.Accessors + +@Accessors +class MeasurementScript { + String inputPath + String outputPath + int timeout + boolean saveModels + boolean saveTemporaryFiles + int warmupIterations + int iterations + Domain domain + Scope scope + List sizes + Solver solver + ScopePropagator scopePropagator + ScopeConstraints propagatedConstraints + PolyhedronSolver polyhedronSolver + ScopeHeuristic scopeHeuristic + + def toCsvHeader() { + '''«domain»,«scope»,«solver»,«scopePropagator ?: "NULL"»,«propagatedConstraints ?: "NULL"»,«polyhedronSolver ?: "NULL"»''' + } +} + +enum Domain { + fs, + ecore, + Yakindu, + FAM, + satellite +} + +enum Scope { + none, + quantiles +} + +enum Solver { + ViatraSolver, + AlloySolver +} + +enum ScopePropagator { + none, + basic, + polyhedral +} + +enum ScopeConstraints { + none, + typeHierarchy, + relations, + hints +} + +enum PolyhedronSolver { + Z3Integer, + Z3Real, + Cbc, + Clp +} + +enum ScopeHeuristic { + basic, + polyhedral +} diff --git a/Tests/hu.bme.mit.inf.dslreasoner.run/src/hu/bme/mit/inf/dslreasoner/run/script/MeasurementScriptRunner.xtend b/Tests/hu.bme.mit.inf.dslreasoner.run/src/hu/bme/mit/inf/dslreasoner/run/script/MeasurementScriptRunner.xtend new file mode 100644 index 00000000..48e750cb --- /dev/null +++ b/Tests/hu.bme.mit.inf.dslreasoner.run/src/hu/bme/mit/inf/dslreasoner/run/script/MeasurementScriptRunner.xtend @@ -0,0 +1,351 @@ +package hu.bme.mit.inf.dslreasoner.run.script + +import com.google.gson.Gson +import hu.bme.mit.inf.dslreasoner.ecore2logic.EClassMapper +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 +import hu.bme.mit.inf.dslreasoner.logic.model.builder.DocumentationLevel +import hu.bme.mit.inf.dslreasoner.logic.model.builder.TypeScopes +import hu.bme.mit.inf.dslreasoner.logic.model.logiclanguage.DefinedElement +import hu.bme.mit.inf.dslreasoner.logic.model.logiclanguage.IntLiteral +import hu.bme.mit.inf.dslreasoner.logic.model.logiclanguage.RealLiteral +import hu.bme.mit.inf.dslreasoner.logic.model.logiclanguage.StringLiteral +import hu.bme.mit.inf.dslreasoner.logic.model.logiclanguage.Type +import hu.bme.mit.inf.dslreasoner.logic.model.logiclanguage.TypeDefinition +import hu.bme.mit.inf.dslreasoner.logic.model.logicproblem.LogicProblem +import hu.bme.mit.inf.dslreasoner.logic.model.logicresult.IntStatisticEntry +import hu.bme.mit.inf.dslreasoner.logic.model.logicresult.LogicresultFactory +import hu.bme.mit.inf.dslreasoner.logic.model.logicresult.ModelResult +import hu.bme.mit.inf.dslreasoner.logic.model.logicresult.RealStatisticEntry +import hu.bme.mit.inf.dslreasoner.logic.model.logicresult.Statistics +import hu.bme.mit.inf.dslreasoner.logic.model.logicresult.StringStatisticEntry +import hu.bme.mit.inf.dslreasoner.logic2ecore.Logic2Ecore +import hu.bme.mit.inf.dslreasoner.run.EcoreLoader +import hu.bme.mit.inf.dslreasoner.run.FAMLoader +import hu.bme.mit.inf.dslreasoner.run.FileSystemLoader +import hu.bme.mit.inf.dslreasoner.run.MetamodelLoader +import hu.bme.mit.inf.dslreasoner.run.SatelliteLoader +import hu.bme.mit.inf.dslreasoner.run.YakinduLoader +import hu.bme.mit.inf.dslreasoner.util.CollectionsUtil +import hu.bme.mit.inf.dslreasoner.viatra2logic.Viatra2Logic +import hu.bme.mit.inf.dslreasoner.viatra2logic.Viatra2LogicConfiguration +import hu.bme.mit.inf.dslreasoner.viatrasolver.logic2viatra.cardinality.PolyhedralScopePropagatorConstraints +import hu.bme.mit.inf.dslreasoner.viatrasolver.logic2viatra.cardinality.PolyhedralScopePropagatorSolver +import hu.bme.mit.inf.dslreasoner.viatrasolver.logic2viatra.cardinality.ScopePropagatorStrategy +import hu.bme.mit.inf.dslreasoner.viatrasolver.partialinterpretation2logic.InstanceModel2Logic +import hu.bme.mit.inf.dslreasoner.viatrasolver.partialinterpretationlanguage.partial2logicannotations.PartialModelRelation2Assertion +import hu.bme.mit.inf.dslreasoner.viatrasolver.reasoner.ViatraReasoner +import hu.bme.mit.inf.dslreasoner.viatrasolver.reasoner.ViatraReasonerConfiguration +import hu.bme.mit.inf.dslreasoner.workspace.FileSystemWorkspace +import hu.bme.mit.inf.dslreasoner.workspace.ReasonerWorkspace +import java.io.FileReader +import java.util.HashMap +import java.util.HashSet +import java.util.Map +import java.util.Set +import org.eclipse.emf.ecore.EObject +import org.eclipse.emf.ecore.resource.Resource +import org.eclipse.emf.ecore.xmi.impl.XMIResourceFactoryImpl +import org.eclipse.viatra.query.patternlanguage.emf.EMFPatternLanguageStandaloneSetup +import org.eclipse.viatra.query.runtime.api.ViatraQueryEngineOptions +import org.eclipse.viatra.query.runtime.rete.matcher.ReteBackendFactory +import org.eclipse.xtend.lib.annotations.Data + +class MeasurementScriptRunner { + static val MODEL_SIZE_GAP = 0 + static val SCOPE_PROPAGATOR_TIMEOUT = 10 + static val USEC_TO_MSEC = 1000000 + + static extension val LogicresultFactory = LogicresultFactory.eINSTANCE + + val MeasurementScript script + val ReasonerWorkspace inputWorkspace + val ReasonerWorkspace outputWorkspace + val MetamodelLoader metamodelLoader + + new(MeasurementScript script) { + this.script = script + inputWorkspace = new FileSystemWorkspace(script.inputPath + "/", "") + outputWorkspace = new FileSystemWorkspace(script.outputPath + + "/", '''«script.domain»_«script.solver»_«script.scope»_«script.scopePropagator ?: "na"»_«script.propagatedConstraints ?: "na"»_«script.polyhedronSolver ?: "na"»_''') + metamodelLoader = switch (script.domain) { + case fs: new FileSystemLoader(inputWorkspace) + case ecore: new EcoreLoader(inputWorkspace) + case Yakindu: new YakinduLoader(inputWorkspace) => [useSynchronization = false; useComplexStates = true] + case FAM: new FAMLoader(inputWorkspace) + case satellite: new SatelliteLoader(inputWorkspace) + default: throw new IllegalArgumentException("Unsupported domain: " + script.domain) + } + } + + def run() { + if (script.sizes.empty) { + return + } + val start = System.currentTimeMillis + val warmupSize = script.sizes.head + for (var int i = 0; i < script.warmupIterations; i++) { + System.err.println('''Warmup «i + 1»/«script.warmupIterations»...''') + runExperiment(warmupSize) + } + val warmupEnd = System.currentTimeMillis + System.err.println('''Warmup completed in «(warmupEnd - start) / 1000» seconds''') + for (size : script.sizes) { + var int failures = 0 + for (var int i = 0; i < script.iterations; i++) { + System.err.println("Running GC...") + runGc() + System.err.println('''Iteration «i + 1»/«script.iterations» of size «size»...''') + val startTime = System.currentTimeMillis + val result = runExperiment(size) + val headerPrefix = '''«script.toCsvHeader»,«size»,«i + 1»,«result.resultName»''' + println('''«headerPrefix»,startTime,«startTime»''') + println('''«headerPrefix»,logic2SolverTransformationTime,«result.statistics.transformationTime»''') + println('''«headerPrefix»,solverTime,«result.statistics.solverTime»''') + for (statistic : result.statistics.entries) { + val valueString = switch (statistic) { + IntStatisticEntry: statistic.value.toString + RealStatisticEntry: statistic.value.toString + StringStatisticEntry: statistic.value.toString + default: statistic.toString + } + println('''«headerPrefix»,«statistic.name»,«valueString»''') + } + if (script.saveModels && result.model !== null) { + outputWorkspace.writeModel(result.model, '''«size»_«i + 1».xmi''') + } + if (result.resultName === "InsuficientResourcesResultImpl") { + failures++ + } + System.out.flush + } + if (failures == script.iterations) { + System.err.println("All measurements failed") + return + } + } + val end = System.currentTimeMillis + System.err.println('''Measurement completed in «(end - start) / 1000» seconds''') + } + + private static def void runGc() { + System.gc + Thread.sleep(100) + System.gc + Thread.sleep(100) + System.gc + Thread.sleep(800) + } + + private def runExperiment(int modelSize) { + if (script.solver != Solver.ViatraSolver) { + throw new IllegalArgumentException("Only VIATRA-Generator is supported") + } + val config = new ViatraReasonerConfiguration + config.solutionScope.numberOfRequiredSolutions = 1 + config.scopePropagatorStrategy = switch (script.scopePropagator) { + case none: + ScopePropagatorStrategy.None + case basic: + switch (script.propagatedConstraints) { + case none: + ScopePropagatorStrategy.Basic + case typeHierarchy: + ScopePropagatorStrategy.BasicTypeHierarchy + case relations, + case hints: + throw new IllegalArgumentException( + "Basic scope propagator does not support relational and hint constraints") + default: + throw new IllegalArgumentException("Unknown scope constraints: " + script.propagatedConstraints) + } + case polyhedral: { + val constraints = switch (script.propagatedConstraints) { + case none: + throw new IllegalArgumentException( + "Polyhedral scope propagator needs at least type hierarchy constraints") + case typeHierarchy: + PolyhedralScopePropagatorConstraints.TypeHierarchy + case relations, + case hints: + PolyhedralScopePropagatorConstraints.Relational + default: + throw new IllegalArgumentException("Unknown scope constraints: " + script.propagatedConstraints) + } + val polyhedronSolver = switch (script.polyhedronSolver) { + case Z3Integer: PolyhedralScopePropagatorSolver.Z3Integer + case Z3Real: PolyhedralScopePropagatorSolver.Z3Real + case Cbc: PolyhedralScopePropagatorSolver.Cbc + case Clp: PolyhedralScopePropagatorSolver.Clp + default: throw new IllegalArgumentException("Unknown polyhedron solver: " + script.polyhedronSolver) + } + val updateHeuristic = script.scopeHeuristic != ScopeHeuristic.basic + new ScopePropagatorStrategy.Polyhedral(constraints, polyhedronSolver, updateHeuristic, + SCOPE_PROPAGATOR_TIMEOUT) + } + default: + throw new IllegalArgumentException("Unknown scope propagator: " + script.scopePropagator) + } + config.runtimeLimit = script.timeout + config.documentationLevel = if(script.saveTemporaryFiles) DocumentationLevel.NORMAL else DocumentationLevel.NONE + config.debugConfiguration.partialInterpretatioVisualiser = null + config.searchSpaceConstraints.additionalGlobalConstraints += metamodelLoader.additionalConstraints + + val modelLoadingStart = System.nanoTime + val metamodelDescriptor = metamodelLoader.loadMetamodel + val partialModelDescriptor = metamodelLoader.loadPartialModel + val queryDescriptor = metamodelLoader.loadQueries(metamodelDescriptor) + val modelLoadingTime = System.nanoTime - modelLoadingStart + + val domain2LogicTransformationStart = System.nanoTime + val Ecore2Logic ecore2Logic = new Ecore2Logic + val Viatra2Logic viatra2Logic = new Viatra2Logic(ecore2Logic) + val InstanceModel2Logic instanceModel2Logic = new InstanceModel2Logic + var modelGeneration = ecore2Logic.transformMetamodel(metamodelDescriptor, new Ecore2LogicConfiguration()) + var problem = modelGeneration.output + problem = instanceModel2Logic.transform( + modelGeneration, + partialModelDescriptor + ).output + problem = viatra2Logic.transformQueries( + queryDescriptor, + modelGeneration, + new Viatra2LogicConfiguration + ).output + initializeScope(config, modelSize, problem, ecore2Logic, modelGeneration.trace) + if (script.propagatedConstraints == ScopeConstraints.hints) { + config.hints = metamodelLoader.getHints(ecore2Logic, modelGeneration.trace) + } + val domain2LogicTransformationTime = System.nanoTime - domain2LogicTransformationStart + + if (config.documentationLevel != DocumentationLevel.NONE) { + outputWorkspace.writeModel(problem, "initial.logicproblem") + } + + val solver = new ViatraReasoner + val result = solver.solve(problem, config, outputWorkspace) + val statistics = result.statistics + statistics.entries += createIntStatisticEntry => [ + name = "modelLoadingTime" + value = (modelLoadingTime / USEC_TO_MSEC) as int + ] + statistics.entries += createIntStatisticEntry => [ + name = "domain2LogicTransformationTime" + value = (domain2LogicTransformationTime / USEC_TO_MSEC) as int + ] + var EObject modelResult = null + if (result instanceof ModelResult) { + val intepretations = solver.getInterpretations(result) + if (intepretations.size != 1) { + throw new IllegalStateException("Expected 1 interpretation, got " + intepretations.size) + } + var resultTransformationStart = System.nanoTime + val logic2Ecore = new Logic2Ecore(ecore2Logic) + modelResult = logic2Ecore.transformInterpretation(intepretations.head, modelGeneration.trace) + val resultTransformationTime = System.nanoTime - resultTransformationStart + statistics.entries += createIntStatisticEntry => [ + name = "ecore2LogicTransformationTime" + value = (resultTransformationTime / USEC_TO_MSEC) as int + ] + } + + new ExperimentResult(result.class.simpleName, statistics, modelResult) + } + + private def initializeScope(ViatraReasonerConfiguration config, int modelSize, LogicProblem problem, + EClassMapper eClassMapper, Ecore2Logic_Trace trace) { + val knownElements = initializeKnownElements(problem, config.typeScopes) + if (modelSize < 0) { + config.typeScopes.minNewElements = 0 + config.typeScopes.maxNewElements = TypeScopes.Unlimited + } else { + val numberOfKnownElements = knownElements.values.flatten.toSet.size + val newElementCount = modelSize - numberOfKnownElements + config.typeScopes.minNewElements = newElementCount + config.typeScopes.maxNewElements = newElementCount + MODEL_SIZE_GAP + } + switch (script.scope) { + case none: + return + case quantiles: { + val quantiles = metamodelLoader.typeQuantiles + for (eClassInScope : eClassMapper.allClassesInScope(trace)) { + val quantile = quantiles.get(eClassInScope.name) + if (quantile !== null) { + val type = eClassMapper.TypeofEClass(trace, eClassInScope) + val knownInstances = knownElements.get(type) + val currentCount = if(knownInstances === null) 0 else knownInstances.size + val lowCount = Math.floor(modelSize * quantile.low) as int + val highCount = Math.ceil((modelSize + MODEL_SIZE_GAP) * quantile.high) as int + config.typeScopes.minNewElementsByType.put(type, lowCount - currentCount) + config.typeScopes.maxNewElementsByType.put(type, highCount - currentCount) + } + } + } + default: + throw new IllegalArgumentException("Unknown scope: " + script.scope) + } + } + + /* + * Copied from hu.bme.mit.inf.dslreasoner.application.execution.ScopeLoader.initialiseknownElements(LogicProblem, TypeScopes) + */ + private static def initializeKnownElements(LogicProblem p, TypeScopes s) { + val Map> res = new HashMap + + // 1. fill map with every types + for (t : p.types) { + res.put(t, new HashSet) + } + + // 2. fill map with every objects + for (definedType : p.types.filter(TypeDefinition)) { + val supertypes = CollectionsUtil.transitiveClosureStar(definedType)[supertypes] + for (supertype : supertypes) { + for (element : definedType.elements) { + res.get(supertype).add(element) + } + } + } + val partialModelContents = p.annotations.filter(PartialModelRelation2Assertion).map[target].toList.map [ + eAllContents.toIterable + ].flatten.toList + s.knownIntegers += partialModelContents.filter(IntLiteral).map[it.value] + s.knownReals += partialModelContents.filter(RealLiteral).map[it.value] + s.knownStrings += partialModelContents.filter(StringLiteral).map[it.value] + + res + } + + public static def void main(String[] args) { + if (args.length != 1) { + System.err.println("Missing measurement script name.") + System.exit(-1) + } + EMFPatternLanguageStandaloneSetup.doSetup + ViatraQueryEngineOptions.setSystemDefaultBackends(ReteBackendFactory.INSTANCE, ReteBackendFactory.INSTANCE, + ReteBackendFactory.INSTANCE) + Resource.Factory.Registry.INSTANCE.getExtensionToFactoryMap().put("*", new XMIResourceFactoryImpl) + val config = readConfig(args.get(0)) + val runnner = new MeasurementScriptRunner(config) + runnner.run() + } + + static def readConfig(String scriptPath) { + val gson = new Gson + val reader = new FileReader(scriptPath) + try { + gson.fromJson(reader, MeasurementScript) + } finally { + reader.close + } + } + + @Data + private static class ExperimentResult { + String resultName + Statistics statistics + EObject model + } +} -- cgit v1.2.3-54-g00ecf