Skip to content

Plan, apply and roll back changes to a production ontology, with a blast radius report and a proof an auditor can re-check.

feat: add sparql_update method to GraphStore for INSERT/DELETE support

FABIOTESS226c886a5a91
diff --git a/src/graph.rs b/src/graph.rs
index 6d15a9ee..d65c4c40 100644
--- a/src/graph.rs
+++ b/src/graph.rs
@@ -127,6 +127,16 @@ impl GraphStore {
}
}
+ /// Run a SPARQL UPDATE (INSERT/DELETE) against the store.
+ /// Returns the number of new triples (delta).
+ pub fn sparql_update(&self, update: &str) -> anyhow::Result<usize> {
+ let store = self.store.lock().unwrap();
+ let before = store.len()?;
+ store.update(update)?;
+ let after = store.len()?;
+ Ok(after.saturating_sub(before))
+ }
+
pub fn serialize(&self, format: &str) -> anyhow::Result<String> {
let store = self.store.lock().unwrap();
let rdf_format = Self::parse_format(format)?;
diff --git a/tests/graph_test.rs b/tests/graph_test.rs
index 125b8493..2eada5be 100644
--- a/tests/graph_test.rs
+++ b/tests/graph_test.rs
@@ -96,3 +96,29 @@ fn test_get_stats() {
assert!(stats.contains("classes"));
assert!(stats.contains("triples"));
}
+
+#[test]
+fn test_sparql_update_insert() {
+ let store = GraphStore::new();
+ let ttl = r#"
+ @prefix ex: <http://example.org/> .
+ @prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
+ ex:Cat rdfs:subClassOf ex:Animal .
+ ex:Tabby a ex:Cat .
+ "#;
+ store.load_turtle(ttl, None).unwrap();
+ assert_eq!(store.triple_count(), 2);
+
+ // Insert inferred triple: Tabby is also an Animal via subclass
+ let update = r#"
+ PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
+ INSERT { ?x a ?super }
+ WHERE {
+ ?x a ?sub .
+ ?sub rdfs:subClassOf ?super .
+ }
+ "#;
+ let result = store.sparql_update(update);
+ assert!(result.is_ok());
+ assert_eq!(store.triple_count(), 3); // original 2 + 1 inferred
+}