clm-kernel 0.0.11

Cubical Logic Model (CLM) sovereign execution engine, CPN-VM, and Cordis spatiotemporal substrate
Documentation
//! Card Graph Navigation Modalities — Sprint 364
//!
//! Provides the three navigation modalities over the MCard knowledge graph:
//! 1. Covariant: forward traversal along next fireable downstream PCards.
//! 2. Contravariant: backward traversal along upstream VCard execution provenance.
//! 3. Invariant: equivalence grouping across isomorphic content-equivalent MCards.

use serde::{Deserialize, Serialize};
use std::collections::HashMap;

#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum NavigationDirection {
    Covariant,
    Contravariant,
    Invariant,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct CardNode {
    pub hash: String,
    pub handle: Option<String>,
    pub card_type: String,
    pub content_hash: String,
    pub upstream_vcards: Vec<String>,
    pub downstream_pcards: Vec<String>,
}

pub struct CardGraph {
    pub nodes: HashMap<String, CardNode>,
}

impl Default for CardGraph {
    fn default() -> Self {
        Self::new()
    }
}

impl CardGraph {
    pub fn new() -> Self {
        Self {
            nodes: HashMap::new(),
        }
    }

    pub fn insert_node(&mut self, node: CardNode) {
        self.nodes.insert(node.hash.clone(), node);
    }

    pub fn navigate(&self, start_hash: &str, direction: NavigationDirection) -> Vec<String> {
        let node = match self.nodes.get(start_hash) {
            Some(n) => n,
            None => return Vec::new(),
        };

        match direction {
            NavigationDirection::Covariant => {
                let mut results = node.downstream_pcards.clone();
                results.sort();
                results
            }
            NavigationDirection::Contravariant => {
                let mut results = node.upstream_vcards.clone();
                results.sort();
                results
            }
            NavigationDirection::Invariant => {
                let mut results: Vec<String> = self
                    .nodes
                    .values()
                    .filter(|n| n.content_hash == node.content_hash)
                    .map(|n| n.hash.clone())
                    .collect();
                results.sort();
                results
            }
        }
    }
}