diff options
author | Aren Babikian <aren.babikian@mail.mcgill.ca> | 2021-01-05 23:44:50 +0100 |
---|---|---|
committer | Aren Babikian <aren.babikian@mail.mcgill.ca> | 2021-01-05 23:44:50 +0100 |
commit | d53ae070b68fc3bc429c1467f62779ba08d35755 (patch) | |
tree | 88e2de252727011a2ba2033aa0267eb211eec208 /Tests/MODELS2020-CaseStudies/models20.diversity-calculator/src/hu/bme/mit/inf | |
parent | Prefer the global scope insted of nsURI in application configuration (diff) | |
parent | add readme and diversity measurements (diff) | |
download | VIATRA-Generator-d53ae070b68fc3bc429c1467f62779ba08d35755.tar.gz VIATRA-Generator-d53ae070b68fc3bc429c1467f62779ba08d35755.tar.zst VIATRA-Generator-d53ae070b68fc3bc429c1467f62779ba08d35755.zip |
merge models20 measurements
Diffstat (limited to 'Tests/MODELS2020-CaseStudies/models20.diversity-calculator/src/hu/bme/mit/inf')
2 files changed, 390 insertions, 0 deletions
diff --git a/Tests/MODELS2020-CaseStudies/models20.diversity-calculator/src/hu/bme/mit/inf/dslreasoner/diversitycalculator/MeasureDiversity.xtend b/Tests/MODELS2020-CaseStudies/models20.diversity-calculator/src/hu/bme/mit/inf/dslreasoner/diversitycalculator/MeasureDiversity.xtend new file mode 100644 index 00000000..8a2bff1a --- /dev/null +++ b/Tests/MODELS2020-CaseStudies/models20.diversity-calculator/src/hu/bme/mit/inf/dslreasoner/diversitycalculator/MeasureDiversity.xtend | |||
@@ -0,0 +1,283 @@ | |||
1 | package hu.bme.mit.inf.dslreasoner.diversitycalculator | ||
2 | |||
3 | import TaxationWithRoot.TaxationWithRootPackage | ||
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.EcoreMetamodelDescriptor | ||
8 | import hu.bme.mit.inf.dslreasoner.ecore2logic.ecore2logicannotations.Ecore2logicannotationsPackage | ||
9 | import hu.bme.mit.inf.dslreasoner.logic.model.logiclanguage.DefinedElement | ||
10 | import hu.bme.mit.inf.dslreasoner.logic.model.logiclanguage.LogiclanguagePackage | ||
11 | import hu.bme.mit.inf.dslreasoner.logic.model.logicproblem.LogicproblemPackage | ||
12 | import hu.bme.mit.inf.dslreasoner.viatra2logic.viatra2logicannotations.Viatra2LogicAnnotationsPackage | ||
13 | import hu.bme.mit.inf.dslreasoner.viatrasolver.partialinterpretation2logic.InstanceModel2PartialInterpretation | ||
14 | import hu.bme.mit.inf.dslreasoner.viatrasolver.partialinterpretationlanguage.neighbourhood.AbstractNodeDescriptor | ||
15 | import hu.bme.mit.inf.dslreasoner.viatrasolver.partialinterpretationlanguage.neighbourhood.PartialInterpretation2ImmutableTypeLattice | ||
16 | import hu.bme.mit.inf.dslreasoner.viatrasolver.partialinterpretationlanguage.partialinterpretation.PartialInterpretation | ||
17 | import hu.bme.mit.inf.dslreasoner.viatrasolver.partialinterpretationlanguage.partialinterpretation.PartialinterpretationPackage | ||
18 | import hu.bme.mit.inf.dslreasoner.workspace.FileSystemWorkspace | ||
19 | import java.io.File | ||
20 | import java.io.PrintWriter | ||
21 | import java.util.ArrayList | ||
22 | import java.util.Collections | ||
23 | import java.util.Comparator | ||
24 | import java.util.HashMap | ||
25 | import java.util.HashSet | ||
26 | import java.util.LinkedList | ||
27 | import java.util.List | ||
28 | import java.util.Map | ||
29 | import org.eclipse.emf.ecore.EClass | ||
30 | import org.eclipse.emf.ecore.EEnum | ||
31 | import org.eclipse.emf.ecore.EObject | ||
32 | import org.eclipse.emf.ecore.EcorePackage | ||
33 | import org.eclipse.emf.ecore.resource.Resource | ||
34 | import org.eclipse.emf.ecore.xmi.impl.XMIResourceFactoryImpl | ||
35 | import org.eclipse.viatra.query.runtime.api.ViatraQueryEngineOptions | ||
36 | import org.eclipse.viatra.query.runtime.localsearch.matcher.integration.LocalSearchEMFBackendFactory | ||
37 | import org.eclipse.viatra.query.runtime.rete.matcher.ReteBackendFactory | ||
38 | import org.eclipse.xtend.lib.annotations.Data | ||
39 | import satellite.SatellitePackage | ||
40 | |||
41 | class MeasureDiversity { | ||
42 | static val partialInterpretation2Logic = new InstanceModel2PartialInterpretation | ||
43 | static val neiguboirhoodComputer = new PartialInterpretation2ImmutableTypeLattice | ||
44 | static val Ecore2Logic ecore2Logic = new Ecore2Logic | ||
45 | |||
46 | static private def init() { | ||
47 | LogiclanguagePackage.eINSTANCE.class | ||
48 | LogicproblemPackage.eINSTANCE.class | ||
49 | PartialinterpretationPackage.eINSTANCE.class | ||
50 | Ecore2logicannotationsPackage.eINSTANCE.class | ||
51 | Viatra2LogicAnnotationsPackage.eINSTANCE.class | ||
52 | Resource.Factory.Registry.INSTANCE.extensionToFactoryMap.put("*",new XMIResourceFactoryImpl) | ||
53 | ViatraQueryEngineOptions.setSystemDefaultBackends(ReteBackendFactory.INSTANCE, ReteBackendFactory.INSTANCE, LocalSearchEMFBackendFactory.INSTANCE) | ||
54 | } | ||
55 | static class BenchmarkConfig { | ||
56 | int range; | ||
57 | int size | ||
58 | PrintWriter writer | ||
59 | String scenario | ||
60 | } | ||
61 | |||
62 | def static void main(String[] args) { | ||
63 | init() | ||
64 | |||
65 | val path = '''inputs''' | ||
66 | val scenarios = #[ | ||
67 | |||
68 | '''FamilyTree''', | ||
69 | '''Satellite''', | ||
70 | '''Taxation''', | ||
71 | '''FamilyTreeNoNum''', | ||
72 | '''SatelliteNoNum''', | ||
73 | '''TaxationNoNum''' | ||
74 | ] | ||
75 | |||
76 | val writer = new PrintWriter(path+"/diversity.csv", "UTF-8"); | ||
77 | for(scenario : scenarios) { | ||
78 | val config=new BenchmarkConfig(); | ||
79 | config.writer=writer | ||
80 | config.range = 3 | ||
81 | config.scenario = scenario | ||
82 | //config.size = 5 | ||
83 | //for (var i=1; i<=21;i++) { | ||
84 | measureDiversity(scenario,path+"/"+scenario,config) | ||
85 | //} | ||
86 | println(scenario+" finished") | ||
87 | } | ||
88 | writer.close(); | ||
89 | } | ||
90 | |||
91 | def static void measureDiversity(String scenario, String path, BenchmarkConfig config) { | ||
92 | val file = new File(path) | ||
93 | if(file.isDirectory) { | ||
94 | val subfiles = file.list | ||
95 | val xmiSubfiles = subfiles.filter[it.endsWith(".xmi")] | ||
96 | if(!xmiSubfiles.empty) { | ||
97 | measureDiversity(scenario, file,xmiSubfiles.map[new File(path+"/"+it)].toList,path,config) | ||
98 | } else { | ||
99 | for(subfile : subfiles) { | ||
100 | measureDiversity(scenario, path+"/"+subfile,config) | ||
101 | } | ||
102 | } | ||
103 | } else if(file.isFile) { | ||
104 | // Do nothing | ||
105 | } | ||
106 | } | ||
107 | |||
108 | def static void measureDiversity(String scenario, File parent, List<File> files, String path, BenchmarkConfig config) { | ||
109 | val depth=config.range | ||
110 | |||
111 | //collect and order models | ||
112 | val workspace = new FileSystemWorkspace(path, "") | ||
113 | EcorePackage.eINSTANCE.eClass | ||
114 | TaxationWithRootPackage.eINSTANCE.eClass | ||
115 | SatellitePackage.eINSTANCE.eClass | ||
116 | FamilytreePackage.eINSTANCE.eClass | ||
117 | files.sort(new FileComparator) | ||
118 | |||
119 | //calculate shapes | ||
120 | val modelShapeLists=new HashMap<File,List<Map<? extends AbstractNodeDescriptor, Integer>>>(); | ||
121 | val modelShapes=new HashMap<File,Map<? extends AbstractNodeDescriptor, Integer>>(); | ||
122 | for (file : files) { | ||
123 | |||
124 | val model = workspace.readModel(EObject, file.name) | ||
125 | |||
126 | val pckg = model.eClass.EPackage | ||
127 | val metamodel = new EcoreMetamodelDescriptor( | ||
128 | pckg.EClassifiers.filter(EClass).toList, | ||
129 | Collections::emptySet, | ||
130 | false, | ||
131 | pckg.EClassifiers.filter(EEnum).toList, | ||
132 | pckg.EClassifiers.filter(EEnum).map[ELiterals].flatten.toList, | ||
133 | pckg.EClassifiers.filter(EClass).map[EReferences].flatten.toList, | ||
134 | pckg.EClassifiers.filter(EClass).map[EAttributes].flatten.toList | ||
135 | ) | ||
136 | val metamodelTransformationOutput = ecore2Logic.transformMetamodel(metamodel,new Ecore2LogicConfiguration) | ||
137 | |||
138 | |||
139 | val partialModelOutput = partialInterpretation2Logic.transform(metamodelTransformationOutput, model.eResource, | ||
140 | false) | ||
141 | |||
142 | |||
143 | |||
144 | val list = shapeList(partialModelOutput, depth/*,matchesList*/) | ||
145 | // modelShapeLists.put(file,list) | ||
146 | modelShapes.put(file,list.get(depth-1)) | ||
147 | } | ||
148 | //println('''shapes done.''') | ||
149 | val writer=config.writer | ||
150 | println("Printing internal diversity of models") | ||
151 | //writer.println("Scenario;File;Diversity") | ||
152 | //val size= config.size as double | ||
153 | for (model:files) { | ||
154 | val modelshape=modelShapes.get(model) | ||
155 | val amounts = modelshape.values.reduce[a,b|a+b] | ||
156 | val shapes=modelshape.size() as double | ||
157 | writer.println(config.scenario+","+model+","+shapes/amounts) | ||
158 | } | ||
159 | } | ||
160 | |||
161 | def static getRandomOrders(List<File> files, int number) { | ||
162 | val sequences=new ArrayList<List<File>>(); | ||
163 | for (var i=0;i<number;i++) { | ||
164 | val seq=new ArrayList<File>(files); | ||
165 | Collections.shuffle(seq); | ||
166 | sequences.add(seq); | ||
167 | } | ||
168 | return sequences | ||
169 | } | ||
170 | |||
171 | def static printShapeNumbersOnRandomOrders(List<File> files, HashMap<File, Map<? extends AbstractNodeDescriptor, Integer>> shapes, int number) { | ||
172 | val sequences=new ArrayList<List<File>>(); | ||
173 | println("Printing generated random orders (models apper in original order)") | ||
174 | for (var i=0;i<number;i++) { | ||
175 | val seq=new ArrayList<File>(files); | ||
176 | Collections.shuffle(seq); | ||
177 | sequences.add(seq); | ||
178 | println("Model;NewIndex") | ||
179 | for (model:files) { | ||
180 | println(model+","+seq.indexOf(model)); | ||
181 | } | ||
182 | } | ||
183 | |||
184 | println("Printing generated random orders (models apper in original order)") | ||
185 | println("SeqNo;Index;Model;NewShapes;AllShapes") | ||
186 | |||
187 | var seqindx=0; | ||
188 | for (seq:sequences) { | ||
189 | val currshapes=new HashSet<AbstractNodeDescriptor>() | ||
190 | var indx=0 | ||
191 | for (model:seq){ | ||
192 | val beforesize=currshapes.size | ||
193 | currshapes.addAll(shapes.get(model).keySet) | ||
194 | val aftersize=currshapes.size | ||
195 | val diff=aftersize-beforesize | ||
196 | println(seqindx+","+indx+","+model+","+diff+","+currshapes.size) | ||
197 | indx++; | ||
198 | } | ||
199 | seqindx++; | ||
200 | } | ||
201 | } | ||
202 | |||
203 | def static void printShapeNumbers(List<File> order, Map<File,Map<? extends AbstractNodeDescriptor, Integer>> shapes) { | ||
204 | println("Model;InternalDiversity;NewShapes;AllCurrentShapes") | ||
205 | val currshapes=new HashSet<AbstractNodeDescriptor>() | ||
206 | |||
207 | for (file:order) { | ||
208 | val beforesize=currshapes.size | ||
209 | currshapes.addAll(shapes.get(file).keySet) | ||
210 | val aftersize=currshapes.size | ||
211 | val diff=aftersize-beforesize | ||
212 | println(file+","+shapes.get(file).size+","+diff+","+currshapes.size) | ||
213 | } | ||
214 | } | ||
215 | |||
216 | |||
217 | protected def static runIndex(String name) { | ||
218 | val res = name.split("\\.").head.split('_').get(0) | ||
219 | if(res.startsWith("result")) { | ||
220 | return res.substring(6) | ||
221 | } else { | ||
222 | return res | ||
223 | } | ||
224 | } | ||
225 | protected def static modelIndex(String name) { | ||
226 | name.split("\\.").head.split('_').get(1) | ||
227 | } | ||
228 | |||
229 | protected def static representationList(PartialInterpretation partialModel, int depth) { | ||
230 | val list = new LinkedList | ||
231 | for(i : 0..<depth) { | ||
232 | val neighbourhood = neiguboirhoodComputer.createRepresentation(partialModel,i,0,0) | ||
233 | //val m = neighbourhood.modelRepresentation | ||
234 | //println(m) | ||
235 | list.add(neighbourhood.modelRepresentation.keySet.map[it.hashCode].toSet) | ||
236 | } | ||
237 | return list | ||
238 | } | ||
239 | |||
240 | protected def static List<Map<? extends AbstractNodeDescriptor, Integer>> shapeList(PartialInterpretation partialModel, int depth/*, List<PatternWithMatches> matches*/) { | ||
241 | |||
242 | val list = new LinkedList | ||
243 | for(i : 0..<depth) { | ||
244 | |||
245 | val neighbourhood = neiguboirhoodComputer.createRepresentation(partialModel,i,Integer.MAX_VALUE,Integer.MAX_VALUE/*, matchmap*/) | ||
246 | |||
247 | val openWorldElements = partialModel.openWorldElements //+ partialModel.newElements.filter(PrimitiveElement) + partialModel.problem.elements.filter(PrimitiveElement) | ||
248 | val representationsOfOpenElements = openWorldElements.map[neighbourhood.nodeRepresentations.get(it)].toSet | ||
249 | val allElementRepresentation = new HashMap(neighbourhood.modelRepresentation) | ||
250 | representationsOfOpenElements.forEach[allElementRepresentation.remove(it)] | ||
251 | |||
252 | val Map<Integer, Integer> hashedRepresentation = new HashMap() | ||
253 | for(entry : allElementRepresentation.entrySet) { | ||
254 | hashedRepresentation.put(entry.key.dataHash,entry.value) | ||
255 | } | ||
256 | list.add(allElementRepresentation) | ||
257 | } | ||
258 | return list | ||
259 | } | ||
260 | } | ||
261 | |||
262 | @Data | ||
263 | class PatternWithMatches { | ||
264 | String name; | ||
265 | List<List<DefinedElement>> matches | ||
266 | } | ||
267 | |||
268 | class FileComparator implements Comparator<File> { | ||
269 | |||
270 | override compare(File arg0, File arg1) { | ||
271 | val r1 = Integer.parseInt(MeasureDiversity::runIndex(arg0.name)) | ||
272 | val r2 = Integer.parseInt(MeasureDiversity::runIndex(arg1.name)) | ||
273 | val runRes = Integer.compare(r1, r2) | ||
274 | if(runRes === 0) { | ||
275 | val a = Integer.parseInt(MeasureDiversity::modelIndex(arg0.name)) | ||
276 | val b = Integer.parseInt(MeasureDiversity::modelIndex(arg1.name)) | ||
277 | Integer.compare(a,b) | ||
278 | } else { | ||
279 | return runRes | ||
280 | } | ||
281 | arg0.name.compareTo(arg1.name) | ||
282 | } | ||
283 | } \ No newline at end of file | ||
diff --git a/Tests/MODELS2020-CaseStudies/models20.diversity-calculator/src/hu/bme/mit/inf/dslreasoner/diversitycalculator/MeasureInternalDiversity.xtend b/Tests/MODELS2020-CaseStudies/models20.diversity-calculator/src/hu/bme/mit/inf/dslreasoner/diversitycalculator/MeasureInternalDiversity.xtend new file mode 100644 index 00000000..1510eeb8 --- /dev/null +++ b/Tests/MODELS2020-CaseStudies/models20.diversity-calculator/src/hu/bme/mit/inf/dslreasoner/diversitycalculator/MeasureInternalDiversity.xtend | |||
@@ -0,0 +1,107 @@ | |||
1 | package hu.bme.mit.inf.dslreasoner.diversitycalculator | ||
2 | |||
3 | import hu.bme.mit.inf.dslreasoner.ecore2logic.Ecore2Logic | ||
4 | import hu.bme.mit.inf.dslreasoner.ecore2logic.Ecore2LogicConfiguration | ||
5 | import hu.bme.mit.inf.dslreasoner.ecore2logic.ecore2logicannotations.Ecore2logicannotationsPackage | ||
6 | import hu.bme.mit.inf.dslreasoner.logic.model.logiclanguage.LogiclanguagePackage | ||
7 | import hu.bme.mit.inf.dslreasoner.logic.model.logicproblem.LogicproblemPackage | ||
8 | import hu.bme.mit.inf.dslreasoner.viatra2logic.viatra2logicannotations.Viatra2LogicAnnotationsPackage | ||
9 | import hu.bme.mit.inf.dslreasoner.viatrasolver.partialinterpretation2logic.InstanceModel2PartialInterpretation | ||
10 | import hu.bme.mit.inf.dslreasoner.viatrasolver.partialinterpretationlanguage.neighbourhood.PartialInterpretation2ImmutableTypeLattice | ||
11 | import hu.bme.mit.inf.dslreasoner.viatrasolver.partialinterpretationlanguage.partialinterpretation.PartialInterpretation | ||
12 | import hu.bme.mit.inf.dslreasoner.viatrasolver.partialinterpretationlanguage.partialinterpretation.PartialinterpretationPackage | ||
13 | import hu.bme.mit.inf.dslreasoner.workspace.FileSystemWorkspace | ||
14 | import java.io.File | ||
15 | import java.util.LinkedList | ||
16 | import java.util.List | ||
17 | import org.eclipse.emf.ecore.EObject | ||
18 | import org.eclipse.emf.ecore.resource.Resource | ||
19 | import org.eclipse.emf.ecore.xmi.impl.XMIResourceFactoryImpl | ||
20 | import hu.bme.mit.inf.dslreasoner.ecore2logic.EcoreMetamodelDescriptor | ||
21 | |||
22 | class MeasureInternalDiversity { | ||
23 | static val partialInterpretation2Logic = new InstanceModel2PartialInterpretation | ||
24 | static val neiguboirhoodComputer = new PartialInterpretation2ImmutableTypeLattice | ||
25 | |||
26 | static val Ecore2Logic ecore2Logic = new Ecore2Logic | ||
27 | |||
28 | static private def init() { | ||
29 | LogiclanguagePackage.eINSTANCE.class | ||
30 | LogicproblemPackage.eINSTANCE.class | ||
31 | PartialinterpretationPackage.eINSTANCE.class | ||
32 | Ecore2logicannotationsPackage.eINSTANCE.class | ||
33 | Viatra2LogicAnnotationsPackage.eINSTANCE.class | ||
34 | Resource.Factory.Registry.INSTANCE.extensionToFactoryMap.put("*",new XMIResourceFactoryImpl) | ||
35 | } | ||
36 | |||
37 | |||
38 | def static void main(String[] args) { | ||
39 | init() | ||
40 | } | ||
41 | |||
42 | def static run(EcoreMetamodelDescriptor metamodel, String path, String scenario) { | ||
43 | val range = 20 | ||
44 | |||
45 | measureDiversity(metamodel,scenario,path,range) | ||
46 | } | ||
47 | |||
48 | def static void measureDiversity(EcoreMetamodelDescriptor metamodel, String scenario, String path, int depth) { | ||
49 | val file = new File(path) | ||
50 | if(file.isDirectory) { | ||
51 | val subfiles = file.list | ||
52 | val xmiSubfiles = subfiles.filter[it.endsWith(".xmi")] | ||
53 | if(!xmiSubfiles.empty) { | ||
54 | measureDiversity(metamodel, scenario,file,xmiSubfiles.map[new File(path+"/"+it)].toList,path,depth) | ||
55 | } else { | ||
56 | for(subfile : subfiles) { | ||
57 | measureDiversity(metamodel, scenario,path+"/"+subfile,depth) | ||
58 | } | ||
59 | } | ||
60 | } else if(file.isFile) { | ||
61 | // Do nothing | ||
62 | } | ||
63 | } | ||
64 | |||
65 | def static void measureDiversity(EcoreMetamodelDescriptor metamodel, String scenario, File parent, List<File> files, String path, int depth) { | ||
66 | val workspace = new FileSystemWorkspace(path,"") | ||
67 | //val file2Neighbourhood = new HashMap<File,List<?>> | ||
68 | for(file : files) { | ||
69 | |||
70 | val metamodelTransformationOutput = ecore2Logic.transformMetamodel(metamodel,new Ecore2LogicConfiguration) | ||
71 | |||
72 | val model = workspace.readModel(EObject,file.name) | ||
73 | val partialModel = partialInterpretation2Logic.transform(metamodelTransformationOutput,model.eResource,false) | ||
74 | val list = representationList(partialModel,depth) | ||
75 | try { | ||
76 | val runIndex = file.name.runIndex | ||
77 | val modelIndex = file.name.modelIndex | ||
78 | print('''«scenario»;«runIndex»;«modelIndex»''') | ||
79 | } catch (Exception e) { | ||
80 | print('''«scenario»;0;«file.name»''') | ||
81 | } | ||
82 | for(i : 0..<list.size) { | ||
83 | print(''';«list.get(i).keySet.size»''') | ||
84 | } | ||
85 | print(";"+(list.last.keySet.size*1.0 /partialModel.newElements.size)) | ||
86 | println | ||
87 | } | ||
88 | } | ||
89 | |||
90 | protected def static runIndex(String name) { | ||
91 | name.split("\\.").head.split('_').get(1) | ||
92 | } | ||
93 | protected def static modelIndex(String name) { | ||
94 | name.split("\\.").head.split('_').get(2) | ||
95 | } | ||
96 | |||
97 | protected def static representationList(PartialInterpretation partialModel, int depth) { | ||
98 | val list = new LinkedList | ||
99 | for(i : 0..<depth) { | ||
100 | val neighbourhood = neiguboirhoodComputer.createRepresentation(partialModel,i,0,0) | ||
101 | //val m = neighbourhood.modelRepresentation | ||
102 | //println(m) | ||
103 | list.add(neighbourhood.modelRepresentation) | ||
104 | } | ||
105 | return list | ||
106 | } | ||
107 | } \ No newline at end of file | ||