ontocast.tool.triple_manager.core¶
Triple store management tools for OntoCast.
This module provides functionality for managing RDF triple stores, including abstract interfaces and concrete implementations for different triple store backends.
TripleStoreManager
¶
Bases: Tool
Base class for managing RDF triple stores.
This class defines the interface for triple store management operations, including fetching and storing ontologies and their graphs. All concrete triple store implementations should inherit from this class.
This is an abstract base class that must be implemented by specific triple store backends (e.g., Fuseki, In-Memory).
Source code in ontocast/tool/triple_manager/core.py
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 | |
__init__(**kwargs)
¶
Initialize the triple store manager.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
**kwargs
|
Additional keyword arguments passed to the parent class. |
{}
|
aconstruct(query, *, store='ontologies')
async
¶
Run a SPARQL CONSTRUCT against the active partition.
Unlike :meth:aselect, the result carries real RDF terms, so blank nodes
and datatypes survive. Prefix bindings do not -- they are serialization
metadata rather than triples, and must be re-sourced by the caller.
Implementations must raise rather than return an empty graph on failure,
for the same reason :meth:aselect must raise: an empty result is
indistinguishable from "nothing matched".
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
query
|
str
|
A SPARQL CONSTRUCT (or DESCRIBE) query. |
required |
store
|
StoreKind
|
Which partition to query -- |
'ontologies'
|
Returns:
| Name | Type | Description |
|---|---|---|
RDFGraph |
RDFGraph
|
The constructed triples, without prefix bindings. |
Raises:
| Type | Description |
|---|---|
NotImplementedError
|
If the backend has no SPARQL engine. |
Source code in ontocast/tool/triple_manager/core.py
afetch_ontologies()
async
¶
afetch_ontologies_by_iri(iris)
async
¶
Fetch terminal ontologies restricted to iris.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
iris
|
Sequence[str]
|
Ontology IRIs to fetch. Empty means "no restriction", matching
how :meth: |
required |
Returns:
| Type | Description |
|---|---|
list[Ontology]
|
list[Ontology]: The requested ontologies, with graphs. |
Source code in ontocast/tool/triple_manager/core.py
afetch_ontology_catalog()
async
¶
Fetch per-named-graph ontology header metadata.
Headers carry the lineage fields terminal-version selection needs without the graphs themselves. The default implementation materializes the catalog and derives headers from it; SPARQL-capable backends should override with a single SELECT.
Note the default returns one header per terminal ontology (whatever
:meth:afetch_ontologies returns), while a native implementation returns
one per stored version. Callers that re-run terminal selection over the
result are correct either way; that is why they should.
Returns:
| Type | Description |
|---|---|
list[OntologyHeader]
|
list[OntologyHeader]: Header metadata for stored ontologies. |
Source code in ontocast/tool/triple_manager/core.py
aselect(query, *, store='ontologies')
async
¶
Run a SPARQL SELECT against the active partition.
Rows map variable name to the term's lexical value only; term kind and
datatype are not preserved, so constrain kinds in the query itself
(FILTER(isIRI(?x))). Unbound variables are absent from the row dict.
Implementations must raise rather than return an empty list on failure -- an empty result set is indistinguishable from "nothing matched", which would silently disable callers that treat no-rows as a valid answer.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
query
|
str
|
A SPARQL SELECT query. |
required |
store
|
StoreKind
|
Which partition to query -- |
'ontologies'
|
Returns:
| Type | Description |
|---|---|
list[dict[str, str]]
|
list[dict[str, str]]: One dict per solution. |
Raises:
| Type | Description |
|---|---|
NotImplementedError
|
If the backend has no SPARQL engine. |
Source code in ontocast/tool/triple_manager/core.py
aserialize(o, **kwargs)
async
¶
Async serialize helper for backends without native async I/O.
aserialize_graph(graph, **kwargs)
async
¶
Async serialize helper for backends without native async I/O.
async_init()
async
¶
clean(*, include_shapes=False)
abstractmethod
async
¶
Clean/flush data managed by this store (backend-specific scope).
The shapes partition is retained by default. Facts and ontologies are
reproducible from a rerun; shapes are the deployment's validation
contract, and dropping them turns the SHACL gate off silently -- a
cleared run then reports shacl_evaluated: null rather than failing.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
include_shapes
|
bool
|
Also drop the shapes partition. Opt in explicitly. |
False
|
Raises:
| Type | Description |
|---|---|
NotImplementedError
|
If the triple store doesn't support cleaning. |
Source code in ontocast/tool/triple_manager/core.py
clean_tenancy(tenant, project, *, include_shapes=False)
async
¶
Remove all triples for datasets derived from tenant / project.
Shapes are retained unless include_shapes is set -- see :meth:clean.
Backends without per-tenant partitions raise :class:NotImplementedError.
Source code in ontocast/tool/triple_manager/core.py
close()
async
¶
Release any connection held by this backend.
Default is a no-op for in-process backends.
drop_all_ontology_graphs_for_iri(ontology_iri, *, store='ontologies')
async
¶
Remove named graphs for ontology_iri (base and versioned).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ontology_iri
|
str
|
Base IRI whose |
required |
store
|
StoreKind
|
Partition to drop from. Shapes documents are addressed the same
way and live in |
'ontologies'
|
Source code in ontocast/tool/triple_manager/core.py
drop_named_graph(graph_uri, *, store='ontologies')
async
¶
Drop a single named graph.
fetch_ontologies()
abstractmethod
¶
Fetch all available ontologies from the triple store.
This method should retrieve all ontologies stored in the triple store and return them as Ontology objects with their associated RDF graphs.
Returns:
| Type | Description |
|---|---|
list[Ontology]
|
list[Ontology]: List of available ontologies with their graphs. |
Source code in ontocast/tool/triple_manager/core.py
last_catalog_was_complete()
¶
True when the most recent full catalog fetch returned every graph.
Consulted before destructive reconciliation (vector-store orphan pruning): a backend that fetched only part of its catalog reports False so callers treat the result as non-authoritative rather than concluding that the missing ontologies were deleted. Backends that cannot fetch partially always report True.
Source code in ontocast/tool/triple_manager/core.py
serialize(o, **kwargs)
abstractmethod
¶
serialize_graph(graph, **kwargs)
abstractmethod
¶
strip_provenance(graph)
classmethod
¶
Return a graph without reification/provenance scaffolding triples.
Source code in ontocast/tool/triple_manager/core.py
supports_sparql_construct()
¶
True when :meth:aconstruct reaches a real SPARQL engine.
Separate from :meth:supports_sparql_select because a backend can answer
row queries without being able to return triples: the Fuseki SELECT path
speaks application/sparql-results+json only.
Source code in ontocast/tool/triple_manager/core.py
supports_sparql_select()
¶
True when :meth:aselect reaches a real SPARQL engine.
Callers branch on this to choose targeted queries over materializing the
whole catalog. Backends returning False still answer every catalog
method correctly, just by fetching more than they need.
Source code in ontocast/tool/triple_manager/core.py
supports_tenancy_partition()
¶
update_tenancy(tenant, project, *, sep=TENANCY_SEP)
async
¶
Switch the active tenant/project partition when supported.
Source code in ontocast/tool/triple_manager/core.py
TripleStoreManagerWithAuth
¶
Bases: TripleStoreManager
Base class for triple store managers that require authentication.
This class provides common functionality for triple store managers that need URI and authentication credentials. It handles environment variable loading and credential parsing.
Attributes:
| Name | Type | Description |
|---|---|---|
uri |
str | None
|
The connection URI for the triple store. |
auth |
tuple | None
|
Authentication tuple (username, password) for the triple store. |
Source code in ontocast/tool/triple_manager/core.py
__init__(uri=None, auth=None, env_uri=None, env_auth=None, **kwargs)
¶
Initialize the triple store manager with authentication.
This method handles loading URI and authentication credentials from either direct parameters or environment variables. It also parses authentication strings in the format "user/password".
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
uri
|
Direct URI for the triple store connection. |
None
|
|
auth
|
Direct authentication tuple or string in "user/password" format. |
None
|
|
env_uri
|
Environment variable name for the URI (e.g., "NEO4J_URI"). |
None
|
|
env_auth
|
Environment variable name for authentication (e.g., "NEO4J_AUTH"). |
None
|
|
**kwargs
|
Additional keyword arguments passed to the parent class. |
{}
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If the authentication string is neither "user/password" nor "user:password". |
Example
manager = TripleStoreManagerWithAuth( ... env_uri="NEO4J_URI", ... env_auth="NEO4J_AUTH" ... )
Source code in ontocast/tool/triple_manager/core.py
TripleStoreUnavailableError
¶
Bases: RuntimeError
The triple store could not answer a read that must not degrade silently.
Raised instead of returning an empty result when the difference between "the store is unreachable" and "the store is empty" is load-bearing -- catalog listing being the case that matters, since an empty catalog is grounds for pruning the vector index.