Skip to main content

Python bindings for the OntoEnv Rust library. Manages ontology-based environments for building knowledge graphs.

Project description

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 (rdflib.Graph, 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:
    def add_graph(self, iri: str, graph: Graph, overwrite: bool = False) -> None: ...
    def get_graph(self, iri: str) -> Graph: ...
    def remove_graph(self, iri: str) -> None: ...
    def graph_ids(self) -> list[str]: ...
    def size(self) -> dict[str, int]: ...  # optional, returns {"num_graphs": ..., "num_triples": ...}

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.

Project details


Release history Release notifications | RSS feed

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.0a2.tar.gz (1.2 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.0a2-cp311-abi3-win_amd64.whl (6.4 MB view details)

Uploaded CPython 3.11+Windows x86-64

ontoenv-0.6.0a2-cp311-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (64.8 MB view details)

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

ontoenv-0.6.0a2-cp311-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (63.8 MB view details)

Uploaded CPython 3.11+manylinux: glibc 2.17+ ARM64

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

Uploaded CPython 3.11+macOS 11.0+ ARM64

ontoenv-0.6.0a2-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.0a2-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.0a2.tar.gz.

File metadata

  • Download URL: ontoenv-0.6.0a2.tar.gz
  • Upload date:
  • Size: 1.2 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.0a2.tar.gz
Algorithm Hash digest
SHA256 fd7211f3216b6e22c7a8ee4029b9aab93df0da8573e4ff98ec770b2a8705531d
MD5 ca5e473ab91a7f079af05411c2abad12
BLAKE2b-256 25277a61e29be1d1bd54a5f63da82baee9decf6caac80965b282001405e25660

See more details on using hashes here.

File details

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

File metadata

  • Download URL: ontoenv-0.6.0a2-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.0a2-cp311-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 193378a0f1e076069177492089aac9ce503db6aaf9ed15e18d5fdaa8059f1fe4
MD5 b55a22a148842c4c1e8616358d31d052
BLAKE2b-256 298a8c61fcd8ca1cca2f6f59739fb28c70d162cf4f6172cbe0562ef9ba8d2e9d

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ontoenv-0.6.0a2-cp311-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 26aaa4981a55e613362a5a304014c6c99c91a65a365020ad7a5c453a09246c73
MD5 b600e5acaa426cbb5ceb02782beb1fce
BLAKE2b-256 ac7b42a0d44faaeb229772c1fdc022c2f87476c3a944e73a51f91a390f6d1a47

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ontoenv-0.6.0a2-cp311-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 a2711e5657d6f99cbfef6ac022d1c6fc8563ce005e491410effedf363440b41a
MD5 717e4052714adfa0c935e948427169d6
BLAKE2b-256 0111f90979203c3c702b092ebb94f929aebce45908c2e9f0b85a7dd94368bfd5

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ontoenv-0.6.0a2-cp311-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 98dbdd41bf1932f277565904a64dab0510be2509c9f9f19ed025a63bd69ca3d2
MD5 7ae333b60f41e64e5127d96f6739a29f
BLAKE2b-256 0e96e20beb0815e7e6303f54c4b3b9e9e3b4a4d8c1dd5ab8c4c1bd14fc9a1e24

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ontoenv-0.6.0a2-cp311-abi3-macosx_10_14_x86_64.whl
Algorithm Hash digest
SHA256 0dea3780e5f5e6ee26ec05010af6a7f874afcbf2ea2951a5a3d56fb18c382212
MD5 08549f1c8cc776a24048bc7c74dca53a
BLAKE2b-256 271e0acf7e4f839cd84c0c141369e442dc864dc1e5027f026ce2fef6e008b73c

See more details on using hashes here.

File details

Details for the file ontoenv-0.6.0a2-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.0a2-cp311-abi3-macosx_10_14_x86_64.macosx_11_0_arm64.macosx_10_14_universal2.whl
Algorithm Hash digest
SHA256 85788ab9f298344efc8316c48f6ec0a5309c4bc7b93050cf3192d24e6a392d2a
MD5 7e482a607182c4bc5bab99ba72ebf347
BLAKE2b-256 0b3facf864572ad2b4344d18167c9e64943414b2c1a1188c667bc5df4bf5cd8c

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