-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathdmn_model.py
69 lines (60 loc) · 2.76 KB
/
dmn_model.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import xml.etree.ElementTree as ET
from dmn_types import *
from collections import deque
from copy import deepcopy
class DmnModel:
def __init__(self, model_path):
self.model_path = model_path
self.decisions = {}
model_tree = ET.parse(self.model_path)
model_root = model_tree.getroot()
decisions = model_root.findall("dmn:decision", NS)
for decision in decisions:
d = DMN_MAPPINGS["dmn:decision"]()
d.parse(decision)
self.decisions[d._id] = d
async def create_instance(self, _id, bpmn_input_variables):
instance = DmnInstance(_id, bpmn_input_variables, model=self)
return instance
class DmnInstance:
def __init__(self, _id, bpmn_input_variables, model):
self._id = _id
self.bpmn_input_variables = bpmn_input_variables
self.model = model
self.decisions = model.decisions
self.decisions_queue = deque(self.sort_required_decision_list())
print("Final Decision queue : ", self.decisions_queue)
def sort_required_decision_list(self):
helper_list = []
for current, _ in self.model.decisions.items():
helper_list.append(current)
list_copy = deepcopy(helper_list)
if not self.decisions[current].required_decisions:
helper_list.remove(current)
helper_list.insert(0, current)
continue
for pos, dec in enumerate(list_copy):
# Current is required for decisions in helper list
if current in self.decisions[dec].required_decisions:
# Current is already in good position
if helper_list.index(current) < helper_list.index(dec):
continue
# Put current before decision it is required for
else:
helper_list.remove(current)
helper_list.insert(pos, current)
if dec in self.decisions[current].required_decisions:
# Current is before its required decision...
# I don't think this case is possible, but additional testing is needed
if helper_list.index(current) < helper_list.index(dec):
print("Intervention needed")
return helper_list
async def run(self):
decisions_queue = deepcopy(self.decisions_queue)
input_variables = deepcopy(self.bpmn_input_variables)
while decisions_queue:
current_decision = decisions_queue.popleft()
current_decision = self.decisions[current_decision]
output = current_decision.run(input_variables)
input_variables = {**output, **input_variables}
return output