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.xtend112
1 files changed, 112 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..053e0da3
--- /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,112 @@
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.EdgeTypeMetric
5import ca.mcgill.ecse.dslreasoner.realistic.metrics.calculator.metrics.Metric
6import ca.mcgill.ecse.dslreasoner.realistic.metrics.calculator.metrics.MultiplexParticipationCoefficientMetric
7import ca.mcgill.ecse.dslreasoner.realistic.metrics.calculator.metrics.NodeActivityMetric
8import ca.mcgill.ecse.dslreasoner.realistic.metrics.calculator.metrics.NodeTypeMetric
9import ca.mcgill.ecse.dslreasoner.realistic.metrics.calculator.metrics.OutDegreeMetric
10import ca.mcgill.ecse.dslreasoner.realistic.metrics.calculator.metrics.TypedOutDegree
11import java.io.File
12import java.io.FileNotFoundException
13import java.util.ArrayList
14import java.util.List
15import org.eclipse.emf.common.util.URI
16import org.eclipse.emf.ecore.EObject
17import org.eclipse.emf.ecore.EPackage
18import org.eclipse.emf.ecore.EReference
19import org.eclipse.emf.ecore.resource.Resource
20import org.eclipse.emf.ecore.resource.ResourceSet
21import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl
22import org.eclipse.emf.ecore.xmi.impl.XMIResourceFactoryImpl
23import org.eclipse.emf.ecore.EGenericType
24import org.eclipse.emf.ecore.EStructuralFeature
25import ca.mcgill.ecse.dslreasoner.realistic.metrics.calculator.metrics.TypedClusteringCoefficientMetric
26
27class GraphReader{
28 val ResourceSet resSet = new ResourceSetImpl();
29 val referenceTypes = new ArrayList<EReference>();
30 var String suffix;
31
32 new(EPackage metaModel, String suffix) {
33 Resource.Factory.Registry.INSTANCE.extensionToFactoryMap.put("*",new XMIResourceFactoryImpl)
34 this.suffix = suffix;
35
36 //find all reference types in the meta model
37 metaModel.eAllContents.forEach[
38 if(it instanceof EReference){
39 referenceTypes.add(it);
40 }
41 ]
42 }
43
44 def List<EMFGraph> readModels(String path){
45 val dir = new File(path);
46 if(!dir.isDirectory){
47 throw new Exception("expecting a directory");
48 }
49
50 val graphs = new ArrayList<EMFGraph>();
51
52 val metrics = new ArrayList<Metric>();
53 metrics.add(new OutDegreeMetric());
54 metrics.add(new NodeActivityMetric());
55 metrics.add(new MultiplexParticipationCoefficientMetric());
56 metrics.add(new TypedOutDegree());
57 metrics.add(new NodeTypeMetric());
58 metrics.add(new EdgeTypeMetric());
59 var count = 1
60 //check all files in the directory with suffix
61 for(String name : dir.list.filter[it| it.endsWith(suffix)]){
62 val file = new File(name);
63 val roots = readModel(EObject, path, file.name);
64 //add a list of metrics
65 val g = new EMFGraph();
66 for(root : roots){
67 g.init(root, metrics, name.replaceFirst(suffix, ""), referenceTypes);
68 }
69
70 count ++;
71 graphs.add(g);
72 }
73
74 return graphs;
75 }
76
77 def EMFGraph readModel(String path, String filename){
78 val metrics = new ArrayList<Metric>();
79 metrics.add(new OutDegreeMetric());
80 metrics.add(new NodeActivityMetric());
81 metrics.add(new MultiplexParticipationCoefficientMetric());
82 metrics.add(new TypedOutDegree());
83 metrics.add(new NodeTypeMetric());
84 metrics.add(new EdgeTypeMetric());
85
86 val file = new File(filename);
87 val roots = readModel(EObject, path, file.name);
88 //add a list of metrics
89 val g = new EMFGraph();
90 for(root : roots){
91 g.init(root, metrics, filename.replaceFirst(suffix, ""), referenceTypes);
92 }
93 return g
94 }
95
96 def <RootType extends EObject> List<RootType> readModel(Class<RootType> type, String path, String name) {
97 try {
98 val resource = resSet.getResource(getURI(path, name),true);
99 if(resource === null) throw new FileNotFoundException(getURI(path, name).toString)
100 else {
101 return resource.contents as List<RootType>
102 }
103 } catch(Exception e) {
104 e.printStackTrace();
105 throw new Exception(getURI(path, name).toString());
106 }
107 }
108
109 def static getURI(String path, String name) {
110 URI.createFileURI(path + "/" + name)
111 }
112} \ No newline at end of file