Expand description
rete-graph — the Rust library for Rete
graph files, under the name it already has on PyPI and npm.
This crate is a facade: every item is re-exported from
rete_core, which is where the engine actually
lives and where the implementation documentation belongs. Depending on either
gets you the same code; this one exists so that
pip install rete-graph # Python
npm install rete-graph # JavaScript
cargo add rete-graph # Rustall name the same thing. Before it existed, a Rust user following any of the project’s other install instructions had to know that the crate was called something else.
§What this is NOT
It does not pull in the whole workspace, because the three published crates are not interchangeable:
rete-coreis the library — what a Rust program depends on, and what this crate re-exports.rete-cliis a binary. You install it (cargo install rete-cli); depending on it from a library would drag an executable into your build for nothing.rete-wasmis the browser binding, meaningful only onwasm32targets.
PyPI’s and npm’s rete-graph are likewise the library for their language,
not the CLI, so re-exporting the library is the faithful mapping.
§Features
compression (default), parallel and wasm-js are forwarded verbatim to
rete-core, so this crate can be configured exactly like it.
§Example
use rete_graph::Rete;
let bytes = std::fs::read("graph.rete")?;
let graph = Rete::open(&bytes)?;
println!("{} quads", graph.dump(None).len());Modules§
- format
- Stable file-format, reader, and in-memory build API.
- geo3
- A 3D extension of the GeoSPARQL built-ins (
geo3). - ingest
- Ingestion: parse RDF text (N-Triples / N-Quads / Turtle) and assemble a
complete
.retefile image. Shared by the CLI’sbuild/validatecommands and the wasm bindings (the playground’s in-browser builder). - query
- Stable SPARQL, graph-pattern, federation, and result API.
- range
- Stable byte-range and lazy-open API for local or remote
.retefiles. - reasoning
- Stable RDFS/OWL reasoning and schema-coherence API.
- validation
- Stable integrity and SHACL validation API.
Structs§
- Block
Cache Reader - Byte
Range - A byte range in the
.retefile image. - CharSet
- A characteristic set — one entity “shape”: the sorted set of predicates a subject carries, plus how many subjects share exactly that shape. Computed at build (top-N shapes by subject count), range-fetched with the pyramid. Useful for data profiling (the distinct entity shapes) and for estimating the selectivity of a subject star (subjects matching ALL of several predicates).
- Class
Node - One node of the shipped
subClassOfDAG: a class, all its direct parents, and itsdepth(0 = root). The hierarchy is non-exclusive — a class may have several parents (multiple inheritance), so this is a directed acyclic graph, not a tree. The first parent (parents are sorted) is the canonical one used for the depth/rollup spanning tree; the rest preserve the cross-links. - Class
Relation - A class-to-class relation (the lateral, non-
is-aconnection): subjects ofs_classrelated bypredicateto objects ofo_class, with the instancecount.(literal)/(untyped)are the object-class sentinels. - Community
Descriptor - A per-community refinement descriptor (Phase 4): what a client sees when it zooms into one community, without fetching that community’s triples. Carries the dominant class, the local type histogram, and optional spatial/temporal extents. (The physical per-community triple tiles are still future work; this descriptor index ships in the index-free pyramid-meta and is ready to attach to those tiles when they exist.)
- Community
Partial - One community’s contribution to a community-split evaluation: how many member subjects it holds and how many solution rows it produced.
- Counting
Reader - Wraps a reader and tallies how many ranges were requested and how many bytes
were returned — the metric that matters for a range-streamed format.
Atomically counted, so it stays
Sync(a lazily-faulting remote index holds its reader behind a shared loader). - Data
Graph - A validation data graph held fully in memory as a sorted triple vector. Backs
the shapes graph and the eager data path; the lazy data path uses
ReteGraph. - Dendrogram
- A hierarchy of community partitions (a dendrogram).
levels[0]partitions the base nodes;levels[k]partitions levelk-1’s communities. Round 0 is the finest grouping; the last round is the coarsest (pyramid level 0). - Dict
Section - A parsed, read-only dictionary section (parses its metadata on construction).
- Dict
Section Builder - Build a dictionary section from terms (any order; sorted + deduped here).
- Dictionary
- A read-only dictionary mapping terms ↔ role-specific IDs.
- Dictionary
Builder - Builds a
Dictionaryfrom observed(subject, predicate, object)terms. - Graph
- A weighted undirected graph over nodes
0..n. Parallel edges are summed; self-loops are kept (they affect modularity but not community moves). - Graph
Index - The six tiled permutation sections, queryable by triple pattern.
- Graph
Index Builder - Build a
GraphIndexfrom canonical(s, p, o)integer triples. - Group
Directory - A byte-offset directory of a block’s a-groups: one entry per group, sorted
by leading id (the storage order). Built once per block with
TripleBlock::group_directory;TripleBlock::scan_fromthen binary-searches it to jump a probe straight to its group. - Group
Spec - GROUP BY specification: grouping variables and result aggregates.
- Header
- Decoded file header. All multi-byte fields are little-endian on disk. The
*_offset/*_lenfields are a convenience view over the section directory (populated from it on parse, emitted back to it on serialize). - Inconsistency
- One detected incoherent point (a logical contradiction in the graph).
- Label
Entry - One entry of the label index: the display label of a subject (its
rdfs:label/skos:prefLabel/ … literal) paired with that subject’s id. Computed at build (the most-connected labeled subjects, bounded), the table is sorted by the label’s lowercased form so a prefix query is a binary search — autocomplete and CONTAINS/prefix lookup without scanning the literals. Range-fetched with the pyramid; the label string is stored inline so the search is self-contained on the remote-lazy path (no dictionary fault). - Layout
Segment - One labelled byte region of a
.retefile image (seeRete::file_layout).kindis a stable machine tag:header,metadata,dictionary,directory,tile,pyramid,named-graphs. - Level
Links - The class-relation graph rolled up to
depth— the lateral connections at one semantic-zoom level. Coarse levels show relations between abstract classes (Agent → Agent); finer levels resolve them (Person → Organisation). This is what makes the pyramid a leveled graph, not just a leveled histogram. - Level
Rollup - A type histogram rolled up to
depth— the global class distribution at one semantic-zoom level. Coarse levels (small depth) hold abstract ancestor classes; fine levels (large depth) resolve to leaves.roundis the dendrogram round this level is aligned with (informational). - Partition
- A community partition with dense IDs
0..count. - Pred
Stat - Per-predicate cardinality statistics for the cost-based query planner —
computed at build, range-fetched with the pyramid (index-free).
countis the triple total; the distinct-subject/object counts and max multiplicities let the planner estimate a bound subject’s/object’s selectivity (functionaliffmax_objects_per_subject == 1, inverse-functional iffmax_subjects_per_object == 1). - Pyramid
Meta - Decoded pyramid metadata.
- Reasoning
- The result of reasoning over a base graph.
- Rete
- A read-only, in-memory view over a
.retefile image. - Rete
Graph - A SHACL data-graph view backed directly by a
.retefile’s index: every lookup is a routed pattern query, so over a lazy (Rete::open_ranged_lazy) open a validation faults only the tiles holding the shapes’ target nodes — not the whole graph. Views the default graph (named-graph validation uses the eagerDataGraph). - Routed
Triple Pattern - A single triple pattern that can be answered by the range-routed permutation
reader.
Nonemeans the position is a variable/wildcard;Some(term)means the query pins that term. - Section
- One parsed/encoded section-directory entry: a typed
(offset, length)into the file, plus 16 bits of per-section flags (reserved). - Select
- A lowered SELECT query: solution modifiers plus the evaluation plan tree.
- Shacl
Shapes - Parsed SHACL shapes graph.
- Slice
Reader - A
RangeReaderover an in-memory byte slice (tests, embedded files). - Summary
View - A lightweight, overview-only view of a file: the pyramid summary graph plus just enough dictionary to label predicates. Fetched via ranges without touching the (large) triple index — the “load the coarse graph first” path from SPEC.md §7.2.
- Super
Edge - An aggregated relation between two communities in the summary graph.
- Text
Index - A parsed text index: the token table (always resident) plus a posting source.
- Text
Index Builder - Accumulates
token → subjectsand serializes the section. - Tile
- One tile: a community’s triples and their encoded SPO block.
- Triple
Block - A parsed triple block.
- Triple
Block Builder - Accumulates triples and serializes a block.
- Triple
Pattern - A triple pattern
(subject, predicate, object)ofPatternTerms. - Triple
Provenance - Why a triple-pattern result is present in the file.
- Validation
Report - Validation
Result - ZoneMap
- Per-block summary statistics enabling block-skipping.
Enums§
- Agg
- A supported aggregate function.
- FExpr
- A small boolean/comparison expression for FILTER (a subset of SPARQL exprs).
- Graph
Target - The target of a
GRAPHblock. - Header
Error - Index
Permutation - Which stored permutation to scan. The full six orders (a SPARQL engine’s
classic set, as in QLever): together they sort the triples on every prefix
of
(s, p, o)columns, so for any bound-component prefix and any free component there is a permutation that routes on the prefix and yields the stream sorted on that free component — the precondition for a merge join. - Op
- Comparison operators supported in FILTER.
- PathAst
- A lowered property path expression, evaluated as a binary relation over the graph’s nodes.
- Pattern
Term - A term in a pattern: a named variable or a constant term token.
- Plan
- A SPARQL graph-pattern evaluation plan (the supported algebra subset).
- Pyramid
Algo - Which algorithm forms the pyramid’s communities. The format and every
downstream consumer key on opaque community IDs, so the choice only changes
how the partition is computed — all modes must be deterministic to keep the
file content-hash reproducible (see
docs/BENCHMARK.md). - Query
Output - The result of evaluating any SPARQL query form.
- Rep
- Path repetition operator.
- Section
Kind - A top-level file section, addressed by
SectionKindin the header directory. - Severity
- Shacl
Error - Sparql
Error - Summary
Query Shape - Query shapes that can be answered exactly from
crate::range::SummaryViewpredicate totals, without opening the triple index.
Constants§
- CODEC_
NONE - No compression.
- CODEC_
ZSTD - zstd compression (per section).
- CURRENT_
FORMAT_ VERSION - Current format generation written by this crate.
- DEFAULT_
BLOCK - Default block size: 64 KiB — large enough to swallow a dictionary chunk or an index tile in one fetch, small enough to keep over-fetch modest.
- DEFAULT_
CACHE_ CAP - Default cap on resident cached bytes: 256 MiB. Large enough that a working set (the tiles + dictionary chunks a query family touches) stays warm; small enough that a full sweep of a multi-GB file leaves plenty of the 32-bit wasm address space for the decompressed structures built on top of these bytes. At the auto-tuned 128–512 KiB block sizes this is 512–2048 resident blocks, so the eviction scan is trivial.
- DEFAULT_
TILE_ BUDGET - Default per-tile byte budget
T(SPEC.md §7.1). - ENGINE_
VERSION - The
rete-coreversion this facade re-exports. - HEADER_
LEN - Fixed header size in bytes.
- MAGIC
- Magic bytes at offset 0: ASCII
RETE. - MIN_
STABLE_ READ_ VERSION - Oldest stable format generation accepted by this reader.
- RDF_
TYPE rdf:type— the predicate that assigns a class to a resource.- REASON_
RULESET - Version tag of this reasoner’s rule set. Stamped into a baked coherence card so
a
coherent: truecan never be misread as a guarantee from a different set of rules. Bump this whenevermaterialize/detect_inconsistencieschanges (a rule added/removed/altered), sorete reason --verify-cardrejects a stale stamp. - VERSION
- The engine version, as published on crates.io.
Traits§
- Graph
View - A read-only view of the data graph for SHACL validation. The validator only
ever asks targeted questions — a focus node’s values, the subjects of a
predicate, the instances of a class — so this surface is small enough to back
two ways: an in-memory triple set (
DataGraph, eager) or a.retefile’s index directly (ReteGraph), which routes each lookup as a range read so a remote validation faults only the tiles holding the shapes’ targets, not the whole graph. The class/instance helpers are derived from the primitives, so a backend only implements the six lookups. - Range
Reader - Something that can serve arbitrary byte ranges of a
.reteresource. - Service
Client - Executes one SPARQL query against a remote endpoint (the SPARQL Protocol)
and returns its solutions. Implementations own transport, auth, and
timeouts; they typically
POSTthe query withAccept: application/sparql-results+jsonand feed the body throughparse_sparql_json_results. Errors are strings, surfaced verbatim as the query error (or, underSERVICE SILENT, swallowed per the spec) — name the endpoint in the message, the engine adds no prefix.
Functions§
- auto_
block - Pick a
BlockCacheReaderblock size from the file length: bigger files get bigger blocks so a remote query makes far fewer (but larger) round trips — 128 KiB ≤ 10 MB, 256 KiB ≤ 100 MB, 512 KiB above. The over-fetch is modest next to the round-trip latency it saves on a high-latency link (S3/CDN). Shared by the CLI and the wasm client so both size identically; the file length is known for free from the openingHEAD/Content-Range. - batch_
reach_ serial - Per-seed transitive reach, serial loop (reference). Results are returned in
seed order. The parallel sibling
crate::parallel::batch_reach_parallelproduces an identical result. - build_
adjacency - Forward adjacency in unified node space for one predicate:
node -> [succ]. Built once and shared (read-only) across all seeds. For reverse reachability (“who reaches the seed?”), build the map yourself fromRete::predicate_pairsswapping(s, o) -> (o, s). - build_
dendrogram - Build the full dendrogram by repeated Louvain + coarsening, stopping when a round no longer compresses the graph (or only one node remains).
- build_
pyramid_ meta - Build the encoded pyramid-meta section for a graph: cluster, pick a round
sized to
budget, then emit the summary (quotient) graph. Returns(encoded_meta, pyramid_levels). - build_
pyramid_ meta_ algo - Like
build_pyramid_meta_with, but selects the communityPyramidAlgo.PyramidAlgo::Typespartitions byrdf:type— the deterministic, parallelizable alternative to Louvain (one linear pass, no modularity) that still emits the full summary +query_stats; it falls back to Louvain when the graph has no usable typing. Everything downstream of the dendrogram (round choice, summary, schema pyramid, planner stats) is shared across algorithms. - build_
pyramid_ meta_ with - Like
build_pyramid_meta, buttype_overrideforces the schema-pyramid’s type predicate (e.g.wdt:P31) instead of auto-detection. Uses the defaultPyramidAlgo::Louvaincommunity algorithm — byte-identical to before. - build_
schema_ pyramid - Compute the schema pyramid for a graph at the materialized
round, auto-picking the type predicate. Empty when the graph has no usable typing. - choose_
round_ for_ budget - The coarsest round whose every tile fits
budget_bytes, else round 0. Coarser rounds mean fewer, larger tiles; we want the fewest tiles that still respect the per-tile budget (PMTiles-style). - eval_
bgp - Evaluate a BGP against the file’s default graph, returning all solutions resolved to terms (the public convenience API).
- eval_
query - Evaluate any supported SPARQL query form (SELECT / ASK / CONSTRUCT).
- eval_
query_ reasoned - Like
eval_query, but with OWL 2 QL entailment on: the lowered plan is rewritten by the internal QL lowering pass so the answer includes ontology-entailed solutions (Stage 1a:rdfs:subClassOf), computed over the raw data with no materialization. Opt-in — a plaineval_queryis byte-identical to before. - eval_
select_ communities - Evaluate a SELECT per pyramid community, then merge: each community’s
subjects are pushed into the plan as a VALUES binding, the partial rows
are concatenated, and the solution modifiers (GROUP BY / ORDER BY / LIMIT
/ DISTINCT) run once on the union — so the rows are identical to
eval_query’s answer. Sound only for subject-star queries over the default graph (every triple pattern sharing one subject variable; FILTERs allowed); anything else returnsSparqlError::Unsupportedrather than a possibly-wrong split answer.roundpicks the dendrogram granularity (None= the build’s tile-budget round). Also returns each community’s subject and row counts for display. - eval_
sparql - Parse and evaluate a SELECT against a file, applying the plan then
projection, DISTINCT, OFFSET, and LIMIT. Returns
(projected_vars, solutions). - eval_
sparql_ reasoned - Like
eval_sparql, but with OWL 2 QL entailment on (seeeval_query_reasoned). - louvain_
one_ level - One level of Louvain local-moving modularity optimization.
- parse_
select - Parse a SPARQL
SELECTquery and lower it to aSelect. - parse_
sparql_ json_ results - Parse a SPARQL 1.1 Query Results JSON document (
application/sparql-results+json) into bindings of variable → N-Triples term token. An ASK document (noresults) yields no bindings. Unknown per-binding fields are ignored;xsd:stringdatatypes are dropped (a simple literal — matching how plain literals are tokenized everywhere else in the engine). - project_
graph - Project RDF integer triples onto the undirected node graph the pyramid
clusters: one node per distinct term (via the dictionary’s unified node
space), one unit-weight edge per
(subject, object)pair (predicates are ignored for clustering; parallel edges accumulate weight). - push_
json_ string - Append
vtooutas a JSON string literal (RFC 8259 escaping): the mandatory escapes (",\, and C0 controls, the common ones in short form), every other char — including all UTF-8 — passed through. Matches whatserde_jsonemits for a string by default. - query_
predicates - Collect the concrete predicate IRIs a query constrains on — i.e. every
IRI that appears in the predicate position of a triple pattern, or as a plain
predicate inside a property path. Variable predicates (
?p) and the speciala(rdf:type) keyword are normalized to their IRI tokens (<…>). - reach_
one - Transitive reach of
seedover the adjacency (excludes the seed itself). Plain BFS; deterministicBTreeSetresult. This is the single shared BFS used by both the serial and parallel batch drivers. - read_
metadata_ ranged - Fetch only the metadata section (the opaque Dataset Card blob) via a
RangeReader: read the 128-byte header, then the metadata byte range — nothing else. This is the index-free CARD tier of the exploration model: a remote/S3 client learns the dataset’s self-description in two small range requests, never touching the dictionary, index, or pyramid. ReturnsNonewhen the file carries no metadata. - read_
schema_ coherence_ ranged - Dictionary-free Tier-0 coherence read. Fetch only the header and the
pyramid-meta range (2 small range reads) and run
schema_coherenceover the schema pyramid — never touching the dictionary (which a literal-heavy file makes large) or the triple index.Ok(None)if the file ships no pyramid. - read_
schema_ summary_ ranged - The schema summary (per-class histogram + class relations at the finest
level) read over a
RangeReaderfrom the schema pyramid alone — the index-free, range-readable source for a Schema view of a remote graph. Returns(classes, relations)withclasses = [(class_iri, count)]andrelations = [(s_class, predicate, o_class, count)];Nonewhen the file has no schema pyramid. Likeread_schema_coherence_ranged, it reads only the trailing schema block, so it stays flat at any graph size. - reason
- Forward-chain the supported RDFS/OWL rules to a fixpoint, then scan for
inconsistencies over the closed graph.
inferredexcludes triples already present inbase_triples. - results_
envelope_ json - Serialize
outas{ "kind": …, … }.extrais a raw JSON fragment of additional object members appended before the closing brace (e.g.,"remote":{…}); pass""for none.CONSTRUCTis rendered as atriplesarray — the text formats (Turtle / JSON-LD) are handled by the caller, which owns those serializers. - routed_
triple_ pattern - Classify queries whose graph access is exactly one default-graph triple pattern. Solution modifiers (projection, LIMIT, aggregate wrappers) do not change the underlying range access, but named graphs, FROM, joins, filters, paths, and other algebra need the full SPARQL evaluator.
- schema_
classes - Class populations: the number of resources of each
rdf:typeclass in the default graph, descending by count. The instance-count companion toschema_summary. - schema_
coherence - Compute T-Box coherence points from the schema-pyramid fields alone — no
dictionary, no index, no instance data. Shared by
SummaryView::tbox_coherenceand the dictionary-freeread_schema_coherence_ranged. Emitssubclass-cycleandunsatisfiable-class(a class whose ancestor closure — over all parents, folded throughowl:equivalentClass— contains both ends of a disjoint pair). - schema_
summary - An ontology-aware coarse graph: instead of structural communities, group
entities by their
rdf:typeclass and aggregate relations between classes. Returns(subject_class, predicate, object_class, count)over the default graph. Entities with no type are(untyped); literals are(literal).rdf:typetriples themselves define the classes and are not counted as relations. This is the dataset’s effective schema with instance volumes. - sparql_
json_ ask - Serialize an ASK result as a SPARQL 1.1 Query Results JSON document.
- sparql_
json_ results - Serialize a SELECT result as a SPARQL 1.1 Query Results JSON document.
varsfixes theheadorder (pass the projection); solutions’ bindings are term tokens exactly as the engine returns them. - summarize
- Aggregate triples into the quotient (summary) graph at
round. - summary_
query_ shape - Classify SPARQL queries that can be answered exactly from the pyramid summary’s per-predicate totals. This is intentionally conservative: anything with constants, repeated variables, filters, joins, paths, named graphs, ORDER BY, OFFSET/LIMIT, or non-summary-safe aggregates still requires the index. The only accepted DISTINCT shape is a predicate list over one fully unbound triple pattern.
- tile_
by_ community - Partition triples into per-community tiles at
round, ordered by community. - tokenize
- Split text into index/query tokens: Unicode-alphanumeric runs, lowercased,
length ≥
MIN_TOKEN_LEN. The build and query sides MUST use this same function so a query word matches how it was indexed. - validate_
shacl - Validate
dataagainstshapes.datais anyGraphView— an in-memoryDataGraph(eager) or aReteGraphthat routes lookups as range reads (lazy / remote, fetching only the shapes’ targets). - verify
- Recompute the content hash from a file image and check it against the header — detects corruption or truncation of the payload sections.
- write_
dataset - Serialize a full RDF dataset: the default-graph index plus zero or more
named graphs
(iri, index), all sharing one dictionary. - write_
dataset_ with_ metadata - Serialize a dataset with an opaque metadata payload occupying the file’s
metadata section (the application layer defines its meaning — the CLI stores a
JSON Dataset Card there). The section sits immediately after the header and
before the dictionary, so
metadata_offsetstays atHEADER_LENand every downstream section shifts bymetadata.len(). The payload is folded into thecontent_hash, soverifycovers it and it is tamper-evident. - write_
file - Serialize a complete
.retefile image from a dictionary, index, and an (optionally empty) encoded pyramid-meta section.pyramid_levelsrecords the number of dendrogram rounds the pyramid spans (0 if no pyramid).
Type Aliases§
- Binding
- A solution: variable name → bound term (the public, resolved form).
- Community
Select - The outcome of a community-split SELECT: the projected variables, the merged solution rows, and each community’s contribution.
- NodeId
- A dictionary id in the unified node space — the single id space that covers every term that ever appears as a subject or an object. This is the id reachability, the community pyramid, and the graph index work in.
- Object
Id - A dictionary id in the object id space (terms seen in object position).
Map to a
NodeIdwithDictionary::object_node. - Pattern
- A triple pattern:
Noneis an unbound variable,Some(id)a bound term. - Predicate
Id - A dictionary id in the predicate id space. Predicates have their own dense id space and are never part of the unified node space.
- Subject
Id - A dictionary id in the subject id space (terms seen in subject
position). Map to a
NodeIdwithDictionary::subject_node. - Term
Token - The textual form of an RDF term as stored in the dictionary and emitted in
N-Triples: an IRI (
<…>), a blank node (_:…), or a literal ("…", optionally with an@langor^^<datatype>suffix). An alias forstr; it names intent at API boundaries that take a term rather than arbitrary text. - Term
Triple - A resolved triple as terms.
- Triple
- A triple of dictionary IDs in some permutation’s component order.