diff options
Diffstat (limited to 'Metrics/Metrics-Calculation/ca.mcgill.ecse.dslreasoner.realistic.metrics.calculator/src/ca/mcgill/ecse/dslreasoner/realistic/metrics/calculator/graph/GraphStatistic.xtend')
-rw-r--r-- | Metrics/Metrics-Calculation/ca.mcgill.ecse.dslreasoner.realistic.metrics.calculator/src/ca/mcgill/ecse/dslreasoner/realistic/metrics/calculator/graph/GraphStatistic.xtend | 79 |
1 files changed, 71 insertions, 8 deletions
diff --git a/Metrics/Metrics-Calculation/ca.mcgill.ecse.dslreasoner.realistic.metrics.calculator/src/ca/mcgill/ecse/dslreasoner/realistic/metrics/calculator/graph/GraphStatistic.xtend b/Metrics/Metrics-Calculation/ca.mcgill.ecse.dslreasoner.realistic.metrics.calculator/src/ca/mcgill/ecse/dslreasoner/realistic/metrics/calculator/graph/GraphStatistic.xtend index 84071176..af05a1cd 100644 --- a/Metrics/Metrics-Calculation/ca.mcgill.ecse.dslreasoner.realistic.metrics.calculator/src/ca/mcgill/ecse/dslreasoner/realistic/metrics/calculator/graph/GraphStatistic.xtend +++ b/Metrics/Metrics-Calculation/ca.mcgill.ecse.dslreasoner.realistic.metrics.calculator/src/ca/mcgill/ecse/dslreasoner/realistic/metrics/calculator/graph/GraphStatistic.xtend | |||
@@ -12,7 +12,7 @@ import org.eclipse.emf.ecore.EReference | |||
12 | 12 | ||
13 | class GraphStatistic { | 13 | class GraphStatistic { |
14 | val incomingEdges = new HashMap<String, Multimap<EObject, EObject>>; | 14 | val incomingEdges = new HashMap<String, Multimap<EObject, EObject>>; |
15 | val outcomingEdges = new HashMap<String, Multimap<EObject, EObject>>; | 15 | val outgoingEdges = new HashMap<String, Multimap<EObject, EObject>>; |
16 | 16 | ||
17 | val edgeTypes = new HashSet<String>(); | 17 | val edgeTypes = new HashSet<String>(); |
18 | val nodeToType = new HashMap<EObject, Set<String>>(); | 18 | val nodeToType = new HashMap<EObject, Set<String>>(); |
@@ -22,15 +22,13 @@ class GraphStatistic { | |||
22 | * @param type: type to add | 22 | * @param type: type to add |
23 | */ | 23 | */ |
24 | def void addEdgeType(String type){ | 24 | def void addEdgeType(String type){ |
25 | |||
26 | |||
27 | if(edgeTypes.contains(type)){ | 25 | if(edgeTypes.contains(type)){ |
28 | return; | 26 | return; |
29 | } | 27 | } |
30 | 28 | ||
31 | edgeTypes.add(type); | 29 | edgeTypes.add(type); |
32 | incomingEdges.put(type, ArrayListMultimap.create()); | 30 | incomingEdges.put(type, ArrayListMultimap.create()); |
33 | outcomingEdges.put(type, ArrayListMultimap.create()); | 31 | outgoingEdges.put(type, ArrayListMultimap.create()); |
34 | } | 32 | } |
35 | 33 | ||
36 | /** | 34 | /** |
@@ -43,6 +41,23 @@ class GraphStatistic { | |||
43 | nodeToType.put(n, types); | 41 | nodeToType.put(n, types); |
44 | } | 42 | } |
45 | 43 | ||
44 | def boolean containsNode(EObject o){ | ||
45 | return nodeToType.containsKey(o); | ||
46 | } | ||
47 | |||
48 | def Set<String> getTypesForNode(EObject o){ | ||
49 | return nodeToType.getOrDefault(o, new HashSet<String>()); | ||
50 | } | ||
51 | |||
52 | def void overwriteCurrentType(EObject o, String type){ | ||
53 | var typeSet = nodeToType.getOrDefault(o, new HashSet<String>()); | ||
54 | |||
55 | // clear current types | ||
56 | typeSet.clear(); | ||
57 | typeSet.add(type); | ||
58 | nodeToType.put(o, typeSet); | ||
59 | } | ||
60 | |||
46 | /** | 61 | /** |
47 | * Add a node to the graph with all types in its type hierarchy | 62 | * Add a node to the graph with all types in its type hierarchy |
48 | */ | 63 | */ |
@@ -57,18 +72,66 @@ class GraphStatistic { | |||
57 | * @param type: type of the reference | 72 | * @param type: type of the reference |
58 | */ | 73 | */ |
59 | def void addEdge(EObject source, EObject target, String type){ | 74 | def void addEdge(EObject source, EObject target, String type){ |
60 | outcomingEdges.get(type).put(source, target); | 75 | outgoingEdges.get(type).put(source, target); |
61 | incomingEdges.get(type).put(target, source); | 76 | incomingEdges.get(type).put(target, source); |
62 | } | 77 | } |
63 | 78 | ||
64 | /** | 79 | /** |
80 | * check if this graph contains a specific edge type | ||
81 | */ | ||
82 | def boolean containsEdgeType(String typeName){ | ||
83 | if(outgoingEdges.containsKey(typeName) && incomingEdges.containsKey(typeName)){ | ||
84 | return true; | ||
85 | } | ||
86 | return false; | ||
87 | } | ||
88 | |||
89 | /** | ||
90 | * remove references from the statistics, potentially remove the nodes associated with it | ||
91 | * @Param name: name of the reference | ||
92 | * @Param isContainment: if true then the corresponding nodes on the incoming side will also be removed | ||
93 | */ | ||
94 | def removeReference(String name, boolean isContainment){ | ||
95 | if(!edgeTypes.contains(name)){ | ||
96 | return; | ||
97 | } | ||
98 | |||
99 | edgeTypes.remove(name); | ||
100 | var incomingSet = incomingEdges.remove(name); | ||
101 | outgoingEdges.remove(name); | ||
102 | |||
103 | // if the reference is not a containment, then removing the reference is enough | ||
104 | if(!isContainment){ | ||
105 | return; | ||
106 | } | ||
107 | |||
108 | // else remove all corresponding nodes | ||
109 | val nodesToRemove = incomingSet.keySet(); | ||
110 | |||
111 | //remove nodes from node sets | ||
112 | nodesToRemove.forEach[nodeToType.remove(it)]; | ||
113 | |||
114 | val removeForMultimap = [Multimap<EObject, EObject> refMap| | ||
115 | nodesToRemove.forEach[refMap.removeAll(it)]; | ||
116 | var values = refMap.values() | ||
117 | //remove the values from the list is equavalent to remove it in the multimap | ||
118 | values.removeAll(nodesToRemove); | ||
119 | return; | ||
120 | ]; | ||
121 | |||
122 | //remove nodes from all other references on incomingEdges | ||
123 | incomingEdges.values.forEach(removeForMultimap); | ||
124 | outgoingEdges.values.forEach(removeForMultimap); | ||
125 | } | ||
126 | |||
127 | /** | ||
65 | * calculate the out degree for an object | 128 | * calculate the out degree for an object |
66 | */ | 129 | */ |
67 | def int outDegree(EObject o){ | 130 | def int outDegree(EObject o){ |
68 | var count = 0; | 131 | var count = 0; |
69 | 132 | ||
70 | for (String type : edgeTypes){ | 133 | for (String type : edgeTypes){ |
71 | count += outcomingEdges.get(type).get(o).size(); | 134 | count += outgoingEdges.get(type).get(o).size(); |
72 | } | 135 | } |
73 | return count; | 136 | return count; |
74 | } | 137 | } |
@@ -89,7 +152,7 @@ class GraphStatistic { | |||
89 | * calculate the dimentional degree of a node | 152 | * calculate the dimentional degree of a node |
90 | */ | 153 | */ |
91 | def int dimentionalDegree(EObject o, String type){ | 154 | def int dimentionalDegree(EObject o, String type){ |
92 | return incomingEdges.get(type).get(o).size() + outcomingEdges.get(type).get(o).size(); | 155 | return incomingEdges.get(type).get(o).size() + outgoingEdges.get(type).get(o).size(); |
93 | } | 156 | } |
94 | 157 | ||
95 | /** | 158 | /** |
@@ -120,7 +183,7 @@ class GraphStatistic { | |||
120 | } | 183 | } |
121 | 184 | ||
122 | def HashMap<String, Multimap<EObject, EObject>> getOutgoingEdges(){ | 185 | def HashMap<String, Multimap<EObject, EObject>> getOutgoingEdges(){ |
123 | return outcomingEdges; | 186 | return outgoingEdges; |
124 | } | 187 | } |
125 | 188 | ||
126 | } | 189 | } |