1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
|
package ca.mcgill.ecse.dslreasoner.realistic.metrics.calculator.graph
import com.google.common.collect.ArrayListMultimap
import com.google.common.collect.Multimap
import java.util.HashMap
import java.util.HashSet
import java.util.List
import org.eclipse.emf.ecore.EObject
class GraphStatistic {
val incomingEdges = new HashMap<String, Multimap<EObject, EObject>>;
val outcomingEdges = new HashMap<String, Multimap<EObject, EObject>>;
val edgeTypes = new HashSet<String>();
val nodes = new HashSet<EObject>();
/**
* Add an edge type to to the graph
* @param type: type to add
*/
def void addType(String type){
if(edgeTypes.contains(type)){
return;
}
edgeTypes.add(type);
incomingEdges.put(type, ArrayListMultimap.create());
outcomingEdges.put(type, ArrayListMultimap.create());
}
/**
* Add a node to he graph
* @param node: node to add
*/
def void addNode(EObject n){
if(nodes.contains(n)){
return;
}
nodes.add(n);
}
/**
* Add an edge to the graph
* @param source: source node
* @param target: target node
* @param type: type of the reference
*/
def void addEdge(EObject source, EObject target, String type){
outcomingEdges.get(type).put(source, target);
incomingEdges.get(type).put(target, source);
}
/**
* calculate the out degree for an object
*/
def int outDegree(EObject o){
var count = 0;
for (String type : edgeTypes){
count += outcomingEdges.get(type).get(o).size();
}
return count;
}
/**
* calculate the in degree of an object
*/
def int inDegree(EObject o){
var count = 0;
for (String type : edgeTypes){
count += incomingEdges.get(type).get(o).size();
}
return count;
}
/**
* calculate the dimentional degree of a node
*/
def int dimentionalDegree(EObject o, String type){
return incomingEdges.get(type).get(o).size() + outcomingEdges.get(type).get(o).size();
}
/**
* calculate the number of edge types for a given degree.
*/
def int numOfEdgeTypes(EObject o){
var count = 0;
for(String type : edgeTypes){
if(dimentionalDegree(o, type) > 0){
count++;
}
}
return count;
}
def List<String> getAllTypes(){
return edgeTypes.toList();
}
def List<EObject> getAllNodes(){
return nodes.toList();
}
}
|