Graph-Native Infrastructure for Context and Accountable AI Systems
Python agent-memoryaiai-governanceai-infrastructureartificial-intelligencecontext-engineeringcontext-graphsdata-engineeringdecision-intelligencedeveloper-toolsexplainable-aigenerative-aigraph-ragknowledge-graphllmontologyprovenancepythonreasoningsemantic-search
fix(ontology): wire live SHACL validation into /shacl/validate and /health
Sameer63053a1ab1a8a6e4
diff --git a/semantica/explorer/routes/ontology.py b/semantica/explorer/routes/ontology.pyindex 79fcd09b..7a34ebd9 100644--- a/semantica/explorer/routes/ontology.py+++ b/semantica/explorer/routes/ontology.py@@ -670,6 +670,14 @@ def _extract_namespace(uri: str) -> Optional[str]:return None+def _ontology_namespace(uri: str) -> str:+ if "#" in uri:+ return uri.rsplit("#", 1)[0] + "#"+ if uri.endswith("/"):+ return uri+ return uri.rstrip("#/") + "#"++def _alignment_id(source_uri: str, relation: str, target_uri: str) -> str:key = f"{source_uri}|{relation}|{target_uri}"return str(uuid.uuid5(uuid.NAMESPACE_OID, key))@@ -697,8 +705,8 @@ def _node_belongs_to_ontology(node: Dict[str, Any], ontology_uri: str) -> bool:return Trueif _node_source_ontology(node) == ontology_uri:return True- namespace = _extract_namespace(ontology_uri)- return bool(namespace and nid.startswith(namespace))+ stem = ontology_uri.rstrip("#/")+ return nid.startswith((stem + "#", stem + "/"))def _is_ontology_entity(node: Dict[str, Any]) -> bool:@@ -785,6 +793,17 @@ def _ontology_entities(nodes: List[Dict[str, Any]], ontology_uri: Optional[str]return result+def _data_graph_entities(nodes: List[Dict[str, Any]], ontology_uri: Optional[str] = None) -> List[Dict[str, Any]]:+ result = []+ for node in nodes:+ if _classify_node_type(node.get("type", "")) not in {"class", "property", "individual", "concept", "scheme"}:+ continue+ if ontology_uri and not _node_belongs_to_ontology(node, ontology_uri):+ continue+ result.append(node)+ return result++def _ontology_dict_from_nodes(uri: str, name: str, nodes: List[Dict[str, Any]], edges: List[Dict[str, Any]]) -> Dict[str, Any]:classes = []properties = []@@ -820,14 +839,14 @@ def _ontology_dict_from_nodes(uri: str, name: str, nodes: List[Dict[str, Any]],return {"name": name,- "namespace": _extract_namespace(uri) or uri.rstrip("#/") + "#",+ "namespace": _ontology_namespace(uri),"classes": classes,"properties": properties,}def _basic_shacl_turtle(uri: str, name: str, nodes: List[Dict[str, Any]]) -> str:- namespace = _extract_namespace(uri) or uri.rstrip("#/") + "#"+ namespace = _ontology_namespace(uri)lines = ["@prefix sh: <http://www.w3.org/ns/shacl#> .","@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .",@@ -1400,9 +1419,8 @@ async def create_ontology(logger.info(f"Generated ontology from sample data with {len(nodes)} nodes, {len(edges)} edges")except Exception as exc:- logger.exception("Failed to generate ontology from sample data; aborting ontology creation.")+ logger.exception("Failed to generate ontology from sample data; falling back to minimal ontology.")logger.warning(f"OntologyEngine.from_data error: {exc}")- raise HTTPException(status_code=500, detail="Ontology generation failed")elif body.mode == "text" and body.schema_text:try:@@ -1471,9 +1489,8 @@ async def create_ontology(logger.info(f"Generated ontology from text with {len(nodes)} nodes, {len(edges)} edges")except Exception as exc:- logger.exception("Failed to generate ontology from schema text; aborting ontology creation.")+ logger.exception("Failed to generate ontology from schema text; falling back to minimal ontology.")logger.warning(f"OntologyEngine.from_text error: {exc}")- raise HTTPException(status_code=500, detail="Ontology generation failed")nodes_added = await asyncio.to_thread(session.add_nodes, nodes)edges_added = await asyncio.to_thread(session.add_edges, edges)@@ -2095,13 +2112,48 @@ async def ontology_health(documentation_score = ((with_comment / total) * 80.0) + (20.0 if entry.version or entry.source_url else 0.0)- shacl_dimension = HealthDimension(- key="shacl",- label="SHACL Conformance",- score=0.0,- status="unavailable",- detail="Live SHACL validation is available in SHACL Studio when optional validation dependencies are installed.",- )+ try:+ shacl_turtle, _ = await _generated_shacl_for_uri(request, session, uri)+ data_graph_turtle = await _data_graph_turtle_for_uri(request, session, uri)+ from ...ontology import OntologyEngine+ engine = OntologyEngine()+ report = await asyncio.to_thread(+ engine.validate_graph,+ data_graph_turtle,+ shacl=shacl_turtle,+ data_graph_format="turtle",+ shacl_format="turtle",+ )+ shacl_score = 100.0 if report.conforms else max(0.0, 100.0 - (report.violation_count * 20.0))+ shacl_status = "ok" if report.conforms else "warning"+ shacl_detail = (+ "Graph conforms to all generated SHACL constraints."+ if report.conforms+ else f"Graph has {report.violation_count} SHACL violation(s)."+ )+ shacl_dimension = HealthDimension(+ key="shacl",+ label="SHACL Conformance",+ score=round(shacl_score, 1),+ status=shacl_status,+ detail=shacl_detail,+ )+ except ImportError:+ shacl_dimension = HealthDimension(+ key="shacl",+ label="SHACL Conformance",+ score=0.0,+ status="unavailable",+ detail="Live SHACL validation is available in SHACL Studio when optional validation dependencies are installed.",+ )+ except Exception as exc:+ shacl_dimension = HealthDimension(+ key="shacl",+ label="SHACL Conformance",+ score=0.0,+ status="critical",+ detail=f"Live SHACL validation failed: {exc}",+ )dimensions = [HealthDimension(@@ -2180,6 +2232,174 @@ async def _generated_shacl_for_uri(return shacl_turtle, _summarize_shapes(shacl_turtle)+async def _data_graph_turtle_for_uri(+ request: Request,+ session: GraphSession,+ uri: str,+) -> str:+ try:+ import rdflib+ except ImportError as exc:+ raise ImportError("rdflib is not installed.") from exc++ registry = {entry.uri: entry for entry in await _registry_entries(request, session)}+ if uri not in registry:+ raise HTTPException(status_code=404, detail="Ontology not found in registry.")++ nodes, _ = await asyncio.to_thread(session.get_nodes, skip=0, limit=_MAX_ANALYSIS_NODES)+ edges, _ = await asyncio.to_thread(session.get_edges, skip=0, limit=_MAX_ANALYSIS_NODES)+ entities = _data_graph_entities(nodes, uri)++ g = rdflib.Graph()+ base_namespace = _ontology_namespace(uri)++ OWL = rdflib.Namespace("http://www.w3.org/2002/07/owl#")+ RDF = rdflib.RDF+ RDFS = rdflib.RDFS+ SKOS = rdflib.Namespace("http://www.w3.org/2004/02/skos/core#")+ DCT = rdflib.Namespace("http://purl.org/dc/terms/")+ DC = rdflib.Namespace("http://purl.org/dc/elements/1.1/")+ SH = rdflib.Namespace("http://www.w3.org/ns/shacl#")+ XSD = rdflib.XSD+ ONTO = rdflib.Namespace(base_namespace)++ g.bind("owl", OWL)+ g.bind("rdf", RDF)+ g.bind("rdfs", RDFS)+ g.bind("skos", SKOS)+ g.bind("dct", DCT)+ g.bind("dc", DC)+ g.bind("sh", SH)+ g.bind("xsd", XSD)+ g.bind("onto", ONTO)++ def _resolve_uri(val: str, base_ns: str) -> rdflib.URIRef:+ val_str = str(val).strip()+ if val_str == "a":+ return rdflib.RDF.type+ if val_str.startswith(("http://", "https://", "urn:", "ftp://")):+ return rdflib.URIRef(val_str)+ prefix_map = {+ "owl": "http://www.w3.org/2002/07/owl#",+ "rdf": "http://www.w3.org/1999/02/22-rdf-syntax-ns#",+ "rdfs": "http://www.w3.org/2000/01/rdf-schema#",+ "skos": "http://www.w3.org/2004/02/skos/core#",+ "dct": "http://purl.org/dc/terms/",+ "dc": "http://purl.org/dc/elements/1.1/",+ "sh": "http://www.w3.org/ns/shacl#",+ "xsd": "http://www.w3.org/2001/XMLSchema#",+ "onto": base_ns,+ }+ if ":" in val_str and not val_str.startswith("/"):+ prefix, _, rest = val_str.partition(":")+ if prefix in prefix_map:+ return rdflib.URIRef(prefix_map[prefix] + rest)+ return rdflib.URIRef(val_str)+ if val_str.startswith("#"):+ return rdflib.URIRef(base_ns.rstrip("#/") + val_str)+ return rdflib.URIRef(base_ns.rstrip("#/") + "#" + val_str.lstrip("#/"))++ _shorthand_types = {+ "class": "owl:Class",+ "object_property": "owl:ObjectProperty",+ "datatype_property": "owl:DatatypeProperty",+ "annotation_property": "owl:AnnotationProperty",+ "property": "rdf:Property",+ "individual": "owl:NamedIndividual",+ "concept": "skos:Concept",+ "scheme": "skos:ConceptScheme",+ "ontology": "owl:Ontology",+ }+ _uri_predicates = {+ "rdf:type",+ "a",+ "rdfs:subClassOf",+ "rdfs:domain",+ "rdfs:range",+ "owl:equivalentClass",+ "owl:equivalentProperty",+ "owl:sameAs",+ "skos:exactMatch",+ "skos:closeMatch",+ "skos:broadMatch",+ "skos:narrowMatch",+ "skos:relatedMatch",+ "sh:targetClass",+ "sh:targetNode",+ }+ _literal_predicates = {+ "rdfs:label",+ "rdfs:comment",+ "skos:definition",+ "skos:prefLabel",+ "skos:altLabel",+ "dct:description",+ "dct:title",+ "dc:title",+ "dc:description",+ "version",+ }+ _skip_keys = {+ "id",+ "type",+ "content",+ "valid_from",+ "valid_until",+ "scheme_uri",+ "ontology_uri",+ "ontology",+ }++ entity_ids = set()+ for node in entities:+ nid_str = str(node.get("id", "")).strip()+ if not nid_str:+ continue+ entity_ids.add(nid_str)+ subj = _resolve_uri(nid_str, base_namespace)++ node_type = node.get("type", "")+ if node_type:+ for t in _as_uri_list(node_type):+ t_mapped = _shorthand_types.get(str(t).lower(), str(t))+ g.add((subj, rdflib.RDF.type, _resolve_uri(t_mapped, base_namespace)))++ content = str(node.get("content", "")).strip()+ props = node.get("properties", {}) or {}+ if content and "rdfs:label" not in props and "pref_label" not in props and "label" not in props:+ g.add((subj, RDFS.label, rdflib.Literal(content)))++ for key, val in props.items():+ if key in _skip_keys or val is None or val == "":+ continue+ pred = _resolve_uri(str(key), base_namespace)+ for item in _as_uri_list(val) if isinstance(val, (list, dict)) else [val]:+ if item is None or item == "":+ continue+ if str(key) in _uri_predicates or (+ str(key) not in _literal_predicates+ and str(item).strip().startswith(("http://", "https://", "urn:"))+ ):+ g.add((subj, pred, _resolve_uri(str(item), base_namespace)))+ else:+ g.add((subj, pred, rdflib.Literal(str(item))))++ for edge in edges:+ source = str(edge.get("source", edge.get("source_id", ""))).strip()+ target = str(edge.get("target", edge.get("target_id", ""))).strip()+ pred_str = str(edge.get("type", "related_to")).strip()+ if not source or not target:+ continue+ if source in entity_ids or target in entity_ids:+ g.add((+ _resolve_uri(source, base_namespace),+ _resolve_uri(pred_str, base_namespace),+ _resolve_uri(target, base_namespace),+ ))++ return await asyncio.to_thread(g.serialize, format="turtle")++@router.post("/shacl/generate", response_model=ShaclGenerateResponse)async def generate_shacl(request: Request,@@ -2211,7 +2431,11 @@ async def list_shacl_shapes(@router.post("/shacl/validate", response_model=ShaclValidationResponse)-async def validate_shacl(body: ShaclValidateRequest):+async def validate_shacl(+ request: Request,+ body: ShaclValidateRequest,+ session: GraphSession = Depends(get_session),+):if not body.shacl_turtle.strip():raise HTTPException(status_code=422, detail="SHACL Turtle cannot be empty.")@@ -2228,17 +2452,79 @@ async def validate_shacl(body: ShaclValidateRequest):detail=f"Invalid Turtle syntax: {exc}",) from exc- # Live data-graph validation requires pySHACL wired to OntologyEngine.validate_graph().+ if not body.uri:+ return ShaclValidationResponse(+ uri=body.uri,+ conforms=False,+ status="unavailable",+ message=(+ "Turtle parsed successfully. "+ "No target ontology URI was provided — "+ "specify 'uri' to execute live SHACL validation against an ontology data graph."+ ),+ violations=[],+ )++ try:+ data_graph_turtle = await _data_graph_turtle_for_uri(request, session, body.uri)+ from ...ontology import OntologyEngine+ engine = OntologyEngine()+ report = await asyncio.to_thread(+ engine.validate_graph,+ data_graph_turtle,+ shacl=body.shacl_turtle,+ data_graph_format="turtle",+ shacl_format="turtle",+ )+ except ImportError as exc:+ return ShaclValidationResponse(+ uri=body.uri,+ conforms=False,+ status="unavailable",+ message=(+ "Turtle parsed successfully. "+ f"Live SHACL validation is unavailable: {exc}"+ ),+ violations=[],+ )+ except HTTPException:+ raise+ except Exception as exc:+ return ShaclValidationResponse(+ uri=body.uri,+ conforms=False,+ status="error",+ message=f"SHACL validation error: {exc}",+ violations=[],+ )++ violations = [+ ShaclViolation(+ node=str(v.focus_node) if v.focus_node is not None else None,+ path=str(v.result_path) if v.result_path is not None else None,+ severity=str(v.severity or "Violation"),+ message=str(+ v.message+ or v.explanation+ or f"SHACL constraint violation ({v.constraint}) on {v.focus_node}"+ ),+ focus_node=str(v.focus_node) if v.focus_node is not None else None,+ source_shape=str(v.shape) if v.shape is not None else None,+ )+ for v in report.violations+ ]+ summary_msg = (+ "Graph conforms to SHACL shapes."+ if report.conforms+ else f"SHACL validation found {len(violations)} violation(s)."+ )return ShaclValidationResponse(uri=body.uri,- conforms=False,- status="unavailable",- message=(- "Turtle parsed successfully. "- "Live graph validation is not yet wired to a data graph — "- "install semantica[shacl] and connect OntologyEngine.validate_graph() to enable full validation."- ),- violations=[],+ conforms=report.conforms,+ status="success",+ message=summary_msg,+ violations=violations,+ report_text=report.raw_report,)diff --git a/tests/explorer/test_ontology_subissue3.py b/tests/explorer/test_ontology_subissue3.pyindex a95444d5..2500af0c 100644--- a/tests/explorer/test_ontology_subissue3.py+++ b/tests/explorer/test_ontology_subissue3.py@@ -1,5 +1,7 @@"""Tests for Ontology Hub subissue 3 APIs."""+from unittest.mock import patch+import pytestfrom semantica.context.context_graph import ContextGraph@@ -147,16 +149,29 @@ def test_shacl_validate_returns_unavailable(client):response = client.post("/api/ontology/shacl/validate",json={- "uri": "http://example.org/onto-a","shacl_turtle": "@prefix sh: <http://www.w3.org/ns/shacl#> .",},)assert response.status_code == 200payload = response.json()- assert payload["status"] == "unavailable", "stub must not report conforms=True before validation is wired"+ assert payload["status"] == "unavailable"assert payload["conforms"] is Falseassert isinstance(payload["violations"], list)+ with patch.dict("sys.modules", {"pyshacl": None}):+ res_no_pyshacl = client.post(+ "/api/ontology/shacl/validate",+ json={+ "uri": "http://example.org/onto-a",+ "shacl_turtle": "@prefix sh: <http://www.w3.org/ns/shacl#> .",+ },+ )+ assert res_no_pyshacl.status_code == 200+ payload_no_pyshacl = res_no_pyshacl.json()+ assert payload_no_pyshacl["status"] == "unavailable"+ assert payload_no_pyshacl["conforms"] is False+ assert isinstance(payload_no_pyshacl["violations"], list)+def test_shacl_validate_rejects_empty_turtle(client):response = client.post(@@ -166,20 +181,68 @@ def test_shacl_validate_rejects_empty_turtle(client):assert response.status_code == 422+def test_shacl_validate_detects_missing_required_property(client):+ graph = client.app.state.session.graph+ onto_uri = "http://example.org/onto-a"+ person_a = "http://example.org/onto-a#Person"+ person_inst = "http://example.org/onto-a#person-no-name"+ graph.add_node(+ person_inst,+ node_type="owl:NamedIndividual",+ content="Person Without Name",+ scheme_uri=onto_uri,+ **{+ "rdf:type": person_a,+ "rdfs:label": "Person Without Name",+ },+ )++ shacl_turtle = """+@prefix sh: <http://www.w3.org/ns/shacl#> .+@prefix onto: <http://example.org/onto-a#> .++onto:PersonNameShape a sh:NodeShape ;+ sh:targetClass onto:Person ;+ sh:property [+ sh:path onto:name ;+ sh:minCount 1 ;+ sh:severity sh:Violation ;+ sh:message "Person must have a name." ;+ ] .+"""++ response = client.post(+ "/api/ontology/shacl/validate",+ json={+ "uri": onto_uri,+ "shacl_turtle": shacl_turtle,+ },+ )+ assert response.status_code == 200+ payload = response.json()+ assert payload["status"] == "success"+ assert payload["conforms"] is False+ assert len(payload["violations"]) >= 1+ violation = payload["violations"][0]+ assert violation["severity"] == "Violation"+ assert "person-no-name" in str(violation["focus_node"]) or "person-no-name" in str(violation["node"])++def test_health_returns_404_for_unknown_ontology(client):response = client.get("/api/ontology/health?uri=http%3A%2F%2Fnot-loaded.example%2Fonto")assert response.status_code == 404def test_health_shacl_dimension_is_zero_when_unavailable(client):- payload = client.get("/api/ontology/health?uri=http%3A%2F%2Fexample.org%2Fonto-a").json()- shacl_dim = next(d for d in payload["dimensions"] if d["key"] == "shacl")- assert shacl_dim["status"] == "unavailable"- assert shacl_dim["score"] == 0.0- # Total score must NOT include the unavailable dimension in its average.- scoreable = [d for d in payload["dimensions"] if d["status"] != "unavailable"]- expected_total = round(sum(d["score"] for d in scoreable) / len(scoreable), 1)- assert payload["total_score"] == expected_total+ with patch.dict("sys.modules", {"pyshacl": None}):+ payload = client.get("/api/ontology/health?uri=http%3A%2F%2Fexample.org%2Fonto-a").json()+ shacl_dim = next(d for d in payload["dimensions"] if d["key"] == "shacl")+ assert shacl_dim["status"] == "unavailable"+ assert shacl_dim["score"] == 0.0+ # Total score must NOT include the unavailable dimension in its average.+ scoreable = [d for d in payload["dimensions"] if d["status"] != "unavailable"]+ expected_total = round(sum(d["score"] for d in scoreable) / len(scoreable), 1)+ assert payload["total_score"] == expected_totaldef test_delete_unknown_alignment_returns_404(client):@@ -261,3 +324,76 @@ def test_health_alignment_coverage_uses_set_lookup(client):payload = client.get("/api/ontology/health?uri=http%3A%2F%2Fexample.org%2Fonto-a").json()alignment_dim = next(d for d in payload["dimensions"] if d["key"] == "alignment")assert alignment_dim["score"] > 0.0, "alignment coverage must be non-zero after recording an alignment"+++def test_suggest_alignments_unaffected_by_individuals(client):+ graph = client.app.state.session.graph+ graph.add_node(+ "http://example.org/onto-a#individual-person",+ node_type="owl:NamedIndividual",+ content="Person",+ scheme_uri="http://example.org/onto-a",+ **{"rdf:type": "http://example.org/onto-a#Person", "rdfs:label": "Person"},+ )+ response = client.post(+ "/api/ontology/suggest-alignments",+ json={+ "source_ontology_uri": "http://example.org/onto-a",+ "target_ontology_uri": "http://example.org/onto-b",+ "threshold": 0.35,+ "limit": 5,+ },+ )+ assert response.status_code == 200+ suggestions = response.json()+ assert suggestions+ for s in suggestions:+ assert "individual-person" not in s["source_uri"]+ assert "individual-person" not in s["target_uri"]+++def test_health_shacl_dimension_degrades_gracefully_on_real_error(client):+ import semantica.explorer.routes.ontology as ont_mod+ with patch.object(ont_mod, "_data_graph_turtle_for_uri", side_effect=RuntimeError("boom")):+ response = client.get("/api/ontology/health?uri=http%3A%2F%2Fexample.org%2Fonto-a")+ assert response.status_code == 200+ payload = response.json()+ shacl_dim = next(d for d in payload["dimensions"] if d["key"] == "shacl")+ assert shacl_dim["status"] == "critical"+ assert "boom" in shacl_dim["detail"]+++async def test_data_graph_turtle_resolves_onto_prefix_and_unknown_curies(client):+ from unittest.mock import MagicMock+ from semantica.explorer.routes.ontology import _data_graph_turtle_for_uri++ graph = client.app.state.session.graph+ onto_uri = "http://example.org/onto-a"+ person_a = "http://example.org/onto-a#Person"+ person_inst = "http://example.org/onto-a#person-with-name"+ graph.add_node(+ person_inst,+ node_type="owl:NamedIndividual",+ content="Person With Name",+ scheme_uri=onto_uri,+ **{+ "rdf:type": person_a,+ "onto:name": "Alice",+ "custom:prop": "http://custom.example/val",+ },+ )++ ttl = await _data_graph_turtle_for_uri(MagicMock(), client.app.state.session, onto_uri)+ assert "http://example.org/onto-a#name" in ttl or "onto:name" in ttl+ assert "http://example.org/#onto:name" not in ttl+ assert "http://example.org/onto-a#custom:prop" not in ttl+++def test_ontology_namespace_helper_handles_all_uri_forms():+ from semantica.explorer.routes.ontology import _ontology_namespace++ assert _ontology_namespace("http://example.org/onto-a") == "http://example.org/onto-a#"+ assert _ontology_namespace("http://example.org/onto-a/") == "http://example.org/onto-a/"+ assert _ontology_namespace("http://example.org/onto-a#") == "http://example.org/onto-a#"+ assert _ontology_namespace("http://example.org/onto-a#schema") == "http://example.org/onto-a#"