2022-10-12 10:19:53 -04:00
|
|
|
import os
|
|
|
|
|
|
|
|
from lxml import etree
|
|
|
|
|
|
|
|
from SpiffWorkflow.bpmn.PythonScriptEngine import Box
|
|
|
|
from SpiffWorkflow.dmn.engine.DMNEngine import DMNEngine
|
2022-10-27 10:50:48 -04:00
|
|
|
from SpiffWorkflow.dmn.parser.DMNParser import DMNParser, get_dmn_ns
|
2022-10-12 10:19:53 -04:00
|
|
|
|
|
|
|
|
|
|
|
class Workflow:
|
|
|
|
def __init__(self, script_engine):
|
|
|
|
self.script_engine = script_engine
|
|
|
|
self.outer_workflow = self
|
|
|
|
self.spec = Box({'file': 'my_mock_file'})
|
|
|
|
|
|
|
|
class TaskSpec:
|
|
|
|
def __init__(self):
|
|
|
|
self.name = "MockTestSpec"
|
|
|
|
self.description = "Mock Test Spec"
|
|
|
|
|
|
|
|
class Task:
|
|
|
|
def __init__(self, script_engine, data):
|
|
|
|
self.data = data
|
|
|
|
self.workflow = Workflow(script_engine)
|
|
|
|
self.task_spec = TaskSpec()
|
|
|
|
|
|
|
|
class DecisionRunner:
|
|
|
|
|
|
|
|
def __init__(self, script_engine, filename, path=''):
|
|
|
|
self.script_engine = script_engine
|
|
|
|
fn = os.path.join(os.path.dirname(__file__), path, 'data', filename)
|
|
|
|
|
|
|
|
with open(fn) as fh:
|
|
|
|
node = etree.parse(fh)
|
|
|
|
|
2022-10-27 10:50:48 -04:00
|
|
|
self.dmnParser = DMNParser(None, node.getroot(), get_dmn_ns(node.getroot()))
|
2022-10-12 10:19:53 -04:00
|
|
|
self.dmnParser.parse()
|
|
|
|
|
|
|
|
decision = self.dmnParser.decision
|
|
|
|
assert len(decision.decisionTables) == 1, \
|
|
|
|
'Exactly one decision table should exist! (%s)' \
|
|
|
|
% (len(decision.decisionTables))
|
|
|
|
|
2022-11-25 11:07:31 -05:00
|
|
|
self.decision_table = decision.decisionTables[0]
|
|
|
|
self.dmnEngine = DMNEngine(self.decision_table)
|
2022-10-12 10:19:53 -04:00
|
|
|
|
|
|
|
def decide(self, context):
|
2022-11-25 11:07:31 -05:00
|
|
|
"""Makes the rather ugly assumption that there is only one
|
|
|
|
rule match for a decision - which was previously the case"""
|
2022-10-12 10:19:53 -04:00
|
|
|
if not isinstance(context, dict):
|
|
|
|
context = {'input': context}
|
|
|
|
task = Task(self.script_engine, context)
|
2022-11-25 11:07:31 -05:00
|
|
|
return self.dmnEngine.decide(task)[0]
|
|
|
|
|
|
|
|
def result(self, context):
|
|
|
|
task = Task(self.script_engine, context)
|
|
|
|
return self.dmnEngine.result(task)
|