Generation-pinned reasoning-aware Cypher
gestaltdb.reasoning.adapters.queries.ReasoningQueryView is an explicit,
read-only Cypher surface over a selected logical graph. It prepares a complete
current-graph inference closure once using Current-graph generation admission and execution
and open_inferred_view. Queries reuse those facts; they never silently
run reasoning, write inferred edges, or change ordinary graph.query.
Example (requires the lmdb extra)
Standalone example. Choose the capability-qualified installation tab below.
Generation-pinned current inferred queries require verifiable current-source snapshots.
1python -m pip install "gestaltdb[lmdb]"
1from tempfile import TemporaryDirectory
2from gestaltdb.graphdb import Edge, GraphDB, Node
3from gestaltdb.ontology import Ontology, SubPropertyOf, Transitive, compile_ontology
4from gestaltdb.reasoning import ReasoningLimits
5from gestaltdb.reasoning.adapters.generation import catalog_for_ontology
6from gestaltdb.reasoning.adapters.graph import GraphMapping
7from gestaltdb.reasoning.adapters.queries import QuerySelection, ReasoningQueryView
8
9compiled = compile_ontology(Ontology("paths", [
10 SubPropertyOf("edge", "reach"), Transitive("reach")]))
11mapping = GraphMapping(edge_types=(("LINK", "edge"),))
12selection = QuerySelection(edge_types=(("LINK", "edge"), ("REACH", "reach")))
13with TemporaryDirectory() as path:
14 graph = GraphDB.create(path + "/db", backend='lmdb', serializer='json')
15 try:
16 graph.put_nodes([Node("a"), Node("b"), Node("c")])
17 graph.put_edges_bulk([Edge("ab", "a", "b", {"type": "LINK"}),
18 Edge("bc", "b", "c", {"type": "LINK"})])
19 with graph.current_read_view(require_verifiable=True) as view:
20 queries = ReasoningQueryView(
21 graph, view, compiled, catalog_for_ontology(compiled),
22 mapping=mapping, selection=selection, limits=ReasoningLimits())
23 pin = queries.generation
24 text = ('MATCH (a {id: $id})-[r:REACH]->(b) '
25 'RETURN id(b) AS target, r.reasoning_origin AS origin ORDER BY target')
26 answer = queries.query(text, {"id": "a"}, expected_generation=pin,
27 mode="asserted-plus-inferred")
28 assert answer.result.records == [
29 {"target": "b", "origin": "inferred"},
30 {"target": "c", "origin": "inferred"}]
31 assert answer.generation == pin
32 assert answer.selection.digest == selection.digest
33 assert queries.query(text, {"id": "a"}, expected_generation=pin,
34 mode="asserted-only").result.records == []
35 assert graph.query('MATCH (a)-[:REACH]->(b) RETURN b').records == []
36 finally:
37 graph.close()
1python -m pip install "gestaltdb[lmdb]"
1from tempfile import TemporaryDirectory
2from gestaltdb.graphdb import Edge, GraphDB, Node
3from gestaltdb.ontology import Ontology, SubPropertyOf, Transitive, compile_ontology
4from gestaltdb.reasoning import ReasoningLimits
5from gestaltdb.reasoning.adapters.generation import catalog_for_ontology
6from gestaltdb.reasoning.adapters.graph import GraphMapping
7from gestaltdb.reasoning.adapters.queries import QuerySelection, ReasoningQueryView
8
9compiled = compile_ontology(Ontology("paths", [
10 SubPropertyOf("edge", "reach"), Transitive("reach")]))
11mapping = GraphMapping(edge_types=(("LINK", "edge"),))
12selection = QuerySelection(edge_types=(("LINK", "edge"), ("REACH", "reach")))
13with TemporaryDirectory() as path:
14 graph = GraphDB.create(path + "/db", backend='lmdb', serializer='pickle')
15 try:
16 graph.put_nodes([Node("a"), Node("b"), Node("c")])
17 graph.put_edges_bulk([Edge("ab", "a", "b", {"type": "LINK"}),
18 Edge("bc", "b", "c", {"type": "LINK"})])
19 with graph.current_read_view(require_verifiable=True) as view:
20 queries = ReasoningQueryView(
21 graph, view, compiled, catalog_for_ontology(compiled),
22 mapping=mapping, selection=selection, limits=ReasoningLimits())
23 pin = queries.generation
24 text = ('MATCH (a {id: $id})-[r:REACH]->(b) '
25 'RETURN id(b) AS target, r.reasoning_origin AS origin ORDER BY target')
26 answer = queries.query(text, {"id": "a"}, expected_generation=pin,
27 mode="asserted-plus-inferred")
28 assert answer.result.records == [
29 {"target": "b", "origin": "inferred"},
30 {"target": "c", "origin": "inferred"}]
31 assert answer.generation == pin
32 assert answer.selection.digest == selection.digest
33 assert queries.query(text, {"id": "a"}, expected_generation=pin,
34 mode="asserted-only").result.records == []
35 assert graph.query('MATCH (a)-[:REACH]->(b) RETURN b').records == []
36 finally:
37 graph.close()
1python -m pip install "gestaltdb[lmdb,msgpack]"
1from tempfile import TemporaryDirectory
2from gestaltdb.graphdb import Edge, GraphDB, Node
3from gestaltdb.ontology import Ontology, SubPropertyOf, Transitive, compile_ontology
4from gestaltdb.reasoning import ReasoningLimits
5from gestaltdb.reasoning.adapters.generation import catalog_for_ontology
6from gestaltdb.reasoning.adapters.graph import GraphMapping
7from gestaltdb.reasoning.adapters.queries import QuerySelection, ReasoningQueryView
8
9compiled = compile_ontology(Ontology("paths", [
10 SubPropertyOf("edge", "reach"), Transitive("reach")]))
11mapping = GraphMapping(edge_types=(("LINK", "edge"),))
12selection = QuerySelection(edge_types=(("LINK", "edge"), ("REACH", "reach")))
13with TemporaryDirectory() as path:
14 graph = GraphDB.create(path + "/db", backend='lmdb', serializer='messagepack')
15 try:
16 graph.put_nodes([Node("a"), Node("b"), Node("c")])
17 graph.put_edges_bulk([Edge("ab", "a", "b", {"type": "LINK"}),
18 Edge("bc", "b", "c", {"type": "LINK"})])
19 with graph.current_read_view(require_verifiable=True) as view:
20 queries = ReasoningQueryView(
21 graph, view, compiled, catalog_for_ontology(compiled),
22 mapping=mapping, selection=selection, limits=ReasoningLimits())
23 pin = queries.generation
24 text = ('MATCH (a {id: $id})-[r:REACH]->(b) '
25 'RETURN id(b) AS target, r.reasoning_origin AS origin ORDER BY target')
26 answer = queries.query(text, {"id": "a"}, expected_generation=pin,
27 mode="asserted-plus-inferred")
28 assert answer.result.records == [
29 {"target": "b", "origin": "inferred"},
30 {"target": "c", "origin": "inferred"}]
31 assert answer.generation == pin
32 assert answer.selection.digest == selection.digest
33 assert queries.query(text, {"id": "a"}, expected_generation=pin,
34 mode="asserted-only").result.records == []
35 assert graph.query('MATCH (a)-[:REACH]->(b) RETURN b').records == []
36 finally:
37 graph.close()
1python -m pip install "gestaltdb[lmdb,protobuf]"
1from tempfile import TemporaryDirectory
2from gestaltdb.graphdb import Edge, GraphDB, Node
3from gestaltdb.ontology import Ontology, SubPropertyOf, Transitive, compile_ontology
4from gestaltdb.reasoning import ReasoningLimits
5from gestaltdb.reasoning.adapters.generation import catalog_for_ontology
6from gestaltdb.reasoning.adapters.graph import GraphMapping
7from gestaltdb.reasoning.adapters.queries import QuerySelection, ReasoningQueryView
8
9compiled = compile_ontology(Ontology("paths", [
10 SubPropertyOf("edge", "reach"), Transitive("reach")]))
11mapping = GraphMapping(edge_types=(("LINK", "edge"),))
12selection = QuerySelection(edge_types=(("LINK", "edge"), ("REACH", "reach")))
13with TemporaryDirectory() as path:
14 graph = GraphDB.create(path + "/db", backend='lmdb', serializer='protobuf')
15 try:
16 graph.put_nodes([Node("a"), Node("b"), Node("c")])
17 graph.put_edges_bulk([Edge("ab", "a", "b", {"type": "LINK"}),
18 Edge("bc", "b", "c", {"type": "LINK"})])
19 with graph.current_read_view(require_verifiable=True) as view:
20 queries = ReasoningQueryView(
21 graph, view, compiled, catalog_for_ontology(compiled),
22 mapping=mapping, selection=selection, limits=ReasoningLimits())
23 pin = queries.generation
24 text = ('MATCH (a {id: $id})-[r:REACH]->(b) '
25 'RETURN id(b) AS target, r.reasoning_origin AS origin ORDER BY target')
26 answer = queries.query(text, {"id": "a"}, expected_generation=pin,
27 mode="asserted-plus-inferred")
28 assert answer.result.records == [
29 {"target": "b", "origin": "inferred"},
30 {"target": "c", "origin": "inferred"}]
31 assert answer.generation == pin
32 assert answer.selection.digest == selection.digest
33 assert queries.query(text, {"id": "a"}, expected_generation=pin,
34 mode="asserted-only").result.records == []
35 assert graph.query('MATCH (a)-[:REACH]->(b) RETURN b').records == []
36 finally:
37 graph.close()
1python -m pip install "gestaltdb[rocksdb]"
1from tempfile import TemporaryDirectory
2from gestaltdb.graphdb import Edge, GraphDB, Node
3from gestaltdb.ontology import Ontology, SubPropertyOf, Transitive, compile_ontology
4from gestaltdb.reasoning import ReasoningLimits
5from gestaltdb.reasoning.adapters.generation import catalog_for_ontology
6from gestaltdb.reasoning.adapters.graph import GraphMapping
7from gestaltdb.reasoning.adapters.queries import QuerySelection, ReasoningQueryView
8
9compiled = compile_ontology(Ontology("paths", [
10 SubPropertyOf("edge", "reach"), Transitive("reach")]))
11mapping = GraphMapping(edge_types=(("LINK", "edge"),))
12selection = QuerySelection(edge_types=(("LINK", "edge"), ("REACH", "reach")))
13with TemporaryDirectory() as path:
14 graph = GraphDB.create(path + "/db", backend='pyrex', backend_options={"transactional": True}, serializer='json')
15 try:
16 graph.put_nodes([Node("a"), Node("b"), Node("c")])
17 graph.put_edges_bulk([Edge("ab", "a", "b", {"type": "LINK"}),
18 Edge("bc", "b", "c", {"type": "LINK"})])
19 with graph.current_read_view(require_verifiable=True) as view:
20 queries = ReasoningQueryView(
21 graph, view, compiled, catalog_for_ontology(compiled),
22 mapping=mapping, selection=selection, limits=ReasoningLimits())
23 pin = queries.generation
24 text = ('MATCH (a {id: $id})-[r:REACH]->(b) '
25 'RETURN id(b) AS target, r.reasoning_origin AS origin ORDER BY target')
26 answer = queries.query(text, {"id": "a"}, expected_generation=pin,
27 mode="asserted-plus-inferred")
28 assert answer.result.records == [
29 {"target": "b", "origin": "inferred"},
30 {"target": "c", "origin": "inferred"}]
31 assert answer.generation == pin
32 assert answer.selection.digest == selection.digest
33 assert queries.query(text, {"id": "a"}, expected_generation=pin,
34 mode="asserted-only").result.records == []
35 assert graph.query('MATCH (a)-[:REACH]->(b) RETURN b').records == []
36 finally:
37 graph.close()
1python -m pip install "gestaltdb[rocksdb]"
1from tempfile import TemporaryDirectory
2from gestaltdb.graphdb import Edge, GraphDB, Node
3from gestaltdb.ontology import Ontology, SubPropertyOf, Transitive, compile_ontology
4from gestaltdb.reasoning import ReasoningLimits
5from gestaltdb.reasoning.adapters.generation import catalog_for_ontology
6from gestaltdb.reasoning.adapters.graph import GraphMapping
7from gestaltdb.reasoning.adapters.queries import QuerySelection, ReasoningQueryView
8
9compiled = compile_ontology(Ontology("paths", [
10 SubPropertyOf("edge", "reach"), Transitive("reach")]))
11mapping = GraphMapping(edge_types=(("LINK", "edge"),))
12selection = QuerySelection(edge_types=(("LINK", "edge"), ("REACH", "reach")))
13with TemporaryDirectory() as path:
14 graph = GraphDB.create(path + "/db", backend='pyrex', backend_options={"transactional": True}, serializer='pickle')
15 try:
16 graph.put_nodes([Node("a"), Node("b"), Node("c")])
17 graph.put_edges_bulk([Edge("ab", "a", "b", {"type": "LINK"}),
18 Edge("bc", "b", "c", {"type": "LINK"})])
19 with graph.current_read_view(require_verifiable=True) as view:
20 queries = ReasoningQueryView(
21 graph, view, compiled, catalog_for_ontology(compiled),
22 mapping=mapping, selection=selection, limits=ReasoningLimits())
23 pin = queries.generation
24 text = ('MATCH (a {id: $id})-[r:REACH]->(b) '
25 'RETURN id(b) AS target, r.reasoning_origin AS origin ORDER BY target')
26 answer = queries.query(text, {"id": "a"}, expected_generation=pin,
27 mode="asserted-plus-inferred")
28 assert answer.result.records == [
29 {"target": "b", "origin": "inferred"},
30 {"target": "c", "origin": "inferred"}]
31 assert answer.generation == pin
32 assert answer.selection.digest == selection.digest
33 assert queries.query(text, {"id": "a"}, expected_generation=pin,
34 mode="asserted-only").result.records == []
35 assert graph.query('MATCH (a)-[:REACH]->(b) RETURN b').records == []
36 finally:
37 graph.close()
1python -m pip install "gestaltdb[msgpack,rocksdb]"
1from tempfile import TemporaryDirectory
2from gestaltdb.graphdb import Edge, GraphDB, Node
3from gestaltdb.ontology import Ontology, SubPropertyOf, Transitive, compile_ontology
4from gestaltdb.reasoning import ReasoningLimits
5from gestaltdb.reasoning.adapters.generation import catalog_for_ontology
6from gestaltdb.reasoning.adapters.graph import GraphMapping
7from gestaltdb.reasoning.adapters.queries import QuerySelection, ReasoningQueryView
8
9compiled = compile_ontology(Ontology("paths", [
10 SubPropertyOf("edge", "reach"), Transitive("reach")]))
11mapping = GraphMapping(edge_types=(("LINK", "edge"),))
12selection = QuerySelection(edge_types=(("LINK", "edge"), ("REACH", "reach")))
13with TemporaryDirectory() as path:
14 graph = GraphDB.create(path + "/db", backend='pyrex', backend_options={"transactional": True}, serializer='messagepack')
15 try:
16 graph.put_nodes([Node("a"), Node("b"), Node("c")])
17 graph.put_edges_bulk([Edge("ab", "a", "b", {"type": "LINK"}),
18 Edge("bc", "b", "c", {"type": "LINK"})])
19 with graph.current_read_view(require_verifiable=True) as view:
20 queries = ReasoningQueryView(
21 graph, view, compiled, catalog_for_ontology(compiled),
22 mapping=mapping, selection=selection, limits=ReasoningLimits())
23 pin = queries.generation
24 text = ('MATCH (a {id: $id})-[r:REACH]->(b) '
25 'RETURN id(b) AS target, r.reasoning_origin AS origin ORDER BY target')
26 answer = queries.query(text, {"id": "a"}, expected_generation=pin,
27 mode="asserted-plus-inferred")
28 assert answer.result.records == [
29 {"target": "b", "origin": "inferred"},
30 {"target": "c", "origin": "inferred"}]
31 assert answer.generation == pin
32 assert answer.selection.digest == selection.digest
33 assert queries.query(text, {"id": "a"}, expected_generation=pin,
34 mode="asserted-only").result.records == []
35 assert graph.query('MATCH (a)-[:REACH]->(b) RETURN b').records == []
36 finally:
37 graph.close()
1python -m pip install "gestaltdb[protobuf,rocksdb]"
1from tempfile import TemporaryDirectory
2from gestaltdb.graphdb import Edge, GraphDB, Node
3from gestaltdb.ontology import Ontology, SubPropertyOf, Transitive, compile_ontology
4from gestaltdb.reasoning import ReasoningLimits
5from gestaltdb.reasoning.adapters.generation import catalog_for_ontology
6from gestaltdb.reasoning.adapters.graph import GraphMapping
7from gestaltdb.reasoning.adapters.queries import QuerySelection, ReasoningQueryView
8
9compiled = compile_ontology(Ontology("paths", [
10 SubPropertyOf("edge", "reach"), Transitive("reach")]))
11mapping = GraphMapping(edge_types=(("LINK", "edge"),))
12selection = QuerySelection(edge_types=(("LINK", "edge"), ("REACH", "reach")))
13with TemporaryDirectory() as path:
14 graph = GraphDB.create(path + "/db", backend='pyrex', backend_options={"transactional": True}, serializer='protobuf')
15 try:
16 graph.put_nodes([Node("a"), Node("b"), Node("c")])
17 graph.put_edges_bulk([Edge("ab", "a", "b", {"type": "LINK"}),
18 Edge("bc", "b", "c", {"type": "LINK"})])
19 with graph.current_read_view(require_verifiable=True) as view:
20 queries = ReasoningQueryView(
21 graph, view, compiled, catalog_for_ontology(compiled),
22 mapping=mapping, selection=selection, limits=ReasoningLimits())
23 pin = queries.generation
24 text = ('MATCH (a {id: $id})-[r:REACH]->(b) '
25 'RETURN id(b) AS target, r.reasoning_origin AS origin ORDER BY target')
26 answer = queries.query(text, {"id": "a"}, expected_generation=pin,
27 mode="asserted-plus-inferred")
28 assert answer.result.records == [
29 {"target": "b", "origin": "inferred"},
30 {"target": "c", "origin": "inferred"}]
31 assert answer.generation == pin
32 assert answer.selection.digest == selection.digest
33 assert queries.query(text, {"id": "a"}, expected_generation=pin,
34 mode="asserted-only").result.records == []
35 assert graph.query('MATCH (a)-[:REACH]->(b) RETURN b').records == []
36 finally:
37 graph.close()
Selection and logical graph semantics
Input GraphMapping controls which physical labels, relationship types and
entity-valued properties become base facts. Output QuerySelection is a
different policy: labels=(("Person", "class:Person"),) maps an output
Cypher label to a class ID; edge_types=(("REACH", "reach"),) maps an
output relationship type to a binary predicate. Names are exact; no implicit
hierarchy expansion occurs. Unselected facts are invisible; an absent selected
predicate simply yields no relationships. Type facts can only become labels.
Both modes query this selected logical projection, not a union with all
physical records. asserted-only uses mapped base facts;
asserted-plus-inferred uses the complete closure, including base facts.
One selected fact/type pair yields one directed relationship; parallel source
edges collapse. If a fact is both asserted and derivable, its origin is
asserted. No proof or all-support claim follows from this origin field.
Nodes are subjects of selected type facts plus endpoints of selected relations.
Node IDs remain external entity IDs; endpoints need not have physical node
records (for example mapped entity-valued properties). Class IDs do not become
nodes merely because they appear in type facts. Unmapped isolated physical
nodes and arbitrary physical properties are not copied. Node properties are
empty; id(n) or {id: $id} addresses the external ID. Relationship
properties are type, predicate and reasoning_origin. Relationship
IDs use a deterministic logical: namespace derived from the fact and
output type, never physical edge IDs. Returned entities are invocation-local:
editing them cannot change later queries or source storage.
Supported queries
One MATCH with one node or fixed one-hop pattern; optional WHERE; and
RETURN with ordinary expressions, aggregates, aliases, DISTINCT,
ORDER BY, SKIP and LIMIT. Endpoint labels, label expressions,
{id: ...} filters, relationship property maps, parameters, outgoing/incoming/
undirected patterns, and a one-hop path binding are supported. The adapter
reuses the existing parser, planner and runtime without modifying them.
Writes, DDL, procedures, multiple patterns/clauses, OPTIONAL MATCH,
WITH, UNWIND, unions, subqueries, temporal qualifiers, shortest paths,
multi-hop and variable-length traversal are rejected before execution. Ordinary
Cypher remains available separately for physical-record workflows. Typed n-ary
reasoning, temporal claims, range reasoning and persistent sessions are not
admitted by this current binary adapter.
Pins, lifetime and freshness
Keep both the live source graph and borrowed view open. The adapter
never closes either. Unlike the detached fact-only InferredGraphView, this
surface requires its snapshot to remain active throughout preparation and
querying. A newly opened or unrelated snapshot is not silently substituted.
Each query requires an explicit mode and a full expected_generation. Pins
include source database, snapshot sequence/token, ontology revision, input
mapping, program/catalog digests, semantics profile and dictionary identity.
Optional preparation-time expected_generation rejects incompatible reuse
before record extraction. The output selection has a separate canonical digest;
it does not alter reasoning identity. Results carry generation, selection and
mode alongside their ordinary QueryResult in answer.result.
Active snapshot authentication and live-source freshness are checked before and after query execution. Even unrelated source writes reject reuse; prepare again under a new snapshot/generation. These checks detect observed movement; they are not a writer lock, an atomic latest-state guarantee or a publication permit. Snapshot hashing may be expensive. No caller-created closure/result object can be supplied as trusted input to this adapter.
Bounds and resource costs
Preparation uses GraphSourceLimits and ReasoningLimits through the
existing adapter; base extraction is a second bounded pass over the same
snapshot. Closure computation uses the default indexed Python executor.
QueryLimits caps projected nodes/relationships, cumulative logical candidate
reads/lookups per invocation, and final output rows. Exceeding a cap raises
ReasoningLimitError with no successful partial result. Projection is rebuilt
per query, so all selected facts must fit even for a small LIMIT. Candidate
property seeks use scans plus residual filters, not persistent indexes.
max_result_rows is checked after runtime materialization, not a streaming
allocation guard. Single-pattern cardinality is also bounded by the projection.
Cancellation is cooperative during projection, logical record reads and
authentication boundaries. It does not interrupt snapshot hashing, sorting,
aggregates, scalar functions or native/backend calls mid-operation. Scalar
expressions such as large range/collect calls and caller parameters
retain ordinary allocation costs. These caps are not hard RSS, deadline or
untrusted-query sandbox guarantees. No optimizer, speedup or scale claim is
made; no heavy local benchmark is needed for this correctness-first surface.