Process trees can be learned with the Inductive Miner, converted using BPMN support, and explored through extensive playout.
PM4Py offers support for process trees, including visualization, conversion to Petri nets, log generation, importing/exporting, and tree generation functionality. This section outlines these features.
PM4Py supports importing and exporting process trees in the PTML format. Use the following code to import a process tree from a PTML file:
import pm4py
from pm4py.objects.process_tree.obj import ProcessTree
if __name__ == "__main__":
tree: ProcessTree = pm4py.read_ptml("tests/input_data/running-example.ptml")
Use the following code to export a process tree to a PTML file:
import pm4py
if __name__ == "__main__":
pm4py.write_ptml(tree, "running-example.ptml")The 'PTAndLogGenerator' approach, described in the paper 'PTAndLogGenerator: A Generator for Artificial Event Data', has been implemented in the PM4Py library. The following code snippet can be used to generate a process tree. Review the parameters below:
import pm4py
from pm4py.objects.process_tree.obj import ProcessTree
if __name__ == "__main__":
tree: ProcessTree = pm4py.generate_process_tree()
Below is a table explaining the parameters used in the process tree generation:
| Parameter | Meaning |
|---|---|
| MODE | Most frequent number of visible activities (default 20) |
| MIN | Minimum number of visible activities (default 10) |
| MAX | Maximum number of visible activities (default 30) |
| SEQUENCE | Probability of adding a sequence operator to the tree (default 0.25) |
| CHOICE | Probability of adding a choice operator to the tree (default 0.25) |
| PARALLEL | Probability of adding a parallel operator to the tree (default 0.25) |
| LOOP | Probability of adding a loop operator to the tree (default 0.25) |
| OR | Probability of adding an OR operator to the tree (default 0) |
| SILENT | Probability of adding silent activities to choice or loop operators (default 0.25) |
| DUPLICATE | Probability of duplicating an activity label (default 0) |
| LT_DEPENDENCY | Probability of adding random dependencies to the tree (default 0) |
| INFREQUENT | Probability of making a choice have infrequent paths (default 0.25) |
| NO_MODELS | Number of trees to generate from the model population (default 10) |
| UNFOLD | Whether to unfold loops to include choices in dependencies (0 = False, 1 = True). If LT_DEPENDENCY ≤ 0, this should always be 0 (False). If LT_DEPENDENCY > 0, this can be either 0 or 1 (True or False). (default 10) |
| MAX_REPEAT | Maximum number of loop repetitions (used only when unfolding is True) (default 10) |
Playout returns a legacy EventLog, whose length counts traces. Use pm4py.convert_to_dataframe to analyze the generated events with Pandas.
Use the following code snippet to generate a log with 100 cases from a process tree:
import pm4py
from pm4py.objects.log.obj import EventLog
if __name__ == "__main__":
log: EventLog = pm4py.play_out(tree)
print(len(log))1000Use the following code to convert a process tree into a Petri net:
import pm4py
from pm4py.objects.petri_net.obj import Marking, PetriNet
if __name__ == "__main__":
net: PetriNet
im: Marking
fm: Marking
net, im, fm = pm4py.convert_to_petri_net(tree)
A process tree can be printed as shown below:
if __name__ == "__main__":
print(tree)->( 'register request', *( ->( +( 'check ticket', X( 'examine thoroughly', 'examine casually' ) ), 'decide' ), 'reinitiate request' ), X( 'r …
… (excerpt)A process tree can also be visualized as follows:
import pm4py
if __name__ == "__main__":
pm4py.view_process_tree(tree, format='png')
We present an approach to convert a block-structured accepting Petri net into a process tree. The method is based on the work by van Zelst, Sebastiaan J., "Translating Workflow Nets to Process Trees: An Algorithmic Approach" (arXiv preprint arXiv:2004.08213, 2020).
The approach returns a process tree for block-structured Petri nets and raises an exception if the Petri net is not block-structured. Below is an example of this approach:
First, we load an XES log and discover an accepting Petri net using the Alpha Miner algorithm:
import pm4py
import os
import pandas
from pm4py.objects.petri_net.obj import Marking, PetriNet
if __name__ == "__main__":
log: pandas.DataFrame = pm4py.read_xes(os.path.join("tests", "input_data", "running-example.xes"))
net: PetriNet
im: Marking
fm: Marking
net, im, fm = pm4py.discover_petri_net_alpha(log)
Next, we convert this Petri net into a process tree:
import pm4py
from pm4py.objects.process_tree.obj import ProcessTree
if __name__ == "__main__":
tree: ProcessTree = pm4py.convert_to_process_tree(net, im, fm)
print(tree)->( 'register request', *( ->( +( 'check ticket', X( 'examine thoroughly', 'examine casually' ) ), 'decide' ), 'reinitiate request' ), X( 'r …
… (excerpt)The method succeeds since the accepting Petri net is block-structured and generates a process tree (which, in this case, coincidentally matches the process tree discovered by the inductive miner).
By default, a process tree does not include frequency or performance annotations. To optimally match a log against a process tree, you can use the alignments algorithm. The results of this algorithm provide a list of visited leaves/operators during replay, which can then be used to infer the frequency of each node at the case/event level in the process tree.
The following code can be used to add frequency annotations to the nodes of a process tree:
import pm4py
from pm4py.algo.conformance.alignments.process_tree.util import search_graph_pt_frequency_annotation
from typing import Any
if __name__ == "__main__":
aligned_traces: list[dict[str, Any]] = pm4py.conformance_diagnostics_alignments(log, tree)
tree = search_graph_pt_frequency_annotation.apply(tree, aligned_traces)A frequency-based visualization of the process tree is also available:
from pm4py.visualization.process_tree import visualizer as pt_visualizer
from graphviz import Graph
if __name__ == "__main__":
gviz: Graph = pt_visualizer.apply(tree, parameters={"format": "svg"}, variant=pt_visualizer.Variants.FREQUENCY_ANNOTATION)
pt_visualizer.view(gviz)