API: graph & analyzer

Graph construction and analysis for STL documents.

Module: stl_parser.graph, stl_parser.analyzer Import: from stl_parser import STLGraph, STLAnalyzer


STLGraph

NetworkX-based directed multigraph built from STL statements.

Constructor

STLGraph(parse_result: Optional[ParseResult] = None)

Factory Method

STLGraph.from_parse_result(parse_result: ParseResult) -> STLGraph

Methods

MethodParametersReturn TypeDescription
build_graph(parse_result)ParseResultNoneBuild graph from statements
find_paths(source_id, target_id)str, strList[List[str]]Find all simple paths between nodes
find_cycles()List[List[str]]Find all simple cycles
get_node_degree(anchor_id)strintTotal degree (in + out) of a node
get_node_centrality()Dict[str, float]Degree centrality for all nodes
get_subgraph(domain)strSTLGraphSubgraph filtered by domain modifier
detect_conflicts(functional_relations)Optional[set]List[Dict]Detect functional relation violations
calculate_tension_metrics()Dict[str, Any]Graph-wide tension scores

Properties

PropertyTypeDescription
summaryDict[str, int]Node and edge counts

Example

from stl_parser import parse, STLGraph

result = parse("""
[A] -> [B] ::mod(rule="causal", confidence=0.9)
[B] -> [C] ::mod(rule="causal", confidence=0.8)
[C] -> [A] ::mod(rule="logical", confidence=0.7)
""")

graph = STLGraph(result)
print(graph.summary)            # {"nodes": 3, "edges": 3}
print(graph.find_cycles())      # [["A", "B", "C"]]
print(graph.get_node_centrality())

STLAnalyzer

Comprehensive statistical analysis of STL documents.

Constructor

STLAnalyzer(
    parse_result: ParseResult,
    stl_graph: Optional[STLGraph] = None
)

Auto-constructs an STLGraph if none provided.

Methods

MethodReturn TypeDescription
count_elements()Dict[str, int]Total statements, unique anchors
analyze_anchor_types()DictSource/target anchor type distribution
analyze_path_types()Dict[str, int]Path type distribution
analyze_modifier_usage()DictModifier frequency, min/max, common values
analyze_confidence_metrics()DictConfidence/certainty stats (min, max, mean)
identify_missing_provenance()List[Dict]High-confidence statements missing source
get_graph_metrics()DictNodes, edges, cycles, centrality
get_full_analysis_report()DictAll metrics combined

Convenience Function

from stl_parser.analyzer import analyze_parse_result

report = analyze_parse_result(result)

Example

from stl_parser import parse, STLAnalyzer

result = parse("""
[Rain] -> [Flooding] ::mod(rule="causal", confidence=0.85, strength=0.8)
[Smoking] -> [Lung_Cancer] ::mod(rule="causal", confidence=0.92)
[Water] -> [H2O] ::mod(rule="definitional", confidence=0.99)
""")

analyzer = STLAnalyzer(result)
report = analyzer.get_full_analysis_report()
print(report["counts"])
print(report["confidence_metrics"])