1 ''' @file apg_py/lib/stats.py @brief Collects parser's node statistics.'''
8 @param parser the parser object to attach this Stats object to'''
14 for rule
in self.
parserparser.rules:
15 self.
namesnames[rule[
'lower']] = rule[
'name']
16 for udt
in self.
parserparser.udts:
17 self.
namesnames[udt[
'lower']] = udt[
'name']
22 Called here and by the parser to initialize the hit counts.'''
23 s = {id.EMPTY: 0, id.MATCH: 0, id.NOMATCH: 0}
24 self.
statsstats[id.ALT] = s.copy()
25 self.
statsstats[id.CAT] = s.copy()
26 self.
statsstats[id.REP] = s.copy()
27 self.
statsstats[id.RNM] = s.copy()
28 self.
statsstats[id.TLS] = s.copy()
29 self.
statsstats[id.TBS] = s.copy()
30 self.
statsstats[id.TRG] = s.copy()
31 self.
statsstats[id.UDT] = s.copy()
32 self.
statsstats[id.AND] = s.copy()
33 self.
statsstats[id.NOT] = s.copy()
34 self.
statsstats[id.BKR] = s.copy()
35 self.
statsstats[id.BKA] = s.copy()
36 self.
statsstats[id.BKN] = s.copy()
37 self.
statsstats[id.ABG] = s.copy()
38 self.
statsstats[id.AEN] = s.copy()
39 for name
in self.
namesnames:
43 '''For internal use. Computes the total number of hits.'''
44 return stat[id.EMPTY] + stat[id.MATCH] + stat[id.NOMATCH]
47 '''Called by the parser for each node to collect the hit count.'''
48 state = self.
parserparser.state
49 self.
statsstats[op[
'type']][state] += 1
50 if(op[
'type'] == id.RNM):
51 rule = self.
parserparser.rules[op[
'index']]
52 self.
rule_statsrule_stats[rule[
'lower']][state] += 1
53 if(op[
'type'] == id.UDT):
54 udt = self.
parserparser.udts[op[
'index']]
55 self.
rule_statsrule_stats[udt[
'lower']][state] += 1
58 '''Display the parse tree node hit statistics.
59 It will first display the node statistics for the various
61 It then displays the rule name and UDT name statistics.
62 Operators and rule/UDT names for which the hit count is 0 are
69 print(
' OPERATOR NODE HIT STATISTICS')
71 '%5s %7s %7s %7s %7s' %
72 (
'',
'MATCH',
'EMPTY',
'NOMATCH',
'TOTAL'))
73 for stat_id
in self.
statsstats:
74 stat = self.
statsstats[stat_id]
75 total = self.
totaltotal(stat)
78 print(
'%5s' % (id.dict.get(stat_id)) +
' ', end=
'')
79 mTotal += stat[id.MATCH]
80 eTotal += stat[id.EMPTY]
81 nTotal += stat[id.NOMATCH]
83 p =
'%7d %7d %7d %7d' % (
84 stat[id.MATCH], stat[id.EMPTY], stat[id.NOMATCH], total)
86 print(
'%5s ' % (
'TOTAL'), end=
'')
87 p =
'%7d %7d %7d %7d' % (
88 mTotal, eTotal, nTotal, tTotal)
91 print(
' RULE NAME (RNM/UDT) NODE HIT STATISTICS')
93 '%7s %7s %7s %7s %s' %
94 (
'MATCH',
'EMPTY',
'NOMATCH',
'TOTAL',
'RULE/UDT NAME'))
108 ll.append((name, self.
totaltotal(stat)))
111 ll.sort(key=by_count, reverse=
True)
117 p =
'%7d %7d %7d %7d %s' % (
118 stat[id.MATCH], stat[id.EMPTY], stat[id.NOMATCH],
119 count, self.
namesnames[name])
def collect(self, op)
Called by the parser for each node to collect the hit count.
def total(self, stat)
For internal use.
def clear(self)
For internal use.
def __init__(self, parser)
Class constructor.
def display(self)
Display the parse tree node hit statistics.