Skip to main content

uni_locy/
types.rs

1use std::collections::HashMap;
2
3use uni_cypher::ast::{Clause, Pattern, Query};
4use uni_cypher::locy_ast::{
5    AbduceQuery, AlongBinding, BestByClause, DeriveCommand, ExplainRule, FoldBinding, GoalQuery,
6    RuleCondition, RuleOutput,
7};
8
9/// A fully validated and stratified Locy program, ready for the orchestrator.
10#[derive(Debug, Clone)]
11pub struct CompiledProgram {
12    pub strata: Vec<Stratum>,
13    pub rule_catalog: HashMap<String, CompiledRule>,
14    pub warnings: Vec<CompilerWarning>,
15    pub commands: Vec<CompiledCommand>,
16}
17
18/// A compiled command (non-rule statement) ready for execution.
19#[derive(Debug, Clone)]
20pub enum CompiledCommand {
21    GoalQuery(GoalQuery),
22    Assume(CompiledAssume),
23    Abduce(AbduceQuery),
24    ExplainRule(ExplainRule),
25    DeriveCommand(DeriveCommand),
26    Cypher(Query),
27}
28
29/// A compiled ASSUME block with mutations and body program.
30#[derive(Debug, Clone)]
31pub struct CompiledAssume {
32    pub mutations: Vec<Clause>,
33    pub body_program: CompiledProgram,
34    pub body_commands: Vec<CompiledCommand>,
35}
36
37/// A group of rules that must be evaluated together (one SCC).
38#[derive(Debug, Clone)]
39pub struct Stratum {
40    pub id: usize,
41    pub rules: Vec<CompiledRule>,
42    pub is_recursive: bool,
43    pub depends_on: Vec<usize>,
44}
45
46/// A named rule with all its clauses merged and validated.
47#[derive(Debug, Clone)]
48pub struct CompiledRule {
49    pub name: String,
50    pub clauses: Vec<CompiledClause>,
51    pub yield_schema: Vec<YieldColumn>,
52    pub priority: Option<i64>,
53}
54
55/// A single clause (one CREATE RULE ... AS ... definition).
56#[derive(Debug, Clone)]
57pub struct CompiledClause {
58    pub match_pattern: Pattern,
59    pub where_conditions: Vec<RuleCondition>,
60    pub along: Vec<AlongBinding>,
61    pub fold: Vec<FoldBinding>,
62    pub best_by: Option<BestByClause>,
63    pub output: RuleOutput,
64    pub priority: Option<i64>,
65}
66
67/// A column in a rule's YIELD schema.
68#[derive(Debug, Clone, PartialEq)]
69pub struct YieldColumn {
70    pub name: String,
71    pub is_key: bool,
72    pub is_prob: bool,
73}
74
75/// A non-fatal compiler diagnostic.
76#[derive(Debug, Clone)]
77pub struct CompilerWarning {
78    pub code: WarningCode,
79    pub message: String,
80    pub rule_name: String,
81}
82
83#[derive(Debug, Clone, PartialEq)]
84pub enum WarningCode {
85    MsumNonNegativity,
86    ProbabilityDomainViolation,
87}
88
89/// Classification of runtime warnings emitted during evaluation.
90#[derive(Debug, Clone, PartialEq)]
91pub enum RuntimeWarningCode {
92    /// Two or more proof paths aggregated by MNOR/MPROD share an
93    /// intermediate fact, violating the independence assumption.
94    SharedProbabilisticDependency,
95    /// A shared-proof group exceeded `max_bdd_variables`, so the BDD
96    /// computation fell back to the independence-mode result.
97    BddLimitExceeded,
98    /// Base facts are shared across different KEY groups within the same
99    /// rule. The BDD corrects per-group probabilities but cannot account
100    /// for cross-group correlations.
101    CrossGroupCorrelationNotExact,
102}
103
104/// A non-fatal runtime diagnostic collected during evaluation.
105#[derive(Debug, Clone)]
106pub struct RuntimeWarning {
107    /// Warning classification.
108    pub code: RuntimeWarningCode,
109    /// Human-readable explanation.
110    pub message: String,
111    /// Rule that triggered the warning, when applicable.
112    pub rule_name: String,
113    /// BDD variable count for the affected group (BddLimitExceeded only).
114    pub variable_count: Option<usize>,
115    /// Human-readable KEY group description (BddLimitExceeded only).
116    pub key_group: Option<String>,
117}