|
| 1 | +''' |
| 2 | +PM4Py – A Process Mining Library for Python |
| 3 | +Copyright (C) 2026 Process Intelligence Solutions GmbH |
| 4 | +
|
| 5 | +This program is free software: you can redistribute it and/or modify |
| 6 | +it under the terms of the GNU Affero General Public License as |
| 7 | +published by the Free Software Foundation, either version 3 of the |
| 8 | +License, or any later version. |
| 9 | +
|
| 10 | +This program is distributed in the hope that it will be useful, |
| 11 | +but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | +GNU Affero General Public License for more details. |
| 14 | +
|
| 15 | +You should have received a copy of the GNU Affero General Public License |
| 16 | +along with this program. If not, see this software project's root or |
| 17 | +visit <https://www.gnu.org/licenses/>. |
| 18 | +
|
| 19 | +Website: https://processintelligence.solutions |
| 20 | +Contact: info@processintelligence.solutions |
| 21 | +''' |
| 22 | +"""Convert :class:`WorkingGraph` into a pm4py :class:`BPMN`. |
| 23 | +
|
| 24 | +Self-loops detected during the loops phase are reattached here by |
| 25 | +wrapping the looped task with an XOR-join (predecessor side) and an |
| 26 | +XOR-split (successor side) that connects back to the join. |
| 27 | +""" |
| 28 | +from typing import Any, Dict, Optional |
| 29 | + |
| 30 | +from pm4py.algo.discovery.split_miner.bpmn_export.abc import BPMNExporter |
| 31 | +from pm4py.algo.discovery.split_miner.dtypes.log import END_LABEL, START_LABEL |
| 32 | +from pm4py.algo.discovery.split_miner.dtypes.working_graph import WorkingGraph |
| 33 | +from pm4py.objects.bpmn.obj import BPMN |
| 34 | + |
| 35 | + |
| 36 | +def _make_node(kind: str, label: str, node_id: str) -> BPMN.BPMNNode: |
| 37 | + if kind == "start": |
| 38 | + return BPMN.StartEvent(id=node_id, name="") |
| 39 | + if kind == "end": |
| 40 | + return BPMN.EndEvent(id=node_id, name="") |
| 41 | + if kind == "task": |
| 42 | + return BPMN.Task(id=node_id, name=label) |
| 43 | + if kind == "xor": |
| 44 | + return BPMN.ExclusiveGateway(id=node_id, name="") |
| 45 | + if kind == "and": |
| 46 | + return BPMN.ParallelGateway(id=node_id, name="") |
| 47 | + if kind == "or": |
| 48 | + return BPMN.InclusiveGateway(id=node_id, name="") |
| 49 | + raise ValueError(f"Unknown node kind: {kind}") |
| 50 | + |
| 51 | + |
| 52 | +class ClassicBPMNExporter(BPMNExporter): |
| 53 | + """Materialise the pm4py :class:`BPMN` from the working graph.""" |
| 54 | + |
| 55 | + @classmethod |
| 56 | + def apply( |
| 57 | + cls, |
| 58 | + wg: WorkingGraph, |
| 59 | + parameters: Optional[Dict[str, Any]] = None, |
| 60 | + ) -> BPMN: |
| 61 | + bpmn = BPMN() |
| 62 | + node_map: Dict[str, BPMN.BPMNNode] = {} |
| 63 | + for nid, n in wg.nodes.items(): |
| 64 | + bnode = _make_node(n.kind, n.label, nid) |
| 65 | + bpmn.add_node(bnode) |
| 66 | + node_map[nid] = bnode |
| 67 | + |
| 68 | + for src, tgt in wg.edges(): |
| 69 | + bpmn.add_flow( |
| 70 | + BPMN.SequenceFlow(node_map[src], node_map[tgt]) |
| 71 | + ) |
| 72 | + |
| 73 | + # Sort to keep self-loop attachment order independent of |
| 74 | + # hash randomization; semantically the model is the same, but |
| 75 | + # node/flow ids and rendering order are then reproducible. |
| 76 | + for task_id in sorted(wg.self_loops, reverse=True): |
| 77 | + if task_id not in node_map: |
| 78 | + continue |
| 79 | + if task_id in {START_LABEL, END_LABEL}: |
| 80 | + continue |
| 81 | + cls._attach_self_loop(bpmn, node_map, task_id) |
| 82 | + return bpmn |
| 83 | + |
| 84 | + # ------------------------------------------------------------------ |
| 85 | + # helpers |
| 86 | + # ------------------------------------------------------------------ |
| 87 | + |
| 88 | + @staticmethod |
| 89 | + def _attach_self_loop( |
| 90 | + bpmn: BPMN, |
| 91 | + node_map: Dict[str, BPMN.BPMNNode], |
| 92 | + task_id: str, |
| 93 | + ) -> None: |
| 94 | + task_node = node_map[task_id] |
| 95 | + in_flows = [ |
| 96 | + f for f in bpmn.get_flows() if f.get_target() is task_node |
| 97 | + ] |
| 98 | + out_flows = [ |
| 99 | + f for f in bpmn.get_flows() if f.get_source() is task_node |
| 100 | + ] |
| 101 | + |
| 102 | + loop_join = BPMN.ExclusiveGateway(id=f"{task_id}__loop_join", name="") |
| 103 | + loop_split = BPMN.ExclusiveGateway(id=f"{task_id}__loop_split", name="") |
| 104 | + bpmn.add_node(loop_join) |
| 105 | + bpmn.add_node(loop_split) |
| 106 | + |
| 107 | + for f in in_flows: |
| 108 | + src = f.get_source() |
| 109 | + bpmn.remove_flow(f) |
| 110 | + bpmn.add_flow(BPMN.SequenceFlow(src, loop_join)) |
| 111 | + for f in out_flows: |
| 112 | + tgt = f.get_target() |
| 113 | + bpmn.remove_flow(f) |
| 114 | + bpmn.add_flow(BPMN.SequenceFlow(loop_split, tgt)) |
| 115 | + |
| 116 | + bpmn.add_flow(BPMN.SequenceFlow(loop_join, task_node)) |
| 117 | + bpmn.add_flow(BPMN.SequenceFlow(task_node, loop_split)) |
| 118 | + bpmn.add_flow(BPMN.SequenceFlow(loop_split, loop_join)) |
0 commit comments