Skip to main content

uni_locy/
types.rs

1use std::collections::HashMap;
2
3use uni_cypher::ast::{Clause, Expr, 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    /// Post-FOLD filter conditions (HAVING semantics).
63    pub having: Vec<Expr>,
64    pub best_by: Option<BestByClause>,
65    pub output: RuleOutput,
66    pub priority: Option<i64>,
67}
68
69/// A column in a rule's YIELD schema.
70#[derive(Debug, Clone, PartialEq)]
71pub struct YieldColumn {
72    pub name: String,
73    pub is_key: bool,
74    pub is_prob: bool,
75}
76
77/// A non-fatal compiler diagnostic.
78#[derive(Debug, Clone)]
79pub struct CompilerWarning {
80    pub code: WarningCode,
81    pub message: String,
82    pub rule_name: String,
83}
84
85#[derive(Debug, Clone, PartialEq)]
86pub enum WarningCode {
87    MsumNonNegativity,
88    ProbabilityDomainViolation,
89}
90
91/// Classification of runtime warnings emitted during evaluation.
92#[derive(Debug, Clone, PartialEq)]
93pub enum RuntimeWarningCode {
94    /// Two or more proof paths aggregated by MNOR/MPROD share an
95    /// intermediate fact, violating the independence assumption.
96    SharedProbabilisticDependency,
97    /// A shared-proof group exceeded `max_bdd_variables`, so the BDD
98    /// computation fell back to the independence-mode result.
99    BddLimitExceeded,
100    /// Base facts are shared across different KEY groups within the same
101    /// rule. The BDD corrects per-group probabilities but cannot account
102    /// for cross-group correlations.
103    CrossGroupCorrelationNotExact,
104}
105
106/// A non-fatal runtime diagnostic collected during evaluation.
107#[derive(Debug, Clone)]
108pub struct RuntimeWarning {
109    /// Warning classification.
110    pub code: RuntimeWarningCode,
111    /// Human-readable explanation.
112    pub message: String,
113    /// Rule that triggered the warning, when applicable.
114    pub rule_name: String,
115    /// BDD variable count for the affected group (BddLimitExceeded only).
116    pub variable_count: Option<usize>,
117    /// Human-readable KEY group description (BddLimitExceeded only).
118    pub key_group: Option<String>,
119}