spiff-arena/doc/bpmn/custom_task_spec.rst
burnettk c904ee907b Squashed 'SpiffWorkflow/' changes from 73886584b..01a25fc3f
01a25fc3f Merge pull request #333 from sartography/feature/ruff
99c7bd0c7 ruff linting fixes
56d170ba1 Cleaning up badges in the readme.
51c13be93 tweaking action, adding button
96275ad7c Adding a github action to run tests
c6c40976a minor fix to please sonarcloud.
03316babb Merge pull request #332 from sartography/updates-for-2.0-release
ab70a34b5 Release Notes for 2.0.0_rc1
f0bf79bd9 copy edits
a7c726951 Release Notes for 2.0.0_rc1
5f0468ba4 Merge pull request #330 from sartography/updates-for-2.0-release
b9ad24406 Mostly minor edits
e284dd8e2 corrections and tweaks to documentation
4b2e62600 add more examples
1ea258c6a update spiffworkflow concepts
851d7cdf6 fix a few bugs I found while testing the example repo
7a0a6bdf8 update bpmn docs
07c153f2d save/restore nested subprocess tests
340e9983b Merge branch 'main' of github.com:sartography/spiffworkflow into main
618afbc59 It is rare to submit an update that touches upon both religion and the origins of the universe. I think, for the sake of supporting all view points we must offer the possibility that there can be a thing that is not a child, but rather the beginning of all childen, that there is a chicken to the first egg, a single original big bank.
a68dec77e use raw strings for regexes using escape sequences w/ burnettk
4644f2810 Merge pull request #329 from sartography/task/remove-deprecated-functions
ca65602c0 correct typo in filename
39ab83f1f remove one deprecated and unused feature
23d54e524 Merge pull request #328 from sartography/improvement/task-spec-attributes
544614aa9 change dmn bpmn_id method to property
12ad185a4 update bpmnworkflow.waiting_events to use classname
aec77097d fix some typos & add a few missing licenses
4b87c6d0c add some changes that didn't get included in the merge commit
965a5d4e1 Merge branch 'main' into improvement/task-spec-attributes
a844b34f9 alternate bomnworkflow.cancel
0a455cdd2 Merge pull request #327 from sartography/feature/mark_tasks_in_sub_workflows_as_future_if_reseting_to_a_task_before_subworkflow
2bda992aa cancel tasks in subprocesses and return cancelled tasks
309937362 take account that we reset the parent when checking all sub-process executions.
d4bcf1290 handle nested subprocesses when resetting tasks
032bedea6 reset subprocess task when resetting a task inside the subprocess
3a6abe157 change reset workflow to drop tasks and re-predict
e9cd65757 move exceptions for bpmn into bpmn package
e654f2ff1 add bpmn_id and bpmn_name attributes to task specs
74bb9cf1a Found that tasks within a sub-workflow were left in a state of "READY" after resetting to task before the sub-workflow.
957a8faec make all task specs in bpmn processes bpmn tasks
b6070005c create actual mixin classes & improve package structure
666a9e4e5 Merge pull request #326 from sartography/feature/boundary_event_reset_fix
9fe5ae4ad Whenever a task is reset who's parent is a "_ParentBoundaryEvent" class, reset to that parent boundary event instead, and execute it, so that all the boundary events are reset to the correct point as well.
fbc071af5 remove 'is_engine_step' and use existing 'manual' attribute instead
0d8e53a25 remove unused attributes, minor parser improvements
6ae98b585 Merge pull request #325 from sartography/bugfix/make-data-objects-available-to-gateways
cefcd3733 make data objects available to gateways
6060fe778 Merge pull request #324 from sartography/task/update-license
efa24bed2 update license
56271f7f7 Merge pull request #323 from sartography/bugfix/handle-dash-in-dmn
6de4e7e01 Merge pull request #322 from sartography/improvement/remove-celery
6ee0668cb remove unnecessary dependencies in test
7ceae68c2 change literal '-' in DMN input to None
4cffc7e7a remove celery task and dependency
580d6e516 Merge pull request #321 from sartography/improvement/allow-duplicate-subprocess-names
e4440d4df remove legacy signavio parser
477a23184 remove absolute imports from tests failing in CI
15a812a92 use process ids only when storing process specs
abaf1b9e9 move parallel gateway tests to their own package
29fd2d0d9 remove some redundant, unused, or unnecessary tests & consolidate others
fda1480bc remove unused CORRELATE attribute from tests
21a2fdbee remove signavio files
299c2613c Merge pull request #320 from sartography/parser_funcs
01afc9f6e PR feedback
646737834 Cleanup
dfd3f8214 Add same methods for dmn
764e33ccd Rename file, fix tests
9646abca4 Add bpmn in memory parser functions and tests
58f6bd317 Merge pull request #319 from sartography/feature/better_task_order_for_sub_processes
fd7c9308f By swapping the order of these lines, we can assure that a call activity is returned BEFORE the tasks that it contains, rather than after it.
0a7ec19d6 Merge pull request #318 from sartography/feature/optionally-skip-call-activities-when-parsing
3430a2e9f add option to skip parsing call activities
1b1da1dd2 Merge pull request #317 from sartography/bugfix/non-bpmn-tutorial
e82345d68 remove some bpmn-related stuff from core serializer
6f9bc279c use name for inputs/outputs in base serializer -- not sure why this was ever changed

git-subtree-dir: SpiffWorkflow
git-subtree-split: 01a25fc3f829786c4b65d19fd0fda408de37c79f
2023-05-29 17:31:34 -04:00

133 lines
6.7 KiB
ReStructuredText

Implementing a Custom Task Spec
-------------------------------
Suppose we wanted to manage Timer Start Events outside of SpiffWorkflow. If we have a process loaded up and running that
starts with a timer, the timer waits until the event occurs; this might be days or weeks later.
Of course, we can always check that it's waiting and serialize the workflow until that time. However, we might decide that
we don't want SpiffWorkflow to manage this at all. We could do this with a custom task spec.
First we'll create a new class
.. code:: python
from SpiffWorkflow.bpmn.specs.event_definitions import TimerEventDefinition, NoneEventDefinition
from SpiffWorkflow.bpmn.specs.mixins.events.start_event import StartEvent
from SpiffWorkflow.spiff.specs.spiff_task import SpiffBpmnTask
class CustomStartEvent(StartEvent, SpiffBpmnTask):
def __init__(self, wf_spec, bpmn_id, event_definition, **kwargs):
if isinstance(event_definition, TimerEventDefinition):
super().__init__(wf_spec, bpmn_id, NoneEventDefinition(), **kwargs)
self.timer_event = event_definition
else:
super().__init__(wf_spec, bpmn_id, event_definition, **kwargs)
self.timer_event = None
When we create our custom event, we'll check to see if we're creating a Start Event with a TimerEventDefinition, and if so,
we'll replace it with a NoneEventDefinition.
.. note::
Our class inherits from two classes. We import a mixin class that defines generic BPMN Start Event behavior from
:code:`StartEvent` in the :code:`bpmn` package and the :code:`SpiffBpmnTask` from the :code:`spiff` package, which
extends the default :code:`BpmnSpecMixin`.
We've split the basic behavior for specific BPMN tasks from the :code:`BpmnSpecMixin` to make it easier to extend
them without running into MRO issues.
In general, if you implement a custom task spec, you'll need to inherit from bases of both categories.
Whenever we create a custom task spec, we'll need to create a converter for it so that it can be serialized.
.. code:: python
from SpiffWorkflow.bpmn.serializer.workflow import BpmnWorkflowSerializer
from SpiffWorkflow.bpmn.serializer.task_spec import StartEventConverter
from SpiffWorkflow.spiff.serializer.task_spec import SpiffBpmnTaskConverter
from SpiffWorkflow.spiff.serializer.config import SPIFF_SPEC_CONFIG
class CustomStartEventConverter(SpiffBpmnTaskConverter):
def __init__(self, registry):
super().__init__(CustomStartEvent, registry)
def to_dict(self, spec):
dct = super().to_dict(spec)
if spec.timer_event is not None:
dct['event_definition'] = self.registry.convert(spec.timer_event)
else:
dct['event_definition'] = self.registry.convert(spec.event_definition)
return dct
SPIFF_SPEC_CONFIG['task_specs'].remove(StartEventConverter)
SPIFF_SPEC_CONFIG['task_specs'].append(CustomStartEventConverter)
wf_spec_converter = BpmnWorkflowSerializer.configure_workflow_spec_converter(SPIFF_SPEC_CONFIG)
serializer = BpmnWorkflowSerializer(wf_spec_converter)
Our converter will inherit from the :code:`SpiffBpmnTaskConverter`, since that's our base generic BPMN mixin class.
The :code:`SpiffBpmnTaskConverter` ultimately inherits from
:code:`SpiffWorkflow.bpmn.serializer.helpers.task_spec.BpmnTaskSpecConverter`. which provides some helper methods for
extracting standard attributes from tasks; the :code:`SpiffBpmnTaskConverter` does the same for extensions from the
:code:`spiff` package.
We don't have to do much -- all we do is replace the event definition with the original. The timer event will be
moved when the task is restored.
.. note::
It might be better have the class's init method take both the event definition to use *and* the timer event
definition. Unfortunately, our parser is not terribly intuitive or easily extendable, so I've done it this
way to make this a little easier to follow.
When we create our serializer, we need to tell it about this task. We'll remove the converter for the standard Start
Event and add the one we created to the confiuration and create the :code:`workflow_spec_converter` from the updated
config.
.. note::
We have not instantiated our converter class. When we call :code:`configure_workflow_spec_converter` with a
configuration (which is essentially a list of classes, split up into sections for organizational purposes),
*it* instantiates the classes for us, using the same `registry` for every class. At the end of the configuration
if returns this registry, which now knows about all of the classes that will be used for SpiffWorkflow
specifications. It is possible to pass a separately created :code:`DictionaryConverter` preconfigured with
other converters; in that case, it will be used as the base `registry`, to which specification conversions will
be added.
Because we've built up the `registry` in such a way, we can make use of the :code:`registry.convert` and
:code:`registry.restore` methods rather than figuring out how to serialize them. We can use these methods on any
objects that SpiffWorkflow knows about.
See :doc:`advanced` for more information about the serializer.
Finally, we have to update our parser:
.. code:: python
from SpiffWorkflow.spiff.parser.event_parsers import StartEventParser
from SpiffWorkflow.spiff.parser.process import SpiffBpmnParser
from SpiffWorkflow.bpmn.parser.util import full_tag
parser = SpiffBpmnParser()
parser.OVERRIDE_PARSER_CLASSES[full_tag('startEvent')] = (StartEventParser, CustomStartEvent)
The parser contains class attributes that define how to parse a particular element and the class that should be used to
create the task spec, so rather than pass these in as arguments, we create a parser and then update the values it
will use. This is a bit unintuitive, but that's how it works.
Fortunately, we were able to reuse an existing Task Spec parser, which simplifies the process quite a bit.
Having created a parser and serializer, we could replace the ones we pass in the the :code:`SimpleBpmnRunner` with these.
I am going to leave creating a script that makes use of them to readers of this document, as it should be clear enough
how to do.
There is a very simple diagram `bpmn/tutorial/timer_start.bpmn` with the process ID `timer_start` with a Start Event
with a Duration Timer of one day that can be used to illustrate how the custom task works. If you run this workflow
with `spiff-bpmn-runner.py`, you'll see a `WAITING` Start Event; if you use the parser and serializer we just created,
you'll be propmted to complete the User Task immediately.