Passed
Push — master ( 50e90e...1f6e08 )
by Jan
02:02
created

graph.evaluate   F

Complexity

Total Complexity 87

Size/Duplication

Total Lines 196
Duplicated Lines 0 %

Test Coverage

Coverage 98.94%

Importance

Changes 0
Metric Value
wmc 87
eloc 140
dl 0
loc 196
ccs 93
cts 94
cp 0.9894
rs 2
c 0
b 0
f 0

14 Functions

Rating   Name   Duplication   Size   Complexity  
A and_or_eq_zero() 0 6 3
B error_unknown_noteval_for_operators_and_or() 0 14 7
A error_unknown_eq_noteval_greater_zero() 0 6 4
A eq_zero_unknown_noteval_notappl() 0 6 4
C oval_operator_xor() 0 23 11
A greater_zero() 0 4 2
A eq_or_greater_zero() 0 4 2
A error_unknown_eq_zero() 0 5 3
A eq_or_greater_zero_unknown_noteval_notappl() 0 6 4
A eq_zero() 0 4 2
A error_unknown_noteval_eq_zero() 0 6 4
A oval_operator_and() 0 14 5
A oval_operator_or() 0 14 5
F oval_operator_one() 0 44 31

How to fix   Complexity   

Complexity

Complex classes like graph.evaluate often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

1
"""
2
    Function for evaluate oval operators.
3
4
    AND operator - The AND operator produces a true result if every argument is true. If one or more arguments are     false, the result of the AND is false. If one or more of the arguments are unknown, and if none of              the arguments are false, then the AND operator produces a result of unknown.
5
6
    ONE operator - The ONE operator produces a true result if one and only one argument is true. If there are more than argument is true (or if there are no true arguments), the result of the ONE is false. If one or more of the arguments are unknown, then the ONE operator produces a result of unknown.
7
8
    OR operator - The OR operator produces a true result if one or more arguments is true. If every argument is false, the result of the OR is false. If one or more of the arguments are unknown and if none of arguments are true, then the OR operator produces a result of unknown.
9
10
    XOR operator - XOR is defined to be true if an odd number of its arguments are true, and false otherwise. If any of the arguments are unknown, then the XOR operator produces a result of unknown.
11
"""
12
13
14 1
def oval_operator_and(result):
15
    """
16
        AND operator
17
    """
18 1
    out_result = None
19 1
    if eq_zero(result, 'false_cnt')\
20
            and greater_zero(result, 'true_cnt')\
21
            and error_unknown_noteval_eq_zero(result):
22 1
        out_result = 'true'
23 1
    elif greater_zero(result, 'false_cnt'):
24 1
        out_result = 'false'
25
    else:
26 1
        out_result = error_unknown_noteval_for_operators_and_or(result, 'and')
27 1
    return out_result
28
29
30 1
def oval_operator_one(result):
31
    """
32
        ONE operator
33
    """
34 1
    out_result = None
35 1
    if result['true_cnt'] == 1\
36
            and eq_or_greater_zero(result, 'false_cnt')\
37
            and error_unknown_noteval_eq_zero(result)\
38
            and eq_or_greater_zero(result, 'notappl_cnt'):
39 1
        out_result = 'true'
40 1
    elif result['true_cnt'] >= 2\
41
            and eq_or_greater_zero(result, 'false_cnt')\
42
            and eq_or_greater_zero(result, 'error_cnt')\
43
            and eq_or_greater_zero_unknown_noteval_notappl(result):
44 1
        out_result = 'false'
45 1
    elif eq_zero(result, 'true_cnt')\
46
            and eq_or_greater_zero(result, 'false_cnt')\
47
            and eq_zero(result, 'error_cnt')\
48
            and eq_zero(result, 'unknown_cnt')\
49
            and eq_zero(result, 'noteval_cnt')\
50
            and result['notappl_cnt'] >= 0:
51 1
        out_result = 'false'
52 1
    elif result['true_cnt'] < 2\
53
            and eq_or_greater_zero(result, 'false_cnt')\
54
            and greater_zero(result, 'error_cnt')\
55
            and eq_or_greater_zero_unknown_noteval_notappl(result):
56 1
        out_result = 'error'
57 1
    elif result['true_cnt'] < 2\
58
            and eq_or_greater_zero(result, 'false_cnt')\
59
            and eq_zero(result, 'error_cnt')\
60
            and result['unknown_cnt'] >= 1\
61
            and eq_or_greater_zero(result, 'noteval_cnt')\
62
            and eq_or_greater_zero(result, 'notappl_cnt'):
63 1
        out_result = 'unknown'
64 1
    elif result['true_cnt'] < 2\
65
            and eq_or_greater_zero(result, 'false_cnt')\
66
            and eq_zero(result, 'error_cnt')\
67
            and eq_zero(result, 'unknown_cnt')\
68
            and greater_zero(result, 'noteval_cnt')\
69
            and eq_or_greater_zero(result, 'notappl_cnt'):
70 1
        out_result = 'noteval'
71
    else:
72 1
        out_result = None
73 1
    return out_result
74
75
76 1
def oval_operator_or(result):
77
    """
78
        OR operator
79
    """
80 1
    out_result = None
81 1
    if greater_zero(result, 'true_cnt'):
82 1
        out_result = 'true'
83 1
    elif eq_zero(result, 'true_cnt')\
84
            and greater_zero(result, 'false_cnt')\
85
            and error_unknown_noteval_eq_zero(result):
86 1
        out_result = 'false'
87
    else:
88 1
        out_result = error_unknown_noteval_for_operators_and_or(result, 'or')
89 1
    return out_result
90
91
92 1
def oval_operator_xor(result):
93
    """
94
        XOR operator
95
    """
96 1
    out_result = None
97 1
    if (result['true_cnt'] % 2) == 1\
98
            and eq_zero_unknown_noteval_notappl(result):
99 1
        out_result = 'true'
100 1
    elif (result['true_cnt'] % 2) == 0\
101
            and eq_zero_unknown_noteval_notappl(result):
102 1
        out_result = 'false'
103 1
    elif greater_zero(result, 'error_cnt'):
104 1
        out_result = 'error'
105 1
    elif eq_zero(result, 'error_cnt')\
106
            and greater_zero(result, 'unknown_cnt'):
107 1
        out_result = 'unknown'
108 1
    elif eq_zero(result, 'error_cnt')\
109
            and eq_zero(result, 'unknown_cnt')\
110
            and greater_zero(result, 'noteval_cnt'):
111 1
        out_result = 'noteval'
112
    else:
113 1
        out_result = None
114 1
    return out_result
115
116
117 1
def error_unknown_noteval_for_operators_and_or(result, operator):
118 1
    out_result = None
119 1
    if and_or_eq_zero(operator, result)\
120
            and greater_zero(result, 'error_cnt'):
121 1
        out_result = 'error'
122 1
    elif and_or_eq_zero(operator, result)\
123
            and error_unknown_eq_zero(result):
124 1
        out_result = 'unknown'
125 1
    elif and_or_eq_zero(operator, result)\
126
            and error_unknown_eq_noteval_greater_zero(result):
127 1
        out_result = 'noteval'
128
    else:
129 1
        out_result = None
130 1
    return out_result
131
132
133 1
def and_or_eq_zero(operator, result):
134 1
    if operator == 'and':
135 1
        return eq_zero(result, 'false_cnt')
136 1
    if operator == 'or':
137 1
        return eq_zero(result, 'true_cnt')
138 1
    return None
139
140
141 1
def eq_zero(result, cnt):
142 1
    if result[cnt] == 0:
143 1
        return True
144 1
    return False
145
146
147 1
def greater_zero(result, cnt):
148 1
    if result[cnt] > 0:
149 1
        return True
150 1
    return False
151
152
153 1
def eq_or_greater_zero(result, cnt):
154 1
    if result[cnt] >= 0:
155 1
        return True
156 1
    return False
157
158
159 1
def eq_or_greater_zero_unknown_noteval_notappl(result):
160 1
    if eq_or_greater_zero(result, 'unknown_cnt')\
161
            and eq_or_greater_zero(result, 'noteval_cnt')\
162
            and eq_or_greater_zero(result, 'notappl_cnt'):
163 1
        return True
164
    return False
165
166
167 1
def eq_zero_unknown_noteval_notappl(result):
168 1
    if eq_zero(result, 'error_cnt')\
169
            and eq_zero(result, 'unknown_cnt')\
170
            and eq_zero(result, 'noteval_cnt'):
171 1
        return True
172 1
    return False
173
174
175 1
def error_unknown_noteval_eq_zero(result):
176 1
    if eq_zero(result, 'error_cnt')\
177
            and eq_zero(result, 'unknown_cnt')\
178
            and eq_zero(result, 'noteval_cnt'):
179 1
        return True
180 1
    return False
181
182
183 1
def error_unknown_eq_noteval_greater_zero(result):
184 1
    if eq_zero(result, 'error_cnt')\
185
            and eq_zero(result, 'unknown_cnt')\
186
            and greater_zero(result, 'noteval_cnt'):
187 1
        return True
188 1
    return False
189
190
191 1
def error_unknown_eq_zero(result):
192 1
    if eq_zero(result, 'error_cnt')\
193
            and greater_zero(result, 'unknown_cnt'):
194 1
        return True
195
    return False
196