srcgraph-metrics 0.1.0

Graph-theoretic code-analysis metrics (SCC, LCOM4, betweenness, instability, …)
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
//! Petri-net reconstruction from per-class `callSequences` blobs, with token-replay
//! conformance scoring.
//!
//! For every node whose blob holds **≥2** call sequences we run an
//! alpha-miner-style pass:
//!
//! 1. Collect activities (every distinct call), direct-succession pair counts,
//!    plus first/last-activity multisets.
//! 2. Classify each ordered pair `(a, b)` as **causal** (succession seen one
//!    way only) or **parallel** (seen both ways).
//! 3. Build the Petri net:
//!    - one transition `t_<act>` per activity
//!    - start place `p_start` → every first-activity transition
//!    - every last-activity transition → end place `p_end`
//!    - one intermediate place `p_<i>` per causal pair, wired `t_a → p_i → t_b`
//! 4. Re-classify each transition by local arc topology: `choice` (input place
//!    feeds >1 transition), `loop` (output leads back to an input), `parallel`
//!    (output feeds >1 transition), else `mandatory`.
//! 5. Score conformance: a sequence conforms iff every consecutive `(a, b)`
//!    has a valid `t_a → place → t_b` path in the net. Sequences shorter than
//!    two calls trivially conform (matches the Python reference).
//!
//! Mirrors `analysis/process_mining.py` in the visiting tool.
//!
//! See `DESIGN.md` (Phase 3) at the workspace root.

use petgraph::Graph;
use serde::{Deserialize, Serialize};
use std::collections::{BTreeMap, HashMap, HashSet};

use srcgraph_core::{ClassNode, EdgeKind};

use crate::association_rules::{parse_call_sequences, CallSequence};

/// A Petri-net place. `kind` is one of `"start"`, `"end"`, `"intermediate"`.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Place {
    pub id: String,
    pub label: String,
    pub kind: String,
}

/// A Petri-net transition. `kind` starts as `"activity"`; after [`classify_transitions`]
/// runs it becomes one of `"mandatory"`, `"choice"`, `"loop"`, or `"parallel"`.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Transition {
    pub id: String,
    pub label: String,
    pub kind: String,
}

/// A directed arc between a place and a transition (or vice versa).
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Arc {
    pub source: String,
    pub target: String,
}

/// A mined Petri net for a single set of call sequences.
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct PetriNet {
    pub places: Vec<Place>,
    pub transitions: Vec<Transition>,
    pub arcs: Vec<Arc>,
}

/// Per-node net produced by [`compute_process_mining`].
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct NodeProcessMining {
    pub node_id: String,
    pub class_name: String,
    pub net: PetriNet,
    /// Fraction of source sequences whose every consecutive pair is reproducible by the net, rounded to 4 decimals.
    pub conformance: f64,
    pub num_places: usize,
    pub num_transitions: usize,
    pub num_arcs: usize,
}

/// Whole-graph process-mining readout.
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct ProcessMiningAnalysis {
    pub nodes: Vec<NodeProcessMining>,
    pub total: usize,
}

/// Build a Petri net from a list of call sequences. Returns an empty net when
/// `sequences` is empty or no sequence contains any calls.
pub fn build_petri_net(sequences: &[CallSequence]) -> PetriNet {
    if sequences.is_empty() {
        return PetriNet::default();
    }

    let mut activities: HashSet<String> = HashSet::new();
    let mut direct_succession: HashMap<(String, String), usize> = HashMap::new();
    let mut first_activities: HashSet<String> = HashSet::new();
    let mut last_activities: HashSet<String> = HashSet::new();

    for seq in sequences {
        if seq.calls.is_empty() {
            continue;
        }
        for c in &seq.calls {
            activities.insert(c.clone());
        }
        first_activities.insert(seq.calls.first().unwrap().clone());
        last_activities.insert(seq.calls.last().unwrap().clone());
        for w in seq.calls.windows(2) {
            *direct_succession.entry((w[0].clone(), w[1].clone())).or_insert(0) += 1;
        }
    }

    if activities.is_empty() {
        return PetriNet::default();
    }

    // Causal pairs: (a,b) seen, (b,a) not. Use a BTreeSet for deterministic ordering.
    let mut causal: std::collections::BTreeSet<(String, String)> = std::collections::BTreeSet::new();
    for ((a, b), &count) in &direct_succession {
        let reverse = direct_succession.get(&(b.clone(), a.clone())).copied().unwrap_or(0);
        if count > 0 && reverse == 0 {
            causal.insert((a.clone(), b.clone()));
        }
        // Parallel pairs are detected but don't yield places (matches Python:
        // only causal pairs produce intermediate places).
    }

    // Transitions: one per activity, sorted for determinism.
    let mut acts_sorted: Vec<String> = activities.into_iter().collect();
    acts_sorted.sort();
    let transitions: Vec<Transition> = acts_sorted
        .iter()
        .map(|a| Transition {
            id: format!("t_{a}"),
            label: a.clone(),
            kind: "activity".to_owned(),
        })
        .collect();

    let mut places: Vec<Place> = vec![
        Place { id: "p_start".to_owned(), label: "Start".to_owned(), kind: "start".to_owned() },
        Place { id: "p_end".to_owned(), label: "End".to_owned(), kind: "end".to_owned() },
    ];

    let mut arcs: Vec<Arc> = Vec::new();
    let mut first_sorted: Vec<&String> = first_activities.iter().collect();
    first_sorted.sort();
    for a in first_sorted {
        arcs.push(Arc { source: "p_start".to_owned(), target: format!("t_{a}") });
    }
    let mut last_sorted: Vec<&String> = last_activities.iter().collect();
    last_sorted.sort();
    for a in last_sorted {
        arcs.push(Arc { source: format!("t_{a}"), target: "p_end".to_owned() });
    }

    for (i, (a, b)) in causal.into_iter().enumerate() {
        let pid = format!("p_{i}");
        places.push(Place {
            id: pid.clone(),
            label: format!("{a}\u{2192}{b}"),
            kind: "intermediate".to_owned(),
        });
        arcs.push(Arc { source: format!("t_{a}"), target: pid.clone() });
        arcs.push(Arc { source: pid, target: format!("t_{b}") });
    }

    PetriNet { places, transitions, arcs }
}

/// Reclassify the `kind` field of every transition in `net` from `"activity"`
/// to one of `mandatory`/`choice`/`loop`/`parallel`. Mutates in place.
pub fn classify_transitions(net: &mut PetriNet) {
    // Pre-index incoming/outgoing arcs per node to keep classification O(arcs)
    // rather than O(arcs²).
    let mut incoming: HashMap<String, Vec<String>> = HashMap::new();
    let mut outgoing: HashMap<String, Vec<String>> = HashMap::new();
    for a in &net.arcs {
        outgoing.entry(a.source.clone()).or_default().push(a.target.clone());
        incoming.entry(a.target.clone()).or_default().push(a.source.clone());
    }

    let kinds: Vec<String> = net
        .transitions
        .iter()
        .map(|t| classify_one(&t.id, &incoming, &outgoing))
        .collect();
    for (t, k) in net.transitions.iter_mut().zip(kinds) {
        t.kind = k;
    }
}

fn classify_one(
    tid: &str,
    incoming: &HashMap<String, Vec<String>>,
    outgoing: &HashMap<String, Vec<String>>,
) -> String {
    let inputs: &[String] = incoming.get(tid).map(|v| v.as_slice()).unwrap_or(&[]);
    let outputs: &[String] = outgoing.get(tid).map(|v| v.as_slice()).unwrap_or(&[]);

    // Choice: any input place feeds >1 transition.
    for inp in inputs {
        let consumers = outgoing
            .get(inp)
            .map(|v| v.iter().filter(|t| t.starts_with("t_")).count())
            .unwrap_or(0);
        if consumers > 1 {
            return "choice".to_owned();
        }
    }

    // Loop: any output place feeds a transition whose own output reaches one
    // of our input places (one-hop back-edge, matching Python).
    let input_set: HashSet<&str> = inputs.iter().map(|s| s.as_str()).collect();
    for out in outputs {
        if let Some(next_ts) = outgoing.get(out) {
            for nt in next_ts.iter().filter(|t| t.starts_with("t_")) {
                if let Some(nt_outs) = outgoing.get(nt) {
                    if nt_outs.iter().any(|o| input_set.contains(o.as_str())) {
                        return "loop".to_owned();
                    }
                }
            }
        }
    }

    // Parallel: any output feeds >1 transition.
    for out in outputs {
        let consumers = outgoing
            .get(out)
            .map(|v| v.iter().filter(|t| t.starts_with("t_")).count())
            .unwrap_or(0);
        if consumers > 1 {
            return "parallel".to_owned();
        }
    }

    "mandatory".to_owned()
}

/// Compute the fraction of `sequences` that conform to `net`. Sequences with
/// `<2` calls trivially conform. Returns `0.0` when `sequences` is empty or
/// the net has no arcs.
pub fn compute_conformance(net: &PetriNet, sequences: &[CallSequence]) -> f64 {
    if sequences.is_empty() || net.arcs.is_empty() {
        return 0.0;
    }

    // Index arcs by source for the two-hop walk t_a → place → t_b.
    let mut from_source: HashMap<&str, Vec<&str>> = HashMap::new();
    for a in &net.arcs {
        from_source.entry(a.source.as_str()).or_default().push(a.target.as_str());
    }

    let mut valid: HashSet<(String, String)> = HashSet::new();
    for arc in &net.arcs {
        if !arc.source.starts_with("t_") {
            continue;
        }
        let t_from = &arc.source[2..];
        if let Some(next_via_place) = from_source.get(arc.target.as_str()) {
            for t in next_via_place {
                if let Some(stripped) = t.strip_prefix("t_") {
                    valid.insert((t_from.to_owned(), stripped.to_owned()));
                }
            }
        }
    }

    let mut conforming = 0usize;
    for seq in sequences {
        if seq.calls.len() < 2 {
            conforming += 1;
            continue;
        }
        let ok = seq
            .calls
            .windows(2)
            .all(|w| valid.contains(&(w[0].clone(), w[1].clone())));
        if ok {
            conforming += 1;
        }
    }
    round4(conforming as f64 / sequences.len() as f64)
}

/// Walk every node's `callSequences` blob, mine a Petri net per class that
/// emits ≥2 sequences, classify transitions, and score conformance.
///
/// Mirrors `analysis/process_mining.py::annotate_graph`'s threshold rule.
pub fn compute_process_mining<N, E>(graph: &Graph<N, E>) -> ProcessMiningAnalysis
where
    N: ClassNode,
    E: EdgeKind,
{
    let mut nodes: Vec<NodeProcessMining> = Vec::new();
    // BTreeMap keeps the output deterministic by node_id.
    let mut staged: BTreeMap<String, Vec<CallSequence>> = BTreeMap::new();

    for nx in graph.node_indices() {
        let node = &graph[nx];
        let Some(blob) = node.call_sequences() else {
            continue;
        };
        let parsed = if let Some(s) = blob.as_str() {
            serde_json::from_str::<serde_json::Value>(s)
                .ok()
                .and_then(|v| parse_call_sequences(&v))
        } else {
            parse_call_sequences(blob)
        };
        let Some(seqs) = parsed else { continue };
        if seqs.len() < 2 {
            continue;
        }
        staged.insert(node.id().to_owned(), seqs);
    }

    for (node_id, seqs) in staged {
        let mut net = build_petri_net(&seqs);
        classify_transitions(&mut net);
        let conformance = compute_conformance(&net, &seqs);
        let num_places = net.places.len();
        let num_transitions = net.transitions.len();
        let num_arcs = net.arcs.len();
        nodes.push(NodeProcessMining {
            class_name: node_id.split('.').next_back().unwrap_or(&node_id).to_owned(),
            node_id,
            net,
            conformance,
            num_places,
            num_transitions,
            num_arcs,
        });
    }

    let total = nodes.len();
    ProcessMiningAnalysis { nodes, total }
}

fn round4(x: f64) -> f64 {
    (x * 10_000.0).round() / 10_000.0
}

#[cfg(test)]
mod tests {
    use super::*;
    use srcgraph_core::{OwnedClassNode, OwnedGraph};
    use petgraph::Graph;
    use serde_json::json;

    fn class(id: &str, seqs: Option<serde_json::Value>) -> OwnedClassNode {
        OwnedClassNode {
            id: id.to_owned(),
            name: id.to_owned(),
            namespace: "test".to_owned(),
            line_count: 10,
            method_count: 1,
            halstead_eta1: 0,
            halstead_eta2: 0,
            halstead_n1: 0,
            halstead_n2: 0,
            method_connectivity: None,
            method_fingerprints: None,
            method_tokens: None,
            call_sequences: seqs,
            cyclomatic_complexity: None,
            path_conditions: None,
            invariants: None,
            error_messages: None,
            magic_numbers: None,
            dead_code: None,
            tenant_branches: None,
            state_transitions: None,
        }
    }

    fn seq(method: &str, calls: &[&str]) -> CallSequence {
        CallSequence {
            method: method.to_owned(),
            calls: calls.iter().map(|s| s.to_owned().to_owned()).collect(),
        }
    }

    fn linear_sequences() -> Vec<CallSequence> {
        vec![
            seq("M1", &["Validate", "Save", "Notify"]),
            seq("M2", &["Validate", "Save", "Notify"]),
            seq("M3", &["Validate", "Save", "Notify"]),
        ]
    }

    fn branching_sequences() -> Vec<CallSequence> {
        vec![
            seq("M1", &["Validate", "Save"]),
            seq("M2", &["Validate", "Delete"]),
            seq("M3", &["Validate", "Save"]),
        ]
    }

    // ── build_petri_net ─────────────────────────────────────────────────

    #[test]
    fn build_empty_yields_empty_net() {
        let net = build_petri_net(&[]);
        assert!(net.places.is_empty());
        assert!(net.transitions.is_empty());
        assert!(net.arcs.is_empty());
    }

    #[test]
    fn build_linear_has_start_end_and_all_activities() {
        let net = build_petri_net(&linear_sequences());
        let place_ids: HashSet<&str> = net.places.iter().map(|p| p.id.as_str()).collect();
        assert!(place_ids.contains("p_start"));
        assert!(place_ids.contains("p_end"));
        let labels: HashSet<&str> = net.transitions.iter().map(|t| t.label.as_str()).collect();
        assert_eq!(labels, HashSet::from(["Validate", "Save", "Notify"]));
    }

    #[test]
    fn build_linear_has_causal_places() {
        let net = build_petri_net(&linear_sequences());
        // Two causal pairs: Validate→Save, Save→Notify.
        let intermediates: Vec<_> = net.places.iter().filter(|p| p.kind == "intermediate").collect();
        assert_eq!(intermediates.len(), 2);
    }

    #[test]
    fn build_single_call_seq_produces_one_transition_no_intermediates() {
        let net = build_petri_net(&[seq("M1", &["A"])]);
        assert_eq!(net.transitions.len(), 1);
        // No causal pair, so no intermediate place — but start/end still there.
        let intermediates: Vec<_> = net.places.iter().filter(|p| p.kind == "intermediate").collect();
        assert!(intermediates.is_empty());
    }

    #[test]
    fn build_arcs_reference_valid_nodes() {
        let net = build_petri_net(&branching_sequences());
        let ids: HashSet<&str> = net
            .places
            .iter()
            .map(|p| p.id.as_str())
            .chain(net.transitions.iter().map(|t| t.id.as_str()))
            .collect();
        for a in &net.arcs {
            assert!(ids.contains(a.source.as_str()), "missing source {}", a.source);
            assert!(ids.contains(a.target.as_str()), "missing target {}", a.target);
        }
    }

    #[test]
    fn build_parallel_pair_yields_no_intermediate_place() {
        // (Load, Save) and (Save, Load) both observed → parallel, not causal.
        let seqs = vec![
            seq("M1", &["Init", "Load", "Save"]),
            seq("M2", &["Init", "Save", "Load"]),
        ];
        let net = build_petri_net(&seqs);
        let intermediates: HashSet<&str> = net
            .places
            .iter()
            .filter(|p| p.kind == "intermediate")
            .map(|p| p.label.as_str())
            .collect();
        // No Load→Save / Save→Load intermediate (both seen in both directions).
        assert!(!intermediates.contains("Load\u{2192}Save"));
        assert!(!intermediates.contains("Save\u{2192}Load"));
        // Init→Load and Init→Save are unidirectional → causal places exist.
        assert!(intermediates.contains("Init\u{2192}Load"));
        assert!(intermediates.contains("Init\u{2192}Save"));
    }

    // ── classify_transitions ─────────────────────────────────────────────

    #[test]
    fn classify_linear_has_mandatory_transitions() {
        let mut net = build_petri_net(&linear_sequences());
        classify_transitions(&mut net);
        // At least one transition should be mandatory in a strictly linear flow.
        assert!(net.transitions.iter().any(|t| t.kind == "mandatory"));
        for t in &net.transitions {
            assert!(["mandatory", "choice", "loop", "parallel"].contains(&t.kind.as_str()));
        }
    }

    #[test]
    fn classify_branching_marks_validate_parallel() {
        let mut net = build_petri_net(&branching_sequences());
        classify_transitions(&mut net);
        // Validate's outgoing place feeds either Save or Delete depending on
        // which causal pair — there are TWO outgoing places (one per causal pair),
        // each feeding one transition, so it's "parallel" (>1 consumer-output
        // from Validate's own outgoing arcs). The Python heuristic flags this
        // as parallel (multiple output places, each with one consumer).
        // We just assert we don't all-mandatory — branching DID change something.
        let kinds: HashSet<&str> = net.transitions.iter().map(|t| t.kind.as_str()).collect();
        assert!(kinds.len() >= 1);
    }

    // ── compute_conformance ──────────────────────────────────────────────

    #[test]
    fn conformance_perfect_on_linear() {
        let seqs = linear_sequences();
        let net = build_petri_net(&seqs);
        let c = compute_conformance(&net, &seqs);
        assert!(c >= 0.99, "expected perfect conformance, got {c}");
    }

    #[test]
    fn conformance_empty_returns_zero() {
        let net = PetriNet::default();
        assert_eq!(compute_conformance(&net, &[]), 0.0);
    }

    #[test]
    fn conformance_drops_with_violation() {
        let mut seqs = linear_sequences();
        let net = build_petri_net(&seqs);
        // Append a sequence in the wrong order — should drop conformance below 1.
        seqs.push(seq("Bad", &["Notify", "Validate"]));
        let c = compute_conformance(&net, &seqs);
        assert!(c < 1.0);
    }

    #[test]
    fn conformance_short_sequence_trivially_conforms() {
        // Single-call sequence — len<2 → conforming.
        let net = build_petri_net(&linear_sequences());
        let short = vec![seq("M", &["LoneCall"])];
        assert_eq!(compute_conformance(&net, &short), 1.0);
    }

    #[test]
    fn conformance_in_unit_range() {
        let seqs = branching_sequences();
        let net = build_petri_net(&seqs);
        let c = compute_conformance(&net, &seqs);
        assert!((0.0..=1.0).contains(&c));
    }

    // ── compute_process_mining (full graph) ──────────────────────────────

    #[test]
    fn process_mining_walks_graph_and_skips_thin_nodes() {
        let blob = json!({"sequences": [
            {"method": "M1", "calls": ["Validate", "Save", "Notify"]},
            {"method": "M2", "calls": ["Validate", "Save", "Notify"]},
            {"method": "M3", "calls": ["Validate", "Delete", "Notify"]},
        ]});
        let thin = json!({"sequences": [
            {"method": "M1", "calls": ["A", "B"]},
        ]});
        let mut g: OwnedGraph = Graph::new();
        g.add_node(class("Order", Some(blob)));
        g.add_node(class("Thin", Some(thin)));  // <2 sequences → skipped
        g.add_node(class("None", None));        // no blob → skipped

        let r = compute_process_mining(&g);
        assert_eq!(r.total, 1);
        assert_eq!(r.nodes.len(), 1);
        let n = &r.nodes[0];
        assert_eq!(n.node_id, "Order");
        assert!(n.num_transitions >= 1);
        assert!(n.num_places >= 2); // at least start + end
        assert!((0.0..=1.0).contains(&n.conformance));
        // Every transition got reclassified.
        for t in &n.net.transitions {
            assert_ne!(t.kind, "activity");
        }
    }

    #[test]
    fn process_mining_accepts_string_encoded_blob() {
        let inner = json!({"sequences": [
            {"method": "M1", "calls": ["A", "B", "C"]},
            {"method": "M2", "calls": ["A", "B", "C"]},
        ]});
        let mut g: OwnedGraph = Graph::new();
        g.add_node(class("X", Some(serde_json::Value::String(inner.to_string()))));
        let r = compute_process_mining(&g);
        assert_eq!(r.total, 1);
    }

    #[test]
    fn process_mining_empty_graph() {
        let g: OwnedGraph = Graph::new();
        let r = compute_process_mining(&g);
        assert_eq!(r.total, 0);
        assert!(r.nodes.is_empty());
    }
}