aboutsummaryrefslogtreecommitdiffstats
path: root/Metrics/Metrics-Calculation/ca.mcgill.ecse.dslreasoner.realistic.metrics.calculator/src/ca/mcgill/ecse/dslreasoner/realistic/metrics/calculator/graph/EMFGraph.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/graph/EMFGraph.xtend')
-rw-r--r--Metrics/Metrics-Calculation/ca.mcgill.ecse.dslreasoner.realistic.metrics.calculator/src/ca/mcgill/ecse/dslreasoner/realistic/metrics/calculator/graph/EMFGraph.xtend124
1 files changed, 124 insertions, 0 deletions
diff --git a/Metrics/Metrics-Calculation/ca.mcgill.ecse.dslreasoner.realistic.metrics.calculator/src/ca/mcgill/ecse/dslreasoner/realistic/metrics/calculator/graph/EMFGraph.xtend b/Metrics/Metrics-Calculation/ca.mcgill.ecse.dslreasoner.realistic.metrics.calculator/src/ca/mcgill/ecse/dslreasoner/realistic/metrics/calculator/graph/EMFGraph.xtend
new file mode 100644
index 00000000..8fa29fe6
--- /dev/null
+++ b/Metrics/Metrics-Calculation/ca.mcgill.ecse.dslreasoner.realistic.metrics.calculator/src/ca/mcgill/ecse/dslreasoner/realistic/metrics/calculator/graph/EMFGraph.xtend
@@ -0,0 +1,124 @@
1package ca.mcgill.ecse.dslreasoner.realistic.metrics.calculator.graph
2
3import ca.mcgill.ecse.dslreasoner.realistic.metrics.calculator.metrics.Metric
4import java.util.ArrayList
5import java.util.HashSet
6import java.util.List
7import org.eclipse.emf.common.util.EList
8import org.eclipse.emf.ecore.EObject
9import org.eclipse.emf.ecore.EReference
10import org.eclipse.xtend.lib.annotations.Accessors
11
12class EMFGraph extends Graph{
13 @Accessors(PUBLIC_GETTER)
14 var EObject root;
15
16 def void init (EObject root, List<Metric> metrics, String name, List<EReference> referenceTypes){
17 val otherContents = root.eAllContents.toList();
18 this.root = root;
19 otherContents.add(root);
20 init(otherContents, metrics, name, referenceTypes);
21 }
22
23 /**
24 * init the graph with all nodes and reference types in the meta model
25 * @param objects: objects in the instance model (exclude root)
26 * @param metrics: metrics to be evaluated
27 * @param name: name of the instance model
28 * @param ReferenceTypes: reference types defined in the meta model
29 */
30 def void init(List<EObject> objects, List<Metric> metrics, String name, List<EReference> referenceTypes){
31 objects.forEach[it|
32 // TODO: Maybe want to consider all the super types as well
33 var types = new HashSet();
34 types.add(it.eClass.name);
35 statistic.addNodeWithAllTypes(it, types);
36 ]
37
38 referenceTypes.forEach[it|
39 // Only consider the edges that are not derived to preserve the statistical property
40 if(!it.derived){
41 statistic.addEdgeType(it.name);
42 }
43 ];
44
45 objects.forEach[source|
46 source.eClass.EAllReferences.forEach[r|
47 //many references
48 if(r.isMany){
49 source.getNeighbours(r).forEach[target|
50 addEdge(source, target, r);
51 ]
52 }else{
53 //single references
54 val target = source.eGet(r) as EObject;
55 addEdge(source, target, r);
56 }
57 ]
58 ]
59
60 this.metrics = metrics;
61 this.name = name;
62 }
63
64 def void removeReference(EReference r){
65 if (statistic.containsEdgeType(r.name)){
66 statistic.removeReference(r.name, r.containment);
67 }
68 }
69
70 /**
71 * Set basic information for the output
72 */
73 override setBasicInformation(ArrayList<ArrayList<String>> output){
74 val metaInfo = new ArrayList<String>();
75 metaInfo.add(META_MODEL_HEADER);
76 metaInfo.add(this.metaModel);
77
78 val edgeInfo = new ArrayList<String>();
79 edgeInfo.add(NUM_EDGE_TYPE_HEADER);
80 edgeInfo.add(this.statistic.allTypes.size()+"");
81
82 val nodeInfo = new ArrayList<String>();
83 nodeInfo.add(NUM_NODE_HEADER);
84 nodeInfo.add(this.statistic.allNodes.size()+"");
85
86 val stateInfo = new ArrayList<String>();
87 stateInfo.add(STATE_ID_HEADER);
88 stateInfo.add(this.name);
89
90
91 output.add(metaInfo);
92 output.add(edgeInfo);
93 output.add(nodeInfo);
94 output.add(stateInfo);
95 }
96
97 def EList<EObject> getNeighbours(EObject o, EReference r){
98 return (o.eGet(r, true) as EList<EObject>);
99 }
100
101 def addEdge(EObject source, EObject target, EReference r){
102 //Only add the edge if the reference is not derived to preserve the statistical property
103 if(target !== null && r !== null && !r.derived){
104 statistic.addEdge(source, target, r.name);
105 }
106 }
107
108 override GraphStatistic getStatistic(){
109 return this.statistic;
110 }
111
112 override String getName(){
113 return this.name;
114 }
115
116 def void setMetaModel(String model){
117 this.metaModel = model;
118 }
119
120 def String getMetaModel(){
121 return this.metaModel;
122 }
123
124} \ No newline at end of file