wasm4pm 26.6.25

High-performance process mining algorithms in WebAssembly for JavaScript/TypeScript
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
//! POWL to Process Tree conversion.
//!
//! Converts a POWL model (stored in a `PowlArena`) into a `PowlProcessTree`.
//!
//! Algorithm (mirrors `pm4py/objects/conversion/powl/variants/to_process_tree.py`):
//! 1. Transition leaf -> ProcessTree leaf with same label.
//! 2. OperatorPOWL -> ProcessTree internal node with same operator, recursed children.
//! 3. StrictPartialOrder ->
//!    a. Build a DAG from the partial order (with transitive reduction).
//!    b. Find connected components in the undirected version.
//!    c. For each component, perform BFS-level assignment (topological levelling).
//!    d. Nodes at the same level are wrapped in PARALLEL; levels are sequenced.
//!    e. Components themselves are wrapped in a top-level PARALLEL if > 1.
//! 4. DecisionGraph -> Same as StrictPartialOrder (uses the order relation).
use wasm4pm_compat::powl::{ChoiceGraph, ChoiceGraphNode};

use crate::powl_arena::{Operator, PowlArena, PowlNode};
use crate::powl_models::{PowlProcessTree, PtOperator};

// ─── Graph helpers ────────────────────────────────────────────────────────────

/// Simple directed adjacency list over `usize` node indices.
struct Dag {
    n: usize,
    /// adj[i] = list of successors of i
    adj: Vec<Vec<usize>>,
}

impl Dag {
    fn new(n: usize) -> Self {
        Dag {
            n,
            adj: vec![Vec::new(); n],
        }
    }

    fn add_edge(&mut self, from: usize, to: usize) {
        self.adj[from].push(to);
    }

    /// In-degrees of all nodes.
    fn in_degrees(&self) -> Vec<usize> {
        let mut deg = vec![0usize; self.n];
        for i in 0..self.n {
            for &j in &self.adj[i] {
                deg[j] += 1;
            }
        }
        deg
    }

    /// BFS level assignment starting from zero-in-degree nodes.
    fn assign_levels(&self) -> Vec<usize> {
        let in_deg = self.in_degrees();
        let mut levels = vec![usize::MAX; self.n];
        let mut queue = std::collections::VecDeque::new();
        for i in 0..self.n {
            if in_deg[i] == 0 {
                levels[i] = 0;
                queue.push_back(i);
            }
        }
        while let Some(cur) = queue.pop_front() {
            let next_level = levels[cur] + 1;
            for &succ in &self.adj[cur] {
                if levels[succ] == usize::MAX {
                    levels[succ] = next_level;
                    queue.push_back(succ);
                }
            }
        }
        levels
    }

    /// Transitive reduction: remove edge i->j if there is an alternative path i->...->j.
    fn transitive_reduction(&self) -> Dag {
        let n = self.n;
        // Compute reachability (closure) via DFS from each node.
        let reachable = {
            let mut reach = vec![vec![false; n]; n];
            for (start, adj_row) in self.adj.iter().enumerate() {
                let mut visited = vec![false; n];
                let mut stack = Vec::new();
                for &succ in adj_row {
                    stack.push(succ);
                }
                while let Some(cur) = stack.pop() {
                    if visited[cur] {
                        continue;
                    }
                    visited[cur] = true;
                    reach[start][cur] = true;
                    for &succ in &self.adj[cur] {
                        stack.push(succ);
                    }
                }
            }
            reach
        };

        let mut red = Dag::new(n);
        for i in 0..n {
            for &j in &self.adj[i] {
                // Keep edge i->j unless some intermediate k (k!=j) makes it redundant.
                let redundant = self.adj[i].iter().any(|&k| k != j && reachable[k][j]);
                if !redundant {
                    red.add_edge(i, j);
                }
            }
        }
        red
    }

    /// Connected components of the undirected version of this graph.
    fn undirected_components(&self) -> Vec<Vec<usize>> {
        let mut visited = vec![false; self.n];
        let mut components: Vec<Vec<usize>> = Vec::new();
        for start in 0..self.n {
            if visited[start] {
                continue;
            }
            let mut comp = Vec::new();
            let mut queue = std::collections::VecDeque::new();
            queue.push_back(start);
            visited[start] = true;
            while let Some(cur) = queue.pop_front() {
                comp.push(cur);
                // Forward edges
                for &j in &self.adj[cur] {
                    if !visited[j] {
                        visited[j] = true;
                        queue.push_back(j);
                    }
                }
                // Reverse edges (undirected)
                for (i, row) in self.adj.iter().enumerate() {
                    if !visited[i] && row.contains(&cur) {
                        visited[i] = true;
                        queue.push_back(i);
                    }
                }
            }
            components.push(comp);
        }
        components
    }
}

// ─── Recursive conversion ─────────────────────────────────────────────────────

/// Recursively convert a POWL node to a `PowlProcessTree`.
///
/// Returns a `PowlProcessTree` that mirrors the Python `apply_recursive` output.
pub fn apply_recursive(arena: &PowlArena, node_idx: u32) -> PowlProcessTree {
    match arena.get(node_idx) {
        None => PowlProcessTree::leaf(None),

        // Leaf: Transition / FrequentTransition
        Some(PowlNode::Transition(t)) => PowlProcessTree::leaf(t.label.clone()),
        Some(PowlNode::FrequentTransition(t)) => PowlProcessTree::leaf(Some(t.label.clone())),

        // OperatorPOWL
        Some(PowlNode::OperatorPowl(op)) => {
            let pt_op = match op.operator {
                Operator::Xor => PtOperator::Xor,
                Operator::Loop => PtOperator::Loop,
                Operator::PartialOrder => PtOperator::Sequence,
            };
            let children: Vec<PowlProcessTree> = op
                .children
                .iter()
                .map(|&c| apply_recursive(arena, c))
                .collect();
            PowlProcessTree::internal(pt_op, children)
        }

        // StrictPartialOrder
        Some(PowlNode::StrictPartialOrder(spo)) => {
            convert_partial_order(arena, &spo.children, &spo.order)
        }

        // DecisionGraph (same structure as SPO for conversion purposes)
        Some(PowlNode::DecisionGraph(dg)) => convert_partial_order(arena, &dg.children, &dg.order),

        // ChoiceGraph: process-tree conversion approximates the CG as an XOR
        // of the SubModel sub-trees (lossy — only used for visualization paths).
        Some(PowlNode::ChoiceGraph(cg)) => {
            let children: Vec<PowlProcessTree> = cg
                .graph
                .nodes()
                .iter()
                .filter_map(|n| match n {
                    ChoiceGraphNode::SubModel(idx) => Some(apply_recursive(arena, *idx)),
                    _ => None,
                })
                .collect();
            if children.is_empty() {
                PowlProcessTree::leaf(None)
            } else {
                PowlProcessTree::internal(PtOperator::Xor, children)
            }
        }
    }
}

/// Convert a partial-order node (SPO or DecisionGraph) to a process tree.
///
/// Builds a DAG from the order relation, finds connected components,
/// and converts each component to a leveled tree.
#[allow(clippy::needless_range_loop)]
fn convert_partial_order(
    arena: &PowlArena,
    children: &[u32],
    order: &crate::powl_arena::BinaryRelation,
) -> PowlProcessTree {
    let n = children.len();
    if n == 0 {
        return PowlProcessTree::leaf(None);
    }
    if n == 1 {
        return apply_recursive(arena, children[0]);
    }

    // Build DAG from partial order
    let mut dag = Dag::new(n);
    for i in 0..n {
        for j in 0..n {
            if order.is_edge(i, j) {
                dag.add_edge(i, j);
            }
        }
    }

    // Transitive reduction
    let dag = dag.transitive_reduction();

    // Connected components (undirected)
    let components = dag.undirected_components();

    let mut component_trees: Vec<PowlProcessTree> = Vec::new();

    for comp in &components {
        if comp.len() == 1 {
            let child_idx = children[comp[0]];
            component_trees.push(apply_recursive(arena, child_idx));
            continue;
        }

        // Build subgraph DAG for this component
        let local_to_global: &[usize] = comp;
        let global_to_local: Vec<Option<usize>> = {
            let mut g2l = vec![None; n];
            for (li, &gi) in local_to_global.iter().enumerate() {
                g2l[gi] = Some(li);
            }
            g2l
        };
        let m = comp.len();
        let mut sub_dag = Dag::new(m);
        for (li, &gi) in local_to_global.iter().enumerate() {
            for &succ_gi in &dag.adj[gi] {
                if let Some(succ_li) = global_to_local[succ_gi] {
                    sub_dag.add_edge(li, succ_li);
                }
            }
        }

        // BFS level assignment
        let levels_map = sub_dag.assign_levels();
        let max_level = *levels_map.iter().max().unwrap_or(&0);

        // Group by level
        let mut level_groups: Vec<Vec<usize>> = vec![Vec::new(); max_level + 1];
        for li in 0..m {
            let lv = levels_map[li];
            if lv != usize::MAX {
                level_groups[lv].push(li);
            }
        }

        // Each level -> PARALLEL or single node; levels -> SEQUENCE
        let mut level_trees: Vec<PowlProcessTree> = Vec::new();
        for group in &level_groups {
            if group.is_empty() {
                continue;
            }
            let sub_trees: Vec<PowlProcessTree> = group
                .iter()
                .map(|&li| apply_recursive(arena, children[local_to_global[li]]))
                .collect();
            if sub_trees.len() == 1 {
                level_trees.push(
                    sub_trees
                        .into_iter()
                        .next()
                        .expect("invariant: sub_trees non-empty when len==1"),
                );
            } else {
                level_trees.push(PowlProcessTree::internal(PtOperator::Parallel, sub_trees));
            }
        }

        let subtree = if level_trees.len() == 1 {
            level_trees
                .into_iter()
                .next()
                .expect("invariant: level_trees non-empty when len==1")
        } else {
            PowlProcessTree::internal(PtOperator::Sequence, level_trees)
        };
        component_trees.push(subtree);
    }

    if component_trees.len() == 1 {
        component_trees
            .into_iter()
            .next()
            .expect("invariant: component_trees non-empty when len==1")
    } else {
        PowlProcessTree::internal(PtOperator::Parallel, component_trees)
    }
}

/// Convert a POWL model to a process tree.
pub fn apply(arena: &PowlArena, root: u32) -> PowlProcessTree {
    apply_recursive(arena, root)
}

// ─── tests ───────────────────────────────────────────────────────────────────

#[cfg(test)]
mod tests {
    use super::*;
    use crate::powl_parser::parse_powl_model_string;

    fn build(s: &str) -> (PowlArena, u32) {
        let mut arena = PowlArena::new();
        let root = parse_powl_model_string(s, &mut arena).unwrap();
        (arena, root)
    }

    #[test]
    fn transition_to_leaf() {
        let (arena, root) = build("A");
        let pt = apply(&arena, root);
        assert_eq!(pt.label.as_deref(), Some("A"));
        assert!(pt.operator.is_none());
    }

    #[test]
    fn xor_to_xor() {
        let (arena, root) = build("X ( A, B )");
        let pt = apply(&arena, root);
        assert_eq!(pt.operator, Some(PtOperator::Xor));
        assert_eq!(pt.children.len(), 2);
    }

    #[test]
    fn sequence_po_to_sequence_tree() {
        let (arena, root) = build("PO=(nodes={A, B}, order={A-->B})");
        let pt = apply(&arena, root);
        let repr = pt.to_repr();
        assert!(repr.contains("A") && repr.contains("B"), "got: {}", repr);
    }

    #[test]
    fn concurrent_po_to_parallel() {
        let (arena, root) = build("PO=(nodes={A, B}, order={})");
        let pt = apply(&arena, root);
        assert_eq!(pt.operator, Some(PtOperator::Parallel));
    }

    #[test]
    fn loop_to_loop() {
        let (arena, root) = build("* ( A, B )");
        let pt = apply(&arena, root);
        assert_eq!(pt.operator, Some(PtOperator::Loop));
    }

    #[test]
    fn decision_graph_converts() {
        let (arena, root) =
            build("DG=(nodes={A, B}, order={A-->B}, starts=[A], ends=[B], empty=false)");
        let pt = apply(&arena, root);
        // DG with A-->B should produce a sequence
        let repr = pt.to_repr();
        assert!(repr.contains("A") && repr.contains("B"), "got: {}", repr);
    }

    #[test]
    fn silent_transition_to_tau() {
        let (arena, root) = build("tau");
        let pt = apply(&arena, root);
        assert_eq!(pt.label.as_deref(), None);
        assert!(pt.operator.is_none());
    }

    #[test]
    fn nested_xor_in_loop() {
        let (arena, root) = build("*(X(A, B), C)");
        let pt = apply(&arena, root);
        assert_eq!(pt.operator, Some(PtOperator::Loop));
        assert_eq!(pt.children.len(), 2);
        // First child should be XOR
        assert_eq!(pt.children[0].operator, Some(PtOperator::Xor));
    }
}