Skip to content

Graph-Native Infrastructure for Context and Accountable AI Systems

fix(explorer): serve entity_type from the ontology graph endpoint (#1438)

Wei Tao041b9cc167fe
diff --git a/explorer/src/workspaces/OntologyWorkspace/OntologyEditor.tsx b/explorer/src/workspaces/OntologyWorkspace/OntologyEditor.tsx
index f09ec164..f5e814a5 100644
--- a/explorer/src/workspaces/OntologyWorkspace/OntologyEditor.tsx
+++ b/explorer/src/workspaces/OntologyWorkspace/OntologyEditor.tsx
@@ -27,7 +27,6 @@ import {
import { loadOntologyEntityOwner, loadOntologyGraph } from "./api";
import type { OntologyGraphEdge, OntologyGraphNode } from "./api";
import {
- classifyNodeType,
isEditableEntityType,
ONTOLOGY_MINIMAP_THEME,
resolveEditorOntology,
@@ -150,8 +149,19 @@ function nodeLabel(node: OntologyGraphNode): string {
return trimmed.split("#").pop() || trimmed.split("/").pop() || node.id;
}
+// The backend's remaining kinds (concept, scheme, individual, unknown) have no
+// editor affordances, so they collapse into the read-only "external" kind. That
+// reading belongs here rather than on the wire: "external reference material"
+// is what the editor does about an unnameable type, not what the type is.
function classifyEditorNode(node: OntologyGraphNode): OntologyNodeData["entityType"] {
- return classifyNodeType(node.type);
+ switch (node.entity_type) {
+ case "ontology":
+ case "class":
+ case "property":
+ return node.entity_type;
+ default:
+ return "external";
+ }
}
function layoutEditorNodes(inputNodes: OntologyNode[]): OntologyNode[] {
diff --git a/explorer/src/workspaces/OntologyWorkspace/api.ts b/explorer/src/workspaces/OntologyWorkspace/api.ts
index b14897ae..922c1f21 100644
--- a/explorer/src/workspaces/OntologyWorkspace/api.ts
+++ b/explorer/src/workspaces/OntologyWorkspace/api.ts
@@ -12,6 +12,9 @@ import type {
export type OntologyGraphNode = {
id: string;
type: string;
+ // Required: /graph always classifies. Optional here would make a producer
+ // that forgets read as a graph of read-only nodes rather than a type error.
+ entity_type: string;
content?: string;
properties?: Record<string, unknown>;
};
diff --git a/explorer/src/workspaces/OntologyWorkspace/ontologyEditorModel.ts b/explorer/src/workspaces/OntologyWorkspace/ontologyEditorModel.ts
index 4b82a54a..55ec66a4 100644
--- a/explorer/src/workspaces/OntologyWorkspace/ontologyEditorModel.ts
+++ b/explorer/src/workspaces/OntologyWorkspace/ontologyEditorModel.ts
@@ -20,31 +20,6 @@ export const ONTOLOGY_MINIMAP_THEME = {
},
} as const;
-// The backend emits node types in compact (owl:Class) or full IRI
-// (http://www.w3.org/2002/07/owl#Class) form; classification must accept both.
-const FULL_IRI_PREFIXES: Array<[string, string]> = [
- ["http://www.w3.org/2002/07/owl#", "owl:"],
- ["http://www.w3.org/2000/01/rdf-schema#", "rdfs:"],
- ["http://www.w3.org/2004/02/skos/core#", "skos:"],
-];
-
-export function compactNodeType(type: string): string {
- for (const [iri, prefix] of FULL_IRI_PREFIXES) {
- if (type.startsWith(iri)) {
- return `${prefix}${type.slice(iri.length)}`;
- }
- }
- return type;
-}
-
-export function classifyNodeType(rawType: string): EditorEntityType {
- const type = compactNodeType(rawType);
- if (type === "owl:Ontology") return "ontology";
- if (type === "owl:Class" || type === "rdfs:Class") return "class";
- if (type.includes("Property")) return "property";
- return "external";
-}
-
// Last-resort guess, reached only when the backend gave no verdict: it has no
// notion of nested vocabularies, so it can name a parent that does not contain
// the entity. Authority is owning_ontology from /api/ontology/entity
diff --git a/explorer/tests/ontologyEditorModel.test.ts b/explorer/tests/ontologyEditorModel.test.ts
index 0ca94786..73c7b7a3 100644
--- a/explorer/tests/ontologyEditorModel.test.ts
+++ b/explorer/tests/ontologyEditorModel.test.ts
@@ -2,8 +2,6 @@ import assert from "node:assert/strict";
import test from "node:test";
import {
- classifyNodeType,
- compactNodeType,
inferOntologyUri,
isEditableEntityType,
resolveEditorOntology,
@@ -58,28 +56,6 @@ test("the ontology minimap has an explicit dark, high-contrast theme", () => {
assert.match(ONTOLOGY_MINIMAP_THEME.style.border, /#29435c/);
});
-test("node types classify identically in compact and full IRI form", () => {
- const cases: Array<[string, string, string]> = [
- ["owl:Ontology", "http://www.w3.org/2002/07/owl#Ontology", "ontology"],
- ["owl:Class", "http://www.w3.org/2002/07/owl#Class", "class"],
- ["rdfs:Class", "http://www.w3.org/2000/01/rdf-schema#Class", "class"],
- ["owl:ObjectProperty", "http://www.w3.org/2002/07/owl#ObjectProperty", "property"],
- ["owl:DatatypeProperty", "http://www.w3.org/2002/07/owl#DatatypeProperty", "property"],
- ["owl:AnnotationProperty", "http://www.w3.org/2002/07/owl#AnnotationProperty", "property"],
- ];
- for (const [compact, fullIri, expected] of cases) {
- assert.equal(classifyNodeType(compact), expected, compact);
- assert.equal(classifyNodeType(fullIri), expected, fullIri);
- }
- assert.equal(classifyNodeType("owl:NamedIndividual"), "external");
- assert.equal(classifyNodeType("http://www.w3.org/2004/02/skos/core#Concept"), "external");
-});
-
-test("compactNodeType leaves unknown namespaces untouched", () => {
- assert.equal(compactNodeType("https://example.org/custom#Thing"), "https://example.org/custom#Thing");
- assert.equal(compactNodeType("owl:Class"), "owl:Class");
-});
-
test("an authoritative no-owner verdict suppresses the namespace guess", () => {
// Without suppression the prefix guess would pick the registered parent
// for an unregistered nested entity — the deep link must not do that.
diff --git a/semantica/explorer/routes/ontology.py b/semantica/explorer/routes/ontology.py
index 3e9e8342..028cb788 100644
--- a/semantica/explorer/routes/ontology.py
+++ b/semantica/explorer/routes/ontology.py
@@ -1996,7 +1996,15 @@ async def get_ontology_graph(
str(edge.get("id", "")),
)
)
- return OntologyGraphResponse(uri=uri, nodes=selected_nodes, edges=selected_edges)
+ # Annotate copies: session node dicts may be cached and shared with other callers.
+ return OntologyGraphResponse(
+ uri=uri,
+ nodes=[
+ {**node, "entity_type": _classify_node_type(node.get("type", ""))}
+ for node in selected_nodes
+ ],
+ edges=selected_edges,
+ )
@router.get("/entity/{entity_uri:path}", response_model=EntityDetailResponse)
diff --git a/tests/explorer/test_ontology_subissue3.py b/tests/explorer/test_ontology_subissue3.py
index e7116ffd..9dd8f41d 100644
--- a/tests/explorer/test_ontology_subissue3.py
+++ b/tests/explorer/test_ontology_subissue3.py
@@ -157,6 +157,76 @@ def test_ontology_graph_returns_editable_schema_nodes_and_edges(client):
)
+def test_ontology_graph_nodes_carry_entity_type_for_compact_and_full_iri_types(client):
+ graph = client.app.state.session.graph
+ full_iri_class = "http://example.org/onto-a#Organization"
+ full_iri_property = "http://example.org/onto-a#employs"
+ full_iri_ontology = "http://example.org/full-iri-onto"
+ unrecognized_external = "http://vocab.example/Widget"
+ graph.add_node(
+ full_iri_class,
+ node_type="http://www.w3.org/2002/07/owl#Class",
+ content="Organization",
+ scheme_uri="http://example.org/onto-a",
+ )
+ graph.add_node(
+ full_iri_property,
+ node_type="http://www.w3.org/2002/07/owl#ObjectProperty",
+ content="employs",
+ scheme_uri="http://example.org/onto-a",
+ )
+ graph.add_node(
+ full_iri_ontology,
+ node_type="http://www.w3.org/2002/07/owl#Ontology",
+ content="Full IRI Ontology",
+ scheme_uri="http://example.org/onto-a",
+ )
+ # An outward reference to a node whose type is outside the schema
+ # vocabulary. /graph reports the classifier's verdict verbatim; reading it
+ # as "external reference material" is the editor's job, not the wire's.
+ graph.add_node(unrecognized_external, node_type="ex:Widget", content="Widget")
+ graph.add_edge(full_iri_property, unrecognized_external, edge_type="rdfs:range")
+
+ response = client.get(
+ "/api/ontology/graph",
+ params={"uri": "http://example.org/onto-a"},
+ )
+
+ assert response.status_code == 200
+ entity_types = {node["id"]: node["entity_type"] for node in response.json()["nodes"]}
+ assert entity_types["http://example.org/onto-a"] == "ontology"
+ assert entity_types["http://example.org/onto-a#Person"] == "class"
+ assert entity_types[full_iri_class] == "class"
+ assert entity_types["http://example.org/onto-a#name"] == "property"
+ assert entity_types[full_iri_property] == "property"
+ assert entity_types[full_iri_ontology] == "ontology"
+ assert entity_types[unrecognized_external] == "unknown"
+
+
+def test_graph_and_entity_endpoints_agree_on_entity_type(client):
+ graph = client.app.state.session.graph
+ unrecognized_external = "http://vocab.example/Widget"
+ graph.add_node(unrecognized_external, node_type="ex:Widget", content="Widget")
+ graph.add_edge(
+ "http://example.org/onto-a#name", unrecognized_external, edge_type="rdfs:range"
+ )
+
+ graph_response = client.get(
+ "/api/ontology/graph", params={"uri": "http://example.org/onto-a"}
+ )
+ assert graph_response.status_code == 200
+ from_graph = {
+ node["id"]: node["entity_type"] for node in graph_response.json()["nodes"]
+ }
+
+ for uri in ("http://example.org/onto-a#Person", unrecognized_external):
+ # The fragment has to survive the request or the path resolves to the
+ # parent ontology and the comparison silently passes on the wrong node.
+ detail = client.get(f"/api/ontology/entity/{quote(uri, safe='')}")
+ assert detail.status_code == 200
+ assert from_graph[uri] == detail.json()["entity_type"]
+
+
def test_ontology_graph_rejects_unregistered_namespace(client):
response = client.get(
"/api/ontology/graph",