wasm4pm 26.7.1

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
//! Streaming Inductive Miner discovery.
//!
//! Inductive Miner recursively discovers process trees by detecting cuts in
//! the DFG. This streaming implementation maintains DFG state incrementally
//! and detects cuts at snapshot time to construct a Petri net.
//!
//! Cut detection strategy:
//! - **Sequential cut**: Activities partitioned into start→end groups
//! - **Exclusive cut**: No directly-follows edges between activity groups
//! - **Parallel cut**: Both directions have edges (symmetric)
//! - **Loop cut**: Activities repeat (start == end for some subset)

use crate::models::{PetriNet, PetriNetArc, PetriNetPlace, PetriNetTransition};
use crate::streaming::{
    impl_activity_interner, ActivityInterner, Interner, StreamStats, StreamingAlgorithm,
};
use rustc_hash::FxHashMap;
use std::collections::{BTreeMap, HashSet};

/// Streaming Inductive Miner builder.
///
/// Accumulates directly-follows edge counts during ingestion. At snapshot
/// time, detects the dominant cut pattern and constructs a Petri net:
/// - Sequential cuts become sequences of places
/// - Exclusive cuts create alternative branches
/// - Parallel cuts create concurrent branches
/// - Loop cuts create repetition structures
#[derive(Debug, Clone, Default)]
pub struct StreamingInductiveBuilder {
    /// Activity string interner
    pub interner: Interner,
    /// Activity occurrence counts
    pub activity_counts: Vec<usize>,
    /// Edge counts (for cut detection)
    pub edge_counts: FxHashMap<(u32, u32), usize>,
    /// Start/end activity counts
    pub start_counts: FxHashMap<u32, usize>,
    pub end_counts: FxHashMap<u32, usize>,
    /// Event/trace counters
    pub event_count: usize,
    pub trace_count: usize,
    /// Open traces
    pub open_traces: BTreeMap<String, Vec<u32>>,
}

impl_activity_interner!(StreamingInductiveBuilder);

impl StreamingInductiveBuilder {
    pub fn new() -> Self {
        StreamingInductiveBuilder {
            interner: Interner::new(),
            activity_counts: Vec::new(),
            edge_counts: FxHashMap::default(),
            start_counts: FxHashMap::default(),
            end_counts: FxHashMap::default(),
            event_count: 0,
            trace_count: 0,
            open_traces: BTreeMap::new(),
        }
    }

    /// Detect cut type from accumulated DFG and build a Petri net.
    ///
    /// Cut detection priority:
    /// 1. Sequential: activities form a total order (single start/end per group)
    /// 2. Exclusive: activities never co-occur in traces
    /// 3. Parallel: symmetric edges exist between groups
    /// 4. Loop: start activities overlap with end activities
    /// 5. Fallback: treat as a flower model (single silent transition loop)
    pub fn to_petrinet(&self) -> PetriNet {
        let net = PetriNet::new();

        if self.activity_counts.is_empty() || self.trace_count == 0 {
            return net;
        }

        let n = self.interner.len();
        let activities: Vec<u32> = (0..n as u32)
            .filter(|&id| self.activity_counts.get(id as usize).copied().unwrap_or(0) > 0)
            .collect();

        if activities.is_empty() {
            return net;
        }

        // Build sets for cut detection
        let starts: HashSet<u32> = self.start_counts.keys().cloned().collect();
        let ends: HashSet<u32> = self.end_counts.keys().cloned().collect();
        let mut successors: BTreeMap<u32, HashSet<u32>> = BTreeMap::new();
        let mut predecessors: BTreeMap<u32, HashSet<u32>> = BTreeMap::new();

        for &(from, to) in self.edge_counts.keys() {
            successors.entry(from).or_default().insert(to);
            predecessors.entry(to).or_default().insert(from);
        }

        // Check for sequential cut: activities form a topological order
        let sequential_order =
            self.detect_sequential_order(&activities, &starts, &ends, &successors);
        if let Some(order) = sequential_order {
            return self.build_sequential_net(&order);
        }

        // Check for exclusive cut: disjoint groups with no edges between them
        let exclusive_groups = self.detect_exclusive_groups(&activities, &successors);
        if exclusive_groups.len() >= 2 {
            return self.build_exclusive_net(&exclusive_groups);
        }

        // Check for parallel cut: symmetric edges between groups
        let parallel_groups = self.detect_parallel_groups(&activities, &successors);
        if parallel_groups.len() >= 2 {
            return self.build_parallel_net(&parallel_groups);
        }

        // Check for loop cut: start activities overlap with end activities
        let loop_activities: HashSet<u32> = starts.intersection(&ends).cloned().collect();
        if !loop_activities.is_empty() && loop_activities.len() < activities.len() {
            return self.build_loop_net(&activities, &loop_activities);
        }

        // Fallback: flower model with source → silent transition → sink
        self.build_flower_net(&activities)
    }

    /// Detect sequential order: each activity group has exactly one start and one end.
    fn detect_sequential_order(
        &self,
        activities: &[u32],
        starts: &HashSet<u32>,
        _ends: &HashSet<u32>,
        successors: &BTreeMap<u32, HashSet<u32>>,
    ) -> Option<Vec<Vec<u32>>> {
        // Sequential cut: partition activities into ordered groups where
        // each group has a single start and single end activity
        if activities.len() <= 1 {
            return None;
        }

        // Try to build a chain: each activity's successors determine the order
        let mut order: Vec<Vec<u32>> = Vec::new();
        let mut visited: HashSet<u32> = HashSet::new();
        let mut remaining: HashSet<u32> = activities.iter().copied().collect();

        // Start from activities that have no predecessors (or only from start activities)
        let has_predecessor: HashSet<u32> = activities
            .iter()
            .filter(|id| successors.values().any(|s| s.contains(id)))
            .copied()
            .collect();

        let mut current_starts: Vec<u32> = activities
            .iter()
            .filter(|id| !has_predecessor.contains(id) || starts.contains(id))
            .cloned()
            .collect();
        current_starts.sort_unstable();

        if current_starts.is_empty() {
            return None;
        }

        while !current_starts.is_empty() {
            let group: Vec<u32> = current_starts
                .iter()
                .filter(|id| remaining.contains(id))
                .cloned()
                .collect();

            if group.is_empty() {
                break;
            }

            order.push(group.clone());
            for id in &group {
                visited.insert(*id);
                remaining.remove(id);
            }

            // Next group: activities reachable from current group but not yet visited
            let mut next: HashSet<u32> = HashSet::new();
            for id in &group {
                if let Some(succs) = successors.get(id) {
                    for &s in succs {
                        if !visited.contains(&s) {
                            next.insert(s);
                        }
                    }
                }
            }
            current_starts = next.into_iter().collect();
            current_starts.sort_unstable();
        }

        // Only accept if we covered all activities and found a multi-group sequence
        if remaining.is_empty() && order.len() >= 2 {
            Some(order)
        } else {
            None
        }
    }

    /// Detect exclusive groups: groups of activities with no edges between them.
    fn detect_exclusive_groups(
        &self,
        activities: &[u32],
        successors: &BTreeMap<u32, HashSet<u32>>,
    ) -> Vec<Vec<u32>> {
        // Build connectivity graph
        let activity_set: HashSet<u32> = activities.iter().copied().collect();

        // Find connected components
        let mut groups: Vec<HashSet<u32>> = Vec::new();
        let mut visited: HashSet<u32> = HashSet::new();

        for &start in activities {
            if visited.contains(&start) {
                continue;
            }

            let mut group: HashSet<u32> = HashSet::new();
            let mut stack = vec![start];

            while let Some(current) = stack.pop() {
                if group.contains(&current) {
                    continue;
                }
                if !activity_set.contains(&current) {
                    continue;
                }
                group.insert(current);

                if let Some(succs) = successors.get(&current) {
                    for &s in succs {
                        if !group.contains(&s) {
                            stack.push(s);
                        }
                    }
                }
                // Also follow reverse edges
                for (&from, succs) in successors {
                    if succs.contains(&current) && !group.contains(&from) {
                        stack.push(from);
                    }
                }
            }

            visited.extend(&group);
            groups.push(group);
        }

        // Only return as exclusive if there are multiple disconnected components
        let result: Vec<Vec<u32>> = groups
            .into_iter()
            .map(|g| g.into_iter().collect())
            .filter(|g: &Vec<u32>| !g.is_empty())
            .collect();

        if result.len() >= 2 {
            result
        } else {
            Vec::new()
        }
    }

    /// Detect parallel groups: find activities connected by symmetric edges.
    fn detect_parallel_groups(
        &self,
        activities: &[u32],
        _successors: &BTreeMap<u32, HashSet<u32>>,
    ) -> Vec<Vec<u32>> {
        if activities.len() <= 1 {
            return Vec::new();
        }

        // Find parallel pairs: both (a,b) and (b,a) exist in edge_counts
        let mut parallel_pairs: HashSet<(u32, u32)> = HashSet::new();
        for &(from, to) in self.edge_counts.keys() {
            if self.edge_counts.contains_key(&(to, from)) {
                parallel_pairs.insert((from.min(to), from.max(to)));
            }
        }

        if parallel_pairs.is_empty() {
            return Vec::new();
        }

        // Group activities connected by parallel edges
        let mut groups: Vec<HashSet<u32>> = Vec::new();
        let mut visited: HashSet<u32> = HashSet::new();

        for &start in activities {
            if visited.contains(&start) {
                continue;
            }

            let mut group: HashSet<u32> = HashSet::new();
            let mut stack = vec![start];

            while let Some(current) = stack.pop() {
                if group.contains(&current) {
                    continue;
                }
                group.insert(current);

                for &(a, b) in &parallel_pairs {
                    if a == current && !group.contains(&b) {
                        stack.push(b);
                    }
                    if b == current && !group.contains(&a) {
                        stack.push(a);
                    }
                }
            }

            visited.extend(&group);
            if !group.is_empty() {
                groups.push(group);
            }
        }

        let result: Vec<Vec<u32>> = groups
            .into_iter()
            .map(|g| g.into_iter().collect())
            .collect();

        if result.len() >= 2 {
            result
        } else {
            Vec::new()
        }
    }

    /// Build Petri net from sequential cut groups.
    fn build_sequential_net(&self, order: &[Vec<u32>]) -> PetriNet {
        let mut net = PetriNet::new();

        let source_id = "p_source";
        net.places.push(PetriNetPlace {
            id: source_id.to_string(),
            label: source_id.to_string(),
            marking: Some(1),
        });
        net.initial_marking.insert(source_id.to_string(), 1);

        for (group_idx, group) in order.iter().enumerate() {
            let place_id = format!("p_seq_{}", group_idx);

            net.places.push(PetriNetPlace {
                id: place_id.clone(),
                label: place_id.clone(),
                marking: None,
            });

            for &act_id in group {
                let name = self.interner.get(act_id).unwrap_or("?");
                let t_id = format!("t_{}", name);

                net.transitions.push(PetriNetTransition {
                    id: t_id.clone(),
                    label: name.to_string(),
                    is_invisible: None,
                });

                // Only connect from p_seq_N to transitions for N > 0;
                // group 0 is fed exclusively by p_source (added below).
                if group_idx > 0 {
                    net.arcs.push(PetriNetArc {
                        from: place_id.clone(),
                        to: t_id.clone(),
                        weight: None,
                    });
                }

                let next_place_id = if group_idx + 1 < order.len() {
                    format!("p_seq_{}", group_idx + 1)
                } else {
                    "p_sink".to_string()
                };

                net.arcs.push(PetriNetArc {
                    from: t_id,
                    to: next_place_id,
                    weight: None,
                });
            }
        }

        // Connect source to first group
        if let Some(first_group) = order.first() {
            for &act_id in first_group {
                let name = self.interner.get(act_id).unwrap_or("?");
                let t_id = format!("t_{}", name);
                net.arcs.push(PetriNetArc {
                    from: source_id.to_string(),
                    to: t_id,
                    weight: None,
                });
            }
        }

        // Add sink
        net.places.push(PetriNetPlace {
            id: "p_sink".to_string(),
            label: "p_sink".to_string(),
            marking: None,
        });

        // Deduplicate arcs
        let mut seen: HashSet<(String, String)> = HashSet::new();
        net.arcs
            .retain(|a| seen.insert((a.from.clone(), a.to.clone())));

        net
    }

    /// Build Petri net from exclusive groups (choice construct).
    fn build_exclusive_net(&self, groups: &[Vec<u32>]) -> PetriNet {
        let mut net = PetriNet::new();

        let source_id = "p_source";
        let sink_id = "p_sink";

        net.places.push(PetriNetPlace {
            id: source_id.to_string(),
            label: source_id.to_string(),
            marking: Some(1),
        });
        net.initial_marking.insert(source_id.to_string(), 1);
        net.places.push(PetriNetPlace {
            id: sink_id.to_string(),
            label: sink_id.to_string(),
            marking: None,
        });

        for group in groups {
            let place_id = format!(
                "p_excl_{}",
                group
                    .first()
                    .map_or("?", |id| self.interner.get(*id).unwrap_or("?"))
            );

            net.places.push(PetriNetPlace {
                id: place_id.clone(),
                label: place_id.clone(),
                marking: None,
            });

            // Source → place → first transition
            net.arcs.push(PetriNetArc {
                from: source_id.to_string(),
                to: place_id.clone(),
                weight: None,
            });

            for &act_id in group.iter() {
                let name = self.interner.get(act_id).unwrap_or("?");
                let t_id = format!("t_{}", name);

                net.transitions.push(PetriNetTransition {
                    id: t_id.clone(),
                    label: name.to_string(),
                    is_invisible: None,
                });

                net.arcs.push(PetriNetArc {
                    from: place_id.clone(),
                    to: t_id.clone(),
                    weight: None,
                });

                net.arcs.push(PetriNetArc {
                    from: t_id.clone(),
                    to: sink_id.to_string(),
                    weight: None,
                });
            }
        }

        // Deduplicate transitions and arcs
        let mut seen_t: HashSet<String> = HashSet::new();
        net.transitions.retain(|t| seen_t.insert(t.id.clone()));
        let mut seen_a: HashSet<(String, String)> = HashSet::new();
        net.arcs
            .retain(|a| seen_a.insert((a.from.clone(), a.to.clone())));

        net
    }

    /// Build Petri net from parallel groups.
    fn build_parallel_net(&self, groups: &[Vec<u32>]) -> PetriNet {
        let mut net = PetriNet::new();

        let source_id = "p_source";
        let sink_id = "p_sink";

        net.places.push(PetriNetPlace {
            id: source_id.to_string(),
            label: source_id.to_string(),
            marking: Some(1),
        });
        net.initial_marking.insert(source_id.to_string(), 1);
        net.places.push(PetriNetPlace {
            id: sink_id.to_string(),
            label: sink_id.to_string(),
            marking: None,
        });

        for (group_idx, group) in groups.iter().enumerate() {
            let group_place = format!("p_par_{}", group_idx);

            net.places.push(PetriNetPlace {
                id: group_place.clone(),
                label: group_place.clone(),
                marking: None,
            });

            // Source → group place
            net.arcs.push(PetriNetArc {
                from: source_id.to_string(),
                to: group_place.clone(),
                weight: None,
            });

            for &act_id in group {
                let name = self.interner.get(act_id).unwrap_or("?");
                let t_id = format!("t_{}", name);

                net.transitions.push(PetriNetTransition {
                    id: t_id.clone(),
                    label: name.to_string(),
                    is_invisible: None,
                });

                net.arcs.push(PetriNetArc {
                    from: group_place.clone(),
                    to: t_id.clone(),
                    weight: None,
                });

                net.arcs.push(PetriNetArc {
                    from: t_id,
                    to: sink_id.to_string(),
                    weight: None,
                });
            }
        }

        // Deduplicate transitions and arcs
        let mut seen_t: HashSet<String> = HashSet::new();
        net.transitions.retain(|t| seen_t.insert(t.id.clone()));
        let mut seen_a: HashSet<(String, String)> = HashSet::new();
        net.arcs
            .retain(|a| seen_a.insert((a.from.clone(), a.to.clone())));

        net
    }

    /// Build Petri net with loop construct.
    fn build_loop_net(&self, activities: &[u32], loop_activities: &HashSet<u32>) -> PetriNet {
        let mut net = PetriNet::new();

        let source_id = "p_source";
        let sink_id = "p_sink";
        let loop_place_id = "p_loop";

        net.places.push(PetriNetPlace {
            id: source_id.to_string(),
            label: source_id.to_string(),
            marking: Some(1),
        });
        net.initial_marking.insert(source_id.to_string(), 1);
        net.places.push(PetriNetPlace {
            id: sink_id.to_string(),
            label: sink_id.to_string(),
            marking: None,
        });
        net.places.push(PetriNetPlace {
            id: loop_place_id.to_string(),
            label: loop_place_id.to_string(),
            marking: None,
        });

        // Silent transition for loop body
        let tau_id = "t_tau_loop";
        net.transitions.push(PetriNetTransition {
            id: tau_id.to_string(),
            label: "tau".to_string(),
            is_invisible: Some(true),
        });

        // Source → tau → loop_place
        net.arcs.push(PetriNetArc {
            from: source_id.to_string(),
            to: tau_id.to_string(),
            weight: None,
        });
        net.arcs.push(PetriNetArc {
            from: tau_id.to_string(),
            to: loop_place_id.to_string(),
            weight: None,
        });

        for &act_id in activities {
            let name = self.interner.get(act_id).unwrap_or("?");
            let t_id = format!("t_{}", name);

            net.transitions.push(PetriNetTransition {
                id: t_id.clone(),
                label: name.to_string(),
                is_invisible: None,
            });

            // Loop place → activity
            net.arcs.push(PetriNetArc {
                from: loop_place_id.to_string(),
                to: t_id.clone(),
                weight: None,
            });

            // Activity → sink or back to loop place
            net.arcs.push(PetriNetArc {
                from: t_id.clone(),
                to: sink_id.to_string(),
                weight: None,
            });

            if loop_activities.contains(&act_id) {
                // Loop back: activity → loop place
                net.arcs.push(PetriNetArc {
                    from: t_id,
                    to: loop_place_id.to_string(),
                    weight: None,
                });
            }
        }

        net
    }

    /// Build flower model (fallback): source → all activities → sink with loop.
    fn build_flower_net(&self, activities: &[u32]) -> PetriNet {
        let mut net = PetriNet::new();

        let source_id = "p_source";
        let sink_id = "p_sink";
        let body_id = "p_body";

        net.places.push(PetriNetPlace {
            id: source_id.to_string(),
            label: source_id.to_string(),
            marking: Some(1),
        });
        net.initial_marking.insert(source_id.to_string(), 1);
        net.places.push(PetriNetPlace {
            id: sink_id.to_string(),
            label: sink_id.to_string(),
            marking: None,
        });
        net.places.push(PetriNetPlace {
            id: body_id.to_string(),
            label: body_id.to_string(),
            marking: None,
        });

        // Silent transition from source to body
        let tau_id = "t_tau_flower";
        net.transitions.push(PetriNetTransition {
            id: tau_id.to_string(),
            label: "tau".to_string(),
            is_invisible: Some(true),
        });
        net.arcs.push(PetriNetArc {
            from: source_id.to_string(),
            to: tau_id.to_string(),
            weight: None,
        });
        net.arcs.push(PetriNetArc {
            from: tau_id.to_string(),
            to: body_id.to_string(),
            weight: None,
        });

        for &act_id in activities {
            let name = self.interner.get(act_id).unwrap_or("?");
            let t_id = format!("t_{}", name);

            net.transitions.push(PetriNetTransition {
                id: t_id.clone(),
                label: name.to_string(),
                is_invisible: None,
            });

            // Body → activity → body (loop)
            net.arcs.push(PetriNetArc {
                from: body_id.to_string(),
                to: t_id.clone(),
                weight: None,
            });
            net.arcs.push(PetriNetArc {
                from: t_id.clone(),
                to: body_id.to_string(),
                weight: None,
            });

            // Activity → sink (exit)
            net.arcs.push(PetriNetArc {
                from: t_id,
                to: sink_id.to_string(),
                weight: None,
            });
        }

        net
    }
}

impl StreamingAlgorithm for StreamingInductiveBuilder {
    type Model = PetriNet;

    fn new() -> Self {
        Self::new()
    }

    #[inline]
    fn add_event(&mut self, case_id: &str, activity: &str) {
        let id = self.intern(activity);
        self.open_traces
            .entry(case_id.to_owned())
            .or_default()
            .push(id);

        if id as usize >= self.activity_counts.len() {
            self.activity_counts.resize(id as usize + 1, 0);
        }

        self.event_count += 1;
    }

    #[inline]
    fn close_trace(&mut self, case_id: &str) -> bool {
        let Some(events) = self.open_traces.remove(case_id) else {
            return false;
        };
        if events.is_empty() {
            return true;
        }

        for &id in &events {
            self.activity_counts[id as usize] += 1;
        }

        for pair in events.windows(2) {
            *self.edge_counts.entry((pair[0], pair[1])).or_default() += 1;
        }

        *self.start_counts.entry(events[0]).or_default() += 1;
        if let Some(last) = events.last() {
            *self.end_counts.entry(*last).or_default() += 1;
        }

        self.trace_count += 1;
        true
    }

    fn snapshot(&self) -> Self::Model {
        self.to_petrinet()
    }

    fn stats(&self) -> StreamStats {
        let open_trace_events: usize = self.open_traces.values().map(|v| v.len()).sum();
        let memory_bytes = self.open_traces.len()
            * (std::mem::size_of::<String>() + std::mem::size_of::<Vec<u32>>())
            + open_trace_events * std::mem::size_of::<u32>()
            + self.activity_counts.len() * std::mem::size_of::<usize>()
            + self.edge_counts.len()
                * (std::mem::size_of::<(u32, u32)>() + std::mem::size_of::<usize>());

        StreamStats {
            event_count: self.event_count,
            trace_count: self.trace_count,
            open_traces: self.open_traces.len(),
            memory_bytes,
            activities: self.interner.len(),
        }
    }

    fn open_trace_ids(&self) -> Vec<String> {
        self.open_traces.keys().cloned().collect()
    }
}

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

    #[test]
    fn test_inductive_basic() {
        let mut stream = StreamingInductiveBuilder::new();

        stream.add_event("case1", "A");
        stream.add_event("case1", "B");
        stream.add_event("case1", "C");
        stream.close_trace("case1");

        let stats = stream.stats();
        assert_eq!(stats.event_count, 3);
        assert_eq!(stats.trace_count, 1);
    }

    #[test]
    fn test_inductive_sequential_cut() {
        let mut stream = StreamingInductiveBuilder::new();

        // Pure sequential: A → B → C always
        for i in 0..3 {
            stream.add_event(&format!("c{}", i), "A");
            stream.add_event(&format!("c{}", i), "B");
            stream.add_event(&format!("c{}", i), "C");
            stream.close_trace(&format!("c{}", i));
        }

        let net = stream.snapshot();
        assert!(
            !net.places.is_empty(),
            "should have places for sequential cut"
        );
        assert!(!net.transitions.is_empty(), "should have transitions");
        assert!(!net.arcs.is_empty(), "should have arcs");
    }

    #[test]
    fn test_inductive_empty_log() {
        let stream = StreamingInductiveBuilder::new();
        let net = stream.snapshot();
        assert!(net.places.is_empty());
        assert!(net.transitions.is_empty());
    }

    #[test]
    fn test_inductive_parallel_detection() {
        let mut stream = StreamingInductiveBuilder::new();

        // Parallel: A and B in both orders
        stream.add_event("c1", "A");
        stream.add_event("c1", "B");
        stream.close_trace("c1");

        stream.add_event("c2", "B");
        stream.add_event("c2", "A");
        stream.close_trace("c2");

        let net = stream.snapshot();
        assert!(
            !net.transitions.is_empty(),
            "should detect parallel and create transitions"
        );
    }

    #[test]
    fn test_inductive_exclusive_detection() {
        let mut stream = StreamingInductiveBuilder::new();

        // Exclusive: A or B but never together
        for i in 0..3 {
            stream.add_event(&format!("c{}", i), "A");
            stream.close_trace(&format!("c{}", i));
        }
        for i in 3..6 {
            stream.add_event(&format!("c{}", i), "B");
            stream.close_trace(&format!("c{}", i));
        }

        let net = stream.snapshot();
        assert!(
            !net.transitions.is_empty(),
            "should detect exclusive and create transitions"
        );
    }
}