Skip to main content

qualia_core_db/query/
visual_model_bridge.rs

1//! Translates visual graph structures from the UI into `NQuin` evaluator inputs.
2
3use crate::{q_hash, NQuin};
4use serde::{Deserialize, Serialize};
5
6#[derive(Serialize, Deserialize, Debug, Clone)]
7pub struct UiLogicNode {
8    pub id: usize,
9    pub title: String,
10    pub kind: String,
11}
12
13#[derive(Serialize, Deserialize, Debug, Clone)]
14pub struct UiLogicEdge {
15    pub from: usize,
16    pub to: usize,
17    pub label: String,
18    pub status: String,
19}
20
21#[derive(Serialize, Deserialize, Debug, Clone)]
22pub struct UiLogicGraph {
23    pub nodes: Vec<UiLogicNode>,
24    pub edges: Vec<UiLogicEdge>,
25}
26
27#[derive(Serialize, Deserialize, Debug, Clone)]
28pub struct EvaluationReport {
29    pub contradictions: Vec<usize>, // IDs of contradicting edges
30    pub validations: Vec<usize>,
31}
32
33/// Converts the graph to a series of zero-heap semantic Quins.
34/// Only writes to the pre-allocated slice up to its capacity, returning the number of Quins written.
35pub fn translate_graph_to_quins(graph: &UiLogicGraph, out: &mut [NQuin]) -> usize {
36    let mut count = 0;
37
38    // Each edge becomes a Quin: Subject(from) -> Predicate(label) -> Object(to)
39    for edge in &graph.edges {
40        if count >= out.len() {
41            break; // Slice full
42        }
43
44        let from_node = graph.nodes.iter().find(|n| n.id == edge.from);
45        let to_node = graph.nodes.iter().find(|n| n.id == edge.to);
46
47        if let (Some(f), Some(t)) = (from_node, to_node) {
48            let mut q = NQuin::default();
49            q.subject = q_hash(&f.title);
50            q.predicate = q_hash(&edge.label);
51            q.object = q_hash(&t.title);
52
53            // Basic parity fold for completeness (subject ^ predicate ^ object ^ context)
54            q.parity = q.subject ^ q.predicate ^ q.object ^ q.context;
55
56            out[count] = q;
57            count += 1;
58        }
59    }
60
61    count
62}
63
64/// Evaluates a translated logic graph and returns a report to update the UI.
65pub fn evaluate_ui_graph(graph: &UiLogicGraph) -> EvaluationReport {
66    let mut out_quins = [NQuin::default(); 128];
67    let _quin_count = translate_graph_to_quins(graph, &mut out_quins);
68
69    // Mock evaluation for now (Paraconsistent Logic / SHACL checking would hook here)
70    // We just return dummy contradiction detection for demonstration.
71    let mut contradictions = Vec::new();
72    let mut validations = Vec::new();
73
74    for edge in &graph.edges {
75        if edge.label == "object" {
76            contradictions.push(edge.from); // dummy logic
77        } else {
78            validations.push(edge.from);
79        }
80    }
81
82    EvaluationReport {
83        contradictions,
84        validations,
85    }
86}