spiff-arena/SpiffWorkflow/bpmn/specs/SubWorkflowTask.py
Dan 35fd8ffc0f Squashed 'SpiffWorkflow/' changes from 11e4b4f96..b439f69f2
b439f69f2 Merge pull request #296 from sartography/bugfix/subprocess-access-to-data-objects
6d2a2031e update spiff subworkflow tasks too
992c38671 make data objects referenceable within subprocesses
6c8ff5cdf allow subprocesses & call activities to have different data copy policies
2b14f3a48 initialize subprocesses in _update_hook instead of _on_ready_before
791f335d5 Merge pull request #295 from sartography/improvement/remove-camunda-from-base-and-misc-cleanup
28b579beb remove a few unused, duplicative, and debugging methods
8f14d1098 remove some other unused diagrams and tests
408bc6734 rely on top level camunda parser for almost all namespace references
895b2cc9b remove camunda namespace from base bpmn parser
76ecbf7cc Merge pull request #294 from sartography/bugfix/reactivate-boundary-event
82b6c8ad4 hack to ensure timers (and other events) are reset if returned to via loop reset
590903f47 Merge pull request #292 from sartography/feature/multiinstance-refactor
537490043 fix bug & typo
f31726db1 raise error on attempting to migrate workflows with MI
44e6d08d8 create spiff multiinstance task
2168c022b create camunda MI that approximates what it used to do
9894cea59 some improvements and bugfixes
f857ad5d4 remove some now unused functionality & tests, create a few more tests
6fead9d04 updated serializer & fixes for most tests
ec662ecdd add parallel multiinstance
bd19b2a8a working sequential multiinstance
2f9c192b6 further cleanup around _update_hook
947792bf6 fix bug in exclusive gateway migration
d3d87b28d add io spec to all tasks
f1586e275 add support for standard loop tasks

git-subtree-dir: SpiffWorkflow
git-subtree-split: b439f69f23b547df4de1e8e0c636997f2fd4e33b
2023-02-18 10:32:56 -05:00

125 lines
4.7 KiB
Python

# -*- coding: utf-8 -*-
from copy import deepcopy
from SpiffWorkflow.task import TaskState
from .BpmnSpecMixin import BpmnSpecMixin
from ..exceptions import WorkflowDataException
class SubWorkflowTask(BpmnSpecMixin):
"""
Task Spec for a bpmn node containing a subworkflow.
"""
def __init__(self, wf_spec, name, subworkflow_spec, transaction=False, **kwargs):
"""
Constructor.
:param bpmn_wf_spec: the BpmnProcessSpec for the sub process.
:param bpmn_wf_class: the BpmnWorkflow class to instantiate
"""
super(SubWorkflowTask, self).__init__(wf_spec, name, **kwargs)
self.spec = subworkflow_spec
self.transaction = transaction
@property
def spec_type(self):
return 'Subprocess'
def _on_ready_hook(self, my_task):
super()._on_ready_hook(my_task)
self.start_workflow(my_task)
def _on_subworkflow_completed(self, subworkflow, my_task):
self.update_data(my_task, subworkflow)
my_task._set_state(TaskState.READY)
def _update_hook(self, my_task):
wf = my_task.workflow._get_outermost_workflow(my_task)
if my_task.id not in wf.subprocesses:
super()._update_hook(my_task)
self.create_workflow(my_task)
return True
def _on_cancel(self, my_task):
subworkflow = my_task.workflow.get_subprocess(my_task)
if subworkflow is not None:
subworkflow.cancel()
def copy_data(self, my_task, subworkflow):
# There is only one copy of any given data object, so it should be updated immediately
subworkflow.data = my_task.workflow.data
start = subworkflow.get_tasks_from_spec_name('Start', workflow=subworkflow)
start[0].set_data(**my_task.data)
def update_data(self, my_task, subworkflow):
my_task.data = deepcopy(subworkflow.last_task.data)
def create_workflow(self, my_task):
subworkflow = my_task.workflow.create_subprocess(my_task, self.spec, self.name)
subworkflow.completed_event.connect(self._on_subworkflow_completed, my_task)
def start_workflow(self, my_task):
subworkflow = my_task.workflow.get_subprocess(my_task)
self.copy_data(my_task, subworkflow)
for child in subworkflow.task_tree.children:
child.task_spec._update(child)
my_task._set_state(TaskState.WAITING)
def task_will_set_children_future(self, my_task):
my_task.workflow.delete_subprocess(my_task)
class CallActivity(SubWorkflowTask):
def __init__(self, wf_spec, name, subworkflow_spec, **kwargs):
super(CallActivity, self).__init__(wf_spec, name, subworkflow_spec, False, **kwargs)
def copy_data(self, my_task, subworkflow):
start = subworkflow.get_tasks_from_spec_name('Start', workflow=subworkflow)
if subworkflow.spec.io_specification is None or len(subworkflow.spec.io_specification.data_inputs) == 0:
# Copy all task data into start task if no inputs specified
start[0].set_data(**my_task.data)
else:
# Otherwise copy only task data with the specified names
for var in subworkflow.spec.io_specification.data_inputs:
if var.name not in my_task.data:
raise WorkflowDataException(
"You are missing a required Data Input for a call activity.",
task=my_task,
data_input=var,
)
start[0].data[var.name] = my_task.data[var.name]
def update_data(self, my_task, subworkflow):
if subworkflow.spec.io_specification is None:
# Copy all workflow data if no outputs are specified
my_task.data = deepcopy(subworkflow.last_task.data)
else:
end = subworkflow.get_tasks_from_spec_name('End', workflow=subworkflow)
# Otherwise only copy data with the specified names
for var in subworkflow.spec.io_specification.data_outputs:
if var.name not in end[0].data:
raise WorkflowDataException(
f"The Data Output was not available in the subprocess output.",
task=my_task,
data_output=var,
)
my_task.data[var.name] = end[0].data[var.name]
@property
def spec_type(self):
return 'Call Activity'
class TransactionSubprocess(SubWorkflowTask):
def __init__(self, wf_spec, name, subworkflow_spec, **kwargs):
super(TransactionSubprocess, self).__init__(wf_spec, name, subworkflow_spec, True, **kwargs)
@property
def spec_type(self):
return 'Transactional Subprocess'