graphlava

Chapter 10

A worked example

The whole running example in one place: one agent's memory, written step by step, queried across its layers, and then partly deleted. Every piece comes from an earlier chapter.

The prefix : is <http://example.org/> throughout. :in is the spec's shorthand for the system membership predicate, whose final IRI is open. Steps marked sketch use syntax the spec has not fixed yet.

Step 1: the schema

From chapter 8, the meta-model declares :met as a multi predicate, :worksAt as a functional predicate (one employer per person, updated in place), and :Meeting as a fixed-arity hyperedge type with three roles, :agent, :target and :topic, plus a literal attribute :when. Everything else uses the defaults: one statement per (s, p, o), literals stored inline.

Step 2: the containers

INSERT DATA {
  :session7    :in :aliceMemory .
  :aliceMemory :in :agentMemory .
  :session7    :startedAt "2026-11-20T09:00:00Z"^^xsd:dateTime .
}

Two nesting edges. The in_star closure now holds (:session7, :aliceMemory), (:session7, :agentMemory) and (:aliceMemory, :agentMemory). The start time is an inline property of the graph node.

Step 3: facts with layers

INSERT DATA {
  GRAPH :session7 {
    :alice :worksAt :acme {| :confidence 0.9 ;
                             :source :chat12 {| :checkedBy :carol |} |} .
    :bob   :worksAt :acme {| :confidence 0.7 |} .
  }
}

This writes, with home graph :session7:

s1 = (:alice, :worksAt, :acme)       # orderings + stmt
     (s1, :confidence, 0.9)          # inline property of s1, no ID
s3 = (s1, :source, :chat12)          # orderings + stmt
s4 = (s3, :checkedBy, :carol)        # an annotation of an annotation
s5 = (:bob, :worksAt, :acme)
     (s5, :confidence, 0.7)          # inline, no ID

Step 4: the meeting

# sketch, syntax not final: lowers to CreateHedge(:Meeting, roles), home graph :session7
create :Meeting { :agent :alice, :target :bob, :topic :pricing }
        :when "2026-11-20"^^xsd:date  in :session7
# returns e1: one hedge record, three incidence keys, and :when in props[e1]

Then a statement about the meeting, and a promoted role:

INSERT { ?e :ledTo :priceChange .
         ?e :agent :alice {| :confidence 0.7 |} . }
WHERE  { ?e a :Meeting ; :agent :alice ; :target :bob ; :topic :pricing }

(e1, :ledTo, :priceChange) is an ordinary statement with e1 as subject. The role (e1, :agent, :alice) is promoted: it gets an ID in hedge_promo and carries the confidence.

Step 5: an annotated membership

The summarizer filed s1 under the session. That is a statement about the membership triple, so the membership is promoted:

INSERT { ?r :in :session7 {| :filedBy :summarizer |} . }
WHERE  { << :alice :worksAt :acme ~ ?r >> }

Step 6: a rule

@prefix : <http://example.org/> .
colleague(?x, ?y) :- :worksAt(?x, ?o), :worksAt(?y, ?o), ?x != ?y .

Stored with define, it is part of the meta-model. It runs on demand.

Querying across the layers

Q1. What does the agent believe about employers, with confidence, anywhere in its memory?

SELECT ?who ?org ?c WHERE {
  GRAPH :agentMemory { << ?who :worksAt ?org ~ ?r >> }
  ?r :confidence ?c .
}

Two rows: Alice at Acme (0.9), Bob at Acme (0.7). Both facts are two levels down, and the transitive scope finds them. With GRAPH! :agentMemory the result is empty.

Q2. Where did that come from, and who checked it?

SELECT ?src ?checker WHERE {
  << :alice :worksAt :acme ~ ?r >> .
  << ?r :source ?src ~ ?r2 >> .
  ?r2 :checkedBy ?checker .
}

:chat12, checked by :carol. Two reifier bindings, no rdf:reifies joins.

Q3. Which meetings did Alice call, and what came of them?

SELECT ?e ?with ?topic ?outcome WHERE {
  ?e a :Meeting ; :agent :alice ; :target ?with ; :topic ?topic .
  OPTIONAL { ?e :ledTo ?outcome }
}

One incidence prefix scan on (:alice, :agent, :Meeting), a record read for e1, and one ordering lookup for :ledTo.

Q4. Which episode holds each fact?

SELECT ?who ?ep WHERE { GRAPH ?ep { ?who :worksAt :acme } }

?ep = :session7 for both: GRAPH ?g binds direct graphs, never ancestors.

Q5. Alice's colleagues

?- colleague(:alice, ?who) .

:bob. The fact colleague(:alice, :bob) exists only for this query; it has no ID.

What the store holds

ItemKindHas an ID?Stored in
s1, s5 worksAt factsstatementyes3 orderings + stmt
confidences 0.9, 0.7inline propertynothe statement's property record
s3 source, s4 checkedBystatement (annotation)yesorderings + stmt
e1 meetinghyperedgeyes (kind 0x6)hedge + 3 incidence keys
role (e1, :agent, :alice)promoted roleyeshedge_promo
other roles of e1virtual role triplesnothe record
(e1, :when, 2026-11-20)literal attribute (inline property)noprops[e1]; no incidence key
(e1, :ledTo, :priceChange)statementyesorderings + stmt
(s1, :in, :session7)promoted membershipyespointer in gmem under the home flag
(s5, :in, :session7)home membershipnonothing: read from the ID's chunk
nesting edgesvirtual :in triples between graphsno (an ID only if annotated)gparent, gchild, closure in in_star
colleague(:alice, :bob)derived factnonowhere (on demand)

Deleting

Alice left Acme, and the agent deletes the fact:

DELETE DATA { :alice :worksAt :acme . }
ItemWhat happensWhy
s1deletedthe target
confidence 0.9deletedan inline property of s1
s3, then s4deletedsubject is s1, then s3: transitive cascade
the promoted membership and its :filedBydeleteds1's gmem entries go, and the promoted statement cascades
e1 and its rolesunchangede1 refers to :alice, not to s1
colleague(:alice, :bob)no longer derivedon-demand rules read the current data

The DELETE has no graph clause, so it deletes the statement itself. Written inside GRAPH :session7 { … } it would remove only the membership in :session7 (ADR 0024); since that is s1's only graph, the statement would go the same way. Had another episode also asserted the fact, it would have survived there.

Deleting :bob outright would fail with ReferencedByHyperedge, because he fills the target role of e1. With DETACH the meeting goes too, and with it (e1, :ledTo, :priceChange) and the promoted role.

graphlava keeps no history, so after the delete the agent cannot ask what it used to believe. If it needs to, the memory belongs in tiramemsu, or the change should be modelled as data (for example an :until annotation instead of a delete).

In graphlava. One memory used all four metagraph kinds: annotations to two levels, a hyperedge with a promoted role, nested containers with an annotated membership, and a rule in the meta-model. Only the parts that were annotated or written as node-valued statements got IDs and index entries.

Takeaways

Contents