wasm4pm 26.6.10

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
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
//! Process tree with typed operator enum replacing the stringly-typed node_type field in wasm4pm ProcessTreeNode. Leaf nodes carry ActivityName. Tree structure is recursive via Box<ProcessTree>.
//! Paper grounding: Leemans, Fahland & van der Aalst 2013 'Discovering Block-Structured Process Models from Event Logs' §2: process tree T with operators {→ (sequence), × (exclusive choice), ∧ (parallel/and), ↺ (loop)}. Tau (silent) leaf is the invisible activity.

extern crate alloc;

use alloc::collections::BTreeMap;
use alloc::collections::BTreeSet;
use alloc::string::String;
use alloc::vec::Vec;
use core::fmt;
use core::ops::Deref;

#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};

// ─── ActivityName ────────────────────────────────────────────────────────────

/// Formal object from [Leemans2013]: observable activity label *a* ∈ Σ — the
/// alphabet of activity names over which a process tree is defined.
///
/// This is a zero-cost transparent newtype over [`String`].  `None` inside
/// [`ProcessTree::Leaf`] represents the silent activity τ (tau); `Some(name)`
/// represents an observable activity with this label.
///
/// # Invariant
/// The inner string is never empty when constructed through [`ActivityName::new`].
/// An empty label is semantically equivalent to τ and should be represented as
/// `ProcessTree::Leaf(None)` instead.
#[derive(Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
#[repr(transparent)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "serde", serde(transparent))]
pub struct ActivityName(String);

impl ActivityName {
    /// Construct an `ActivityName` from any string-like value.
    ///
    /// # Panics
    /// Does not panic; empty strings are accepted at construction but violate
    /// the semantic invariant described above.
    #[inline]
    #[must_use]
    pub fn new(s: impl Into<String>) -> Self {
        ActivityName(s.into())
    }

    /// Return a reference to the underlying string slice.
    #[inline]
    #[must_use]
    pub fn as_str(&self) -> &str {
        &self.0
    }

    /// Consume the newtype and return the owned `String`.
    #[inline]
    #[must_use]
    pub fn into_inner(self) -> String {
        self.0
    }
}

impl Deref for ActivityName {
    type Target = String;

    #[inline]
    fn deref(&self) -> &Self::Target {
        &self.0
    }
}

impl fmt::Display for ActivityName {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str(&self.0)
    }
}

impl From<String> for ActivityName {
    #[inline]
    fn from(s: String) -> Self {
        ActivityName(s)
    }
}

impl From<&str> for ActivityName {
    #[inline]
    fn from(s: &str) -> Self {
        ActivityName(s.into())
    }
}

// ─── ProcessTreeOperator ─────────────────────────────────────────────────────

/// Formal object from [Leemans2013]: ⊕ ∈ {→, ×, ∧, ↺, ∨} — control-flow
/// operator in a process tree node (Leemans et al. 2013 §2 Def 2.1).
///
/// `Or` is included for completeness from the IMf (Inductive Miner with
/// frequency filtering) variant.  Replaces the stringly-typed
/// `node_type: String` field in the  `wasm4pm ProcessTreeNode`.
///
/// # Copy semantics
/// This is a `Copy` enum — no heap allocation.  Replacing a `String` operator
/// tag with this type eliminates both the allocation and the match-on-string
/// anti-pattern throughout the codebase.
///
/// # Formal mapping
/// | Variant           | Symbol | Semantics                                      |
/// |-------------------|--------|------------------------------------------------|
/// | `Sequence`        | →      | Execute children left-to-right sequentially    |
/// | `ExclusiveChoice` | ×      | Execute exactly one child (XOR split/join)     |
/// | `Parallel`        | ∧      | Execute all children in any order (AND)        |
/// | `Loop`            | ↺      | First child is body; second child is redo      |
/// | `Or`              | ∨      | Execute one or more children (inclusive OR)    |
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "serde", serde(rename_all = "snake_case"))]
pub enum ProcessTreeOperator {
    /// → (sequence): children execute left-to-right in order.
    ///
    /// Leemans et al. 2013 §2: `→(T₁, …, Tₙ)` — sequential block.
    Sequence,

    /// × (exclusive choice / XOR): exactly one child executes.
    ///
    /// Leemans et al. 2013 §2: `×(T₁, …, Tₙ)` — XOR block.
    ExclusiveChoice,

    /// ∧ (parallel / AND): all children execute in any interleaving.
    ///
    /// Leemans et al. 2013 §2: `∧(T₁, …, Tₙ)` — AND block.
    Parallel,

    /// ↺ (loop): the first child is the loop body; the second child is the
    /// redo branch executed between iterations.
    ///
    /// Leemans et al. 2013 §2: `↺(T_do, T_redo)` — loop block.
    Loop,

    /// ∨ (inclusive OR): one or more children execute (IMf variant).
    ///
    /// Not in the original Leemans et al. 2013 paper but present in the
    /// frequency-based IMf extension; included for full operator coverage.
    Or,
}

impl ProcessTreeOperator {
    /// Return the canonical symbolic notation for this operator.
    #[must_use]
    pub fn symbol(self) -> &'static str {
        match self {
            ProcessTreeOperator::Sequence => "",
            ProcessTreeOperator::ExclusiveChoice => "×",
            ProcessTreeOperator::Parallel => "",
            ProcessTreeOperator::Loop => "",
            ProcessTreeOperator::Or => "",
        }
    }

    /// Return the short ASCII tag used in textual process tree representations.
    ///
    /// These tags follow pm4py / ProM convention:
    /// `"->"`, `"X"`, `"+"`, `"*"`, `"O"`.
    #[must_use]
    pub fn ascii_tag(self) -> &'static str {
        match self {
            ProcessTreeOperator::Sequence => "->",
            ProcessTreeOperator::ExclusiveChoice => "X",
            ProcessTreeOperator::Parallel => "+",
            ProcessTreeOperator::Loop => "*",
            ProcessTreeOperator::Or => "O",
        }
    }

    /// Try to parse an ASCII tag (case-insensitive) back to an operator.
    ///
    /// Recognises: `"->"`, `"SEQ"`, `"SEQUENCE"` → `Sequence`;
    /// `"X"`, `"XOR"`, `"EXCLUSIVE_CHOICE"` → `ExclusiveChoice`;
    /// `"+"`, `"AND"`, `"PARALLEL"` → `Parallel`;
    /// `"*"`, `"LOOP"` → `Loop`;
    /// `"O"`, `"OR"` → `Or`.
    ///
    /// Returns `None` for unrecognised tags.
    #[must_use]
    pub fn from_tag(tag: &str) -> Option<Self> {
        match tag.to_uppercase().as_str() {
            "->" | "SEQ" | "SEQUENCE" => Some(ProcessTreeOperator::Sequence),
            "X" | "XOR" | "EXCLUSIVE_CHOICE" => Some(ProcessTreeOperator::ExclusiveChoice),
            "+" | "AND" | "PARALLEL" => Some(ProcessTreeOperator::Parallel),
            "*" | "LOOP" => Some(ProcessTreeOperator::Loop),
            "O" | "OR" => Some(ProcessTreeOperator::Or),
            _ => None,
        }
    }

    /// Returns `true` when this operator is a binary operator in the strict
    /// sense (i.e. exactly two children, as with `Loop`).
    #[must_use]
    pub fn is_binary(self) -> bool {
        matches!(self, ProcessTreeOperator::Loop)
    }
}

impl fmt::Display for ProcessTreeOperator {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str(self.symbol())
    }
}

// ─── ProcessTree ─────────────────────────────────────────────────────────────

/// Formal object from [Leemans2013]: T — process tree defined recursively.
///
/// * `Leaf(None)` represents the silent leaf τ (tau) — the invisible activity.
/// * `Leaf(Some(a))` represents an observable leaf activity *a ∈ Σ*.
/// * `Operator { op, children }` represents `op(T₁, …, Tₙ)` — an operator
///   node with one or more sub-trees (Leemans et al. 2013 §2 Def 2.1).
///
/// # Recursive structure
/// Indirection for the recursive case is handled by `Vec<ProcessTree>` (the
/// `Vec` heap-allocates its contents, so no explicit `Box<ProcessTree>` wrapper
/// is required).  This follows the requirement that "Box is implicit in
/// `Vec<ProcessTree>` children".
///
/// # Replaces
/// The  `ProcessTreeNode { node_type: String, children: Vec<ProcessTreeNode> }`
/// and the stringly-typed matching on `"SEQ"`, `"XOR"`, `"AND"`, `"OR"`,
/// `"LOOP"` throughout the codebase.
///
/// # Formal semantics (Leemans et al. 2013 §2)
/// | Tree            | Language L(T)                                |
/// |-----------------|----------------------------------------------|
/// | `Leaf(τ)`       | { ε }  (only the empty trace)                |
/// | `Leaf(a)`       | { ⟨a⟩ }                                      |
/// | `→(T₁,…,Tₙ)`  | L(T₁) · … · L(Tₙ)  (concatenation)          |
/// | `×(T₁,…,Tₙ)`  | L(T₁) ∪ … ∪ L(Tₙ)  (union)                  |
/// | `∧(T₁,…,Tₙ)`  | shuffle(L(T₁), …, L(Tₙ))                    |
/// | `↺(T_do,T_re)` | L(T_do) · (L(T_re) · L(T_do))*              |
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "serde", serde(tag = "kind", rename_all = "snake_case"))]
pub enum ProcessTree {
    /// A leaf node.
    ///
    /// `None` → silent activity τ (invisible to the observer).
    /// `Some(name)` → observable activity with the given label.
    Leaf(Option<ActivityName>),

    /// An operator node with one or more sub-trees.
    ///
    /// The `Vec<ProcessTree>` provides implicit heap indirection (no
    /// `Box` wrapper required).
    Operator {
        /// The control-flow operator governing how children execute.
        op: ProcessTreeOperator,
        /// Ordered list of child sub-trees.  Must be non-empty.
        children: Vec<ProcessTree>,
    },
}

impl ProcessTree {
    // ── Constructors ──────────────────────────────────────────────────────

    /// Construct a silent leaf (τ).
    #[inline]
    #[must_use]
    pub fn tau() -> Self {
        ProcessTree::Leaf(None)
    }

    /// Construct an observable activity leaf.
    #[inline]
    #[must_use]
    pub fn activity(name: impl Into<ActivityName>) -> Self {
        ProcessTree::Leaf(Some(name.into()))
    }

    /// Construct a sequence node `→(children)`.
    ///
    /// # Panics
    /// Panics in debug builds if `children` is empty.
    #[inline]
    #[must_use]
    pub fn sequence(children: Vec<ProcessTree>) -> Self {
        debug_assert!(
            !children.is_empty(),
            "Sequence must have at least one child"
        );
        ProcessTree::Operator {
            op: ProcessTreeOperator::Sequence,
            children,
        }
    }

    /// Construct an exclusive-choice node `×(children)`.
    ///
    /// # Panics
    /// Panics in debug builds if `children` is empty.
    #[inline]
    #[must_use]
    pub fn xor(children: Vec<ProcessTree>) -> Self {
        debug_assert!(
            !children.is_empty(),
            "ExclusiveChoice must have at least one child"
        );
        ProcessTree::Operator {
            op: ProcessTreeOperator::ExclusiveChoice,
            children,
        }
    }

    /// Construct a parallel node `∧(children)`.
    ///
    /// # Panics
    /// Panics in debug builds if `children` is empty.
    #[inline]
    #[must_use]
    pub fn parallel(children: Vec<ProcessTree>) -> Self {
        debug_assert!(
            !children.is_empty(),
            "Parallel must have at least one child"
        );
        ProcessTree::Operator {
            op: ProcessTreeOperator::Parallel,
            children,
        }
    }

    /// Construct a loop node `↺(body, redo)`.
    ///
    /// Per Leemans et al. 2013 §2, a loop has exactly two children:
    /// the loop body (`do` branch) and the redo branch.
    #[inline]
    #[must_use]
    pub fn loop_node(body: ProcessTree, redo: ProcessTree) -> Self {
        ProcessTree::Operator {
            op: ProcessTreeOperator::Loop,
            children: vec![body, redo],
        }
    }

    /// Construct an inclusive-or node `∨(children)` (IMf extension).
    ///
    /// # Panics
    /// Panics in debug builds if `children` is empty.
    #[inline]
    #[must_use]
    pub fn or(children: Vec<ProcessTree>) -> Self {
        debug_assert!(!children.is_empty(), "Or must have at least one child");
        ProcessTree::Operator {
            op: ProcessTreeOperator::Or,
            children,
        }
    }

    // ── Predicates ───────────────────────────────────────────────────────

    /// Returns `true` if this is a leaf node (silent or observable).
    #[inline]
    #[must_use]
    pub fn is_leaf(&self) -> bool {
        matches!(self, ProcessTree::Leaf(_))
    }

    /// Returns `true` if this is the silent leaf τ.
    #[inline]
    #[must_use]
    pub fn is_tau(&self) -> bool {
        matches!(self, ProcessTree::Leaf(None))
    }

    /// Returns `true` if this is an observable activity leaf.
    #[inline]
    #[must_use]
    pub fn is_activity(&self) -> bool {
        matches!(self, ProcessTree::Leaf(Some(_)))
    }

    /// Returns `true` if this is an operator node.
    #[inline]
    #[must_use]
    pub fn is_operator(&self) -> bool {
        matches!(self, ProcessTree::Operator { .. })
    }

    // ── Accessors ────────────────────────────────────────────────────────

    /// Return the activity name if this is an observable leaf, otherwise `None`.
    #[inline]
    #[must_use]
    pub fn activity_name(&self) -> Option<&ActivityName> {
        match self {
            ProcessTree::Leaf(Some(name)) => Some(name),
            _ => None,
        }
    }

    /// Return the operator if this is an operator node, otherwise `None`.
    #[inline]
    #[must_use]
    pub fn operator(&self) -> Option<ProcessTreeOperator> {
        match self {
            ProcessTree::Operator { op, .. } => Some(*op),
            _ => None,
        }
    }

    /// Return a reference to the children slice if this is an operator node.
    ///
    /// Returns an empty slice for leaf nodes.
    #[inline]
    #[must_use]
    pub fn children(&self) -> &[ProcessTree] {
        match self {
            ProcessTree::Operator { children, .. } => children,
            ProcessTree::Leaf(_) => &[],
        }
    }

    /// Return the number of direct children (0 for leaves).
    #[inline]
    #[must_use]
    pub fn arity(&self) -> usize {
        self.children().len()
    }

    // ── Structural metrics ────────────────────────────────────────────────

    /// Return the depth of the tree (0 for a leaf, 1 + max child depth for
    /// an operator node).
    ///
    /// Corresponds to the height metric used in simplicity calculations.
    #[must_use]
    pub fn depth(&self) -> usize {
        match self {
            ProcessTree::Leaf(_) => 0,
            ProcessTree::Operator { children, .. } => {
                1 + children.iter().map(ProcessTree::depth).max().unwrap_or(0)
            }
        }
    }

    /// Count the total number of nodes (leaves + operator nodes) in the tree.
    #[must_use]
    pub fn node_count(&self) -> usize {
        match self {
            ProcessTree::Leaf(_) => 1,
            ProcessTree::Operator { children, .. } => {
                1 + children.iter().map(ProcessTree::node_count).sum::<usize>()
            }
        }
    }

    /// Collect all distinct observable activity names reachable from this node
    /// into a [`BTreeSet`] for deterministic ordering.
    #[must_use]
    pub fn activity_set(&self) -> BTreeSet<ActivityName> {
        let mut set = BTreeSet::new();
        self.collect_activities(&mut set);
        set
    }

    fn collect_activities(&self, acc: &mut BTreeSet<ActivityName>) {
        match self {
            ProcessTree::Leaf(Some(name)) => {
                acc.insert(name.clone());
            }
            ProcessTree::Leaf(None) => {}
            ProcessTree::Operator { children, .. } => {
                for child in children {
                    child.collect_activities(acc);
                }
            }
        }
    }

    /// Build a frequency map of activity names to the number of times they
    /// appear as leaves in the tree.  Returns a [`BTreeMap`] for determinism.
    #[must_use]
    pub fn activity_frequency_map(&self) -> BTreeMap<ActivityName, usize> {
        let mut map = BTreeMap::new();
        self.collect_activity_frequencies(&mut map);
        map
    }

    fn collect_activity_frequencies(&self, acc: &mut BTreeMap<ActivityName, usize>) {
        match self {
            ProcessTree::Leaf(Some(name)) => {
                *acc.entry(name.clone()).or_insert(0) += 1;
            }
            ProcessTree::Leaf(None) => {}
            ProcessTree::Operator { children, .. } => {
                for child in children {
                    child.collect_activity_frequencies(acc);
                }
            }
        }
    }

    // ── Textual representation ────────────────────────────────────────────

    /// Produce a compact human-readable string using the standard pm4py
    /// process tree notation, e.g. `→( ×( A, B ), C )`.
    #[must_use]
    pub fn to_repr(&self) -> String {
        match self {
            ProcessTree::Leaf(None) => "tau".into(),
            ProcessTree::Leaf(Some(name)) => name.as_str().into(),
            ProcessTree::Operator { op, children } => {
                let inner: Vec<String> = children.iter().map(ProcessTree::to_repr).collect();
                alloc::format!("{}( {} )", op.ascii_tag(), inner.join(", "))
            }
        }
    }
}

impl fmt::Display for ProcessTree {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str(&self.to_repr())
    }
}

// ───  compatibility bridge ───────────────────────────────────────────────

///  stringly-typed node kind kept for baseline admissibility with code
/// that still uses the old `ProcessTreeNode` API.
///
/// New code should prefer [`ProcessTree`] and [`ProcessTreeOperator`] directly.
#[derive(Debug, Clone)]
pub enum NodeKind {
    /// An operator node identified by a string tag (`"SEQ"`, `"XOR"`, etc.).
    Operator(String),
    /// A leaf node carrying a named activity.
    Activity(String),
    /// The silent activity τ.
    Silent,
}

///  process tree node kept for baseline admissibility.
///
/// Prefer [`ProcessTree`] for new code.  This type uses a stringly-typed
/// [`NodeKind`] internally; the typed counterpart is [`ProcessTree`].
#[derive(Debug, Clone)]
pub struct ProcessTreeNode {
    /// The kind of this node.
    pub kind: NodeKind,
    /// Direct child nodes.
    pub children: Vec<ProcessTreeNode>,
}

impl ProcessTreeNode {
    /// Construct an operator node with the given tag (e.g. `"SEQ"`).
    #[must_use]
    pub fn operator(op: impl Into<String>) -> Self {
        ProcessTreeNode {
            kind: NodeKind::Operator(op.into()),
            children: Vec::new(),
        }
    }

    /// Construct an observable activity leaf.
    #[must_use]
    pub fn activity(label: impl Into<String>) -> Self {
        ProcessTreeNode {
            kind: NodeKind::Activity(label.into()),
            children: Vec::new(),
        }
    }

    /// Construct a silent leaf (τ).
    #[must_use]
    pub fn silent() -> Self {
        ProcessTreeNode {
            kind: NodeKind::Silent,
            children: Vec::new(),
        }
    }

    /// Append a child and return `self` (builder pattern).
    #[must_use]
    pub fn add_child(mut self, child: ProcessTreeNode) -> Self {
        self.children.push(child);
        self
    }

    /// Convert this  node into a typed [`ProcessTree`].
    ///
    /// Unknown operator tags are mapped to [`ProcessTreeOperator::Sequence`]
    /// as a safe default.
    #[must_use]
    pub fn into_typed(self) -> ProcessTree {
        match self.kind {
            NodeKind::Silent => ProcessTree::tau(),
            NodeKind::Activity(label) => ProcessTree::activity(ActivityName::new(label)),
            NodeKind::Operator(tag) => {
                let op =
                    ProcessTreeOperator::from_tag(&tag).unwrap_or(ProcessTreeOperator::Sequence);
                let children: Vec<ProcessTree> =
                    self.children.into_iter().map(|c| c.into_typed()).collect();
                ProcessTree::Operator { op, children }
            }
        }
    }
}

// ─── WASM-bindgen exports ─────────────────────────────────────────────────────
// (kept for compatibility — no new wasm_bindgen on the typed API)

use crate::models::EventLog;
use serde_json::json;
use wasm_bindgen::prelude::*;

/// Recursively convert a  `ProcessTreeNode` to a JSON `serde_json::Value`.
#[allow(dead_code)]
fn node_to_json(node: &ProcessTreeNode) -> serde_json::Value {
    let children: Vec<serde_json::Value> = node.children.iter().map(node_to_json).collect();
    match &node.kind {
        NodeKind::Operator(op) => json!({
            "type": "operator",
            "operator": op,
            "children": children,
        }),
        NodeKind::Activity(label) => json!({
            "type": "activity",
            "label": label,
        }),
        NodeKind::Silent => json!({
            "type": "silent",
        }),
    }
}

/// Convert a process tree JSON into a simplified flat representation
/// (for JS consumption — the full tree as a JSON string).
///
/// Input JSON follows the same schema as `node_to_json` output.
/// Validates the structure and returns it back as a pretty-printed JSON string.
///
/// ```javascript
/// const treeJson = JSON.stringify({
///   type: "operator", operator: "SEQ",
///   children: [
///     { type: "activity", label: "A" },
///     { type: "activity", label: "B" }
///   ]
/// });
/// const result = pm.validate_process_tree(treeJson);
/// ```
#[wasm_bindgen]
pub fn validate_process_tree(tree_json: &str) -> Result<JsValue, JsValue> {
    let v: serde_json::Value = serde_json::from_str(tree_json)
        .map_err(|e| crate::error::js_val(&format!("Invalid JSON: {}", e)))?;

    fn validate(node: &serde_json::Value, depth: usize) -> Result<serde_json::Value, String> {
        if depth > 50 {
            return Err("Tree depth exceeds maximum (50)".to_string());
        }
        let node_type = node["type"].as_str().ok_or("Node missing 'type' field")?;
        match node_type {
            "operator" => {
                let op = node["operator"]
                    .as_str()
                    .ok_or("Operator node missing 'operator' field")?;
                if ProcessTreeOperator::from_tag(op).is_none() {
                    return Err(alloc::format!(
                        "Unknown operator '{}'. Must be ->/SEQ, X/XOR, +/AND, */LOOP, or O/OR",
                        op
                    ));
                }
                let children = node["children"]
                    .as_array()
                    .ok_or("Operator node missing 'children' array")?;
                if children.is_empty() {
                    return Err(alloc::format!(
                        "Operator '{}' must have at least one child",
                        op
                    ));
                }
                let validated_children: Result<Vec<serde_json::Value>, String> =
                    children.iter().map(|c| validate(c, depth + 1)).collect();
                Ok(json!({
                    "type": "operator",
                    "operator": op,
                    "children": validated_children?,
                }))
            }
            "activity" => {
                let label = node["label"]
                    .as_str()
                    .ok_or("Activity node missing 'label' field")?;
                Ok(json!({"type": "activity", "label": label}))
            }
            "silent" => Ok(json!({"type": "silent"})),
            other => Err(alloc::format!(
                "Unknown node type '{}'. Must be operator, activity, or silent",
                other
            )),
        }
    }

    let validated = validate(&v, 0).map_err(|e| crate::error::js_val(&e))?;
    let out =
        serde_json::to_string(&validated).map_err(|e| crate::error::js_val(&e.to_string()))?;
    Ok(crate::error::js_val(&out))
}

/// Pure-Rust process tree discovery without wasm-bindgen. Used by integration tests.
#[must_use]
pub fn discover_simple_process_tree_from_log(log: &EventLog, activity_key: &str) -> String {
    let mut freq: std::collections::HashMap<String, usize> = std::collections::HashMap::new();

    for trace in &log.traces {
        let acts: Vec<String> = trace
            .events
            .iter()
            .filter_map(|e| {
                e.attributes
                    .get(activity_key)
                    .and_then(|v| v.as_string())
                    .map(str::to_owned)
            })
            .collect();
        for a in &acts {
            *freq.entry(a.clone()).or_insert(0) += 1;
        }
    }

    let mut sorted_acts: Vec<(String, usize)> = freq.into_iter().collect();
    sorted_acts.sort_by_key(|b| std::cmp::Reverse(b.1));

    let children: Vec<serde_json::Value> = sorted_acts
        .iter()
        .map(|(label, _)| json!({"type": "activity", "label": label}))
        .collect();

    let tree = if children.len() == 1 {
        children
            .into_iter()
            .next()
            .expect("children.len() == 1 guarantees one element")
    } else {
        json!({
            "type": "operator",
            "operator": "SEQ",
            "children": children,
        })
    };

    serde_json::to_string(&tree)
        .unwrap_or_else(|_| r#"{"type":"operator","operator":"SEQ","children":[]}"#.to_string())
}

/// Discover a simple process tree from an event log using frequency-based heuristics.
///
/// Returns a JSON string representing the process tree.
#[wasm_bindgen]
pub fn discover_simple_process_tree(
    log_handle: &str,
    activity_key: &str,
) -> Result<JsValue, JsValue> {
    use crate::state::{get_or_init_state, StoredObject};

    let log = get_or_init_state().with_object(log_handle, |obj| match obj {
        Some(StoredObject::EventLog(log)) => Ok(log.clone()),
        Some(_) => Err(crate::error::js_val("Handle is not an EventLog")),
        None => Err(crate::error::js_val("EventLog handle not found")),
    })?;
    Ok(crate::error::js_val(
        &discover_simple_process_tree_from_log(&log, activity_key),
    ))
}

// ─── Unit tests ───────────────────────────────────────────────────────────────

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

    // ActivityName tests

    #[test]
    fn activity_name_deref_and_display() {
        let a = ActivityName::new("Register");
        assert_eq!(a.as_str(), "Register");
        assert_eq!(a.to_string(), "Register");
        assert_eq!(&*a, "Register");
    }

    #[test]
    fn activity_name_from_str() {
        let a: ActivityName = "Approve".into();
        assert_eq!(a.as_str(), "Approve");
    }

    #[test]
    fn activity_name_into_inner() {
        let a = ActivityName::new("Ship");
        assert_eq!(a.into_inner(), "Ship".to_string());
    }

    #[test]
    fn activity_name_ord() {
        let mut names = vec![
            ActivityName::new("C"),
            ActivityName::new("A"),
            ActivityName::new("B"),
        ];
        names.sort();
        assert_eq!(
            names.iter().map(|n| n.as_str()).collect::<Vec<_>>(),
            vec!["A", "B", "C"]
        );
    }

    // ProcessTreeOperator tests

    #[test]
    fn operator_symbol_round_trip() {
        for op in [
            ProcessTreeOperator::Sequence,
            ProcessTreeOperator::ExclusiveChoice,
            ProcessTreeOperator::Parallel,
            ProcessTreeOperator::Loop,
            ProcessTreeOperator::Or,
        ] {
            let tag = op.ascii_tag();
            assert_eq!(ProcessTreeOperator::from_tag(tag), Some(op));
        }
    }

    #[test]
    fn operator_from_tag_case_insensitive() {
        assert_eq!(
            ProcessTreeOperator::from_tag("seq"),
            Some(ProcessTreeOperator::Sequence)
        );
        assert_eq!(
            ProcessTreeOperator::from_tag("SEQUENCE"),
            Some(ProcessTreeOperator::Sequence)
        );
        assert_eq!(
            ProcessTreeOperator::from_tag("loop"),
            Some(ProcessTreeOperator::Loop)
        );
    }

    #[test]
    fn operator_from_tag_unknown() {
        assert_eq!(ProcessTreeOperator::from_tag("UNKNOWN"), None);
        assert_eq!(ProcessTreeOperator::from_tag(""), None);
    }

    #[test]
    fn operator_is_binary() {
        assert!(ProcessTreeOperator::Loop.is_binary());
        assert!(!ProcessTreeOperator::Sequence.is_binary());
        assert!(!ProcessTreeOperator::Parallel.is_binary());
    }

    #[test]
    fn operator_display() {
        assert_eq!(ProcessTreeOperator::Sequence.to_string(), "");
        assert_eq!(ProcessTreeOperator::ExclusiveChoice.to_string(), "×");
        assert_eq!(ProcessTreeOperator::Parallel.to_string(), "");
        assert_eq!(ProcessTreeOperator::Loop.to_string(), "");
        assert_eq!(ProcessTreeOperator::Or.to_string(), "");
    }

    // ProcessTree construction tests

    #[test]
    fn tau_leaf() {
        let t = ProcessTree::tau();
        assert!(t.is_tau());
        assert!(t.is_leaf());
        assert!(!t.is_operator());
        assert_eq!(t.depth(), 0);
        assert_eq!(t.node_count(), 1);
        assert_eq!(t.to_repr(), "tau");
    }

    #[test]
    fn activity_leaf() {
        let t = ProcessTree::activity("Register");
        assert!(t.is_activity());
        assert!(!t.is_tau());
        assert_eq!(t.activity_name().map(|n| n.as_str()), Some("Register"));
        assert_eq!(t.to_repr(), "Register");
    }

    #[test]
    fn sequence_tree_repr() {
        let t = ProcessTree::sequence(vec![ProcessTree::activity("A"), ProcessTree::activity("B")]);
        assert_eq!(t.operator(), Some(ProcessTreeOperator::Sequence));
        assert_eq!(t.arity(), 2);
        assert_eq!(t.depth(), 1);
        assert_eq!(t.node_count(), 3);
        assert_eq!(t.to_repr(), "->( A, B )");
    }

    #[test]
    fn loop_tree() {
        let body = ProcessTree::activity("Work");
        let redo = ProcessTree::tau();
        let t = ProcessTree::loop_node(body, redo);
        assert_eq!(t.operator(), Some(ProcessTreeOperator::Loop));
        assert_eq!(t.arity(), 2);
        assert_eq!(t.to_repr(), "*( Work, tau )");
    }

    #[test]
    fn nested_tree_depth() {
        // →( ×(A, B), ∧(C, D) )
        let t = ProcessTree::sequence(vec![
            ProcessTree::xor(vec![ProcessTree::activity("A"), ProcessTree::activity("B")]),
            ProcessTree::parallel(vec![ProcessTree::activity("C"), ProcessTree::activity("D")]),
        ]);
        assert_eq!(t.depth(), 2);
        assert_eq!(t.node_count(), 7); // root + 2 ops + 4 leaves
    }

    #[test]
    fn activity_set_dedup() {
        // Activities appearing multiple times should be de-duplicated.
        let t = ProcessTree::sequence(vec![
            ProcessTree::activity("A"),
            ProcessTree::activity("B"),
            ProcessTree::activity("A"),
        ]);
        let set = t.activity_set();
        let mut expected = BTreeSet::new();
        expected.insert(ActivityName::new("A"));
        expected.insert(ActivityName::new("B"));
        assert_eq!(set, expected);
    }

    #[test]
    fn activity_frequency_map() {
        let t = ProcessTree::sequence(vec![
            ProcessTree::activity("A"),
            ProcessTree::loop_node(ProcessTree::activity("A"), ProcessTree::tau()),
        ]);
        let freq = t.activity_frequency_map();
        assert_eq!(freq.get(&ActivityName::new("A")), Some(&2));
    }

    #[test]
    fn children_of_leaf_is_empty() {
        let t = ProcessTree::tau();
        assert_eq!(t.children(), &[] as &[ProcessTree]);
    }

    //  compatibility tests

    #[test]
    fn process_tree_node_into_typed_sequence() {
        let node = ProcessTreeNode::operator("SEQ")
            .add_child(ProcessTreeNode::activity("A"))
            .add_child(ProcessTreeNode::activity("B"));
        let typed = node.into_typed();
        assert_eq!(typed.operator(), Some(ProcessTreeOperator::Sequence));
        assert_eq!(typed.arity(), 2);
    }

    #[test]
    fn process_tree_node_into_typed_silent() {
        let node = ProcessTreeNode::silent();
        let typed = node.into_typed();
        assert!(typed.is_tau());
    }

    #[test]
    fn process_tree_node_into_typed_unknown_tag_defaults_to_sequence() {
        let node =
            ProcessTreeNode::operator("UNKNOWN_OP").add_child(ProcessTreeNode::activity("X"));
        let typed = node.into_typed();
        assert_eq!(typed.operator(), Some(ProcessTreeOperator::Sequence));
    }

    // Serde round-trip (only when feature is active — in test builds serde
    // is always available via the workspace dep, so we test unconditionally).
    #[test]
    #[cfg(feature = "serde")]
    fn serde_round_trip_operator() {
        let op = ProcessTreeOperator::Loop;
        let json = serde_json::to_string(&op).unwrap();
        let back: ProcessTreeOperator = serde_json::from_str(&json).unwrap();
        assert_eq!(op, back);
    }

    #[test]
    #[cfg(feature = "serde")]
    fn serde_round_trip_tree() {
        let t = ProcessTree::sequence(vec![
            ProcessTree::tau(),
            ProcessTree::activity("Register"),
            ProcessTree::xor(vec![
                ProcessTree::activity("Approve"),
                ProcessTree::activity("Reject"),
            ]),
        ]);
        let json = serde_json::to_string(&t).unwrap();
        let back: ProcessTree = serde_json::from_str(&json).unwrap();
        assert_eq!(t, back);
    }

    #[test]
    #[cfg(feature = "serde")]
    fn serde_round_trip_activity_name() {
        let a = ActivityName::new("Pay");
        let json = serde_json::to_string(&a).unwrap();
        let back: ActivityName = serde_json::from_str(&json).unwrap();
        assert_eq!(a, back);
    }
}