---
title: Guide: Ontology Parser
description: **Version:** 1.0 **Date:** 2025-10-27
category: how-to
tags:
- tutorial
- backend
- documentation
- reference
- visionclaw
updated-date: 2025-12-18
difficulty-level: intermediate
---
Version: 1.0 Date: 2025-10-27
The OntologyParser module is a crucial component for semantic understanding within the VisionClaw system. It is designed to parse markdown files that contain ontology definitions written in a Logseq-style format. The parser extracts OWL (Web Ontology Language) structures, including classes, properties, and axioms, which are then used to build the knowledge graph's semantic layer.
This guide provides developers with the necessary information to use the parser, understand its syntax, and integrate it into their workflows.
- Markdown Ontology Block Detection: Identifies
### OntologyBlockmarkers in markdown files to isolate ontology definitions. - OWL Class Extraction: Parses class definitions including IRI, label, description, and parent classes (
subClassOf). - OWL Property Extraction: Supports
objectProperty(relations between entities) anddataProperty(relations to literal values), including their domain and range. - Axiom Extraction: Captures logical statements like
subClassOfto define class hierarchies. - Source File Tracking: Automatically records the source file for each parsed class, aiding in traceability.
The parser uses a simple, indented syntax within a designated OntologyBlock.
All ontology definitions must be placed within a block marked as follows:
- ### OntologyBlock
... ontology definitions go here ...- owl-class:: ClassName
- label:: Human Readable Name
- description:: A brief description of the class.
- subClassOf:: ParentClassNameMultiple Parents:
- owl-class:: ChildClass
- subClassOf:: Parent1
- subClassOf:: Parent2Object Property:
- objectProperty:: propertyName
- label:: A human-readable label for the property
- domain:: SourceClass
- range:: TargetClassData Property:
- dataProperty:: propertyName
- label:: A human-readable label
- domain:: SourceClass
- range:: xsd:datatype # e.g., xsd:string, xsd:integerMultiple Domains/Ranges:
- objectProperty:: hasRelative
- domain:: Person, Animal
- range:: Person, AnimalThe parser supports multiple IRI formats for flexibility:
- owl-class:: SimpleName
- owl-class:: prefix:Name
- owl-class:: http://example.org/ontology#NameThe following Rust code demonstrates how to use the OntologyParser.
use webxr::services::parsers::OntologyParser;
use webxr::ports::ontology-repository::OntologyRepository;
async fn parse-and-store-ontology(
repo: &impl OntologyRepository,
markdown-content: &str,
source-filename: &str
) -> Result<(), String> {
// 1. Create a new parser instance
let parser = OntologyParser::new();
// 2. Parse the markdown content
let ontology-data = parser.parse(markdown-content, source-filename)?;
// 3. (Optional) Print the parsed data
println!("Parsed {} classes, {} properties, and {} axioms.",
ontology-data.classes.len(),
ontology-data.properties.len(),
ontology-data.axioms.len()
);
// 4. Store the parsed data in a repository
repo.save-ontology(
&ontology-data.classes,
&ontology-data.properties,
&ontology-data.axioms
).await.map-err(|e| e.to-string())?;
Ok(())
}The parser returns an OntologyData struct, which contains vectors of the core OWL structures.
pub struct OntologyData {
pub classes: Vec<OwlClass>,
pub properties: Vec<OwlProperty>,
pub axioms: Vec<OwlAxiom>,
pub class-hierarchy: Vec<(String, String)>, // (child-iri, parent-iri)
}
pub struct OwlClass {
pub iri: String,
pub label: Option<String>,
pub description: Option<String>,
pub parent-classes: Vec<String>,
pub source-file: Option<String>,
// ... other fields
}
pub struct OwlProperty {
pub iri: String,
pub label: Option<String>,
pub property-type: PropertyType, // ObjectProperty, DataProperty
pub domain: Vec<String>,
pub range: Vec<String>,
}
pub struct OwlAxiom {
pub axiom-type: AxiomType, // e.g., SubClassOf
pub subject: String,
pub object: String,
// ... other fields
}The parse method returns a Result<OntologyData, String>. If parsing fails, it will return an Err with a descriptive error message, such as "No OntologyBlock found in file".
The OntologyParser module includes a comprehensive suite of unit tests to ensure its correctness. You can run these tests with:
cargo test --lib parsers::ontology-parser::testsThe test suite covers:
- Basic class and property parsing
- Class hierarchy and axiom extraction
- Handling of multiple IRI formats
- Error handling for missing
OntologyBlock