Skip to content

Commit

Permalink
update parser to optionally attach pre/post scripts to MI instances
Browse files Browse the repository at this point in the history
  • Loading branch information
essweine committed Aug 2, 2023
1 parent c9a584c commit 9adbce4
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 9 deletions.
6 changes: 3 additions & 3 deletions SpiffWorkflow/bpmn/parser/TaskParser.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ def __init__(self, process_parser, spec_class, node, nsmap=None, lane=None):
self.spec_class = spec_class
self.spec = self.process_parser.spec

def _copy_task_attrs(self, original):
def _copy_task_attrs(self, original, loop_characteristics=None):

self.task.inputs = original.inputs
self.task.outputs = original.outputs
Expand Down Expand Up @@ -95,7 +95,7 @@ def _add_loop_task(self, loop_characteristics):

original = self.spec.task_specs.pop(self.task.name)
self.task = self.STANDARD_LOOP_CLASS(self.spec, original.name, '', maximum, condition, test_before)
self._copy_task_attrs(original)
self._copy_task_attrs(original, loop_characteristics)

def _add_multiinstance_task(self, loop_characteristics):

Expand Down Expand Up @@ -156,7 +156,7 @@ def _add_multiinstance_task(self, loop_characteristics):
self.task = self.SEQUENTIAL_MI_CLASS(self.spec, original.name, **params)
else:
self.task = self.PARALLEL_MI_CLASS(self.spec, original.name, **params)
self._copy_task_attrs(original)
self._copy_task_attrs(original, loop_characteristics)

def _add_boundary_event(self, children):

Expand Down
2 changes: 1 addition & 1 deletion SpiffWorkflow/camunda/parser/task_spec.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ def _add_multiinstance_task(self, loop_characteristics):
self.task = SequentialMultiInstanceTask(self.spec, original.name, **params)
else:
self.task = ParallelMultiInstanceTask(self.spec, original.name, **params)
self._copy_task_attrs(original)
self._copy_task_attrs(original, loop_characteristics)


class BusinessRuleTaskParser(CamundaTaskParser):
Expand Down
11 changes: 6 additions & 5 deletions SpiffWorkflow/spiff/parser/task_spec.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,13 +110,14 @@ def _parse_servicetask_operator(cls, node):
operator['parameters'] = parameters
return operator

def _copy_task_attrs(self, original):
def _copy_task_attrs(self, original, loop_characteristics):
# I am so disappointed I have to do this.
super()._copy_task_attrs(original)
self.task.prescript = original.prescript
self.task.postscript = original.postscript
original.prescript = None
original.postscript = None
if loop_characteristics.attrib.get('{' + SPIFFWORKFLOW_MODEL_NS + '}' + 'scriptsOnInstances') != 'true':
self.task.prescript = original.prescript
self.task.postscript = original.postscript
original.prescript = None
original.postscript = None

def create_task(self):
# The main task parser already calls this, and even sets an attribute, but
Expand Down
23 changes: 23 additions & 0 deletions tests/SpiffWorkflow/spiff/MultiInstanceTaskTest.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,26 @@ def testMultiInstanceTask(self):
'input_data': [2, 3, 4], # Prescript adds 1 to input
'output_data': [3, 5, 7], # Postscript subtracts 1 from output
})

def testMultiInstanceTaskWithInstanceScripts(self):
spec, subprocesses = self.load_workflow_spec('script_on_mi.bpmn', 'Process_1')
self.workflow = BpmnWorkflow(spec, subprocesses)
start = self.workflow.get_tasks_from_spec_name('Start')[0]
start.data = {'input_data': [1, 2, 3]}
self.workflow.do_engine_steps()
task = self.workflow.get_tasks_from_spec_name('any_task')[0]
self.workflow.do_engine_steps()

self.save_restore()

ready_tasks = self.workflow.get_ready_user_tasks()
for task in ready_tasks:
task.data['output_item'] = task.data['input_item'] * 2
task.run()
self.workflow.do_engine_steps()

self.assertTrue(self.workflow.is_completed())
self.assertDictEqual(self.workflow.data, {
'input_data': [1, 2, 3], # Prescript modifies input item
'output_data': [3, 5, 7],
})
49 changes: 49 additions & 0 deletions tests/SpiffWorkflow/spiff/data/script_on_mi.bpmn
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?xml version="1.0" encoding="UTF-8"?>
<bpmn:definitions xmlns:bpmn="http://www.omg.org/spec/BPMN/20100524/MODEL" xmlns:bpmndi="http://www.omg.org/spec/BPMN/20100524/DI" xmlns:dc="http://www.omg.org/spec/DD/20100524/DC" xmlns:di="http://www.omg.org/spec/DD/20100524/DI" xmlns:spiffworkflow="http://spiffworkflow.org/bpmn/schema/1.0/core" xmlns:modeler="http://camunda.org/schema/modeler/1.0" id="Definitions_19o7vxg" targetNamespace="http://bpmn.io/schema/bpmn" exporter="Camunda Modeler" exporterVersion="4.11.1" modeler:executionPlatform="Camunda Platform" modeler:executionPlatformVersion="7.17.0">
<bpmn:process id="Process_1" isExecutable="true">
<bpmn:startEvent id="Event_1ftsuzw">
<bpmn:outgoing>Flow_1hjrex4</bpmn:outgoing>
</bpmn:startEvent>
<bpmn:task id="any_task" name="Any Task">
<bpmn:extensionElements>
<spiffworkflow:preScript>input_item += 1</spiffworkflow:preScript>
<spiffworkflow:postScript>output_item -= 1</spiffworkflow:postScript>
</bpmn:extensionElements>
<bpmn:incoming>Flow_1hjrex4</bpmn:incoming>
<bpmn:outgoing>Flow_1xndbxy</bpmn:outgoing>
<bpmn:multiInstanceLoopCharacteristics spiffworkflow:scriptsOnInstances="true">
<bpmn:loopDataInputRef>input_data</bpmn:loopDataInputRef>
<bpmn:loopDataOutputRef>output_data</bpmn:loopDataOutputRef>
<bpmn:inputDataItem id="input_item" name="input item" />
<bpmn:outputDataItem id="output_item" name="output item" />
</bpmn:multiInstanceLoopCharacteristics>
</bpmn:task>
<bpmn:sequenceFlow id="Flow_1hjrex4" sourceRef="Event_1ftsuzw" targetRef="any_task" />
<bpmn:endEvent id="Event_0c80924">
<bpmn:incoming>Flow_1xndbxy</bpmn:incoming>
</bpmn:endEvent>
<bpmn:sequenceFlow id="Flow_1xndbxy" sourceRef="any_task" targetRef="Event_0c80924" />
</bpmn:process>
<bpmndi:BPMNDiagram id="BPMNDiagram_1">
<bpmndi:BPMNPlane id="BPMNPlane_1" bpmnElement="Process_1">
<bpmndi:BPMNEdge id="Flow_1xndbxy_di" bpmnElement="Flow_1xndbxy">
<di:waypoint x="380" y="120" />
<di:waypoint x="432" y="120" />
</bpmndi:BPMNEdge>
<bpmndi:BPMNEdge id="Flow_1hjrex4_di" bpmnElement="Flow_1hjrex4">
<di:waypoint x="228" y="120" />
<di:waypoint x="280" y="120" />
</bpmndi:BPMNEdge>
<bpmndi:BPMNShape id="Event_1ftsuzw_di" bpmnElement="Event_1ftsuzw">
<dc:Bounds x="192" y="102" width="36" height="36" />
</bpmndi:BPMNShape>
<bpmndi:BPMNShape id="Activity_1iqs4li_di" bpmnElement="any_task">
<dc:Bounds x="280" y="80" width="100" height="80" />
<bpmndi:BPMNLabel />
</bpmndi:BPMNShape>
<bpmndi:BPMNShape id="Event_0c80924_di" bpmnElement="Event_0c80924">
<dc:Bounds x="432" y="102" width="36" height="36" />
</bpmndi:BPMNShape>
</bpmndi:BPMNPlane>
</bpmndi:BPMNDiagram>
</bpmn:definitions>

0 comments on commit 9adbce4

Please sign in to comment.