Passed
Pull Request — master (#173)
by Jan
03:58
created

oval_graph.oval_tree.evaluate.smaller_than_two()   A

Complexity

Conditions 1

Size

Total Lines 2
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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