Skip to content

Graph-Native Infrastructure for Context and Accountable AI Systems

fix(ingest): preserve repeated ontology domain and range values (#1276)

Wei Tao1ed2c6e9356c
diff --git a/semantica/ingest/ontology_ingestor.py b/semantica/ingest/ontology_ingestor.py
index 0d63a770..9b38bb54 100644
--- a/semantica/ingest/ontology_ingestor.py
+++ b/semantica/ingest/ontology_ingestor.py
@@ -386,13 +386,25 @@ class OntologyIngestor:
prop_def["description"] = str(comment)
# Domain and Range
- domain = graph.value(subject, RDFS.domain)
- if domain and isinstance(domain, rdflib.URIRef):
- prop_def["domain"] = str(domain)
-
- range_val = graph.value(subject, RDFS.range)
- if range_val and isinstance(range_val, rdflib.URIRef):
- prop_def["range"] = str(range_val)
+ domains = sorted(
+ {
+ str(domain)
+ for domain in graph.objects(subject, RDFS.domain)
+ if isinstance(domain, rdflib.URIRef)
+ }
+ )
+ if domains:
+ prop_def["domain"] = domains[0] if len(domains) == 1 else domains
+
+ ranges = sorted(
+ {
+ str(range_val)
+ for range_val in graph.objects(subject, RDFS.range)
+ if isinstance(range_val, rdflib.URIRef)
+ }
+ )
+ if ranges:
+ prop_def["range"] = ranges[0] if len(ranges) == 1 else ranges
properties_dict[uri] = prop_def
diff --git a/semantica/visualization/ontology_visualizer.py b/semantica/visualization/ontology_visualizer.py
index b3ec564d..f111af80 100644
--- a/semantica/visualization/ontology_visualizer.py
+++ b/semantica/visualization/ontology_visualizer.py
@@ -428,7 +428,10 @@ class OntologyVisualizer:
prop_name = prop.get("name") or prop.get("uri", "")
# Check if property belongs to class (via domain or direct property list)
domain = prop.get("domain")
- has_prop = prop_name in cls_props or domain == cls.get("name")
+ domains = (
+ domain if isinstance(domain, list) else [domain] if domain else []
+ )
+ has_prop = prop_name in cls_props or cls.get("name") in domains
row.append(1 if has_prop else 0)
matrix.append(row)
@@ -949,12 +952,18 @@ class OntologyVisualizer:
domain = prop.get("domain")
if domain:
- edges.append({"source": prop_name, "target": domain, "type": "domain"})
+ domains = domain if isinstance(domain, list) else [domain]
+ edges.extend(
+ {"source": prop_name, "target": value, "type": "domain"}
+ for value in domains
+ )
range_val = prop.get("range")
if range_val:
- edges.append(
- {"source": prop_name, "target": range_val, "type": "range"}
+ ranges = range_val if isinstance(range_val, list) else [range_val]
+ edges.extend(
+ {"source": prop_name, "target": value, "type": "range"}
+ for value in ranges
)
# Use similar approach as KG visualizer
diff --git a/tests/ingest/test_ontology_ingestor.py b/tests/ingest/test_ontology_ingestor.py
index 11cc246f..df59bd22 100644
--- a/tests/ingest/test_ontology_ingestor.py
+++ b/tests/ingest/test_ontology_ingestor.py
@@ -44,6 +44,77 @@ class TestOntologyIngestor:
if os.path.exists(tmp_path):
os.remove(tmp_path)
+ def test_ingest_preserves_multiple_property_domains_and_ranges(self):
+ ttl_content = """
+ @prefix : <http://example.org/ontology/> .
+ @prefix owl: <http://www.w3.org/2002/07/owl#> .
+ @prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
+ @prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
+
+ <http://example.org/ontology/> a owl:Ontology .
+ :Person a owl:Class .
+ :Place a owl:Class .
+ :name a owl:DatatypeProperty ;
+ rdfs:domain :Person, :Place ;
+ rdfs:range xsd:normalizedString, xsd:string .
+ :label a owl:DatatypeProperty ;
+ rdfs:domain :Person, :Place ;
+ rdfs:range xsd:string .
+ :email a owl:DatatypeProperty ;
+ rdfs:domain :Person ;
+ rdfs:range xsd:string .
+ :description a owl:DatatypeProperty ;
+ rdfs:range xsd:string .
+ """
+ with tempfile.NamedTemporaryFile(delete=False, suffix=".ttl", mode="w") as tmp:
+ tmp.write(ttl_content)
+ tmp_path = tmp.name
+
+ try:
+ result = OntologyIngestor().ingest_ontology(tmp_path)
+
+ # --- multi-domain / multi-range: list output ---
+ name_property = next(
+ prop for prop in result.data["properties"] if prop["name"] == "name"
+ )
+ assert name_property["domain"] == [
+ "http://example.org/ontology/Person",
+ "http://example.org/ontology/Place",
+ ]
+ assert name_property["range"] == [
+ "http://www.w3.org/2001/XMLSchema#normalizedString",
+ "http://www.w3.org/2001/XMLSchema#string",
+ ]
+
+ # --- multi-domain / single-range ---
+ label_property = next(
+ prop for prop in result.data["properties"] if prop["name"] == "label"
+ )
+ assert label_property["domain"] == [
+ "http://example.org/ontology/Person",
+ "http://example.org/ontology/Place",
+ ]
+ # single range value must be a scalar string, not a list
+ assert label_property["range"] == "http://www.w3.org/2001/XMLSchema#string"
+
+ # --- single domain: must be a scalar string, not a one-element list ---
+ email_property = next(
+ prop for prop in result.data["properties"] if prop["name"] == "email"
+ )
+ assert email_property["domain"] == "http://example.org/ontology/Person"
+ assert not isinstance(email_property["domain"], list)
+
+ # --- no rdfs:domain: key must be absent entirely ---
+ description_property = next(
+ prop for prop in result.data["properties"]
+ if prop["name"] == "description"
+ )
+ assert "domain" not in description_property
+
+ finally:
+ if os.path.exists(tmp_path):
+ os.remove(tmp_path)
+
def test_ingest_directory(self, sample_ttl_content):
with tempfile.TemporaryDirectory() as tmp_dir:
# Create two ontology files
diff --git a/tests/visualization/test_visualization.py b/tests/visualization/test_visualization.py
index 424caf0e..8163354b 100644
--- a/tests/visualization/test_visualization.py
+++ b/tests/visualization/test_visualization.py
@@ -63,5 +63,52 @@ class TestVisualization(unittest.TestCase):
self.assertTrue(hasattr(viz, 'visualize_hierarchy'))
# visualize_properties, visualize_structure, visualize_class_property_matrix, visualize_metrics, visualize_semantic_model
+ @patch(
+ 'semantica.visualization.kg_visualizer.KGVisualizer._visualize_network_plotly'
+ )
+ def test_ontology_property_visualization_expands_multi_value_edges(
+ self, mock_visualize
+ ):
+ viz = OntologyVisualizer()
+ properties = [
+ {
+ "name": "name",
+ "domain": ["Person", "Organization"],
+ "range": ["string", "normalizedString"],
+ }
+ ]
+
+ viz._visualize_properties_plotly(properties, [], "interactive", None)
+
+ nodes, edges = mock_visualize.call_args.args[:2]
+ self.assertEqual(nodes, [{"id": "name", "label": "name", "type": "property"}])
+ self.assertEqual(
+ edges,
+ [
+ {"source": "name", "target": "Person", "type": "domain"},
+ {"source": "name", "target": "Organization", "type": "domain"},
+ {"source": "name", "target": "string", "type": "range"},
+ {
+ "source": "name",
+ "target": "normalizedString",
+ "type": "range",
+ },
+ ],
+ )
+
+ def test_class_property_matrix_marks_every_domain_of_a_multi_domain_property(self):
+ viz = OntologyVisualizer()
+ ontology = {
+ "classes": [{"name": "Person"}, {"name": "Place"}],
+ "properties": [
+ {"name": "name", "domain": ["Person", "Place"]},
+ {"name": "label", "domain": "Person"},
+ ],
+ }
+
+ fig = viz.visualize_class_property_matrix(ontology)
+
+ self.assertEqual([list(row) for row in fig.data[0].z], [[1, 1], [1, 0]])
+
if __name__ == '__main__':
unittest.main()