570 likes | 1.28k Views
Executing BPMN 2.0 Workflows in Python. Matt Hampton PYCONZA 2012. Outline. A Tale of Woe Business Process Model and Notation SpiffWorkflow And the Workflow Patterns initiative SpiffWorkflow + BPMN Recommended Use Case Q & A. Outline. A Tale of Woe
E N D
Executing BPMN 2.0 Workflows in Python Matt Hampton PYCONZA 2012
Outline • A Tale of Woe • Business Process Model and Notation • SpiffWorkflow • And the Workflow Patterns initiative • SpiffWorkflow + BPMN • Recommended Use Case • Q & A
Outline • A Tale of Woe • Business Process Model and Notation • SpiffWorkflow • And the Workflow Patterns initiative • SpiffWorkflow + BPMN • Recommended Use Case • Q & A
Completing a Permit Version 2 Version 1
defget_close_button_rights(self): """Whether or not the user has the right to click the Close (Isolations Removed) button""" ifself.statusin ('Complete','Reactivated','Closed (Isolations Maintained)'): session =RequestStack.request_stack.session if session: ifself.approved_by==session.user: permit_work_table=Alchemy.find_recordclass("permit_work") withAlchemy.closing(self.session_class()) assa_session: associated_permits=sa_session.query(permit_work_table).filter(sqlalchemy.and_(permit_work_table.config_logid==self.config_logid,permit_work_table.master_permit=='No', permit_work_table.status=='Closed (Isolations Maintained)')).all() ifself.master_permit=='Yes': ifnotassociated_permitsand (self.num_isolation_pointsin (0, '0') orself.points_deisolated=='True') and (self.check_list_reactivate_complete=='True'orself.num_spec_reactivate_questionsin (0, '0')): ifself.gs_required==u"Yes": ifself.gs_test_due==u"False"orself.gs_button_clicked=="Yes": return'True' else: return'True' elif (notself.num_isolation_pointsin (0, '0') orassociated_permits) andself.check_list_reactivate_complete=='False'andnotself.num_spec_reactivate_questionsin (0, '0'): ifself.gs_required==u"Yes": ifself.gs_test_due==u"False"orself.gs_button_clicked=="Yes": return'True' else: return'True' elif (self.points_deisolated=='True'orself.categoryin ("Vehicle", "Lifting")): ifself.gs_required==u"Yes": ifself.gs_test_due==u"False"orself.gs_button_clicked=="Yes": return'True' else: return'True' elifself.status=='Incomplete': #If the status is Incomplete this button is a Reissue button session =RequestStack.request_stack.session if session: ifself.approved_by==session.user: return'True' return'False' close_button_rights=property(get_close_button_rights)
Gratuitous Gang of Four Quote • Use the State pattern if: • Operations have large, multipart conditional statements that depend on the object’s state. The state is usually represented by one or more enumerated constants. Often, several operations will contain this same conditional structure. • Hmmm.
Why not State Pattern? Version 3 Hmmmm….
OMG(!) BPMN • Object Management Group • Business Process Modelling Notation (v1) • Business Friendly notation in wide use • Unambiguous semantics • Clear analysis / communication of requirements • Asking the right questions • Business Process Model and Notation (v2) • Executable Process Definitions • http://www.bpmn.org
User Task Script Task
Lane Pool Lane
End Event (Terminating) End Event
Annotations for Clarity Facilitates Top-Down Decomposition of Complex Processes
Other BPMN Features • Exception Handling • Compensation • Data Objects • Multiple Instances / Looping
Outline • A Tale of Woe • Business Process Model and Notation • SpiffWorkflow • And the Workflow Patterns initiative • SpiffWorkflow + BPMN • Recommended Use Case • Q & A
SpiffWorkflow • Pure Python Library • Based on the Worklow Patterns initiative • http://www.workflowpatterns.com • Build a ‘Workflow Spec’ • Create a ‘Workflow’ instance • API for getting a list of READY / WAITING tasks • Completing a task, moves the workflow forward • Maintained by Samuel Abels
classMyWorkflowSpec(WorkflowSpec): def__init__(self): WorkflowSpec.__init__(self) # Build one branch. a1 = Simple(self, 'task_a1') self.start.connect(a1) a2 = Simple(self, 'task_a2') a1.connect(a2) # Build another branch. b1 = Simple(self, 'task_b1') self.start.connect(b1) b2 = Simple(self, 'task_b2') b1.connect(b2) # Merge both branches (synchronized). synch_1 = Join(self, 'synch_1') a2.connect(synch_1) b2.connect(synch_1) # If-condition: excl_choice_1 =ExclusiveChoice(self, 'excl_choice_1') synch_1.connect(excl_choice_1) c1 = Simple(self, 'task_c1') excl_choice_1.connect(c1) c2 = Simple(self, 'task_c2') cond= Equal(Attrib('test_attribute1'), Attrib('test_attribute2')) excl_choice_1.connect_if(cond, c2) c3 = Simple(self, 'task_c3') excl_choice_1.connect_if(cond, c3)
Control Flow Features • Sequence • Parallel Split • Exclusive Choice • Simple Merge • Multi-Choice • Structured Synchronizing Merge • …..
Outline • A Tale of Woe • Business Process Model and Notation • SpiffWorkflow • And the Workflow Patterns initiative • SpiffWorkflow + BPMN • Recommended Use Case • Q & A
SpiffWorkflow + BPMN • Pluggable Parser • Extensible Task Spec classes • Allows the use of BPMN extension elements • Pluggable Script Engine • Executes script tasks • Evaluates expressions • Database-friendly Workflow Serialization
Supported BPMN Features • Call Activity • Start Event • End Event (including interrupting) • User and Manual Tasks • Script Task
Supported BPMN Features • Exclusive Gateway • Parallel Gateway • Intermediate Catch Events (Timer and Message) • Boundary Events (Timer and Message, interrupting and non-interrupting)
Gemsbok (ex-Signavio) Custom ‘Stencil Set’ Custom Properties
Project Status • BPMN extension is currently in a fork: • https://github.com/matthewhampton/SpiffWorkflow • Should be merged in to the base project in the next month • Gemsbok is here: • https://github.com/matthewhampton/Gemsbok • Eclipse BPMN2 Modeler is here: • http://eclipse.org/bpmn2-modeler
Outline • A Tale of Woe • Business Process Model and Notation • SpiffWorkflow • And the Workflow Patterns initiative • SpiffWorkflow + BPMN • Recommended Use Case • Q & A
Is BPMN for you? • Small number of states? • Use the State Pattern, or a similar OO decomposition • Single-threaded & No Actions on Transitions • Table-based State Machine • Complex workflows? • Getting tied up in specification round trips? • Like Pretty Pictures? OMG BPMN!
Outline • A Tale of Woe • Business Process Model and Notation • SpiffWorkflow • And the Workflow Patterns initiative • SpiffWorkflow + BPMN • Recommended Use Case • Q & A