Passed
Pull Request — master (#46)
by Jan
02:53
created

oval_graph.oval_node.OvalNode.find_node_with_ID()   B

Complexity

Conditions 6

Size

Total Lines 10
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 6

Importance

Changes 0
Metric Value
cc 6
eloc 9
nop 2
dl 0
loc 10
ccs 9
cts 9
cp 1
crap 6
rs 8.6666
c 0
b 0
f 0
1
'''
2
    Modules form my lib and for create ID
3
'''
4 1
import uuid
5
6 1
import oval_graph.evaluate
7
8
9
'''
10
    This module contains methods and classes for
11
    constructing and controlling an oval tree.
12
'''
13
14
15 1
class OvalNode():
16
    '''
17
    The OvalNode object is one node of oval_graph.
18
19
    Args:
20
        node_id (str|int): identifies node
21
        input_node_type (str): type of node (value or operator)
22
        input_value (str): value of node
23
        input_negation (bool): value indicating whether the node is negated or not
24
        comment (str): text about node
25
        children ([OvalNode]): array of children of node
26
27
    Attributes:
28
        node_id (str): id of node
29
        node_type (str): type node
30
        value (str): value of node for operator and,
31
        or, one etc... and for value true, false, error etc...
32
        children ([OvalNode]): children of node
33
    '''
34
35 1
    def __init__(
36
            self,
37
            node_id,
38
            input_node_type,
39
            input_value,
40
            input_negation,
41
            comment,
42
            children=None
43
    ):
44 1
        self.comment = comment
45 1
        self.node_id = node_id
46 1
        self.negation = self.validate_negation(input_negation)
47 1
        self.node_type = self.validate_type(input_node_type)
48 1
        self.value = self.validate_type_and_value(input_value)
49 1
        self.children = []
50 1
        self.validate_children(children)
51 1
        if children:
52 1
            for child in children:
53 1
                self.add_child(child)
54
55 1
    def validate_negation(self, input_negation):
56 1
        if not isinstance(input_negation, bool):
57 1
            raise ValueError("err- negation is bool (only True or False)")
58 1
        return input_negation
59
60 1
    def validate_type(self, input_node_type):
61 1
        node_type = input_node_type.lower()
62 1
        if node_type != "value" and node_type != "operator":
63 1
            raise ValueError("err- unknown type")
64 1
        return node_type
65
66 1
    def validate_type_and_value(self, input_value):
67 1
        value = input_value.lower()
68
69 1
        allowed_values = [
70
            "true",
71
            "false",
72
            "error",
73
            "unknown",
74
            "noteval",
75
            "notappl"]
76 1
        allowed_operators = ["or", "and", "one", "xor"]
77
78 1
        if self.node_type == "value":
79 1
            if value not in allowed_values:
80 1
                raise ValueError("err- unknown value")
81
82 1
        if self.node_type == "operator":
83 1
            if value not in allowed_operators:
84 1
                raise ValueError("err- unknown operator")
85 1
        return value
86
87 1
    def validate_children(self, children):
88 1
        if children is None and self.node_type == "operator":
89 1
            raise ValueError('err- Operator node has child!')
90
91 1
    def __repr__(self):
92 1
        return self.value
93
94 1
    def add_child(self, node):
95 1
        if self.node_type == "operator":
96 1
            assert isinstance(node, OvalNode)
97 1
            self.children.append(node)
98
        else:
99 1
            self.children = None
100 1
            raise ValueError(
101
                "err- Value node don't has any child!")
102
103 1
    def _get_result_counts(self):
104 1
        result = {
105
            'true_cnt': 0,
106
            'false_cnt': 0,
107
            'error_cnt': 0,
108
            'unknown_cnt': 0,
109
            'noteval_cnt': 0,
110
            'notappl_cnt': 0
111
        }
112
113 1
        for child in self.children:
114 1
            if child.value == 'true':
115 1
                result['true_cnt'] += 1
116 1
            elif child.value == 'false':
117 1
                result['false_cnt'] += 1
118 1
            elif child.value == 'error':
119 1
                result['error_cnt'] += 1
120 1
            elif child.value == 'unknown':
121 1
                result['unknown_cnt'] += 1
122 1
            elif child.value == 'noteval':
123 1
                result['noteval_cnt'] += 1
124 1
            elif child.value == 'notappl':
125 1
                result['notappl_cnt'] += 1
126
            else:
127 1
                if self.node_type == "operator":
128 1
                    result[child.evaluate_tree() + "_cnt"] += 1
129 1
        return result
130
131 1
    def evaluate_tree(self):
132 1
        result = self._get_result_counts()
133 1
        out_result = None
134 1
        if oval_graph.evaluate.is_notapp_result(result):
135 1
            out_result = "notappl"
136
        else:
137 1
            if self.value == "or":
138 1
                out_result = oval_graph.evaluate.oval_operator_or(result)
139 1
            elif self.value == "and":
140 1
                out_result = oval_graph.evaluate.oval_operator_and(result)
141 1
            elif self.value == "one":
142 1
                out_result = oval_graph.evaluate.oval_operator_one(result)
143 1
            elif self.value == "xor":
144 1
                out_result = oval_graph.evaluate.oval_operator_xor(result)
145
146 1
        if out_result == 'true' and self.negation:
147 1
            out_result = 'false'
148 1
        elif out_result == 'false' and self.negation:
149 1
            out_result = 'true'
150
151 1
        return out_result
152
153 1
    def save_tree_to_dict(self):
154 1
        if not self.children:
155 1
            return {
156
                'node_id': self.node_id,
157
                'type': self.node_type,
158
                'value': self.value,
159
                'negation': self.negation,
160
                'comment': self.comment,
161
                'child': None
162
            }
163 1
        return {
164
            'node_id': self.node_id,
165
            'type': self.node_type,
166
            'value': self.value,
167
            'negation': self.negation,
168
            'comment': self.comment,
169
            'child': [child.save_tree_to_dict() for child in self.children]
170
        }
171
172 1
    def find_node_with_ID(self, node_id):
173 1
        if self.node_id == node_id:
174 1
            return self
175
        else:
176 1
            for child in self.children:
177 1
                if child.node_id == node_id:
178 1
                    return child
179 1
            for child in self.children:
180 1
                if child.children != []:
181 1
                    return child.find_node_with_ID(node_id)
182
183 1
    def add_to_tree(self, node_id, newNode):
184 1
        self.find_node_with_ID(node_id).add_child(newNode)
185
186 1
    def change_tree_value(self, node_id, value):
187 1
        self.find_node_with_ID(node_id).value = value
188
189
190 1
def restore_dict_to_tree(dict_of_tree):
191 1
    if dict_of_tree["child"] is None:
192 1
        return OvalNode(
193
            dict_of_tree["node_id"],
194
            dict_of_tree["type"],
195
            dict_of_tree["value"],
196
            dict_of_tree["negation"],
197
            dict_of_tree['comment'])
198 1
    return OvalNode(
199
        dict_of_tree["node_id"],
200
        dict_of_tree["type"],
201
        dict_of_tree["value"],
202
        dict_of_tree["negation"],
203
        dict_of_tree['comment'],
204
        [restore_dict_to_tree(i) for i in dict_of_tree["child"]])
205