Graph Store Interface¶
OntoEnv can route all graph reads and writes through a caller-provided Python object.
This is useful when you already manage graph storage — an in-memory dict, a database,
or a custom triplestore — and want OntoEnv to slot in without touching the filesystem.
Protocol¶
Pass a graph_store= object to OntoEnv(). It must implement the following methods:
Required
add_graph(iri: str, graph: Graph, overwrite: bool = False) → None— store a graph under the given IRI.get_graph(iri: str) → Graph— retrieve a previously stored graph by IRI.remove_graph(iri: str) → None— delete a graph from the store.graph_ids() → list[str]— return all currently stored IRIs.
Optional
size() → dict[str, int]— return{"num_graphs": …, "num_triples": …}for diagnostic use.
Constraint: graph_store cannot be combined with
recreate=True or create_or_use_cached. Graphs are always
passed as rdflib.Graph instances.
Example¶
A minimal in-memory store and how to register it:
from rdflib import Graph
from ontoenv import OntoEnv
class DictGraphStore:
def __init__(self) -> None:
self.graphs: dict[str, Graph] = {}
def add_graph(self, iri: str, graph: Graph, overwrite: bool = False) -> None:
if not overwrite and iri in self.graphs:
return
self.graphs[iri] = graph
def get_graph(self, iri: str) -> Graph:
return self.graphs[iri]
def remove_graph(self, iri: str) -> None:
del self.graphs[iri]
def graph_ids(self) -> list[str]:
return list(self.graphs.keys())
def size(self) -> dict[str, int]:
return {
"num_graphs": len(self.graphs),
"num_triples": sum(len(g) for g in self.graphs.values()),
}
store = DictGraphStore()
env = OntoEnv(graph_store=store, temporary=True)
env.add("./ontologies/my.ttl")
print(store.graph_ids()) # ['https://example.com/myOntology']