How Semantica Generates OWL Ontologies: A Complete Guide to the 6-Stage Pipeline
Semantica generates OWL ontologies through a six-stage pipeline orchestrated by the OntologyGenerator class, transforming raw entities and relationships into validated, standards-compliant OWL files.
Semantica, the open-source semantic AI framework from semantica-agi, converts unstructured and structured data into machine-readable knowledge representations. OWL ontology generation is one of its core capabilities, enabling automated knowledge graph construction for downstream reasoning and analytics.
The Six-Stage OWL Generation Pipeline
The OntologyGenerator class in semantica/ontology/ontology_generator.py implements a deterministic pipeline that processes data into OWL ontologies. Each stage builds upon the previous, ensuring type safety and logical consistency.
Stage 1: Semantic Network Parsing
Raw entities and relationships are normalized and grouped into abstract concepts. This stage handles data cleaning, deduplication, and initial type inference from the input structure.
Stage 2: YAML-to-Definition Conversion
Concepts are transformed into formal class definitions using the ClassInferrer component. The ClassInferrer extracts class candidates from entity types and prepares them for OWL serialization.
Stage 3: Definition-to-Types Enrichment
Class definitions receive explicit OWL type annotations (owl:Class, owl:ObjectProperty, owl:DatatypeProperty). The PropertyGenerator infers property definitions—including domains, ranges, and cardinalities—from relationship patterns in the source data.
Stage 4: Hierarchy Generation
Class hierarchies and sub-class relationships are constructed via ClassInferrer.build_class_hierarchy. This stage analyzes entity co-occurrence and type specificity to infer rdfs:subClassOf relationships.
Stage 5: TTL/OWL Serialization
The fully-formed ontology dictionary is passed to OWLExporter (semantica/export/owl_exporter.py), which generates either Turtle or OWL-XML syntax. The exporter handles IRI resolution, namespace prefixing, and property splitting between object and datatype properties.
Stage 6: Symbolic Validation
The OntologyValidator executes OWL reasoning using HermiT or Pellet reasoners to verify consistency, satisfiability, and quality metrics before final output.
Core Components in the Generation Stack
| Component | Source File | Role |
|---|---|---|
OntologyGenerator |
semantica/ontology/ontology_generator.py |
Orchestrates the pipeline; produces dict with uri, name, classes, properties |
ClassInferrer |
semantica/ontology/class_inferrer.py |
Infers classes from entities and builds hierarchies |
PropertyGenerator |
semantica/ontology/property_generator.py |
Infers object/data properties with domains, ranges, cardinalities |
NamespaceManager |
semantica/ontology/namespace_manager.py |
Supplies base URI and creates IRIs for all terms |
OWLExporter |
semantica/export/owl_exporter.py |
Serializes to Turtle or OWL-XML; writes output files |
OntologyValidator |
semantica/ontology/ontology_validator.py |
Runs OWL reasoning for validation |
Generate OWL Ontologies: Practical Code Examples
Complete Pipeline: Generation and Export
The generate_ontology method executes stages 1–4 and 6 (validation). The export method handles stage 5.
from semantica.ontology import OntologyGenerator
from semantica.export import OWLExporter
# Example input data
data = {
"entities": [
{"type": "Person", "name": "Alice"},
{"type": "Company", "name": "Acme Corp"},
],
"relationships": [
{"source": "Alice", "type": "worksFor", "target": "Acme Corp"},
],
}
# Generate the ontology dictionary
generator = OntologyGenerator(base_uri="https://example.org/ontology/")
ontology = generator.generate_ontology(data)
# Export to Turtle format
exporter = OWLExporter(ontology_uri="https://example.org/ontology/")
exporter.export(ontology, "my_ontology.ttl", format="turtle")
Multi-Format Export
from semantica.ontology import OntologyGenerator
from semantica.export import OWLExporter
gen = OntologyGenerator(base_uri="https://myorg.com/ont/")
ont = gen.generate_ontology(
{
"entities": [{"type": "Person", "name": "Bob"}],
"relationships": [{"source": "Bob", "type": "hasSkill", "target": "Python"}],
}
)
owl = OWLExporter()
owl.export(ont, "person_ontology.owl") # OWL-XML (default)
owl.export(ont, "person_ontology.ttl", format="turtle")
Export Class Definitions Only
classes = [
{"uri": "https://myorg.com/ont/Person", "name": "Person"},
{"uri": "https://myorg.com/ont/Company", "name": "Company"},
]
exporter = OWLExporter()
exporter.export_classes(classes, "classes.owl")
Export Property Definitions Only
props = [
{"uri": "https://myorg.com/ont/worksFor", "name": "worksFor",
"type": "object", "domain": ["Person"], "range": ["Company"]},
]
exporter = OWLExporter()
exporter.export_properties(props, "props.owl", property_type="object")
Input Data Format for OWL Generation
The pipeline accepts a dictionary with two required keys:
entities: List of objects withtype(required) and additional propertiesrelationships: List of objects withsource,type, andtarget
The OntologyGenerator uses type fields to infer class names and relationship types to infer property names. The NamespaceManager automatically generates persistent IRIs by combining the base_uri with sanitized local names.
Validation and Quality Assurance
The OntologyValidator (semantica/ontology/ontology_validator.py) performs automated quality checks:
- Consistency checking: Detects logical contradictions using HermiT or Pellet
- Satisfiability testing: Verifies that all defined classes can have instances
- Metrics computation: Generates quality scores for ontology completeness
Validation runs automatically during generate_ontology and can be invoked separately for external ontologies.
Summary
- The six-stage pipeline in
OntologyGeneratortransforms raw data into OWL through parsing, definition inference, type enrichment, hierarchy building, serialization, and validation ClassInferrerandPropertyGeneratorhandle the semantic heavy lifting for classes and propertiesOWLExportersupports both Turtle and OWL-XML output formats via theformatparameterOntologyValidatorensures logical consistency using standard OWL reasoners- All components are located in
semantica/ontology/andsemantica/export/directories
Frequently Asked Questions
What input formats does Semantica accept for OWL generation?
Semantica expects a Python dictionary with entities and relationships keys. Each entity requires a type field; relationships require source, type, and target. The pipeline internally normalizes this structure before OWL generation begins.
Can I export to formats other than Turtle and OWL-XML?
According to the current source code in semantica/export/owl_exporter.py, the OWLExporter class supports turtle and owl (OWL-XML) formats through its format parameter. Additional formats would require extending the exporter implementation.
How does Semantica handle ontology validation?
The OntologyValidator class integrates HermiT and Pellet reasoners to perform automated consistency and satisfiability checking. Validation runs by default during ontology generation and can be disabled or invoked separately for custom validation workflows.
What determines the IRI structure in generated ontologies?
The NamespaceManager (semantica/ontology/namespace_manager.py) combines the base_uri parameter from OntologyGenerator with sanitized local names derived from entity and relationship types. This ensures persistent, resolvable identifiers for all ontology terms.
Have a question about this repo?
These articles cover the highlights, but your codebase questions are specific. Give your agent direct access to the source. Share this with your agent to get started:
curl -s "https://instagit.com/install.md" Maintain an open-source project? Get it listed too →