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
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
//! Discovery Algorithm Audit — Determinism, Monotonicity, and Edge Case Testing
//!
//! **Audit Scope:**
//! 1. Determinism: Same input → Same output (bit-identical)
//! 2. Monotonicity: Parameter changes should have monotonic effects
//! 3. Edge Cases: Rare/boundary conditions should not crash
//!
//! **Findings:**
//! - ISSUE 1: GA/PSO/ACO use StdRng::seed_from_u64(42) (hardcoded seed)
//!   → All runs are deterministic, but seed is not configurable
//! - ISSUE 2: Heuristic Miner threshold filtering can produce empty DFGs
//!   → No guard against invalid threshold ranges
//! - ISSUE 3: Inductive Miner depth limit (100) is arbitrary
//!   → Stack overflow risk on deeply nested process trees
//! - ISSUE 4: Empty log handling inconsistent across algorithms
//!   → Some return empty DFG, others panic or return "flower" model
//! - ISSUE 5: Rare event crashes in activity vocabulary building
//!   → Non-ASCII activity names can cause panic in certain algorithms
//!
//! **Guards Implemented:**
//! - Determinism test for all stochastic algorithms
//! - Monotonicity test for parameterized algorithms (threshold, iterations)
//! - Edge case tests for empty logs, single-event logs, rare characters
//! - Consistency guards for output schema validation

use std::collections::BTreeMap;
use wasm4pm::advanced_algorithms::discover_heuristic_miner_from_log;
use wasm4pm::discovery::discover_dfg_from_log;
use wasm4pm::genetic_discovery::{
    discover_aco_algorithm_from_log, discover_genetic_algorithm_from_log,
    discover_pso_algorithm_from_log,
};
use wasm4pm::models::{AttributeValue, Event, EventLog, Trace};
use wasm4pm::more_discovery::{
    discover_inductive_miner_from_log, discover_simulated_annealing_from_log,
};

// ============================================================================
// Test Fixtures
// ============================================================================

fn empty_log() -> EventLog {
    EventLog::new()
}

fn single_trace_single_event() -> EventLog {
    let mut log = EventLog::new();
    let mut trace = Trace {
        attributes: {
            let mut m = BTreeMap::new();
            m.insert(
                "concept:name".to_string(),
                AttributeValue::String("case-1".to_string()),
            );
            m
        },
        events: vec![],
    };
    let mut attrs = BTreeMap::new();
    attrs.insert(
        "concept:name".to_string(),
        AttributeValue::String("Activity".to_string()),
    );
    trace.events.push(Event { attributes: attrs });
    log.traces.push(trace);
    log
}

fn standard_log() -> EventLog {
    let mut log = EventLog::new();
    for case in 0..10 {
        let mut trace = Trace {
            attributes: {
                let mut m = BTreeMap::new();
                m.insert(
                    "concept:name".to_string(),
                    AttributeValue::String(format!("case-{}", case)),
                );
                m
            },
            events: vec![],
        };

        for (i, act) in vec!["Start", "Process", "End"].iter().enumerate() {
            let mut attrs = BTreeMap::new();
            attrs.insert(
                "concept:name".to_string(),
                AttributeValue::String(act.to_string()),
            );
            attrs.insert(
                "time:timestamp".to_string(),
                AttributeValue::String(format!("2024-01-01T00:{:02}:00Z", i)),
            );
            trace.events.push(Event { attributes: attrs });
        }
        log.traces.push(trace);
    }
    log
}

fn rare_char_log() -> EventLog {
    let mut log = EventLog::new();
    let mut trace = Trace {
        attributes: {
            let mut m = BTreeMap::new();
            m.insert(
                "concept:name".to_string(),
                AttributeValue::String("case-rare".to_string()),
            );
            m
        },
        events: vec![],
    };

    // Test with non-ASCII characters, emoji, special symbols
    for act in &["Café", "データ処理", "🔧", "A|B"] {
        let mut attrs = BTreeMap::new();
        attrs.insert(
            "concept:name".to_string(),
            AttributeValue::String(act.to_string()),
        );
        trace.events.push(Event { attributes: attrs });
    }
    log.traces.push(trace);
    log
}

// ============================================================================
// ISSUE 1: Determinism — Stochastic algorithms MUST be deterministic
// ============================================================================

#[test]
fn ga_determinism_same_seed_bit_identical() {
    let log = standard_log();
    let (dfg1, fitness1) = discover_genetic_algorithm_from_log(&log, "concept:name", 20, 30)
        .expect("GA must succeed on standard log");
    let (dfg2, fitness2) = discover_genetic_algorithm_from_log(&log, "concept:name", 20, 30)
        .expect("GA must succeed on standard log");

    assert_eq!(
        fitness1, fitness2,
        "GA FITNESS NOT DETERMINISTIC: {:.6} vs {:.6}",
        fitness1, fitness2
    );
    assert_eq!(
        dfg1.edges.len(),
        dfg2.edges.len(),
        "GA EDGE COUNT NOT DETERMINISTIC: {} vs {}",
        dfg1.edges.len(),
        dfg2.edges.len()
    );
}

#[test]
fn pso_determinism_same_seed_bit_identical() {
    let log = standard_log();
    let (dfg1, fitness1) = discover_pso_algorithm_from_log(&log, "concept:name", 20, 30)
        .expect("PSO must succeed on standard log");
    let (dfg2, fitness2) = discover_pso_algorithm_from_log(&log, "concept:name", 20, 30)
        .expect("PSO must succeed on standard log");

    assert_eq!(
        fitness1, fitness2,
        "PSO FITNESS NOT DETERMINISTIC: {:.6} vs {:.6}",
        fitness1, fitness2
    );
    assert_eq!(
        dfg1.edges.len(),
        dfg2.edges.len(),
        "PSO EDGE COUNT NOT DETERMINISTIC: {} vs {}",
        dfg1.edges.len(),
        dfg2.edges.len()
    );
}

#[test]
fn aco_determinism_same_seed_bit_identical() {
    let log = standard_log();
    let (dfg1, fitness1) = discover_aco_algorithm_from_log(&log, "concept:name", 20, 30)
        .expect("ACO must succeed on standard log");
    let (dfg2, fitness2) = discover_aco_algorithm_from_log(&log, "concept:name", 20, 30)
        .expect("ACO must succeed on standard log");

    assert_eq!(
        fitness1, fitness2,
        "ACO FITNESS NOT DETERMINISTIC: {:.6} vs {:.6}",
        fitness1, fitness2
    );
    assert_eq!(
        dfg1.edges.len(),
        dfg2.edges.len(),
        "ACO EDGE COUNT NOT DETERMINISTIC: {} vs {}",
        dfg1.edges.len(),
        dfg2.edges.len()
    );
}

#[test]
fn sa_determinism_same_seed_bit_identical() {
    let log = standard_log();
    let (dfg1, fitness1) = discover_simulated_annealing_from_log(&log, "concept:name", 1.0, 0.95);
    let (dfg2, fitness2) = discover_simulated_annealing_from_log(&log, "concept:name", 1.0, 0.95);

    assert_eq!(
        fitness1, fitness2,
        "SA FITNESS NOT DETERMINISTIC: {:.6} vs {:.6}",
        fitness1, fitness2
    );
    assert_eq!(
        dfg1.edges.len(),
        dfg2.edges.len(),
        "SA EDGE COUNT NOT DETERMINISTIC: {} vs {}",
        dfg1.edges.len(),
        dfg2.edges.len()
    );
}

// ============================================================================
// ISSUE 2: Monotonicity — Parameter changes should have monotonic effects
// ============================================================================

#[test]
fn heuristic_miner_threshold_monotonicity() {
    // Lower threshold → More edges (monotonic property)
    let log = standard_log();

    let dfg_strict = discover_heuristic_miner_from_log(&log, "concept:name", 0.9);
    let dfg_lenient = discover_heuristic_miner_from_log(&log, "concept:name", 0.1);

    assert!(
        dfg_lenient.edges.len() >= dfg_strict.edges.len(),
        "MONOTONICITY VIOLATED: Lower threshold should produce >= edges. \
         Strict (0.9): {}, Lenient (0.1): {}",
        dfg_strict.edges.len(),
        dfg_lenient.edges.len()
    );
}

#[test]
fn ga_iterations_monotonicity() {
    // More generations → Fitness never decreases (monotonic property)
    let log = standard_log();

    let (_, f1) =
        discover_genetic_algorithm_from_log(&log, "concept:name", 20, 5).expect("GA must succeed");
    let (_, f50) =
        discover_genetic_algorithm_from_log(&log, "concept:name", 20, 50).expect("GA must succeed");

    assert!(
        f50 >= f1 - 1e-9,
        "MONOTONICITY VIOLATED: More generations should maintain fitness. \
         5-gen: {:.6}, 50-gen: {:.6}",
        f1,
        f50
    );
}

#[test]
fn pso_iterations_monotonicity() {
    // More iterations → Fitness never decreases
    let log = standard_log();

    let (_, f5) =
        discover_pso_algorithm_from_log(&log, "concept:name", 20, 5).expect("PSO must succeed");
    let (_, f50) =
        discover_pso_algorithm_from_log(&log, "concept:name", 20, 50).expect("PSO must succeed");

    assert!(
        f50 >= f5 - 1e-9,
        "MONOTONICITY VIOLATED: More iterations should maintain fitness. \
         5-iter: {:.6}, 50-iter: {:.6}",
        f5,
        f50
    );
}

// ============================================================================
// ISSUE 3: Edge Case — Empty Log
// ============================================================================

#[test]
fn dfg_empty_log_returns_empty_dfg() {
    let log = empty_log();
    let dfg = discover_dfg_from_log(&admitted_log(log.clone()), "concept:name");
    assert_eq!(
        dfg.nodes.len(),
        0,
        "DFG of empty log should have 0 nodes, got {}",
        dfg.nodes.len()
    );
    assert_eq!(
        dfg.edges.len(),
        0,
        "DFG of empty log should have 0 edges, got {}",
        dfg.edges.len()
    );
}

#[test]
fn heuristic_miner_empty_log_returns_empty_dfg() {
    let log = empty_log();
    let dfg = discover_heuristic_miner_from_log(&log, "concept:name", 0.5);
    assert_eq!(
        dfg.nodes.len(),
        0,
        "Heuristic Miner on empty log should have 0 nodes, got {}",
        dfg.nodes.len()
    );
}

#[test]
fn inductive_miner_empty_log_returns_flower() {
    let log = empty_log();
    let result = discover_inductive_miner_from_log(&admitted_log(log.clone()), "concept:name");
    // Should not panic; should return some string (flower or error)
    assert!(
        !result.is_empty(),
        "Inductive Miner should return non-empty result"
    );
}

// ============================================================================
// ISSUE 4: Edge Case — Single-Event Log
// ============================================================================

#[test]
fn dfg_single_event_returns_single_node() {
    let log = single_trace_single_event();
    let dfg = discover_dfg_from_log(&admitted_log(log.clone()), "concept:name");
    assert_eq!(
        dfg.nodes.len(),
        1,
        "DFG of single-event log should have exactly 1 node, got {}",
        dfg.nodes.len()
    );
    assert_eq!(
        dfg.edges.len(),
        0,
        "DFG of single-event log should have 0 edges (no follows), got {}",
        dfg.edges.len()
    );
}

#[test]
fn ga_single_event_no_panic() {
    let log = single_trace_single_event();
    // Should not crash; may return None if edge_vocab is empty
    let result = discover_genetic_algorithm_from_log(&log, "concept:name", 20, 30);
    // Acceptable outcomes: None (no edges to evolve) or Some with trivial fitness
    assert!(result.is_some() || result.is_none());
}

// ============================================================================
// ISSUE 5: Edge Case — Rare/Non-ASCII Characters
// ============================================================================

#[test]
fn dfg_rare_chars_no_panic() {
    let log = rare_char_log();
    let dfg = discover_dfg_from_log(&admitted_log(log.clone()), "concept:name");
    assert!(
        dfg.nodes.len() > 0,
        "DFG should handle UTF-8 activity names"
    );
    // Verify all node IDs are valid UTF-8 strings
    for node in &dfg.nodes {
        assert!(!node.id.is_empty(), "Node ID should not be empty");
    }
}

#[test]
fn heuristic_miner_rare_chars_no_panic() {
    let log = rare_char_log();
    let dfg = discover_heuristic_miner_from_log(&log, "concept:name", 0.5);
    assert!(
        dfg.nodes.len() > 0,
        "Heuristic Miner should handle UTF-8 activity names"
    );
}

#[test]
fn inductive_miner_rare_chars_no_panic() {
    let log = rare_char_log();
    let result = discover_inductive_miner_from_log(&admitted_log(log.clone()), "concept:name");
    assert!(
        !result.is_empty(),
        "Inductive Miner should handle UTF-8 activity names"
    );
}

// ============================================================================
// Consistency Guards — Output Schema Validation
// ============================================================================

#[test]
fn dfg_output_schema_valid() {
    let log = standard_log();
    let dfg = discover_dfg_from_log(&admitted_log(log.clone()), "concept:name");

    // Consistency check: all edges reference nodes
    let node_ids: std::collections::HashSet<_> = dfg.nodes.iter().map(|n| &n.id).collect();
    for edge in &dfg.edges {
        assert!(
            node_ids.contains(&edge.from),
            "DFG edge FROM '{}' not in nodes",
            edge.from
        );
        assert!(
            node_ids.contains(&edge.to),
            "DFG edge TO '{}' not in nodes",
            edge.to
        );
    }
}

#[test]
fn heuristic_miner_output_schema_valid() {
    let log = standard_log();
    let dfg = discover_heuristic_miner_from_log(&log, "concept:name", 0.5);

    let node_ids: std::collections::HashSet<_> = dfg.nodes.iter().map(|n| &n.id).collect();
    for edge in &dfg.edges {
        assert!(
            node_ids.contains(&edge.from),
            "Heuristic Miner edge FROM '{}' not in nodes",
            edge.from
        );
        assert!(
            node_ids.contains(&edge.to),
            "Heuristic Miner edge TO '{}' not in nodes",
            edge.to
        );
    }
}

// ============================================================================
// Non-Determinism Root Cause Analysis
// ============================================================================

#[test]
#[ignore] // Informational test — documents the hardcoded seed issue
fn ga_seed_is_hardcoded() {
    // FINDING: GA uses StdRng::seed_from_u64(42) — seed is hardcoded, not configurable.
    // Impact: All GA runs are deterministic (good), but users cannot seed for reproducibility.
    // Recommendation: Add optional seed parameter to discover_genetic_algorithm_from_log().
    let log = standard_log();
    let (dfg1, _) =
        discover_genetic_algorithm_from_log(&log, "concept:name", 20, 30).expect("GA must succeed");
    let (dfg2, _) =
        discover_genetic_algorithm_from_log(&log, "concept:name", 20, 30).expect("GA must succeed");

    // Both runs use same seed, so results are identical
    assert_eq!(
        dfg1.edges.len(),
        dfg2.edges.len(),
        "Hardcoded seed ensures determinism"
    );
}

fn admitted_log(
    log: wasm4pm::models::EventLog,
) -> wasm4pm_compat::evidence::Evidence<
    wasm4pm::models::EventLog,
    wasm4pm_compat::state::Admitted,
    (),
> {
    wasm4pm_compat::admission::Admission::<_, ()>::new(log).into_evidence()
}