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.

fix: project RDF triple terms in Python query results

Rudy Celekli5b089053eafc
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 6646495d..dc05d943 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -6,6 +6,8 @@ All notable changes to Open Ontologies are documented here.
### Fixed
+- Python: serialize RDF triple-term values in SELECT, CONSTRUCT and DESCRIBE query results instead of raising `AttributeError`; ordinary scalar values remain unchanged.
+
- Keep the last active ontology intact when parsing a replacement source, cache, or refresh fails; publish successful replacements in one graph transaction.
- Bound wildcard repository filtering to pattern/filename pairs so repeated stars cannot stall `onto_repo_list`.
- Sanitize quotes and ASCII control bytes in mapped IRI components so valid structured data does not fail RDF ingestion.
diff --git a/python/src/open_ontologies_lite/engine.py b/python/src/open_ontologies_lite/engine.py
index 2711e08e..9648dd93 100644
--- a/python/src/open_ontologies_lite/engine.py
+++ b/python/src/open_ontologies_lite/engine.py
@@ -139,12 +139,13 @@ class OntologyEngine:
row = {}
for name, var in zip(names, var_objs):
term = sol[var]
- row[name] = term.value if term is not None else None
+ row[name] = _query_value(term)
rows.append(row)
return {"type": "table", "variables": names, "rows": rows}
# QueryTriples (CONSTRUCT / DESCRIBE)
triples = [
- {"subject": t.subject.value, "predicate": t.predicate.value, "object": t.object.value}
+ {"subject": _query_value(t.subject), "predicate": _query_value(t.predicate),
+ "object": _query_value(t.object)}
for t in result
]
return {"type": "graph", "triples": triples}
@@ -306,6 +307,13 @@ class OntologyEngine:
return report
+def _query_value(term) -> str | None:
+ """Keep scalar query values; spell RDF triple terms that have no .value."""
+ if term is None:
+ return None
+ return str(term) if isinstance(term, ox.Triple) else term.value
+
+
def _triple_key(t) -> str:
tr = getattr(t, "triple", t)
return f"{tr.subject} {tr.predicate} {tr.object}"
diff --git a/python/tests/test_query_triple_terms.py b/python/tests/test_query_triple_terms.py
new file mode 100644
index 00000000..ca64605d
--- /dev/null
+++ b/python/tests/test_query_triple_terms.py
@@ -0,0 +1,45 @@
+import pytest
+from open_ontologies_lite import OntologyEngine
+
+DATA='<http://x/a> <http://x/b> <<( <http://x/s> <http://x/p> <http://x/o> )>> .'
+QUERIES={
+ 'select':'SELECT ?o WHERE { <http://x/a> <http://x/b> ?o }',
+ 'construct':'CONSTRUCT { ?s ?p ?o } WHERE { ?s ?p ?o }',
+ 'describe':'DESCRIBE <http://x/a>',
+}
[email protected]('kind',QUERIES)
+def test_query_rdf_triple_term(kind):
+ engine=OntologyEngine()
+ assert engine.load(DATA)==1
+ result=engine.query(QUERIES[kind])
+ expected=str(next(iter(engine.store)).object)
+ if kind=='select':
+ assert result=={'type':'table','variables':['o'],'rows':[{'o':expected}]}
+ else:
+ assert result=={'type':'graph','triples':[{'subject':'http://x/a','predicate':'http://x/b','object':expected}]}
+
[email protected]('kind',QUERIES)
+def test_ordinary_literal_query_control(kind):
+ engine=OntologyEngine()
+ engine.load('<http://x/a> <http://x/b> "ordinary" .')
+ result=engine.query(QUERIES[kind])
+ if kind=='select':assert result=={'type':'table','variables':['o'],'rows':[{'o':'ordinary'}]}
+ else:assert result=={'type':'graph','triples':[{'subject':'http://x/a','predicate':'http://x/b','object':'ordinary'}]}
+
+def test_ask_control():
+ engine=OntologyEngine();engine.load(DATA)
+ assert engine.query('ASK { ?s ?p ?o }')=={'type':'boolean','boolean':True}
+
+def test_unbound_select_control():
+ engine=OntologyEngine();engine.load('<http://x/a> <http://x/b> "ordinary" .')
+ assert engine.query('SELECT ?s ?unbound WHERE {?s ?p ?o}')=={'type':'table','variables':['s','unbound'],'rows':[{'s':'http://x/a','unbound':None}]}
+
[email protected]('term,expected', [
+ ('<http://x/o>', 'http://x/o'),
+ ('"7"^^<http://www.w3.org/2001/XMLSchema#integer>', '7'),
+ ('"bonjour"@fr', 'bonjour'),
+])
+def test_scalar_lexical_value_control(term,expected):
+ engine=OntologyEngine()
+ engine.load('<http://x/a> <http://x/b> '+term+' .')
+ assert engine.query(QUERIES['select'])=={'type':'table','variables':['o'],'rows':[{'o':expected}]}