aboutsummaryrefslogtreecommitdiffstats
path: root/Metrics/Metrics-Calculation/ca.mcgill.ecse.dslreasoner.realistic.metrics.calculator/src/ca/mcgill/ecse/dslreasoner/realistic/metrics/calculator/io/GraphReader.xtend
diff options
context:
space:
mode:
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.xtend81
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 @@
1package ca.mcgill.ecse.dslreasoner.realistic.metrics.calculator.io;
2
3import ca.mcgill.ecse.dslreasoner.realistic.metrics.calculator.graph.EMFGraph
4import ca.mcgill.ecse.dslreasoner.realistic.metrics.calculator.metrics.Metric
5import ca.mcgill.ecse.dslreasoner.realistic.metrics.calculator.metrics.MultiplexParticipationCoefficientMetric
6import ca.mcgill.ecse.dslreasoner.realistic.metrics.calculator.metrics.NodeActivityMetric
7import ca.mcgill.ecse.dslreasoner.realistic.metrics.calculator.metrics.OutDegreeMetric
8import java.io.File
9import java.io.FileNotFoundException
10import java.util.ArrayList
11import java.util.List
12import org.eclipse.emf.common.util.URI
13import org.eclipse.emf.ecore.EObject
14import org.eclipse.emf.ecore.EPackage
15import org.eclipse.emf.ecore.EReference
16import org.eclipse.emf.ecore.resource.Resource
17import org.eclipse.emf.ecore.resource.ResourceSet
18import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl
19import org.eclipse.emf.ecore.xmi.impl.XMIResourceFactoryImpl
20
21class 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