Skip to content

Commit

Permalink
slightly different algorithms for some metrics
Browse files Browse the repository at this point in the history
git-svn-id: https://slps.svn.sourceforge.net/svnroot/slps@939 ab42f6e0-554d-0410-b580-99e487e6eeb2
  • Loading branch information
grammarware committed Feb 4, 2011
1 parent 5fa220b commit 754e9ba
Show file tree
Hide file tree
Showing 17 changed files with 114 additions and 40 deletions.
110 changes: 92 additions & 18 deletions shared/python/metrics.py
Expand Up @@ -134,20 +134,21 @@ def getADigraph(g):
return adg

def getLevels(g):
calls = getCallGraph(g)
calls = getClosure(calls)
return calculateLevels(getClosure(getCallGraph(g)))

#
def calculateLevels(calls):
unassigned = calls.keys()
levels = []
while len(unassigned)>0:
nt = unassigned[0]
nt = unassigned[0]
unassigned = unassigned[1:]
levels.append([])
levels[-1].append(nt)
unassigned = unassigned[1:]
for n in calls[nt]:
if nt in calls[n]:
if (nt in calls[n]) and (n not in levels[-1]) and (n in unassigned):
levels[-1].append(n)
if n in unassigned:
unassigned.remove(n)
unassigned.remove(n)
return levels

def getCallGraph(g):
Expand All @@ -166,11 +167,13 @@ def getCallGraph(g):
return calls

def getClosure(cg):
calls = cg.copy()
# simple copy doesn't work because of the inner arrays
#calls = cg.copy()
calls = {}
for x in cg.keys():
calls[x]=cg[x][:]
for n in calls.keys():
for x in calls[n]:
if x not in calls.keys():
calls[x] = []
for y in calls[x]:
if y not in calls[n]:
calls[n].append(y)
Expand All @@ -180,6 +183,18 @@ def getClosure(cg):
#print '--------------------'
return calls

#
def makeOneStep(cg):
calls = cg.copy()
for n in cg.keys():
for x in cg[n]:
calls[n] = union(calls[n],cg[x])
calls[n].sort()
#for n in calls.keys():
# print n,'▻*',calls[n]
#print '--------------------'
return calls

# DEP - cardinality of the largest grammatical level
def DEP(g):
return max(map(len,getLevels(g)))
Expand Down Expand Up @@ -221,16 +236,25 @@ def HEI(g):

# TIMP - tree impurity
def TIMP(g):
cg = getClosure(getCallGraph(g))
return impurityOfCallGraph(getClosure(getCallGraph(g)))

# TIMP - tree impurity of the immediate successor graph
def TIMPI(g):
return impurityOfCallGraph(getCallGraph(g))

def impurityOfCallGraph(cg):
n = len(cg)
e = sum(map(len,cg.values()))
# Power and Malloy made two mistakes:
# (1) the number of edges in a complete directed graph is n(n-1), not n(n-1)/2, as in a complete undirected graph!
# (2) we don't have to substract another 1 from the number of nonterminals to account for a start symbol
# To compute TIMP exactly as they intended to, run this:
# return (100*2*(e-n+1)/(0.0+(n-1)*(n-2)))
# To run our fixed version, uncomment this:
return (100*(e-n+1)/(0.0+n*(n-1)))
if n<2:
return 100
else:
# Power and Malloy made two mistakes:
# (1) the number of edges in a complete directed graph is n(n-1), not n(n-1)/2, as in a complete undirected graph!
# (2) we don't have to substract another 1 from the number of nonterminals to account for a start symbol
# To compute TIMP exactly as they intended to, run this:
# return (100*2*(e-n+1)/(0.0+(n-1)*(n-2)))
# To run our fixed version, uncomment this:
return (100*(e-n+1)/(0.0+n*(n-1)))

######################################################################
########## Complexity metrics ##########
Expand Down Expand Up @@ -291,6 +315,9 @@ def rhssize(node):
def AVS(g):
return sum(map(rhssize,g.prods))/(0.0+VAR(g))

def AVSp(g):
return sum(map(rhssize,g.prods))/(0.0+PROD(g))

# HAL - Halstead effort
def opr(node):
# number of occurrences of operators
Expand Down Expand Up @@ -418,3 +445,50 @@ def HADIF(g):
def HAEFF(g):
hal = HADIF(g)*HAVOL(g)
return hal

# HALEV - Halstead level
def HALEV(g):
mu1 = hal_mu1(g)
mu2 = hal_mu2(g)
eta1 = hal_eta1(g)
eta2 = hal_eta2(g)
hal = (2.0*mu2)/(mu1*eta2)
return hal

# HATIM - Halstead time
def HATIM(g):
return HAEFF(g)/18.0

######################################################################
# Experiments
def shortestPath(a,b,cg):
return shortestPathAllPars(a,b,cg,getClosure(cg),[])

def shortestPathAllPars(a,b,cg,ccg,visited):
visited.append(a)
if a == b:
#print '∆:',a,'is',b
return 0
if b in cg[a]:
#print '∆:',a,'is next to',b
return 1
if b not in ccg[a]:
#print '∆:',a,'is inaccessible from',b
return 1000000
if not cg[a]:
#print '∆:',a,'is inaccessible from',b
return 1000000
#print '∆:',a,'-->?'
goto = filter(lambda x:x not in visited,cg[a])
if not goto:
#print '∆:',a,'is inaccessible from',b
return 1000000
return 1 + min(map(lambda x:shortestPathAllPars(x,b,cg,ccg,visited),goto))

def nameLevel(level,roots,g):
return '/'.join(union([],map(lambda x:nameLevel1Root(level,x,getCallGraph(g)),roots)))+'('+str(len(level))+')'

def nameLevel1Root(level,root,cg):
deltas = map(lambda x:shortestPath(root,x,cg),level)
#d = min(deltas):
return '|'.join(map(lambda i:level[i],filter(lambda i:deltas[i]==min(deltas),range(0,len(level)))))
2 changes: 1 addition & 1 deletion topics/investigation/structural/tests/antlr.bgf.baseline
@@ -1,4 +1,4 @@
6
5
7
63.6
1
Expand Down
2 changes: 1 addition & 1 deletion topics/investigation/structural/tests/dcg.bgf.baseline
@@ -1,4 +1,4 @@
3
2
7
87.5
1
Expand Down
2 changes: 1 addition & 1 deletion topics/investigation/structural/tests/ecore.bgf.baseline
@@ -1,4 +1,4 @@
9
8
5
41.7
1
Expand Down
2 changes: 1 addition & 1 deletion topics/investigation/structural/tests/ecore2.bgf.baseline
@@ -1,4 +1,4 @@
5
4
6
66.7
1
Expand Down
4 changes: 2 additions & 2 deletions topics/investigation/structural/tests/impl1.bgf.baseline
@@ -1,7 +1,7 @@
34
33
83
58.5
20
20
4
22
37.7
2 changes: 1 addition & 1 deletion topics/investigation/structural/tests/impl2.bgf.baseline
@@ -1,4 +1,4 @@
52
51
41
45.1
1
Expand Down
2 changes: 1 addition & 1 deletion topics/investigation/structural/tests/impl3.bgf.baseline
@@ -1,4 +1,4 @@
91
90
35
27.8
2
Expand Down
2 changes: 1 addition & 1 deletion topics/investigation/structural/tests/jaxb.bgf.baseline
@@ -1,4 +1,4 @@
4
3
9
81.8
1
Expand Down
2 changes: 1 addition & 1 deletion topics/investigation/structural/tests/om.bgf.baseline
@@ -1,4 +1,4 @@
4
3
8
80.0
1
Expand Down
4 changes: 2 additions & 2 deletions topics/investigation/structural/tests/read1.bgf.baseline
@@ -1,7 +1,7 @@
34
33
97
61.8
26
26
4
22
31.9
4 changes: 2 additions & 2 deletions topics/investigation/structural/tests/read2.bgf.baseline
@@ -1,7 +1,7 @@
94
93
68
42.0
21
21
2
13
56.8
4 changes: 2 additions & 2 deletions topics/investigation/structural/tests/read3.bgf.baseline
@@ -1,7 +1,7 @@
124
123
75
35.5
19
19
3
14
58.7
4 changes: 2 additions & 2 deletions topics/investigation/structural/tests/sdf.bgf.baseline
@@ -1,7 +1,7 @@
2
1
7
100.0
1
1
0
3
21.4
4 changes: 2 additions & 2 deletions topics/investigation/structural/tests/txl.bgf.baseline
@@ -1,7 +1,7 @@
2
1
7
100.0
1
1
0
3
21.4
2 changes: 1 addition & 1 deletion topics/investigation/structural/tests/xml.bgf.baseline
@@ -1,4 +1,4 @@
5
4
7
70.0
1
Expand Down
2 changes: 1 addition & 1 deletion topics/investigation/structural/tests/xsd.bgf.baseline
@@ -1,4 +1,4 @@
5
4
7
70.0
1
Expand Down

0 comments on commit 754e9ba

Please sign in to comment.