clm-kernel 0.0.11

Cubical Logic Model (CLM) sovereign execution engine, CPN-VM, and Cordis spatiotemporal substrate
Documentation
//! Layer 0: Stateless Kenotic Boolean Gate Predicates (FND X -> 2)
//! Canonical Path: kernel/clm_rust_core/src/layer0/gates.rs

use serde::{Deserialize, Serialize};
use serde_json::Value;

/// Primitive Boolean Gate Predicate operating over arbitrary JSON or MCard state.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(tag = "type", content = "config")]
pub enum GatePredicate {
    /// Always yields true.
    AlwaysTrue,
    /// Always yields false.
    AlwaysFalse,
    /// Exact match with expected JSON value.
    Equals(Value),
    /// Inverted match with expected JSON value.
    NotEquals(Value),
    /// Conjunction of child predicates (AND).
    And(Vec<GatePredicate>),
    /// Disjunction of child predicates (OR).
    Or(Vec<GatePredicate>),
    /// Negation of child predicate (NOT).
    Not(Box<GatePredicate>),
    /// M-of-N threshold evaluation.
    Threshold {
        required: usize,
        predicates: Vec<GatePredicate>,
    },
}

impl GatePredicate {
    /// Evaluates the predicate against input value in a purely stateless manner.
    pub fn evaluate(&self, input: &Value) -> bool {
        match self {
            Self::AlwaysTrue => true,
            Self::AlwaysFalse => false,
            Self::Equals(expected) => input == expected,
            Self::NotEquals(expected) => input != expected,
            Self::And(preds) => preds.iter().all(|p| p.evaluate(input)),
            Self::Or(preds) => preds.iter().any(|p| p.evaluate(input)),
            Self::Not(p) => !p.evaluate(input),
            Self::Threshold { required, predicates } => {
                let satisfied = predicates.iter().filter(|p| p.evaluate(input)).count();
                satisfied >= *required
            }
        }
    }
}

/// Evaluates a collection of named gates against an input state map.
pub fn evaluate_gate_set(gates: &[(String, GatePredicate)], state: &Value) -> Vec<(String, bool)> {
    gates
        .iter()
        .map(|(name, pred)| (name.clone(), pred.evaluate(state)))
        .collect()
}