Executing BPMN 2.0 Workflows in Python Matt Hampton PYCONZA 2012 1 Outline • A Tale of Woe • Business Process Model and Notation • SpiffWorkflow o And the Workflow Patterns initiative • SpiffWorkflow + BPMN • Recommended Use Case •Q&A 2 Outline • A Tale of Woe • Business Process Model and Notation • SpiffWorkflow o And the Workflow Patterns initiative • SpiffWorkflow + BPMN • Recommended Use Case •Q&A 3 But First - Some Context 4 Permit to Work 5 j5 Interface 6 j5 Interface 7 The Flow Chart 8 Completing a Permit Version 2 Version 1 9 def get_close_button_rights(self): """Whether or not the user has the right to click the Close (Isolations Removed) button""" if self.status in ('Complete','Reactivated','Closed (Isolations Maintained)'): session = RequestStack.request_stack.session if session: if self.approved_by == session.user: permit_work_table = Alchemy.find_recordclass("permit_work") with Alchemy.closing(self.session_class()) as sa_session: associated_permits = sa_session.query(permit_work_table).filter(sqlalchemy.and_(permit_wor if self.master_permit == 'Yes': if not associated_permits and (self.num_isolation_points in (0, '0') or self.points_de if self.gs_required == u"Yes": if self.gs_test_due == u"False" or self.gs_button_clicked == "Yes": return 'True' else: return 'True' elif (not self.num_isolation_points in (0, '0') or associated_permits) and self.check_ if self.gs_required == u"Yes": if self.gs_test_due == u"False" or self.gs_button_clicked == "Yes": return 'True' else: return 'True' elif (self.points_deisolated == 'True' or self.category in ("Vehicle", "Lifting")): if self.gs_required == u"Yes": if self.gs_test_due == u"False" or self.gs_button_clicked == "Yes": return 'True' else: return 'True' elif self.status == 'Incomplete': #If the status is Incomplete this button is a Reissue button session = RequestStack.request_stack.session if session: if self.approved_by == session.user: return 'True' return 'False' close_button_rights = property(get_close_button_rights) 10 Gratuitous Gang of Four Quote • Use the State pattern if: o 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. o Hmmm. 11 Why not State Pattern? Version 3 Hmmmm…. 12 OMG! 13 OMG! 14 OMG(!) BPMN • Object Management Group • Business Process Modelling Notation (v1) o Business Friendly notation in wide use o Unambiguous semantics o Clear analysis / communication of requirements o Asking the right questions • Business Process Model and Notation (v2) o Executable Process Definitions o http://www.bpmn.org 15 16 Start Event 17 User Task Script Task 18 Pool Lane Lane 19 Exclusive Gateway (Diverging) 20 Parallel Gateway (Diverging) 21 Intermediate Catching Event (Timer) 22 Call Activity 23 24 Boundary Event (Non-interrupting) 25 End Event (Terminating) End Event 26 Annotations for Clarity Facilitates Top-Down Decomposition of Complex Processes 27 Other BPMN Features • Exception Handling • Compensation • Data Objects • Multiple Instances / Looping 28 Outline • A Tale of Woe • Business Process Model and Notation • SpiffWorkflow o And the Workflow Patterns initiative • SpiffWorkflow + BPMN • Recommended Use Case •Q&A 29 SpiffWorkflow • Pure Python Library • Based on the Worklow Patterns initiative o http://www.workflowpatterns.com • Build a ‘Workflow Spec’ • Create a ‘Workflow’ instance • API for getting a list of READY / WAITING tasks o Completing a task, moves the workflow forward • Maintained by Samuel Abels 30 class MyWorkflowSpec(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) 31 Control Flow Features • Sequence • Parallel Split • Exclusive Choice • Simple Merge • Multi-Choice • Structured Synchronizing Merge • ….. 32 Outline • A Tale of Woe • Business Process Model and Notation • SpiffWorkflow o And the Workflow Patterns initiative • SpiffWorkflow + BPMN • Recommended Use Case •Q&A 33 SpiffWorkflow + BPMN • Pluggable Parser • Extensible Task Spec classes o Allows the use of BPMN extension elements • Pluggable Script Engine o Executes script tasks o Evaluates expressions • Database-friendly Workflow Serialization 34 Supported BPMN Features • Call Activity • Start Event • End Event (including interrupting) • User and Manual Tasks • Script Task 35 Supported BPMN Features • Exclusive Gateway • Parallel Gateway • Intermediate Catch Events (Timer and Message) • Boundary Events (Timer and Message, interrupting and non-interrupting) 36 37 38 39 40 41 Eclipse BPMN2 Modeler 42 Gemsbok (ex-Signavio) Custom ‘Stencil Set’ Custom Properties 43 Project Status • BPMN extension is currently in a fork: o https://github.com/matthewhampton/SpiffWorkflow o Should be merged in to the base project in the next month • Gemsbok is here: o https://github.com/matthewhampton/Gemsbok • Eclipse BPMN2 Modeler is here: o http://eclipse.org/bpmn2-modeler 44 Outline • A Tale of Woe • Business Process Model and Notation • SpiffWorkflow o And the Workflow Patterns initiative • SpiffWorkflow + BPMN • Recommended Use Case •Q&A 45 Is BPMN for you? • Small number of states? o Use the State Pattern, or a similar OO decomposition • Single-threaded & No Actions on Transitions o Table-based State Machine • Complex workflows? • Getting tied up in specification round trips? • Like Pretty Pictures? OMG BPMN! 46 Outline • A Tale of Woe • Business Process Model and Notation • SpiffWorkflow o And the Workflow Patterns initiative • SpiffWorkflow + BPMN • Recommended Use Case •Q&A 47 48 49