spiff-arena/tests/SpiffWorkflow/dmn/DecisionRunner.py
Dan a3e8bcbd65 Squashed 'SpiffWorkflow/' changes from 46f410a28..46d3de27f
46d3de27f Merge pull request #267 from sartography/feature/dmn_collect_policy
2d5ca32d5 Support for the "COLLECT" hit policy. * DecisionTable constructor now expects a third argument (the HitPolicy) * DMNParser now checks for a hitPolicy attribute, but defaults ot "UNIQUE" as Camunda doesn't put another in there if Unique is selected. * DecisionTable deserializer will default to a hitPolicy of "UNIQUE" if not value is in the Json.

git-subtree-dir: SpiffWorkflow
git-subtree-split: 46d3de27ffbcaf60025f09d1cf04fcc7ee98658a
2022-11-25 11:07:31 -05:00

58 lines
1.8 KiB
Python

import os
from lxml import etree
from SpiffWorkflow.bpmn.PythonScriptEngine import Box
from SpiffWorkflow.dmn.engine.DMNEngine import DMNEngine
from SpiffWorkflow.dmn.parser.DMNParser import DMNParser, get_dmn_ns
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)
self.dmnParser = DMNParser(None, node.getroot(), get_dmn_ns(node.getroot()))
self.dmnParser.parse()
decision = self.dmnParser.decision
assert len(decision.decisionTables) == 1, \
'Exactly one decision table should exist! (%s)' \
% (len(decision.decisionTables))
self.decision_table = decision.decisionTables[0]
self.dmnEngine = DMNEngine(self.decision_table)
def decide(self, context):
"""Makes the rather ugly assumption that there is only one
rule match for a decision - which was previously the case"""
if not isinstance(context, dict):
context = {'input': context}
task = Task(self.script_engine, context)
return self.dmnEngine.decide(task)[0]
def result(self, context):
task = Task(self.script_engine, context)
return self.dmnEngine.result(task)