RDF / OWL ontology conversion

The cvcdocdb.rdf_schema module provides the full pipeline for converting RDF/OWL ontologies into DRM YAML schemas and Python entity classes.

Supported input formats

Turtle (.ttl), RDF/XML (.rdf), N-Triples (.nt), N-Quads (.nq), TriX (.trix), Trig (.trig), and JSON-LD (.jsonld).

OWL constructs mapped to DRM

RDF / OWL construct

DRM mapping

owl:Class

Node label

rdfs:subClassOf

WeakNode hierarchy (parent)

owl:DatatypeProperty

Node properties (rdfs:domain)

owl:ObjectProperty

Relationships (rdfs:domain/range)

owl:hasKey

Primary key fields

owl:uniqueIdentity

Unique identifier fields

rdfs:range

Relationship destination label

rdfs:comment

Class docstring

Usage examples

Download and convert in one step:

from cvcdocdb.rdf_schema import download_ontology_and_convert

output_path = download_ontology_and_convert(
    "https://raw.githubusercontent.com/ICA-EGAD/RiC-O/master/ontology/current-version/RiC-O_1-1.rdf",
    "rico",
    output_dir="cvcdocdb/"
)
# Generates cvcdocdb/rico_entities.py

Step by step:

from cvcdocdb.rdf_schema import download_ontology, rdf_to_yaml
from cvcdocdb.schema_gen import generate_classes

# 1. Download
ont_path = download_ontology(url, output_dir="ontologies/")

# 2. Convert RDF -> YAML
yaml_str = rdf_to_yaml(ont_path, "my_db")

# 3. Generate Python classes
py_source = generate_classes(yaml_str)

# 4. Write file
with open("cvcdocdb/entities_my_db.py", "w") as f:
    f.write(py_source)

Convert RDF/OWL ontology to DRM YAML schema.

Parses an RDF file (Turtle, RDF/XML, N-Triples, etc.) and produces a YAML schema compatible with cvcdocdb.schema_gen.generate_classes().

Ontology features mapped:

  • owl:Class / rdfs:Class → Node label

  • rdfs:subClassOf → WeakNode hierarchy (parent)

  • owl:DatatypeProperty → Node properties (rdfs:domain)

  • owl:ObjectProperty → Relationships (rdfs:domain / range)

  • owl:hasKey → Primary key fields

  • owl:uniqueIdentity → Unique identifier fields

  • rdfs:range → Relationship destination label

  • rdfs:comment → Class docstring

Usage:

from cvcdocdb.rdf_schema import download_ontology, rdf_to_yaml

# Download and convert in one step:
yaml_str = download_ontology_and_convert(
    "https://raw.githubusercontent.com/ICA-EGAD/RiC-O/master/ontology/current-version/RiC-O_1-1.rdf",
    "rico",
    output_dir="cvcdocdb/"
)

# Or download first, then convert:
path = download_ontology(url, output_dir="ontologies/")
yaml_str = rdf_to_yaml(path, "my_db")
Supported input formats:

Turtle (.ttl), RDF/XML (.rdf), N-Triples (.nt), N-Quads (.nq), TriX (.trix), Trig (.trig), JSON-LD (.jsonld).

cvcdocdb.rdf_schema.download_ontology(url, output_dir='ontologies', filename=None)[source]

Download an RDF ontology from a URL and save it locally.

Parameters:
  • url (str) – The URL of the RDF file (RDF/XML, Turtle, etc.).

  • output_dir (str) – Directory where the file will be saved.

  • filename (str | None) – Output filename (default: derived from URL path).

Returns:

The absolute path to the downloaded file.

Return type:

str

cvcdocdb.rdf_schema.download_ontology_and_convert(url, db_name, output_dir='cvcdocdb', filename=None, ontology_ns=None)[source]

Download an RDF ontology, convert to YAML, and generate Python classes.

Convenience function that downloads the ontology, converts it to DRM YAML schema, and writes the Python entity classes to a file.

Parameters:
  • url (str) – The URL of the RDF file.

  • db_name (str) – Database name for the YAML header.

  • output_dir (str) – Directory where the Python file will be saved.

  • filename (str | None) – Output Python filename (default: entities_{db_name}.py).

  • ontology_ns (str | None) – The ontology namespace URI (e.g. "https://www.ica.org/standards/RiC/ontology#"). If not provided, the namespace is auto-detected from the ontology.

Returns:

The absolute path to the generated Python file.

Return type:

str

cvcdocdb.rdf_schema.rdf_to_yaml(source, db_name, ontology_ns=None)[source]

Convert an RDF/OWL ontology to a DRM YAML schema.

Parameters:
  • source (str) – Path to the RDF file or a URL.

  • db_name (str) – Human-readable database name for the YAML header.

  • ontology_ns (str | None) – The ontology namespace URI. If not provided, the most common ontology namespace in the file is auto-detected.

Returns:

A YAML string matching the format of GraphStore.schema_yaml(db_name).

Return type:

str