Skip to main content

Semantic RDF manipulation toolkit - order, serialize, and diff RDF ontologies

Project description

rdf-construct

"The ROM construct itself is a hardwired ROM cassette replicating a dead man's skills..." — William Gibson, Neuromancer

Semantic RDF manipulation toolkit for ordering, documenting, validating, comparing, and visualising RDF ontologies.

License: MIT Python 3.10+ PyPI version Downloads

Features

  • Semantic Ordering: Serialise RDF/Turtle with intelligent ordering instead of alphabetical chaos
  • Documentation Generation: Create navigable HTML, Markdown, or JSON documentation from ontologies
  • UML Generation: Create PlantUML class diagrams from RDF ontologies
  • PUML2RDF: Convert PlantUML diagrams to RDF/OWL ontologies (diagram-first design)
  • SHACL Generation: Generate SHACL validation shapes from OWL definitions
  • Semantic Diff: Compare ontology versions and identify meaningful changes
  • Ontology Linting: Check quality with 11 configurable rules
  • Competency Question Testing: Validate ontologies against SPARQL-based tests
  • Ontology Statistics: Comprehensive metrics with comparison mode
  • Flexible Styling: Configure colours, layouts, and visual themes for diagrams
  • Profile-Based: Define multiple strategies in YAML configuration
  • Deterministic: Same input + profile = same output, always

Why?

RDFlib's built-in serialisers always sort alphabetically, which:

  • Obscures semantic structure
  • Makes diffs noisy (unrelated changes mixed together)
  • Loses author's intentional organisation
  • Makes large ontologies hard to navigate

rdf-construct preserves semantic meaning in serialisation, making RDF files more maintainable.

Quick Start

Installation

# From PyPI (recommended)
pip install rdf-construct

# From source
git clone https://github.com/aigora-de/rdf-construct.git
cd rdf-construct
pip install -e .

# For development
poetry install

Compare Ontology Versions

# Basic comparison
rdf-construct diff v1.0.ttl v1.1.ttl

# Generate markdown changelog
rdf-construct diff v1.0.ttl v1.1.ttl --format markdown -o CHANGELOG.md

# JSON output for CI/scripting
rdf-construct diff old.ttl new.ttl --format json

Generate Documentation

# HTML documentation with search
rdf-construct docs ontology.ttl -o api-docs/

# Markdown for GitHub wiki
rdf-construct docs ontology.ttl --format markdown

# JSON for custom rendering
rdf-construct docs ontology.ttl --format json

Generate UML Diagrams

# Generate diagrams from an example ontology
rdf-construct uml examples/animal_ontology.ttl -C examples/uml_contexts.yml

# With styling and layout
rdf-construct uml examples/animal_ontology.ttl -C examples/uml_contexts.yml \
  --style-config examples/uml_styles.yml --style default \
  --layout-config examples/uml_layouts.yml --layout hierarchy

Reorder RDF Files

# Order an ontology using all profiles
rdf-construct order ontology.ttl order_config.yml

# Generate specific profiles only
rdf-construct order ontology.ttl order_config.yml -p alpha -p logical_topo

Check Ontology Quality

# Run all lint rules
rdf-construct lint ontology.ttl

# Strict checking for CI
rdf-construct lint ontology.ttl --level strict --format json

Run Competency Question Tests

# Run tests
rdf-construct cq-test ontology.ttl tests.yml

# JUnit output for CI
rdf-construct cq-test ontology.ttl tests.yml --format junit -o results.xml

Generate SHACL Shapes

# Basic generation
rdf-construct shacl-gen ontology.ttl -o shapes.ttl

# Strict mode with closed shapes
rdf-construct shacl-gen ontology.ttl --level strict --closed

Ontology Statistics

# Display statistics
rdf-construct stats ontology.ttl

# Compare two versions
rdf-construct stats v1.ttl v2.ttl --compare --format markdown

Documentation

📚 Complete Documentation - Start here

For Users:

For Developers:

Additional:

Example

Input (Alphabetically Ordered - Hard to Read)

ex:Bird rdfs:subClassOf ex:Animal .
ex:Cat rdfs:subClassOf ex:Mammal .
ex:Dog rdfs:subClassOf ex:Mammal .
ex:Eagle rdfs:subClassOf ex:Bird .
ex:Mammal rdfs:subClassOf ex:Animal .
ex:Sparrow rdfs:subClassOf ex:Bird .

Output (Semantically Ordered - Easy to Understand)

# Root class first
ex:Animal a owl:Class .

# Then its direct subclasses
ex:Mammal rdfs:subClassOf ex:Animal .
ex:Bird rdfs:subClassOf ex:Animal .

# Then their subclasses
ex:Dog rdfs:subClassOf ex:Mammal .
ex:Cat rdfs:subClassOf ex:Mammal .

ex:Eagle rdfs:subClassOf ex:Bird .
ex:Sparrow rdfs:subClassOf ex:Bird .

Semantic Diff

Compare ontology versions and see what actually changed:

$ rdf-construct diff v1.0.ttl v1.1.ttl

Comparing v1.0.ttl  v1.1.ttl

ADDED (2 entities):
  + Class ex:SmartBuilding (subclass of ex:Building)
  + DataProperty ex:energyRating

REMOVED (1 entity):
  - Class ex:DeprecatedStructure

MODIFIED (1 entity):
  ~ Class ex:Building
    + rdfs:comment "A constructed physical structure."@en

Summary: 2 added, 1 removed, 1 modified

Unlike text-based diff, semantic diff ignores:

  • Statement reordering
  • Prefix rebinding (ex:example:)
  • Whitespace and formatting changes

Complete Toolkit

Semantic Ordering

Topological Sort: Parents before children using Kahn's algorithm

profiles:
  logical_topo:
    sections:
      - classes:
          sort: topological
          roots: ["ies:Element"]

Root-Based Ordering: Organise by explicit hierarchy

sections:
  - classes:
      sort: topological
      roots:
        - ex:Mammal
        - ex:Bird

Documentation Generation

Generate professional documentation in multiple formats:

# HTML with search, navigation, and cross-references
rdf-construct docs ontology.ttl -o docs/

# Markdown for GitHub/GitLab wikis
rdf-construct docs ontology.ttl --format markdown

# JSON for custom rendering
rdf-construct docs ontology.ttl --format json

UML Context System

Root Classes Strategy: Start from specific concepts

animal_taxonomy:
  root_classes: [ex:Animal]
  include_descendants: true

Focus Classes Strategy: Hand-pick classes

key_concepts:
  focus_classes: [ex:Dog, ex:Cat, ex:Eagle]

Property Filtering: Control what relationships show

properties:
  mode: domain_based  # or connected, explicit, all, none

Quality Checking

11 built-in lint rules across three categories:

Category Rules
Structural orphan-class, dangling-reference, circular-subclass, property-no-type, empty-ontology
Documentation missing-label, missing-comment
Best Practice redundant-subclass, property-no-domain, property-no-range, inconsistent-naming

Styling and Layout

Visual Themes:

  • default - Professional blue scheme
  • high_contrast - Bold colours for presentations
  • grayscale - Black and white for academic papers
  • minimal - Bare-bones for debugging

Layout Control:

  • Direction (top-to-bottom, left-to-right)
  • Arrow hints for hierarchy
  • Spacing and grouping

Project Status

Current: v0.2.0 - Feature complete for core ontology workflows
License: MIT

Implemented

✅ RDF semantic ordering
✅ Topological sorting with root-based branches
✅ Custom Turtle serialisation (preserves order)
✅ PlantUML diagram generation from RDF
✅ PlantUML to RDF conversion
✅ Configurable styling and layouts
✅ Semantic diff (compare ontology versions)
✅ Documentation generation (HTML, Markdown, JSON)
✅ SHACL shape generation
✅ Ontology linting (11 rules)
✅ Competency question testing
✅ Ontology statistics
✅ Comprehensive documentation

(Possible) Roadmap

  • Multi-format input support (JSON-LD, RDF/XML)
  • Streaming mode for very large graphs
  • Web UI for diagram configuration
  • Additional lint rules

Contributing

Contributions welcome! See CONTRIBUTING.md for guidelines.

# Setup development environment
git clone https://github.com/aigora-de/rdf-construct.git
cd rdf-construct
poetry install
pre-commit install

# Run tests
pytest

# Format and lint
black src/ tests/
ruff check src/ tests/

Dependencies

Runtime:

  • Python 3.10+
  • rdflib >= 7.0.0
  • click >= 8.1.0
  • pyyaml >= 6.0
  • rich >= 13.0.0
  • jinja2 >= 3.1.0

Development:

  • black, ruff, mypy
  • pytest, pytest-cov
  • pre-commit

Inspiration

Named after the ROM construct from William Gibson's Neuromancer—preserved, structured knowledge that can be queried and transformed.

The project aims to preserve the semantic structure of RDF ontologies in serialised form, making them as readable and maintainable as the author intended.

Credits

Built on the excellent rdflib library.

Influenced by the need for better RDF tooling in ontology engineering and the IES (Information Exchange Standard) work.

License

MIT License - see LICENSE file for details.

Links


Status: v0.2.0
Python: 3.10+ required
Maintainer: See CONTRIBUTING.md

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

rdf_construct-0.2.1.tar.gz (136.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

rdf_construct-0.2.1-py3-none-any.whl (179.6 kB view details)

Uploaded Python 3

File details

Details for the file rdf_construct-0.2.1.tar.gz.

File metadata

  • Download URL: rdf_construct-0.2.1.tar.gz
  • Upload date:
  • Size: 136.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.2.1 CPython/3.12.0 Darwin/24.5.0

File hashes

Hashes for rdf_construct-0.2.1.tar.gz
Algorithm Hash digest
SHA256 abbccdd7c47b83f3101d6f08c11d38b0097da1703ff9afaa651656a563a87fa3
MD5 cdbb4be4bc19851e57b73366bff85cd3
BLAKE2b-256 494c6a07edcc2ad8d52f1dde06da357f880dd280475eae9c143e2631f6bdc691

See more details on using hashes here.

File details

Details for the file rdf_construct-0.2.1-py3-none-any.whl.

File metadata

  • Download URL: rdf_construct-0.2.1-py3-none-any.whl
  • Upload date:
  • Size: 179.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.2.1 CPython/3.12.0 Darwin/24.5.0

File hashes

Hashes for rdf_construct-0.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 6cf620623d7328c58261f937c16016b2a29ef3f87eb49db7e6c05b5e9a59c27e
MD5 aad8c1ef98e2f71b82711f3686d6d37b
BLAKE2b-256 047f7d15511b9eefa367911db87a8961e5f4fcf6c8231cedf6d6300cdf85ab9d

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page