Skip to content

Graph-Native Infrastructure for Context and Accountable AI Systems

fix(ontology): reject normalized class name collisions (#1230)

Guofang.Tangdc81acaefd33
diff --git a/semantica/ontology/class_inferrer.py b/semantica/ontology/class_inferrer.py
index 08a0aec9..16c8e912 100644
--- a/semantica/ontology/class_inferrer.py
+++ b/semantica/ontology/class_inferrer.py
@@ -148,6 +148,26 @@ class ClassInferrer:
entity_type = entity.get("type") or entity.get("entity_type", "Entity")
entity_types[entity_type].append(entity)
+ normalized_types = defaultdict(list)
+ for entity_type, type_entities in entity_types.items():
+ if len(type_entities) >= self.min_occurrences:
+ normalized_name = self.naming_conventions.normalize_class_name(
+ str(entity_type)
+ )
+ normalized_types[normalized_name].append(str(entity_type))
+
+ collisions = {
+ normalized_name: source_types
+ for normalized_name, source_types in normalized_types.items()
+ if len(source_types) > 1
+ }
+ if collisions:
+ raise ValidationError(
+ "Entity types normalize to duplicate class names; "
+ "rename the source types or provide an explicit mapping.",
+ validation_context={"normalized_type_collisions": collisions},
+ )
+
# Infer classes from entity types
self.progress_tracker.update_tracking(
tracking_id,
diff --git a/tests/ontology/test_ontology_normalized_properties.py b/tests/ontology/test_ontology_normalized_properties.py
index 5cef53d3..6f07ead9 100644
--- a/tests/ontology/test_ontology_normalized_properties.py
+++ b/tests/ontology/test_ontology_normalized_properties.py
@@ -1,6 +1,9 @@
+import pytest
+
from semantica.ontology.class_inferrer import ClassInferrer
from semantica.ontology.ontology_generator import OntologyGenerator
from semantica.ontology.property_generator import PropertyGenerator
+from semantica.utils.exceptions import ValidationError
def _entities():
@@ -39,3 +42,15 @@ def test_ontology_pipeline_emits_data_properties_for_normalized_types():
email = next(prop for prop in ontology["properties"] if prop["name"] == "email")
assert email["domain"] == ["SoftwareEngineer"]
assert email["range"] == "xsd:string"
+
+
+def test_class_inference_rejects_normalized_type_collisions():
+ entities = [
+ {"type": "Person", "name": "Alice"},
+ {"type": "Person", "name": "Bob"},
+ {"type": "person", "name": "Carol"},
+ {"type": "person", "name": "Dan"},
+ ]
+
+ with pytest.raises(ValidationError, match="duplicate class names"):
+ ClassInferrer().infer_classes(entities)