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;
9}
10
11pub struct IngestionPipeline<'a> {
15 payload: &'a str,
16}
17
18impl<'a> IngestionPipeline<'a> {
19 pub fn new(payload: &'a str) -> Self {
21 Self { payload }
22 }
23
24 pub fn parse_line(line: &str) -> Option<NQuin> {
26 let trimmed = line.trim();
27 if trimmed.is_empty() || trimmed.starts_with('#') {
28 return None; }
30
31 let mut metadata: u64 = 0;
32
33 if trimmed.contains("=>") || trimmed.contains("@forAll") || trimmed.contains("@forSome") {
35 metadata |= 0b10 << 61;
38 metadata |= 0x00A3;
40 }
41 else if trimmed.contains("<<") && trimmed.contains(">>") {
43 if trimmed.contains("qualia:guardian") || trimmed.contains("SPIN:") {
44 metadata |= 0b10 << 61;
46 } else {
47 metadata |= 0b00 << 61;
50 }
51 }
52 else if let Some(quin) = QueryCompiler::compile_to_quin(trimmed) {
54 return Some(quin);
55 } else {
56 metadata |= 0b00 << 61;
58 }
59
60 Some(NQuin {
61 subject: 0,
62 predicate: 0,
63 object: 0,
64 context: 0,
65 metadata,
66 parity: 0,
67 })
68 }
69}
70
71impl<'a> ZeroCopyStream<'a> for IngestionPipeline<'a> {
72 type Item = NQuin;
73
74 fn stream_parse(&self) -> impl Iterator<Item = Self::Item> + 'a {
76 self.payload
77 .lines()
78 .filter_map(|line| Self::parse_line(line))
79 }
80}
81
82#[cfg(test)]
83mod tests {
84 use super::*;
85
86 #[test]
87 fn test_n3logic_routing() {
88 let n3_rule = "{ ?x a :Man } => { ?x a :Mortal } .";
89 let quin = IngestionPipeline::parse_line(n3_rule).unwrap();
90
91 let expected_routing = 0b10 << 61;
93 assert_eq!(
94 quin.metadata & (0b11 << 61),
95 expected_routing,
96 "N3Logic implication failed to route to Core 1"
97 );
98 }
99
100 #[test]
101 fn test_rdf_star_nesting() {
102 let nested_triple = "<< :AgentX :prescribed :MedicationY >> :assertedBy :DoctorZ .";
103 let quin = IngestionPipeline::parse_line(nested_triple).unwrap();
104
105 let expected_routing = 0b00 << 61;
107 assert_eq!(
108 quin.metadata & (0b11 << 61),
109 expected_routing,
110 "RDF-Star nesting failed to route correctly"
111 );
112 }
113}