pynmms.rdf.backends¶
pynmms.rdf.backends
¶
Graph backends: where the stored graph G and its closure live.
A backend answers membership and pattern queries against the asserted
graph and against its closure under the backend's regime (the same graph
when no regime is configured). Proof search mostly asks for membership of
a handful of triples per node and hands conjunctions to :meth:join; a
backend is iterated only when a whole side must be walked (a
:class:~pynmms.rdf.view.GraphView read as a set, a procedural rule
scanning its trigger predicates during materialisation).
GraphBackend
¶
Bases: Protocol
What a store must provide to serve as the base of a :class:GraphView.
Source code in src/pynmms/rdf/backends/__init__.py
generation
property
¶
Bumped whenever the asserted graph or the closure changes.
regime
property
¶
The entailment regime whose closure this backend maintains, if any.
resolver
property
¶
Prefix resolver for parsing and display.
closure_contains(t)
¶
closure_triples(pattern)
¶
contains(t)
¶
is_inconsistent()
¶
join(patterns, bindings)
¶
All extensions of bindings making every pattern a closure triple.
Patterns contain :class:~pynmms.rdf.rules.Var terms. One round trip
on a remote store; the in-memory backend joins locally.
Source code in src/pynmms/rdf/backends/__init__.py
size()
¶
MemoryBackend
¶
rdflib Graph as the store, with the regime closure kept in a second graph.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
graph
|
Graph | None
|
Existing graph to wrap (copied by reference, not duplicated). |
None
|
regime
|
Regime | None
|
Entailment regime to materialise; |
None
|
skolemize
|
bool
|
Replace blank nodes by Skolem IRIs on load (sound in the
antecedent, |
True
|
Source code in src/pynmms/rdf/backends/memory.py
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 | |
closure
property
¶
cl_R(G) (the asserted graph itself when there is no regime).
add(triples, *, source=None)
¶
Assert triples and extend the closure incrementally (source is not kept).
Source code in src/pynmms/rdf/backends/memory.py
graphs_of(t)
¶
join(patterns, bindings)
¶
Nested-loop join over the closure graph (no batching to be had locally).
Source code in src/pynmms/rdf/backends/memory.py
load(source, format=None)
¶
Parse a file into the graph, Skolemize if configured, re-materialise.
Returns the number of triples loaded.
Source code in src/pynmms/rdf/backends/memory.py
OxigraphBackend
¶
A :class:~pynmms.rdf.backends.GraphBackend over an embedded Oxigraph store.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str | Path | None
|
Directory of an on-disk store (created if absent); |
None
|
regime
|
Regime | None
|
The entailment regime to materialise. |
None
|
skolemize
|
bool
|
Replace blank nodes by Skolem IRIs on load ( |
True
|
prefixes
|
dict[str, str] | None
|
Prefix bindings for parsing and display. |
None
|
materialize
|
bool
|
Materialise on construction when the store's recorded
regime differs from regime (default). Pass |
True
|
in_memory
|
bool | None
|
For an on-disk store, compute the closure in a temporary
in-memory store and bulk-load the result (fast, but about 600
bytes of memory per closure triple) rather than by rule updates
against the disk store (RocksDB-write-bound, no memory cost).
|
None
|
Source code in src/pynmms/rdf/backends/oxigraph.py
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 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 | |
materialised
property
¶
Does the store hold cl_R(G) for this regime? (Always true without one.)
store
property
¶
The underlying pyoxigraph.Store.
add(triples, *, source=None)
¶
Assert triples and extend the closure incrementally in process.
The new triples are closed by :meth:ClosureEngine.extend with the
store as the join partner, and the result is written to the closure
graph; the store is never re-materialised. With source, they are
also recorded in that named graph.
Source code in src/pynmms/rdf/backends/oxigraph.py
close()
¶
dump(destination, format=None)
¶
Write the asserted graph to a file (format from the extension).
Source code in src/pynmms/rdf/backends/oxigraph.py
graphs_of(t)
¶
The named graphs (sources) holding t, the backend's own graphs excluded.
Source code in src/pynmms/rdf/backends/oxigraph.py
join(patterns, bindings)
¶
One SELECT over the closure for the whole conjunction (an ASK
when every term is bound).
Source code in src/pynmms/rdf/backends/oxigraph.py
load(source, format=None, *, materialize=True)
¶
Load an RDF file into the store, Skolemize, and (re)materialise.
Parsing is Oxigraph's own; format is a MIME type or guessed from
the extension. Turtle @prefix lines are bound in the resolver.
Returns the number of asserted triples added. With
materialize=False the closure is left stale until
:meth:materialize is called.
Source code in src/pynmms/rdf/backends/oxigraph.py
load_graph(graph, *, source=None)
¶
Load an rdflib graph (its namespace bindings included).
With source, the triples are also recorded in that named graph, so
:meth:graphs_of reports them as coming from it (provenance).
Source code in src/pynmms/rdf/backends/oxigraph.py
materialize()
¶
Recompute cl_R(G) in the store from the asserted graph.
An in-memory store runs the rules in place. An on-disk store does so
too when in_memory=False was given, or when it was left to choose
and holds more than :data:IN_MEMORY_LIMIT asserted triples;
otherwise it computes the closure in a temporary in-memory store and
bulk-loads the result, since RocksDB writes made the rule updates
five times slower than in memory, slower than the Python engine at
two million closure triples.
Source code in src/pynmms/rdf/backends/oxigraph.py
SPARQLBackend
¶
A remote (or local server) graph reached over SPARQL.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
endpoint
|
str
|
Query endpoint URL. |
required |
regime
|
Regime | None
|
The regime the endpoint materialises (declared, not enforced). |
None
|
probe
|
tuple[Triple, ...] | None
|
Optional triples that the endpoint must entail under the
declared regime; raises |
None
|
update_endpoint
|
str | None
|
SPARQL UPDATE endpoint for :meth: |
None
|
prefixes
|
dict[str, str] | None
|
|
None
|
Source code in src/pynmms/rdf/backends/sparql.py
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 | |
stats
property
¶
Round trips and cumulative latency, for post-run analysis.
add(triples)
¶
Insert triples through the update endpoint in chunked INSERT DATA updates.
The store is expected to maintain its own materialisation; the generation is bumped once per call so views and caches invalidate.
Source code in src/pynmms/rdf/backends/sparql.py
bump()
¶
Signal that the remote graph changed (invalidates views and caches).
join(patterns, bindings)
¶
One SELECT for the whole conjunction, bound terms inlined.
Source code in src/pynmms/rdf/backends/sparql.py
pynmms.rdf.backends.oxigraph¶
pynmms.rdf.backends.oxigraph
¶
Oxigraph backend: an embedded store that materialises the regime itself.
Oxigraph <https://github.com/oxigraph/oxigraph>_ is a Rust triplestore with
a SPARQL 1.1 engine, usable in process through pyoxigraph either in
memory or on disk (RocksDB). This backend keeps the asserted graph in the
named graph <urn:pynmms:asserted> and cl_R(G) in the default graph,
and computes the closure inside the store: every pattern rule of the regime
runs as a SPARQL INSERT ... WHERE update
(:mod:pynmms.rdf.sparql_rules), iterated to a fixpoint, with the rules a
store cannot run (guarded rules, :class:~pynmms.rdf.rules.ProceduralRule\s)
evaluated in process against the store between rounds. Membership, pattern,
and join queries are then microseconds, and a store on disk keeps its
closure across sessions: reopening a path whose recorded regime matches skips
materialisation.
add() extends the closure semi-naively through
:meth:~pynmms.rdf.closure.ClosureEngine.extend with the store as the join
partner, so a TELL of a few triples costs milliseconds at any size.
Rules whose conclusions are generalized triples with a literal subject
(rdfs1, rdfD1) cannot be stored and are skipped, so literal typing
is absent from the materialised closure; the in-process extras closure still
derives them for a query's own triples.
Requires pip install pyoxigraph (the oxigraph extra).
OxigraphBackend
¶
A :class:~pynmms.rdf.backends.GraphBackend over an embedded Oxigraph store.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str | Path | None
|
Directory of an on-disk store (created if absent); |
None
|
regime
|
Regime | None
|
The entailment regime to materialise. |
None
|
skolemize
|
bool
|
Replace blank nodes by Skolem IRIs on load ( |
True
|
prefixes
|
dict[str, str] | None
|
Prefix bindings for parsing and display. |
None
|
materialize
|
bool
|
Materialise on construction when the store's recorded
regime differs from regime (default). Pass |
True
|
in_memory
|
bool | None
|
For an on-disk store, compute the closure in a temporary
in-memory store and bulk-load the result (fast, but about 600
bytes of memory per closure triple) rather than by rule updates
against the disk store (RocksDB-write-bound, no memory cost).
|
None
|
Source code in src/pynmms/rdf/backends/oxigraph.py
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 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 | |
materialised
property
¶
Does the store hold cl_R(G) for this regime? (Always true without one.)
store
property
¶
The underlying pyoxigraph.Store.
add(triples, *, source=None)
¶
Assert triples and extend the closure incrementally in process.
The new triples are closed by :meth:ClosureEngine.extend with the
store as the join partner, and the result is written to the closure
graph; the store is never re-materialised. With source, they are
also recorded in that named graph.
Source code in src/pynmms/rdf/backends/oxigraph.py
close()
¶
dump(destination, format=None)
¶
Write the asserted graph to a file (format from the extension).
Source code in src/pynmms/rdf/backends/oxigraph.py
graphs_of(t)
¶
The named graphs (sources) holding t, the backend's own graphs excluded.
Source code in src/pynmms/rdf/backends/oxigraph.py
join(patterns, bindings)
¶
One SELECT over the closure for the whole conjunction (an ASK
when every term is bound).
Source code in src/pynmms/rdf/backends/oxigraph.py
load(source, format=None, *, materialize=True)
¶
Load an RDF file into the store, Skolemize, and (re)materialise.
Parsing is Oxigraph's own; format is a MIME type or guessed from
the extension. Turtle @prefix lines are bound in the resolver.
Returns the number of asserted triples added. With
materialize=False the closure is left stale until
:meth:materialize is called.
Source code in src/pynmms/rdf/backends/oxigraph.py
load_graph(graph, *, source=None)
¶
Load an rdflib graph (its namespace bindings included).
With source, the triples are also recorded in that named graph, so
:meth:graphs_of reports them as coming from it (provenance).
Source code in src/pynmms/rdf/backends/oxigraph.py
materialize()
¶
Recompute cl_R(G) in the store from the asserted graph.
An in-memory store runs the rules in place. An on-disk store does so
too when in_memory=False was given, or when it was left to choose
and holds more than :data:IN_MEMORY_LIMIT asserted triples;
otherwise it computes the closure in a temporary in-memory store and
bulk-loads the result, since RocksDB writes made the rule updates
five times slower than in memory, slower than the Python engine at
two million closure triples.
Source code in src/pynmms/rdf/backends/oxigraph.py
from_ox(t, ox)
¶
pyoxigraph term -> rdflib term (plain strings lose their xsd:string).
Source code in src/pynmms/rdf/backends/oxigraph.py
to_ox(n, ox)
¶
rdflib term -> pyoxigraph term.
Source code in src/pynmms/rdf/backends/oxigraph.py
pynmms.rdf.sparql_rules¶
pynmms.rdf.sparql_rules
¶
Translate regime rules into SPARQL so a store can run the closure itself.
A pattern :class:~pynmms.rdf.rules.Rule <A, c> (def:entailmentregime)
with no guard becomes one INSERT { c } WHERE { A FILTER NOT EXISTS { c } }
update; running the whole set to a fixpoint materialises cl_R(G) inside
the store, with the store's own join engine doing the work. A ⊥ rule becomes
ASK { A }: true iff the store's graph is R-inconsistent
(prop:incoherence). Rules with a Python guard and
:class:~pynmms.rdf.rules.ProceduralRule\s are not translatable and are
reported by :func:partition; a backend runs those in process against the
store.
Terms are written in N3: rdflib nodes through Node.n3(), rule variables
as ?name.
JoinQuery
¶
Bases: NamedTuple
A backend's join() as SPARQL: the ordered BGP with bound terms inlined,
the SELECT DISTINCT over it, the free variables in projection order, and
each free variable's name in the query (?v0, ?v1, ...).
Source code in src/pynmms/rdf/sparql_rules.py
StoreRules
dataclass
¶
A regime split into what the store runs and what stays in process.
Source code in src/pynmms/rdf/sparql_rules.py
bgp(premises)
¶
build_select(patterns, bindings)
¶
One SELECT for a conjunction of patterns, bindings inlined.
Variables already bound are written as their values, the others as
?vN in first-seen order over the BGP as :func:order_bgp arranges
it. With no free variable the projection is * (a backend may prefer
an ASK { bgp } then).
Source code in src/pynmms/rdf/sparql_rules.py
order_bgp(patterns, bindings)
¶
Order a basic graph pattern for a store without a statistics planner.
Oxigraph evaluates a BGP left to right, so ?x a ex:C2 . ?x ex:p ex:j7
scans every instance of the class before touching the selective triple
(2.8 s against 0.4 ms on a 10⁷ graph). Most bound terms first, an
rdf:type pattern with an unbound subject last, and among equals a
pattern sharing a variable with an earlier one before one that does not.
Source code in src/pynmms/rdf/sparql_rules.py
partition(regime, *, skip=frozenset())
¶
Split regime into SPARQL updates, SPARQL asks, and in-process rules.
Rule names in skip are dropped altogether (with a log line); a backend uses this for rules whose conclusions it cannot store.
Source code in src/pynmms/rdf/sparql_rules.py
rule_to_ask(rule)
¶
ASK { A } for a false-concluding pattern rule.
Source code in src/pynmms/rdf/sparql_rules.py
rule_to_update(rule, *, into=None)
¶
INSERT { c } WHERE { A FILTER NOT EXISTS { c } } for a pattern rule.
With into an IRI, the conclusions go to that named graph while the
premises and the NOT EXISTS check read the default graph.
Source code in src/pynmms/rdf/sparql_rules.py
term_n3(t)
¶
N3 for a rule term: ?x for a variable, Node.n3() otherwise.
Source code in src/pynmms/rdf/sparql_rules.py
translatable(rule)
¶
Can rule run as one SPARQL update or ASK?
Pattern rules without a guard are translatable. A rule whose conclusion
has a literal in subject position is not: it is a generalized triple
(def:rdftriple) that a SPARQL store cannot hold.