Passed
Pull Request — master (#173)
by Jan
04:56
created

oval_graph.oval_tree.evaluate.oval_operator_xor()   C

Complexity

Conditions 11

Size

Total Lines 30
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 11

Importance

Changes 0
Metric Value
eloc 15
dl 0
loc 30
ccs 15
cts 15
cp 1
rs 5.4
c 0
b 0
f 0
cc 11
nop 1
crap 11

How to fix   Complexity   

Complexity

Complex classes like oval_graph.oval_tree.evaluate.oval_operator_xor() 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
5
6 1
def oval_operator_and(result):
7
    """The AND operator produces a true result if every argument is true. If one or more arguments
8
       are false, the result of the AND is false. If one or more of the arguments are unknown, and
9
       if none of the arguments are false, then the AND operator produces a result of unknown.
10
11
    Args:
12
            result (dict): Dictionary storing occurrence of given values
13
14
    Returns:
15
            str. return values::
16
17
                true
18
                false
19
                error
20
                unknown
21
                noteval
22
    """
23 1
    out_result = None
24 1
    eval_eq_zero = eq_zero(result, 'false_cnt')
25 1
    eval_greater_zero = greater_zero(result, 'true_cnt')
26 1
    if eval_eq_zero and eval_greater_zero and error_unknown_noteval_eq_zero(result):
27 1
        out_result = 'true'
28 1
    elif greater_zero(result, 'false_cnt'):
29 1
        out_result = 'false'
30
    else:
31 1
        out_result = error_unknown_noteval_for_operators_and_or(result, 'and')
32 1
    return out_result
33
34
35 1
def oval_operator_one(result):
36
    """The ONE operator produces a true result if one and only one argument is true. If there are
37
       more than argument is true (or if there are no true arguments), the result of the ONE
38
       is false. If one or more of the arguments are unknown, then the ONE operator produces
39
       a result of unknown.
40
41
    Args:
42
            result (dict): Dictionary storing occurrence of given values
43
44
    Returns:
45
            str. return values::
46
47
                true
48
                false
49
                error
50
                unknown
51
                noteval
52
    """
53 1
    out_result = None
54 1
    if one_is_true(result):
55 1
        out_result = 'true'
56 1
    elif one_is_false(result):
57 1
        out_result = 'false'
58 1
    elif one_is_error(result):
59 1
        out_result = 'error'
60 1
    elif one_is_unknown(result):
61 1
        out_result = 'unknown'
62 1
    elif one_is_noteval(result):
63 1
        out_result = 'noteval'
64 1
    return out_result
65
66
67 1
def oval_operator_or(result):
68
    """The OR operator produces a true result if one or more arguments is true. If every argument
69
       is false, the result of the OR is false. If one or more of the arguments are unknown and
70
       if none of arguments are true, then the OR operator produces a result of unknown.
71
72
    Args:
73
            result (dict): Dictionary storing occurrence of given values
74
75
    Returns:
76
            str. return values::
77
78
                true
79
                false
80
                error
81
                unknown
82
                noteval
83
    """
84 1
    out_result = None
85 1
    eval_eq_zero = eq_zero(result, 'true_cnt')
86 1
    eval_greater_zero = greater_zero(result, 'false_cnt')
87 1
    if greater_zero(result, 'true_cnt'):
88 1
        out_result = 'true'
89 1
    elif eval_eq_zero and eval_greater_zero and error_unknown_noteval_eq_zero(result):
90 1
        out_result = 'false'
91
    else:
92 1
        out_result = error_unknown_noteval_for_operators_and_or(result, 'or')
93 1
    return out_result
94
95
96 1
def oval_operator_xor(result):
97
    """XOR is defined to be true if an odd number of its arguments are true, and false otherwise.
98
       If any of the arguments are unknown, then the XOR operator produces a result of unknown.
99
100
    Args:
101
            result (dict): Dictionary storing occurrence of given values
102
103
    Returns:
104
            str. return values::
105
106
                true
107
                false
108
                error
109
                unknown
110
                noteval
111
    """
112 1
    out_result = None
113 1
    eval_error_cnt = eq_zero(result, 'error_cnt')
114 1
    eval_unknown_cnt = eq_zero(result, 'unknown_cnt')
115 1
    if (result['true_cnt'] % 2) == 1 and eq_zero_unknown_noteval_notappl(result):
116 1
        out_result = 'true'
117 1
    elif (result['true_cnt'] % 2) == 0 and eq_zero_unknown_noteval_notappl(result):
118 1
        out_result = 'false'
119 1
    elif greater_zero(result, 'error_cnt'):
120 1
        out_result = 'error'
121 1
    elif eq_zero(result, 'error_cnt') and greater_zero(result, 'unknown_cnt'):
122 1
        out_result = 'unknown'
123 1
    elif eval_error_cnt and eval_unknown_cnt and greater_zero(result, 'noteval_cnt'):
124 1
        out_result = 'noteval'
125 1
    return out_result
126
127
128 1
def error_unknown_noteval_for_operators_and_or(result, operator):
129
    """Evaluates if the result match the values for error, unknown, noteval.
130
131
    Args:
132
            result (dict): Dictionary storing occurrence of given values
133
            operator (str): Specifies for which operator is used
134
135
    Returns:
136
            str or None. return values::
137
138
                error
139
                unknown
140
                notappl
141
    """
142 1
    out_result = None
143 1
    value_for_check = 'false_cnt' if operator == 'and' else 'true_cnt'
144 1
    if eq_zero(result, value_for_check) and greater_zero(result, 'error_cnt'):
145 1
        out_result = 'error'
146 1
    elif eq_zero(result, value_for_check) and error_unknown_eq_zero(result):
147 1
        out_result = 'unknown'
148 1
    elif eq_zero(result, value_for_check) and error_unknown_eq_noteval_greater_zero(result):
149 1
        out_result = 'noteval'
150 1
    return out_result
151
152
153 1
def eq_zero(result, cnt):
154 1
    return result[cnt] == 0
155
156
157 1
def eq_zero_duo(result, cnt0, cnt1):
158 1
    return result[cnt0] == 0 and result[cnt1] == 0
159
160
161 1
def greater_zero(result, cnt):
162 1
    return result[cnt] > 0
163
164
165 1
def eq_or_greater_zero(result, cnt):
166 1
    return result[cnt] >= 0
167
168
169 1
def eq_or_greater_zero_duo(result, cnt0, cnt1):
170 1
    return result[cnt0] >= 0 and result[cnt1] >= 0
171
172
173 1
def smaller_than_two(result, cnt):
174 1
    return result[cnt] < 2
175
176
177 1
def one_is_noteval(result):
178
    """Evaluates if the result match noteval for one operator.
179
180
    Args:
181
            result (dict): Dictionary storing occurrence of given values
182
183
    Returns:
184
            bool.
185
    """
186 1
    true_cnt = smaller_than_two(result, 'true_cnt')
187 1
    false_cnt_notappl_cnt = eq_or_greater_zero_duo(result, 'false_cnt', 'notappl_cnt')
188 1
    error_cnt_unknown_cnt = eq_zero_duo(result, 'error_cnt', 'unknown_cnt')
189 1
    noteval_cnt = greater_zero(result, 'noteval_cnt')
190 1
    return true_cnt and false_cnt_notappl_cnt and error_cnt_unknown_cnt and noteval_cnt
191
192
193 1
def one_is_unknown(result):
194
    """Evaluates if the result match unknown for one operator.
195
196
    Args:
197
            result (dict): Dictionary storing occurrence of given values
198
199
    Returns:
200
            bool.
201
    """
202 1
    true_cnt = smaller_than_two(result, 'true_cnt')
203 1
    false_cnt = eq_or_greater_zero(result, 'false_cnt')
204 1
    error_cnt = eq_zero(result, 'error_cnt')
205 1
    unknown_cnt = result['unknown_cnt'] >= 1
206 1
    noteval_cnt_notappl_cnt = eq_or_greater_zero_duo(result, 'noteval_cnt', 'notappl_cnt')
207 1
    return true_cnt and false_cnt and error_cnt and unknown_cnt and noteval_cnt_notappl_cnt
208
209
210 1
def one_is_error(result):
211
    """Evaluates if the result match unknown for one operator.
212
213
    Args:
214
            result (dict): Dictionary storing occurrence of given values
215
216
    Returns:
217
            bool.
218
    """
219 1
    true_cnt = smaller_than_two(result, 'true_cnt')
220 1
    false_cnt = eq_or_greater_zero(result, 'false_cnt')
221 1
    error_cnt = greater_zero(result, 'error_cnt')
222 1
    unknown_noteval_notappl = eq_or_greater_zero_unknown_noteval_notappl(result)
223 1
    return true_cnt and false_cnt and error_cnt and unknown_noteval_notappl
224
225
226 1
def one_is_false(result):
227
    """Evaluates if the result match false for one operator.
228
       Operator ONE has two cases of false state.
229
230
    Args:
231
            result (dict): Dictionary storing occurrence of given values
232
233
    Returns:
234
            bool.
235
    """
236
    # The first case for false
237 1
    true_cnt = eq_zero(result, 'true_cnt')
238 1
    false_cnt = eq_or_greater_zero(result, 'false_cnt')
239 1
    unknown_noteval_cnt = error_unknown_noteval_eq_zero(result)
240 1
    notappl_cnt = result['notappl_cnt'] >= 0
241
242 1
    first_case = true_cnt and false_cnt and unknown_noteval_cnt and notappl_cnt
243
244
    # The second case for false
245 1
    true_cnt = result['true_cnt'] >= 2
246 1
    false_error_cnt = eq_or_greater_zero_duo(result, 'false_cnt', 'error_cnt')
247 1
    unknown_noteval_notappl = eq_or_greater_zero_unknown_noteval_notappl(result)
248 1
    second_case = true_cnt and false_error_cnt and unknown_noteval_notappl
249
250 1
    return first_case or second_case
251
252
253 1
def one_is_true(result):
254
    """Evaluates if the result match true for one operator.
255
256
    Args:
257
            result (dict): Dictionary storing occurrence of given values
258
259
    Returns:
260
            bool.
261
    """
262 1
    true_cnt = result['true_cnt'] == 1
263 1
    false_notappl_cnt = eq_or_greater_zero_duo(result, 'false_cnt', 'notappl_cnt')
264 1
    unknown_noteval_cnt = error_unknown_noteval_eq_zero(result)
265 1
    return true_cnt and false_notappl_cnt and unknown_noteval_cnt
266
267
268 1
def eq_or_greater_zero_unknown_noteval_notappl(result):
269 1
    unknown_cnt = eq_or_greater_zero(result, 'unknown_cnt')
270 1
    noteval_cnt = eq_or_greater_zero(result, 'noteval_cnt')
271 1
    notappl_cnt = eq_or_greater_zero(result, 'notappl_cnt')
272 1
    return unknown_cnt and notappl_cnt and noteval_cnt
273
274
275 1
def eq_zero_unknown_noteval_notappl(result):
276 1
    error_cnt = eq_zero(result, 'error_cnt')
277 1
    unknown_cnt = eq_zero(result, 'unknown_cnt')
278 1
    noteval_cnt = eq_zero(result, 'noteval_cnt')
279 1
    return error_cnt and unknown_cnt and noteval_cnt
280
281
282 1
def error_unknown_noteval_eq_zero(result):
283 1
    error_cnt = eq_zero(result, 'error_cnt')
284 1
    unknown_cnt = eq_zero(result, 'unknown_cnt')
285 1
    noteval_cnt = eq_zero(result, 'noteval_cnt')
286 1
    return error_cnt and unknown_cnt and noteval_cnt
287
288
289 1
def error_unknown_eq_noteval_greater_zero(result):
290 1
    error_cnt = eq_zero(result, 'error_cnt')
291 1
    unknown_cnt = eq_zero(result, 'unknown_cnt')
292 1
    noteval_cnt = greater_zero(result, 'noteval_cnt')
293 1
    return error_cnt and unknown_cnt and noteval_cnt
294
295
296 1
def error_unknown_eq_zero(result):
297 1
    return eq_zero(result, 'error_cnt') and greater_zero(result, 'unknown_cnt')
298
299
300 1
def is_notapp_result(result):
301
    """Evaluates if the counts of values in
302
       the result matches the notapp result.
303
304
    Args:
305
            result (dict): Dictionary storing occurrence of given values
306
307
    Returns:
308
            bool.
309
     """
310 1
    notappl_cnt = result['notappl_cnt'] > 0
311 1
    false_cnt = eq_zero(result, 'false_cnt')
312 1
    unknown_noteval_cnt = error_unknown_noteval_eq_zero(result)
313 1
    true_cnt = eq_zero(result, 'true_cnt')
314
    return notappl_cnt and false_cnt and unknown_noteval_cnt and true_cnt
315