-
Notifications
You must be signed in to change notification settings - Fork 54
Tutorial. Examples of usage
Our package allows to create a BPMNDiagramGraph instance manually (not only by exporting XML file) by creating a Python script. By now, the user is able to create basic BPMN element, such as start, stop events, tasks, subprocesses or gateways (inclusive, exclusive, parallel). This page presents two samples of manula diagram generation.
This part shows an example of simple BPMN diagram generated using methods provided by this package. It will be similar to examples used created in BPMN, editors described in section [Examples of BPMN diagrams](Examples of BPMN diagrams).
Remember about importing package to your script:
import bpmn_python.bpmn_diagram_rep as diagram
Create a BPMNDiagramGraph object:
bpmn_graph = diagram.BPMNDiagramGraph()
bpmn_graph.create_new_diagram_graph(diagram_name="diagram1")
Using add_start_event_to_diagram and add_task_to_diagram, create a new start event and task. Then, connect those elements with sequence flow.
User is allowed to change few attributes of created elements - for each element created in this example a name will be given (by default name is an empty string).
Notice that all of the methods used to add new elements into diagram returns two objects - a string representing an ID of a new element and a reference to object representing this element. Since this example requires only an ID of element, we skip the object reference using anonymous variable (denoted by _).
[start_id, _] = bpmn_graph.add_start_event_to_diagram(start_event_name="start_event")
[task1_id, _] = bpmn_graph.add_task_to_diagram(task_name="task1")
bpmn_graph.add_sequence_flow_to_diagram(start_id, task1_id, "start_to_one")
Now you can create an exclusive gateway (fork and join element) with two tasks:
[exclusive_gate_fork_id, _] = bpmn_graph.add_exclusive_gateway_to_diagram(gateway_name="exclusive_gate_fork")
[task1_ex_id, _] = bpmn_graph.add_task_to_diagram(task_name="task1_ex")
[task2_ex_id, _] = bpmn_graph.add_task_to_diagram(task_name="task2_ex")
[exclusive_gate_join_id, _] = bpmn_graph.add_exclusive_gateway_to_diagram(gateway_name="exclusive_gate_join")
bpmn_graph.add_sequence_flow_to_diagram(task1_id, exclusive_gate_fork_id, "one_to_ex_fork")
bpmn_graph.add_sequence_flow_to_diagram(exclusive_gate_fork_id, task1_ex_id, "ex_fork_to_ex_one")
bpmn_graph.add_sequence_flow_to_diagram(exclusive_gate_fork_id, task2_ex_id, "ex_fork_to_ex_two")
bpmn_graph.add_sequence_flow_to_diagram(task1_ex_id, exclusive_gate_join_id, "ex_one_to_ex_join")
bpmn_graph.add_sequence_flow_to_diagram(task2_ex_id, exclusive_gate_join_id, "ex_two_to_ex_join")
And finally, add second task and end event:
[task2_id, _] = bpmn_graph.add_task_to_diagram(task_name="task2")
[end_id, _] = bpmn_graph.add_end_event_to_diagram(end_event_name="end_event")
bpmn_graph.add_sequence_flow_to_diagram(exclusive_gate_join_id, task2_id, "ex_join_to_two")
bpmn_graph.add_sequence_flow_to_diagram(task2_id, end_id, "two_to_end")
Full example can be found in file manually-generated-simple-diagram.py, included in tests directory.
Visualization of generated diagram (using bpmn.io):
- This is not a pure effect of importing generated XML file into editor. Since we don't have a refined layouting algorithm, all of the elements are entangled and actual diagram looks like a mess. We present this example to show correctness of generated process. Generation of diagram interchange data isn't working yet.
A more complex example includes additional subprocess element and all three types of gateways handled by the package (inclusive, exclusive, parallel).
Since the former example has been described step-by-step, current example will be presented as a single piece of code:
bpmn_graph = diagram.BPMNDiagramGraph()
bpmn_graph.create_new_diagram_graph(diagram_name="diagram1")
[start_id, _] = bpmn_graph.add_start_event_to_diagram(start_event_name="start_event")
[task1_id, _] = bpmn_graph.add_task_to_diagram(task_name="First task")
[subprocess1_id, _] = bpmn_graph.add_subprocess_to_diagram(subprocess_name="Subprocess")
bpmn_graph.add_sequence_flow_to_diagram(start_id, task1_id)
bpmn_graph.add_sequence_flow_to_diagram(task1_id, subprocess1_id)
[parallel_gate_fork_id, _] = bpmn_graph.add_parallel_gateway_to_diagram(gateway_name="parallel_gate_fork")
[task1_par_id, _] = bpmn_graph.add_task_to_diagram(task_name="task1_par")
[task2_par_id, _] = bpmn_graph.add_task_to_diagram(task_name="task2_par")
[parallel_gate_join_id, _] = bpmn_graph.add_parallel_gateway_to_diagram(gateway_name="parallel_gate_join")
bpmn_graph.add_sequence_flow_to_diagram(subprocess1_id, parallel_gate_fork_id)
bpmn_graph.add_sequence_flow_to_diagram(parallel_gate_fork_id, task1_par_id)
bpmn_graph.add_sequence_flow_to_diagram(parallel_gate_fork_id, task2_par_id)
bpmn_graph.add_sequence_flow_to_diagram(task1_par_id, parallel_gate_join_id)
bpmn_graph.add_sequence_flow_to_diagram(task2_par_id, parallel_gate_join_id)
[exclusive_gate_fork_id, _] = bpmn_graph.add_exclusive_gateway_to_diagram(gateway_name="exclusive_gate_fork")
[task1_ex_id, _] = bpmn_graph.add_task_to_diagram(task_name="task1_ex")
[task2_ex_id, _] = bpmn_graph.add_task_to_diagram(task_name="task2_ex")
[exclusive_gate_join_id, _] = bpmn_graph.add_exclusive_gateway_to_diagram(gateway_name="exclusive_gate_join")
bpmn_graph.add_sequence_flow_to_diagram(parallel_gate_join_id, exclusive_gate_fork_id)
bpmn_graph.add_sequence_flow_to_diagram(exclusive_gate_fork_id, task1_ex_id)
bpmn_graph.add_sequence_flow_to_diagram(exclusive_gate_fork_id, task2_ex_id)
bpmn_graph.add_sequence_flow_to_diagram(task1_ex_id, exclusive_gate_join_id)
bpmn_graph.add_sequence_flow_to_diagram(task2_ex_id, exclusive_gate_join_id)
[inclusive_gate_fork_id, _] = bpmn_graph.add_inclusive_gateway_to_diagram(gateway_name="inclusive_gate_fork")
[task1_in_id, _] = bpmn_graph.add_task_to_diagram(task_name="task1_in")
[task2_in_id, _] = bpmn_graph.add_task_to_diagram(task_name="task2_in")
[inclusive_gate_join_id, _] = bpmn_graph.add_inclusive_gateway_to_diagram(gateway_name="inclusive_gate_join")
bpmn_graph.add_sequence_flow_to_diagram(exclusive_gate_join_id, inclusive_gate_fork_id)
bpmn_graph.add_sequence_flow_to_diagram(inclusive_gate_fork_id, task1_in_id)
bpmn_graph.add_sequence_flow_to_diagram(inclusive_gate_fork_id, task2_in_id)
bpmn_graph.add_sequence_flow_to_diagram(task1_in_id, inclusive_gate_join_id)
bpmn_graph.add_sequence_flow_to_diagram(task2_in_id, inclusive_gate_join_id)
[end_id, _] = bpmn_graph.add_end_event_to_diagram(end_event_name="end_event")
bpmn_graph.add_sequence_flow_to_diagram(inclusive_gate_join_id, end_id)
bpmn_graph.export_xml_file(self.output_directory, self.output_file_with_di)
bpmn_graph.export_xml_file_no_di(self.output_directory, self.output_file_no_di)
visualizer.visualize_diagram(bpmn_graph)
visualizer.bpmn_diagram_to_dot_file(bpmn_graph, self.output_directory + self.output_dot_file)
visualizer.bpmn_diagram_to_png(bpmn_graph, self.output_directory + self.output_png_file)
Full example can be found in file manually_generated_complex_diagram.py, included in tests directory.
Visualization of generated diagram (using bpmn.io):