Skip to main content
Pre-release

This release is a pre-release and may not be stable for production use.

OntoEnv Python Bindings

Installation

pip install ontoenv

Usage

from ontoenv import OntoEnv
from rdflib import Graph

# creates a new environment in the current directory, or loads
# an existing one. To use a different directory, pass the 'path'
# argument: OntoEnv(path="/path/to/env")
# OntoEnv() will discover ontologies in the current directory and
# its subdirectories
env = OntoEnv()

# add an ontology from a file path.
# env.add returns the name of the ontology, which is its URI
# e.g. "https://brickschema.org/schema/1.4-rc1/Brick"
brick_name = env.add("../brick/Brick.ttl")
print(f"Added ontology {brick_name}")

# When you add from a URL whose declared ontology name differs (for example a
# versioned IRI served at a versionless URL), ontoenv records that alias. You
# can later refer to the ontology by either the canonical name or the original
# URL when resolving imports or querying.

# get the graph of the ontology we just added
# env.get_graph returns a read-only store-backed rdflib.Graph
brick_graph = env.get_graph(brick_name)
print(f"Brick graph has {len(brick_graph)} triples")

# if you need a mutable in-memory graph, copy it explicitly
mutable_brick_graph = env.copy_graph(brick_name)

# get a read-only view of the full closure of the ontology, including all of its imports
# returns a tuple (ViewGraph, list[str])
brick_closure_graph, _ = env.get_closure(brick_name)
print(f"Brick closure has {len(brick_closure_graph)} triples")

# if you need a mutable materialized closure, copy it explicitly
mutable_brick_closure_graph, _ = env.copy_closure(brick_name)

# you can also add ontologies from a URL
rec_name = env.add("https://w3id.org/rec/rec.ttl")
rec_graph = env.get_graph(rec_name)
print(f"REC graph has {len(rec_graph)} triples")

# you can add an in-memory rdflib.Graph directly
in_memory = Graph()
in_memory.parse(data="""
@prefix owl: <http://www.w3.org/2002/07/owl#> .
<http://example.com/in-memory> a owl:Ontology .
""", format="turtle")
in_memory_name = env.add(in_memory)
print(f"Added in-memory ontology {in_memory_name}")

# if you have an rdflib.Graph with an owl:Ontology declaration,
# you can transitively import its dependencies into the graph
g = Graph()
# this graph just has one triple: the ontology declaration for Brick
g.parse(data="""
@prefix owl: <http://www.w3.org/2002/07/owl#> .
<https://brickschema.org/schema/1.4-rc1/Brick> a owl:Ontology .
""")
# this will load all of the owl:imports of the Brick ontology into 'g'
env.import_dependencies(g)
print(f"Graph with imported dependencies has {len(g)} triples")

Namespace prefixes

OntoEnv can extract namespace prefix mappings from ontology source files. Prefixes come from both parser-level declarations (@prefix in Turtle, PREFIX in SPARQL-style syntaxes) and SHACL sh:declare entries.

# Get all namespaces across the entire environment
all_ns = env.get_namespaces()
# {'owl': 'http://www.w3.org/2002/07/owl#', 'brick': 'https://brickschema.org/schema/Brick#', ...}

# Get namespaces for a single ontology
ns = env.get_namespaces("https://brickschema.org/schema/1.4-rc1/Brick")

# Include namespaces from transitive owl:imports
ns_with_imports = env.get_namespaces("https://brickschema.org/schema/1.4-rc1/Brick", include_closure=True)

From the CLI:

ontoenv namespaces                                     # all namespaces
ontoenv namespaces https://example.org/my-ontology     # single ontology
ontoenv namespaces https://example.org/my-ontology --closure   # with imports
ontoenv namespaces --json                              # JSON output

Custom graph store

If you want OntoEnv to write graphs into an existing Python-backed store, pass a graph_store object that implements a small protocol:

class GraphStore:
    # Required
    def add_graph(self, iri: str, graph: Graph, overwrite: bool = False) -> None: ...
    def get_graph(self, iri: str) -> Graph: ...   # used for read-only views (get_*)
    def remove_graph(self, iri: str) -> None: ...
    def graph_ids(self) -> list[str]: ...

    # Optional
    def copy_graph(self, iri: str) -> Graph: ...  # used for mutable copies (copy_*)
                                                  # falls back to get_graph when absent
    def size(self) -> dict[str, int]: ...         # returns {"num_graphs": ..., "num_triples": ...}

copy_graph lets stores distinguish between returning a live view (get_graph) and a detached mutable copy (copy_graph). All copy_* methods (copy_graph, copy_closure, copy_union, copy_dataset) dispatch to copy_graph when it is present, and fall back to get_graph otherwise. The get_* methods always use get_graph.

Example:

store = DictGraphStore()
env = OntoEnv(graph_store=store, temporary=True)

graph_store is currently incompatible with recreate and create_or_use_cached.

RDFLib store with Rust SPARQL

If you want to use ontoenv as an rdflib store directly, use OntoEnvStore. This gives you normal rdflib.Graph and rdflib.Dataset objects, but executes SPARQL through the Rust backend instead of rdflib's Python query engine.

Use env.get_dataset() to get a read-only rdflib.Dataset view of the env. It uses the zero-copy rdf5d snapshot when a persistent .ontoenv/store.r5tu exists and otherwise falls back to an in-memory view. Use env.copy_dataset() when you need a mutable in-memory dataset.

from rdflib import URIRef
from ontoenv import OntoEnv

env = OntoEnv(path=".demo-env", recreate=True, offline=True, search_directories=["./brick"])
brick_name = env.add("./brick/Brick.ttl")
env.update()
env.flush()

dataset = env.get_dataset()

for row in dataset.query(
    """
    SELECT ?entity ?label
    WHERE {
      GRAPH <https://brickschema.org/schema/1.4/Brick> {
        ?entity <http://www.w3.org/2000/01/rdf-schema#label> ?label .
      }
    }
    LIMIT 5
    """
):
    print(row.entity, row.label)

brick_graph = dataset.graph(URIRef(brick_name))
print(len(brick_graph))
env.close()

Importing ontoenv also registers the rdflib plugin name "ontoenv", so this works too:

from rdflib import Graph
import ontoenv

graph = Graph(store="ontoenv")

See demo_rdflib_store.py for a complete runnable example.

CLI Entrypoint

Installing ontoenv also provides the Rust-backed ontoenv command-line tool:

pip install ontoenv
ontoenv --help

The CLI is identical to the standalone ontoenv-cli binary; see the top-level README for usage.

Download files

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

Source Distribution

ontoenv-0.6.0a6.tar.gz (1.3 MB view details)

Uploaded Source

Built Distributions

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

ontoenv-0.6.0a6-cp311-abi3-win_amd64.whl (6.4 MB view details)

Uploaded CPython 3.11+Windows x86-64

ontoenv-0.6.0a6-cp311-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (65.2 MB view details)

Uploaded CPython 3.11+manylinux: glibc 2.17+ x86-64

ontoenv-0.6.0a6-cp311-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (64.2 MB view details)

Uploaded CPython 3.11+manylinux: glibc 2.17+ ARM64

ontoenv-0.6.0a6-cp311-abi3-macosx_11_0_arm64.whl (7.2 MB view details)

Uploaded CPython 3.11+macOS 11.0+ ARM64

ontoenv-0.6.0a6-cp311-abi3-macosx_10_14_x86_64.whl (7.5 MB view details)

Uploaded CPython 3.11+macOS 10.14+ x86-64

ontoenv-0.6.0a6-cp311-abi3-macosx_10_14_x86_64.macosx_11_0_arm64.macosx_10_14_universal2.whl (14.6 MB view details)

Uploaded CPython 3.11+macOS 10.14+ universal2 (ARM64, x86-64)macOS 10.14+ x86-64macOS 11.0+ ARM64

File details

Details for the file ontoenv-0.6.0a6.tar.gz.

File metadata

  • Download URL: ontoenv-0.6.0a6.tar.gz
  • Upload date:
  • Size: 1.3 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for ontoenv-0.6.0a6.tar.gz
Algorithm Hash digest
SHA256 b11b49641e42b4e2f8c04740f4b312a042b1e6a931708acac14f4ced01fa5507
MD5 18b4da03e5268d0ad1501383b212fd73
BLAKE2b-256 a17995d81e8c387fe016a08fc71017e4c7921b65d8752e515fea74fe68c7aef4

See more details on using hashes here.

File details

Details for the file ontoenv-0.6.0a6-cp311-abi3-win_amd64.whl.

File metadata

  • Download URL: ontoenv-0.6.0a6-cp311-abi3-win_amd64.whl
  • Upload date:
  • Size: 6.4 MB
  • Tags: CPython 3.11+, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for ontoenv-0.6.0a6-cp311-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 e3da6e1c3c2386c494698c4104953131344d328bc4a085f46cea3e4e4c7a7b0c
MD5 00ef52f943911e4bf34478be8b0afc88
BLAKE2b-256 b59cf32d3cff9572425715699e10d41881e723f1f821059c6fef4b6dc58d4aa4

See more details on using hashes here.

File details

Details for the file ontoenv-0.6.0a6-cp311-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for ontoenv-0.6.0a6-cp311-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 80d7c3ceb797447b58e65ac151478754df79cb47afdff9d4699a9384a88bb932
MD5 c89ca6f7f302bc068a093b608beb85e2
BLAKE2b-256 6663d1e0ac3dda409ce989a4d86aedc5e85780d414da34766f52ac4c99d74a53

See more details on using hashes here.

File details

Details for the file ontoenv-0.6.0a6-cp311-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl.

File metadata

File hashes

Hashes for ontoenv-0.6.0a6-cp311-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 b3d8200a16ff4447e13302f7e7a01204df785cff79337652f42b8b0ee8f15f83
MD5 4634154f022a5e4a9806a7416435a8e1
BLAKE2b-256 cbd990ecef73c332c45c5851abe23e4d72d453ea07aaed9359514a39ab59b37e

See more details on using hashes here.

File details

Details for the file ontoenv-0.6.0a6-cp311-abi3-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for ontoenv-0.6.0a6-cp311-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 8286a16b672bd2c3055dfda7d4b5c7788839a4b8e56911caed6e650864b72d09
MD5 b6073972e559150cd7e5b666ccead7f8
BLAKE2b-256 106e5a04820cd03939cc60772c0d1fbe845707ee5ee40eea6eaadd210d7de621

See more details on using hashes here.

File details

Details for the file ontoenv-0.6.0a6-cp311-abi3-macosx_10_14_x86_64.whl.

File metadata

File hashes

Hashes for ontoenv-0.6.0a6-cp311-abi3-macosx_10_14_x86_64.whl
Algorithm Hash digest
SHA256 8df7e9115c9ed90497cd4b67531e7b1036819213a689d9f1219112c1656a376c
MD5 acb86721aaa957bba3276f6c058913ca
BLAKE2b-256 f26a6dd4458b0aba73f0ad276af72833df2b395bdc5a304e97f470bf319525d2

See more details on using hashes here.

File details

Details for the file ontoenv-0.6.0a6-cp311-abi3-macosx_10_14_x86_64.macosx_11_0_arm64.macosx_10_14_universal2.whl.

File metadata

File hashes

Hashes for ontoenv-0.6.0a6-cp311-abi3-macosx_10_14_x86_64.macosx_11_0_arm64.macosx_10_14_universal2.whl
Algorithm Hash digest
SHA256 e066711f3cdbb6b6c7b4dc481d103dc0effbe73b827c06cf4d107792a0fa0dca
MD5 1073130b0932706e5a80c6d53f932def
BLAKE2b-256 f8d00d50f180fb937eeb93c3491d6f713a98197e8fb39683cfdffac62e68f39a

See more details on using hashes here.

Release history Release notifications | RSS feed

0.6.2

7 files

0.6.1

7 files

0.6.0

7 files

This release

0.6.0a6 This release

7 files

0.5.5

7 files

0.5.4

7 files

0.5.3

7 files

0.5.2

7 files

0.5.1

7 files

0.5.0

5 files

0.4.0

11 files

0.3.9

2 files

0.3.8

2 files

0.3.7

2 files

0.3.6

2 files

0.3.5

2 files

0.3.4

2 files

0.3.3

2 files

0.3.2

2 files

0.3.1

2 files

0.3.0

2 files

0.2.2

2 files

0.2.0

2 files

0.1.6

2 files

0.1.5

2 files

0.1.4

2 files

0.1.3

2 files

0.1.1

2 files

0.1.0

3 files

Supported by

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