lean_ctx_ext/core/graph_analysis/
mod.rs1mod centrality;
10mod cycles;
11mod god_nodes;
12mod surprising;
13
14pub use centrality::{BridgeCentrality, compute_bridge_centrality, compute_bridge_nodes};
15pub use cycles::{ImportCycle, find_import_cycles};
16pub use god_nodes::{GodNode, compute_god_nodes};
17pub use surprising::{SurprisingConnection, find_surprising_connections};
18
19use crate::core::graph_provider::EdgeInfo;
20
21pub(crate) const DEP_EDGE_KINDS: [&str; 2] = ["import", "reexport"];
24
25pub fn is_dependency_kind(kind: &str) -> bool {
27 DEP_EDGE_KINDS.contains(&kind)
28}
29
30pub fn dependency_edges(edges: &[EdgeInfo]) -> Vec<(&str, &str)> {
33 edges
34 .iter()
35 .filter(|e| is_dependency_kind(&e.kind))
36 .filter(|e| e.from != e.to)
37 .map(|e| (e.from.as_str(), e.to.as_str()))
38 .collect()
39}
40
41pub fn edge_confidence(kind: &str, weight: f64) -> f64 {
49 match kind {
50 "import" => 1.0,
51 "reexport" => 0.95,
52 "module" => 0.6,
53 "cochange" => (0.30 + weight.max(0.0).ln_1p() * 0.18).clamp(0.30, 0.85),
55 "sibling" => 0.25,
56 _ => 0.5,
57 }
58}
59
60#[cfg(test)]
61mod confidence_tests {
62 use super::edge_confidence;
63
64 #[test]
65 fn explicit_refs_rank_above_heuristics() {
66 let import = edge_confidence("import", 0.0);
67 let reexport = edge_confidence("reexport", 0.0);
68 let module = edge_confidence("module", 0.0);
69 let sibling = edge_confidence("sibling", 0.0);
70 assert!(import >= reexport);
71 assert!(reexport > module);
72 assert!(module > sibling);
73 assert!((0.0..=1.0).contains(&sibling));
74 }
75
76 #[test]
77 fn cochange_scales_with_weight_and_is_bounded() {
78 let low = edge_confidence("cochange", 1.0);
79 let high = edge_confidence("cochange", 50.0);
80 assert!(high > low, "more co-changes should raise confidence");
81 assert!((0.30..=0.85).contains(&low));
82 assert!((0.30..=0.85).contains(&high));
83 }
84
85 #[test]
86 fn unknown_kind_is_neutral() {
87 assert_eq!(edge_confidence("mystery", 0.0), 0.5);
88 }
89}