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
use crate::models::*;
use crate::state::{get_or_init_state, StoredObject};
use crate::utilities::to_js_str;
use rustc_hash::FxHashMap;
use serde_json::json;
use smallvec::SmallVec;
use std::collections::HashSet;
use wasm_bindgen::prelude::*;

#[cfg(not(feature = "bcinr"))]
const FNV_OFFSET_BASIS: u64 = 0xcbf29ce484222325;
#[cfg(not(feature = "bcinr"))]
const FNV_PRIME: u64 = 0x100000001b3;

/// Pure-Rust Heuristic Miner without wasm-bindgen. Used by integration tests.
pub fn discover_heuristic_miner_from_log(
    log: &EventLog,
    activity_key: &str,
    dependency_threshold: f64,
) -> DFG {
    let mut dfg = DFG::new();
    let col_owned = log.to_columnar_owned(activity_key);
    let col = ColumnarLog::from_owned(&col_owned);

    dfg.nodes.extend(col.vocab.iter().map(|&act| DFGNode {
        id: act.to_owned(),
        label: act.to_owned(),
        frequency: 0,
    }));

    // Pre-size the follows map: n² / 4 is a practical upper bound for sparse DFGs.
    let n = col.vocab.len();
    let mut follows: FxHashMap<(u32, u32), usize> =
        FxHashMap::with_capacity_and_hasher(n.saturating_mul(n) / 4 + 1, Default::default());

    for t in 0..col.trace_offsets.len().saturating_sub(1) {
        let start = col.trace_offsets[t];
        let end = col.trace_offsets[t + 1];
        if start >= end {
            continue;
        }
        for &id in &col.events[start..end] {
            dfg.nodes[id as usize].frequency += 1;
        }
        for i in start..end - 1 {
            let (a, b) = (col.events[i], col.events[i + 1]);
            *follows.entry((a, b)).or_default() += 1;
        }
        *dfg.start_activities
            .entry(col.vocab[col.events[start] as usize].to_owned())
            .or_default() += 1;
        *dfg.end_activities
            .entry(col.vocab[col.events[end - 1] as usize].to_owned())
            .or_default() += 1;
    }

    for (&(a, b), &count) in &follows {
        // dep(a,b) = (|a>b| - |b>a|) / (|a>b| + |b>a| + 1) per Weijters et al.
        let reverse_count = follows.get(&(b, a)).copied().unwrap_or(0);
        let ab = f64::from(count as u32);
        let ba = f64::from(reverse_count as u32);
        if (ab - ba) / (ab + ba + 1.0) >= dependency_threshold {
            dfg.edges.push(DirectlyFollowsRelation {
                from: col.vocab[a as usize].to_owned(),
                to: col.vocab[b as usize].to_owned(),
                frequency: count,
            });
        }
    }

    dfg
}

/// Discover a process model using the Heuristic Miner algorithm.
///
/// More robust than Alpha++ for noisy, real-world logs. Filters low-frequency
/// directly-follows relations based on a dependency threshold.
///
/// # Parameters
/// * `eventlog_handle` — Handle from `load_eventlog_from_xes` / `load_eventlog_from_json`.
/// * `activity_key` — XES attribute for activity names (e.g. `"concept:name"`).
/// * `dependency_threshold` — Minimum dependency score `[0.0, 1.0]` for an edge to be included.
///   Use `0.2`–`0.4` for real-world logs; `0.8` filters out most edges.
///   **Do not use `0.8` on small logs** — it will produce empty or near-empty models.
///
/// # Returns
/// `Result<JsValue, JsValue>` — On success, a DFG JSON with `{nodes, edges}`.
///
/// # Note
/// The function uses a dependency measure rather than raw frequency. An edge `A→B` is
/// kept if `(freq(A,B) - freq(B,A)) / (freq(A,B) + freq(B,A) + 1) >= dependency_threshold`.
#[wasm_bindgen]
pub fn discover_heuristic_miner(
    eventlog_handle: &str,
    activity_key: &str,
    dependency_threshold: f64,
) -> Result<JsValue, JsValue> {
    tracing::info!(
        target: "wasm4pm.discovery.heuristic_miner",
        algorithm = "heuristic_miner",
        activity_key = activity_key,
        dependency_threshold = dependency_threshold,
        "Heuristic Miner discovery started"
    );

    // Borrow the log in-place via with_object — avoids cloning the entire EventLog.
    // discover_heuristic_miner_from_log accepts &EventLog, so no ownership needed.
    // The log_size and activity_count for tracing are derived from the DFG result,
    // eliminating the extra get_activities() pass that previously ran before the clone.
    let (dfg, log_size) = get_or_init_state().with_event_log(eventlog_handle, |log| {
        let log_size = log.traces.len();
        tracing::info!(
            target: "wasm4pm.discovery.heuristic_miner",
            checkpoint = "feature_extraction",
            log_size = log_size,
            "Log loaded"
        );
        let dfg = discover_heuristic_miner_from_log(log, activity_key, dependency_threshold);
        Ok((dfg, log_size))
    })?;

    let n_nodes = dfg.nodes.len();
    let n_edges = dfg.edges.len();

    tracing::info!(
        target: "wasm4pm.discovery.heuristic_miner",
        checkpoint = "result_generation",
        log_size = log_size,
        node_count = n_nodes,
        edge_count = n_edges,
        complexity = if n_nodes > 0 { n_edges as f64 / n_nodes as f64 } else { 0.0 },
        "DFG model constructed"
    );

    let handle = get_or_init_state()
        .store_object(StoredObject::DFG(dfg))
        .map_err(|_e| crate::error::js_val("Failed to store DFG"))?;

    to_js_str(&json!({
        "handle": handle,
        "nodes": n_nodes,
        "edges": n_edges,
        "algorithm": "heuristic_miner",
        "dependency_threshold": dependency_threshold,
    }))
}

/// Discover infrequent behavior patterns (deviations from main process)
#[wasm_bindgen]
pub fn analyze_infrequent_paths(
    eventlog_handle: &str,
    activity_key: &str,
    frequency_threshold: f64,
) -> Result<JsValue, JsValue> {
    get_or_init_state().with_event_log(eventlog_handle, |log| {
        let total_traces = log.traces.len() as f64;

        // Build activity vocabulary
        let mut vocab: std::collections::HashMap<&str, u32> = std::collections::HashMap::default();
        let mut vocab_len: u32 = 0;
        for trace in &log.traces {
            for event in &trace.events {
                if let Some(AttributeValue::String(activity)) = event.attributes.get(activity_key) {
                    vocab.entry(activity.as_str()).or_insert_with(|| {
                        let id = vocab_len;
                        vocab_len += 1;
                        id
                    });
                }
            }
        }

        let mut path_frequencies: FxHashMap<u64, (Vec<String>, usize)> = FxHashMap::default();

        // Hoisted outside the loop — reused across every trace via .clear().
        // SmallVec<[u32; 16]> covers the common case (≤16 activities) without
        // heap allocation; longer traces spill transparently.
        let mut trace_ids: SmallVec<[u32; 16]> = SmallVec::new();

        // Extract activity sequences (paths) and hash them
        for trace in &log.traces {
            trace_ids.clear();
            for event in &trace.events {
                if let Some(AttributeValue::String(activity)) = event.attributes.get(activity_key) {
                    if let Some(&id) = vocab.get(activity.as_str()) {
                        trace_ids.push(id);
                    }
                }
            }

            // Hash the u32 sequence
            #[cfg(feature = "bcinr")]
            let path_hash: u64 = trace_ids.iter().fold(0u64, |h, &id| {
                crate::bcinr_compat::sketch::fnv1a_64(&(h ^ (id as u64)).to_le_bytes())
            });

            #[cfg(not(feature = "bcinr"))]
            let path_hash: u64 = trace_ids.iter().fold(FNV_OFFSET_BASIS, |h, &id| {
                (h ^ (id as u64)).wrapping_mul(FNV_PRIME)
            });

            path_frequencies
                .entry(path_hash)
                .and_modify(|(_, count)| *count += 1)
                .or_insert_with(|| {
                    // Cold path: first time we see this variant.  Build path_str
                    // only here — skipped entirely for every subsequent occurrence.
                    let path_str = trace
                        .events
                        .iter()
                        .filter_map(|e| {
                            e.attributes
                                .get(activity_key)?
                                .as_string()
                                .map(str::to_owned)
                        })
                        .collect::<Vec<String>>();
                    (path_str, 1)
                });
        }

        // Find infrequent paths
        let total_distinct_paths = path_frequencies.len();
        let mut infrequent_paths = Vec::new();
        for (_hash, (path, count)) in path_frequencies {
            let frequency = count as f64 / total_traces;
            if frequency < frequency_threshold {
                infrequent_paths.push(json!({
                    "path": path,
                    "count": count,
                    "frequency": frequency,
                }));
            }
        }

        infrequent_paths.sort_unstable_by(|a, b| {
            let freq_a = a["frequency"].as_f64().unwrap_or(0.0);
            let freq_b = b["frequency"].as_f64().unwrap_or(0.0);
            freq_b
                .partial_cmp(&freq_a)
                .unwrap_or(std::cmp::Ordering::Equal)
                .then_with(|| {
                    let pa = a["path"].as_array().map(|v| v.len()).unwrap_or(0);
                    let pb = b["path"].as_array().map(|v| v.len()).unwrap_or(0);
                    pa.cmp(&pb)
                })
        });

        to_js_str(&json!({
            "infrequent_paths": infrequent_paths,
            "total_distinct_paths": total_distinct_paths,
            "frequency_threshold": frequency_threshold,
        }))
    })
}

/// Detect rework patterns (activities that are repeated in same trace)
#[wasm_bindgen]
pub fn detect_rework(eventlog_handle: &str, activity_key: &str) -> Result<JsValue, JsValue> {
    get_or_init_state().with_event_log(eventlog_handle, |log| {
        let mut rework_stats: FxHashMap<String, usize> = FxHashMap::default();
        let mut traces_with_rework = 0;
        let mut total_rework_count = 0;

        for trace in &log.traces {
            // Collect all activity names present in this trace into a sorted vec.
            // Sorting groups identical activities together so a single .windows(2)
            // pass can identify duplicates without a per-trace HashMap allocation.
            let mut activities: Vec<&str> = trace
                .events
                .iter()
                .filter_map(|e| e.attributes.get(activity_key)?.as_string())
                .collect();

            activities.sort_unstable();

            // Each consecutive equal pair in the sorted list represents one extra
            // occurrence (rework).  Counting them gives the rework contribution of
            // this trace without any HashMap or explicit `if` inside the loop.
            let trace_rework: usize = activities
                .windows(2)
                .filter(|w| w[0] == w[1])
                .inspect(|w| {
                    *rework_stats.entry(w[0].to_owned()).or_default() += 1;
                })
                .count();

            if trace_rework > 0 {
                traces_with_rework += 1;
                total_rework_count += trace_rework;
            }
        }

        let mut rework_vec: Vec<(String, usize)> = rework_stats.into_iter().collect();
        rework_vec.sort_unstable_by_key(|b| std::cmp::Reverse(b.1));

        to_js_str(&json!({
            "traces_with_rework": traces_with_rework,
            "rework_percentage": (traces_with_rework as f64 / log.traces.len() as f64) * 100.0,
            "total_rework_instances": total_rework_count,
            "rework_by_activity": rework_vec,
        }))
    })
}

/// Detect bottlenecks - activities with high duration or long waiting times
#[wasm_bindgen]
pub fn detect_bottlenecks(
    eventlog_handle: &str,
    activity_key: &str,
    timestamp_key: &str,
    duration_threshold_seconds: u64,
) -> Result<JsValue, JsValue> {
    get_or_init_state().with_event_log(eventlog_handle, |log| {
        let mut activity_durations: FxHashMap<String, Vec<u64>> = FxHashMap::default();

        for trace in &log.traces {
            for i in 0..trace.events.len() - 1 {
                if let (
                    Some(AttributeValue::String(activity)),
                    Some(AttributeValue::Date(start_time)),
                    Some(AttributeValue::Date(end_time)),
                ) = (
                    trace.events[i].attributes.get(activity_key),
                    trace.events[i].attributes.get(timestamp_key),
                    trace.events[i + 1].attributes.get(timestamp_key),
                ) {
                    let duration = crate::parse_iso8601_duration(start_time, end_time).abs() as u64;

                    if duration > duration_threshold_seconds {
                        activity_durations
                            .entry(activity.clone())
                            .or_default()
                            .push(duration);
                    }
                }
            }
        }

        let mut bottlenecks = Vec::new();
        for (activity, durations) in activity_durations {
            if !durations.is_empty() {
                let avg = durations.iter().sum::<u64>() as f64 / durations.len() as f64;
                let max = *durations.iter().max().unwrap_or(&0);

                bottlenecks.push(json!({
                    "activity": activity,
                    "occurrences": durations.len(),
                    "avg_duration": avg,
                    "max_duration": max,
                }));
            }
        }

        bottlenecks.sort_unstable_by(|a, b| {
            let avg_a = a["avg_duration"].as_f64().unwrap_or(0.0);
            let avg_b = b["avg_duration"].as_f64().unwrap_or(0.0);
            avg_b
                .partial_cmp(&avg_a)
                .unwrap_or(std::cmp::Ordering::Equal)
                .then_with(|| a["activity"].as_str().cmp(&b["activity"].as_str()))
        });

        to_js_str(&json!({
            "bottlenecks": bottlenecks,
            "duration_threshold": duration_threshold_seconds,
        }))
    })
}

/// Get process model complexity metrics
#[wasm_bindgen]
pub fn compute_model_metrics(
    eventlog_handle: &str,
    activity_key: &str,
) -> Result<JsValue, JsValue> {
    get_or_init_state().with_event_log(eventlog_handle, |log| {
        let activities = log.get_activities(activity_key);
        let relations = log.get_directly_follows(activity_key);

        // Calculate metrics
        let avg_degree = if !activities.is_empty() {
            (relations.len() as f64 * 2.0) / activities.len() as f64
        } else {
            0.0
        };

        // Density: ratio of actual to possible edges
        let max_edges = activities.len() * (activities.len() - 1);
        let density = if max_edges > 0 {
            relations.len() as f64 / max_edges as f64
        } else {
            0.0
        };

        // Variant count (number of unique case traces)
        let mut variants = HashSet::new();
        for trace in &log.traces {
            let mut path = Vec::new();
            for event in &trace.events {
                if let Some(AttributeValue::String(activity)) = event.attributes.get(activity_key) {
                    path.push(activity.clone());
                }
            }
            variants.insert(path);
        }

        to_js_str(&json!({
            "num_activities": activities.len(),
            "num_edges": relations.len(),
            "num_variants": variants.len(),
            "avg_degree": avg_degree,
            "density": density,
            "complexity_score": (activities.len() as f64 * variants.len() as f64).sqrt(),
        }))
    })
}

#[wasm_bindgen]
pub fn advanced_algorithms_info() -> String {
    json!({
        "status": "advanced_algorithms_available",
        "algorithms": [
            {
                "name": "heuristic_miner",
                "description": "Discovers process models with configurable dependency threshold",
                "better_for": "Real-world logs with noise and incomplete data"
            },
            {
                "name": "analyze_infrequent_paths",
                "description": "Identifies rare or exceptional process variants",
                "better_for": "Detecting outliers and uncommon behaviors"
            },
            {
                "name": "detect_rework",
                "description": "Finds activities that are repeated in the same case",
                "better_for": "Process optimization and quality assurance"
            },
            {
                "name": "detect_bottlenecks",
                "description": "Identifies slow activities with high duration",
                "better_for": "Performance analysis and optimization"
            },
            {
                "name": "compute_model_metrics",
                "description": "Calculates complexity and structure metrics",
                "better_for": "Model quality assessment"
            }
        ]
    })
    .to_string()
}