Skip to main content

qualia_client_core/engine/
semantic.rs

1use qualia_core_db::daemon_graph::graph_read_guard;
2use qualia_core_db::sparql_library::sparql_executor::QueryExecutor;
3use qualia_core_db::sparql_library::sparql_parser::parse_sparql;
4use qualia_core_db::sparql_library::sparql_planner::QueryPlanner;
5
6/// Executes a SPARQL query against the local in-memory NQuin graph.
7pub fn execute_local_sparql(query: &str) -> Result<Vec<(String, String, String)>, String> {
8    let guard = graph_read_guard();
9    let quins = guard.as_slice();
10
11    let (sparql_query, ctx) = parse_sparql(query)?;
12    let plan = QueryPlanner::plan(&sparql_query, &ctx)?;
13    let executor = QueryExecutor::new(quins);
14
15    let raw_rows = executor.execute(&plan, &ctx)?;
16
17    // Map to simple 3-tuple for UI
18    let mut mapped = Vec::with_capacity(raw_rows.len());
19    for row in raw_rows {
20        let s = row.slots[0]
21            .map(|v| format!("{:016X}", v))
22            .unwrap_or_else(|| "".to_string());
23        let p = row.slots[1]
24            .map(|v| format!("{:016X}", v))
25            .unwrap_or_else(|| "".to_string());
26        let o = row.slots[2]
27            .map(|v| format!("{:016X}", v))
28            .unwrap_or_else(|| "".to_string());
29        mapped.push((s, p, o));
30    }
31
32    Ok(mapped)
33}
34
35/// Validates a node against a SHACL shape within the local graph.
36///
37/// HONESTY / fail-closed: there is no shape registry yet that resolves a
38/// `shape_uri` to real SHACL constraints. The former implementation built a
39/// shape with `constraint_count: 0` and validated against it — an empty
40/// constraint set trivially "conforms", so this returned `Ok(true)` for ANY
41/// node and graph, lighting up the UI's "Graph Valid" unconditionally (a
42/// false green light). Until shape resolution exists we refuse rather than
43/// fabricate a conformance verdict. The real `ShaclValidator` /
44/// `sparql_shacl::run` engine is genuine — the missing piece is the
45/// `shape_uri → constraints` lookup that would feed it.
46pub fn validate_local_shacl(_node: u64, shape_uri: u64) -> Result<bool, String> {
47    Err(format!(
48        "SHACL validation unavailable: no constraints resolved for shape {shape_uri:#018x} \
49         (shape-registry lookup is not implemented). Refusing to report conformance against \
50         an empty shape."
51    ))
52}
53
54/// Executes an SLG computational VM frame locally.
55pub fn execute_slg_vm(frame_data: &[u8]) -> Result<String, String> {
56    use qualia_core_db::governance::webizen::{execute_vm_frame, SlgArena, VmFrame};
57
58    let mut arena = SlgArena::new();
59    let mut frame = VmFrame::default();
60
61    // Parse the JSON array into the frame
62    if let Ok(value) = serde_json::from_slice::<serde_json::Value>(frame_data) {
63        if let Some(payload) = value.get("payload") {
64            if let Some(arr) = payload.as_array() {
65                if arr.len() >= 3 {
66                    frame.subject_reg = arr[0].as_u64().unwrap_or(0);
67                    frame.predicate_reg = arr[1].as_u64().unwrap_or(0);
68                    frame.object_reg = arr[2].as_u64().unwrap_or(0);
69                }
70            }
71        }
72    }
73
74    let bytecode = [];
75    match execute_vm_frame(&mut arena, &bytecode, &mut frame) {
76        Some(quin) => Ok(format!("Computed: {:016X}", quin.subject)),
77        None => Ok(format!(
78            "VM Execution Completed for Subject: {}",
79            frame.subject_reg
80        )),
81    }
82}