-
Notifications
You must be signed in to change notification settings - Fork 0
/
measures.py
212 lines (144 loc) · 4.91 KB
/
measures.py
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
import json
from collections import defaultdict, OrderedDict
def pdd(pkg: list, depgraph: dict):
"""
Calculates P-DepDegree
"""
tdg = {}
fringe = set(pkg)
while fringe:
c = fringe.pop()
deps = depgraph[c]
tdg[c] = deps
fringe.update({d for d in deps if d not in tdg})
# return ratio of the number of edges in tdg to the number of edges in depgraph
return sum([len(tdg[c]) for c in tdg]) / sum([len(depgraph[c]) for c in depgraph])
def _dist(a: str, b: str) -> int:
"""
Calculates the distance between to packages a, b
>>> _dist("a.b.c.d.e", "a.b.f.g.h")
6
>>> _dist("a.b.c", "a.b.c")
0
>>> _dist("a.b.c.d", "a.b")
2
"""
path_a = a.split(".")
path_b = b.split(".")
# remove shared path
# i.e. "a.b.c.d" & "a.b.e.f" => "c.d" & "e.f"
zipped = list(zip(path_a, path_b))
for i, j in zipped:
if i == j:
path_a.pop(0)
path_b.pop(0)
return len(path_a) + len(path_b)
def dlm(pkg_name: str, deps: set) -> int:
"""
Calculates the dependency locality measure for package pkg and its dependencies
Format of strings must be "a.b.c.A" (i.e. package tree path + class name)
"""
from collections import Counter
# 1. Determine the packages the dependencies lie in
packages = Counter()
for d in deps:
p = d.rpartition(".")[0]
packages[p] += 1
# 2. Determine the distance between pkg and each p in packages
distances = {p: _dist(pkg_name, p) for p in packages}
# 3. Calculate dlm for pkg
return sum([distances[p] * packages[p] for p in packages])
def dcm_lcom3(pkg: list):
"""
Variant of dcm based on LCOM3
"""
counter = 0
for i, c in enumerate(pkg):
for j in pkg[i + 1 :]:
if c & j:
counter += 1
return counter
def dcm_sim(pkg: list):
"""
Variant of dcm based on similarity measure
"""
# Similarity function
sim = lambda a, b: len(a & b) / len(a | b) if len(a | b) > 0 else 0
pairs = []
for i, c in enumerate(pkg):
pairs.extend([(c, j) for j in pkg[i + 1 :]])
sims = [sim(i, j) for (i, j) in pairs]
return sum(sims) / len(pairs) if len(pairs) > 0 else 0
def dcm_cc(pkg: list):
"""
Variant of dcm based on cohesion count
"""
# Count function
count = lambda d, package: len([c for c in pkg if d in c])
deps = set.union(*pkg)
counts = [count(d, pkg) for d in deps]
return sum(counts) / (len(pkg) * len(deps)) if len(pkg) > 0 and len(deps) > 0 else 0
def noc(pkg):
"""
Number of classes (and interfaces)
"""
return len(pkg)
def ca(pkg: list, depgraph: dict):
"""
Afferent coupling
The number of classes outside the package that dependend on classes within the package
"""
return len(
[c for c in depgraph if c not in pkg and set(depgraph[c]).intersection(pkg)]
)
def ce(pkg: list, depgraph: dict):
"""
Efferent coupling
The number of classes of a package that depend on classes outside the package
"""
return len([c for c in pkg if set(depgraph[c]).difference(pkg)])
def instability(pkg, depgraph):
"""
Instability of a package
"""
aff = ca(pkg, depgraph)
eff = ce(pkg, depgraph)
return eff / (aff + eff) if aff + eff > 0 else 0
def group_by_package(depgraph:dict):
packages = defaultdict(list)
for c in depgraph:
p = c.rpartition(".")[0]
packages[p].append(c)
return packages
def calc() -> None:
"""
Calculates all defined measures for all packages based on the dependency graph of a system
"""
depgraph = json.load(open("./data/depgraph.json", "r"))
# group classes by packages
packages = group_by_package(depgraph)
results = defaultdict(dict)
# calculate measures for all packages
for p, pkg_classes in packages.items():
# number of classes (and interfaces)
results["noc"][p] = noc(pkg_classes)
# coupling measure
results["ca"][p] = ca(pkg_classes, depgraph)
results["ce"][p] = ce(pkg_classes, depgraph)
results["instability"][p] = instability(pkg_classes, depgraph)
# dependency cohesion measure
class_deps = [set(depgraph[c]) for c in pkg_classes]
results["dcm_lcom3"][p] = dcm_lcom3(class_deps)
results["dcm_sim"][p] = dcm_sim(class_deps)
results["dcm_cc"][p] = dcm_cc(class_deps)
# package depdegree
results["p-depdegree"][p] = pdd(pkg_classes, depgraph)
# dependency locality measure
pkg_deps = set().union(*[depgraph[c] for c in pkg_classes])
results["dlm"][p] = dlm(p, pkg_deps)
# store the measurement values
for m, values in results.items():
od = OrderedDict(sorted(values.items(), key = lambda i: i[1]))
json.dump(od, open(f"./data/{m}.json", "w"), indent = 4)
if __name__ == "__main__":
calc()