qualia_core_db/query/
ingestion.rs1use crate::{query_compiler::QueryCompiler, NQuin};
2
3pub trait ZeroCopyStream<'a> {
6 type Item;
7 fn stream_parse(&self) -> impl Iterator<Item = Self::Item> + 'a;
8}
9
10pub struct IngestionPipeline<'a> {
14 payload: &'a str,
15}
16
17impl<'a> IngestionPipeline<'a> {
18 pub fn new(payload: &'a str) -> Self {
19 Self { payload }
20 }
21
22 pub fn parse_line(line: &str) -> Option<NQuin> {
24 let trimmed = line.trim();
25 if trimmed.is_empty() || trimmed.starts_with('#') {
26 return None; }
28
29 let mut metadata: u64 = 0;
30
31 if trimmed.contains("=>") || trimmed.contains("@forAll") || trimmed.contains("@forSome") {
33 metadata |= 0b10 << 61;
36 metadata |= 0x00A3;
38 }
39 else if trimmed.contains("<<") && trimmed.contains(">>") {
41 if trimmed.contains("qualia:guardian") || trimmed.contains("SPIN:") {
42 metadata |= 0b10 << 61;
44 } else {
45 metadata |= 0b00 << 61;
48 }
49 }
50 else if let Some(quin) = QueryCompiler::compile_to_quin(trimmed) {
52 return Some(quin);
53 } else {
54 metadata |= 0b00 << 61;
56 }
57
58 Some(NQuin {
59 subject: 0,
60 predicate: 0,
61 object: 0,
62 context: 0,
63 metadata,
64 parity: 0,
65 })
66 }
67}
68
69impl<'a> ZeroCopyStream<'a> for IngestionPipeline<'a> {
70 type Item = NQuin;
71
72 fn stream_parse(&self) -> impl Iterator<Item = Self::Item> + 'a {
74 self.payload
75 .lines()
76 .filter_map(|line| Self::parse_line(line))
77 }
78}
79
80#[cfg(test)]
81mod tests {
82 use super::*;
83
84 #[test]
85 fn test_n3logic_routing() {
86 let n3_rule = "{ ?x a :Man } => { ?x a :Mortal } .";
87 let quin = IngestionPipeline::parse_line(n3_rule).unwrap();
88
89 let expected_routing = 0b10 << 61;
91 assert_eq!(
92 quin.metadata & (0b11 << 61),
93 expected_routing,
94 "N3Logic implication failed to route to Core 1"
95 );
96 }
97
98 #[test]
99 fn test_rdf_star_nesting() {
100 let nested_triple = "<< :AgentX :prescribed :MedicationY >> :assertedBy :DoctorZ .";
101 let quin = IngestionPipeline::parse_line(nested_triple).unwrap();
102
103 let expected_routing = 0b00 << 61;
105 assert_eq!(
106 quin.metadata & (0b11 << 61),
107 expected_routing,
108 "RDF-Star nesting failed to route correctly"
109 );
110 }
111}