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+

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.0.tar.gz (136.5 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.0-py3-none-any.whl (179.6 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: rdf_construct-0.2.0.tar.gz
  • Upload date:
  • Size: 136.5 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.0.tar.gz
Algorithm Hash digest
SHA256 dacccc4e4fbbd0e61e197b53076beada6071d101ace9f63e2210dec0e476089d
MD5 d48648d93465385c454b501863df1054
BLAKE2b-256 f9615c58f7a595745fe5b62394b40f60f9813524fe52c85d81992078564058ad

See more details on using hashes here.

File details

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

File metadata

  • Download URL: rdf_construct-0.2.0-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.0-py3-none-any.whl
Algorithm Hash digest
SHA256 48068a105bff28b59d2b9d42d9951272616ad85386461940b7fbb684fbb7a59d
MD5 f9232b4e6fb6bd59d803e3f89881b64f
BLAKE2b-256 74bd24bb3c8f8a6d7c130a92074f408a6ec3d04370f03aa06688b05e5b0f4b2b

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