diff options
Diffstat (limited to 'Metrics/Metrics-Calculation/ca.mcgill.ecse.dslreasoner.realistic.metrics.calculator/src/ca/mcgill/ecse/dslreasoner/realistic/metrics/calculator/io/GraphReader.xtend')
-rw-r--r-- | Metrics/Metrics-Calculation/ca.mcgill.ecse.dslreasoner.realistic.metrics.calculator/src/ca/mcgill/ecse/dslreasoner/realistic/metrics/calculator/io/GraphReader.xtend | 81 |
1 files changed, 81 insertions, 0 deletions
diff --git a/Metrics/Metrics-Calculation/ca.mcgill.ecse.dslreasoner.realistic.metrics.calculator/src/ca/mcgill/ecse/dslreasoner/realistic/metrics/calculator/io/GraphReader.xtend b/Metrics/Metrics-Calculation/ca.mcgill.ecse.dslreasoner.realistic.metrics.calculator/src/ca/mcgill/ecse/dslreasoner/realistic/metrics/calculator/io/GraphReader.xtend new file mode 100644 index 00000000..fc56e142 --- /dev/null +++ b/Metrics/Metrics-Calculation/ca.mcgill.ecse.dslreasoner.realistic.metrics.calculator/src/ca/mcgill/ecse/dslreasoner/realistic/metrics/calculator/io/GraphReader.xtend | |||
@@ -0,0 +1,81 @@ | |||
1 | package ca.mcgill.ecse.dslreasoner.realistic.metrics.calculator.io; | ||
2 | |||
3 | import ca.mcgill.ecse.dslreasoner.realistic.metrics.calculator.graph.EMFGraph | ||
4 | import ca.mcgill.ecse.dslreasoner.realistic.metrics.calculator.metrics.Metric | ||
5 | import ca.mcgill.ecse.dslreasoner.realistic.metrics.calculator.metrics.MultiplexParticipationCoefficientMetric | ||
6 | import ca.mcgill.ecse.dslreasoner.realistic.metrics.calculator.metrics.NodeActivityMetric | ||
7 | import ca.mcgill.ecse.dslreasoner.realistic.metrics.calculator.metrics.OutDegreeMetric | ||
8 | import java.io.File | ||
9 | import java.io.FileNotFoundException | ||
10 | import java.util.ArrayList | ||
11 | import java.util.List | ||
12 | import org.eclipse.emf.common.util.URI | ||
13 | import org.eclipse.emf.ecore.EObject | ||
14 | import org.eclipse.emf.ecore.EPackage | ||
15 | import org.eclipse.emf.ecore.EReference | ||
16 | import org.eclipse.emf.ecore.resource.Resource | ||
17 | import org.eclipse.emf.ecore.resource.ResourceSet | ||
18 | import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl | ||
19 | import org.eclipse.emf.ecore.xmi.impl.XMIResourceFactoryImpl | ||
20 | |||
21 | class GraphReader{ | ||
22 | val ResourceSet resSet = new ResourceSetImpl(); | ||
23 | val referenceTypes = new ArrayList<String>(); | ||
24 | |||
25 | new(EPackage metaModel) { | ||
26 | Resource.Factory.Registry.INSTANCE.extensionToFactoryMap.put("*",new XMIResourceFactoryImpl) | ||
27 | |||
28 | //find all reference types in the meta model | ||
29 | metaModel.eAllContents.forEach[ | ||
30 | if(it instanceof EReference){ | ||
31 | referenceTypes.add(it.name); | ||
32 | } | ||
33 | ] | ||
34 | } | ||
35 | |||
36 | def List<EMFGraph> readModels(String path){ | ||
37 | val dir = new File(path); | ||
38 | if(!dir.isDirectory){ | ||
39 | throw new Exception("expecting a directory"); | ||
40 | } | ||
41 | |||
42 | val graphs = new ArrayList<EMFGraph>(); | ||
43 | |||
44 | val metrics = new ArrayList<Metric>(); | ||
45 | metrics.add(new OutDegreeMetric()); | ||
46 | metrics.add(new NodeActivityMetric()); | ||
47 | metrics.add(new MultiplexParticipationCoefficientMetric()); | ||
48 | |||
49 | //check all files in the directory with xmi | ||
50 | for(String name : dir.list.filter[it| it.endsWith(".xmi")]){ | ||
51 | val file = new File(name); | ||
52 | val roots = readModel(EObject, path, file.name); | ||
53 | //add a list of metrics | ||
54 | val g = new EMFGraph(); | ||
55 | for(root : roots){ | ||
56 | g.init(root, metrics, name.replaceFirst(".xmi", ""), referenceTypes); | ||
57 | } | ||
58 | |||
59 | graphs.add(g); | ||
60 | } | ||
61 | |||
62 | return graphs; | ||
63 | } | ||
64 | |||
65 | def <RootType extends EObject> List<RootType> readModel(Class<RootType> type, String path, String name) { | ||
66 | try { | ||
67 | val resource = resSet.getResource(getURI(path, name),true); | ||
68 | if(resource === null) throw new FileNotFoundException(getURI(path, name).toString) | ||
69 | else { | ||
70 | return resource.contents as List<RootType> | ||
71 | } | ||
72 | } catch(Exception e) { | ||
73 | e.printStackTrace(); | ||
74 | throw new FileNotFoundException(getURI(path, name).toString + "reason: " + e.message) | ||
75 | } | ||
76 | } | ||
77 | |||
78 | def static getURI(String path, String name) { | ||
79 | URI.createFileURI(path + "/" + name) | ||
80 | } | ||
81 | } \ No newline at end of file | ||