Grouped stock records yield location counts, totals, minimums and maximums.
inventory-srl.srl · output · proof · check · playground
The question
How much stock does each product have, and across how many locations?
Historical background
SPARQL 1.1 added grouping and aggregates to RDF querying in 2013. That made summaries such as a count per item or a total across warehouses expressible over triples. This example uses the aggregate vocabulary to derive reusable stock summaries. W3C, SPARQL 1.1 Aggregates
What the program says
inventory-srl.srl:
# A warehouse lists stock per product and location. For each product the
# rule counts its locations and sums and bounds its stock; a product with no
# stock records gets no total. Shows GROUP BY with COUNT, SUM, MIN and MAX.
PREFIX : <http://example.org/inventory#>
DATA {
:bolt a :Product . :nut a :Product . :gear a :Product .
:s1 :product :bolt ; :location :ghent ; :quantity 120 .
:s2 :product :bolt ; :location :lyon ; :quantity 80 .
:s3 :product :nut ; :location :ghent ; :quantity 500 .
}
RULE { ?p :locations ?n ; :total ?sum ; :smallest ?min ; :largest ?max }
WHERE {
?p a :Product
GROUP BY ?p { ?s :product ?p ; :location ?l ; :quantity ?q }
[ ?n := COUNT(DISTINCT ?l), ?sum := SUM(?q), ?min := MIN(?q), ?max := MAX(?q) ]
}
What eyemind concludes
The command writes these conclusions in its native output format:
PREFIX : <http://example.org/inventory#>
:bolt a :Product .
:nut a :Product .
:gear a :Product .
:s1 :product :bolt .
:s1 :location :ghent .
:s1 :quantity 120 .
:s2 :product :bolt .
:s2 :location :lyon .
:s2 :quantity 80 .
:s3 :product :nut .
:s3 :location :ghent .
:s3 :quantity 500 .
:bolt :locations 2 .
:bolt :total 200 .
:bolt :smallest 80 .
:bolt :largest 120 .
:nut :locations 1 .
:nut :total 500 .
:nut :smallest 500 .
:nut :largest 500 .
Grouping counts distinct locations and computes SUM, MIN and MAX. Bolts total 200 across two locations; nuts total 500 at one. The gear has no stock records, so it gets no aggregate result. SRL output also includes the base graph.
The check report records the corresponding claims as JavaScript expressions:
// Checked answers: these follow from the input program and are every answer of its questions.
inf(iri('http://example.org/inventory#bolt'), iri('http://example.org/inventory#locations'), 2)
inf(iri('http://example.org/inventory#bolt'), iri('http://example.org/inventory#total'), 200)
inf(iri('http://example.org/inventory#bolt'), iri('http://example.org/inventory#smallest'), 80)
inf(iri('http://example.org/inventory#bolt'), iri('http://example.org/inventory#largest'), 120)
inf(iri('http://example.org/inventory#nut'), iri('http://example.org/inventory#locations'), 1)
inf(iri('http://example.org/inventory#nut'), iri('http://example.org/inventory#total'), 500)
inf(iri('http://example.org/inventory#nut'), iri('http://example.org/inventory#smallest'), 500)
inf(iri('http://example.org/inventory#nut'), iri('http://example.org/inventory#largest'), 500)
inf(iri('http://example.org/inventory#bolt'), iri('http://www.w3.org/1999/02/22-rdf-syntax-ns#type'), iri('http://example.org/inventory#Product'))
inf(iri('http://example.org/inventory#nut'), iri('http://www.w3.org/1999/02/22-rdf-syntax-ns#type'), iri('http://example.org/inventory#Product'))
inf(iri('http://example.org/inventory#gear'), iri('http://www.w3.org/1999/02/22-rdf-syntax-ns#type'), iri('http://example.org/inventory#Product'))
inf(iri('http://example.org/inventory#s1'), iri('http://example.org/inventory#product'), iri('http://example.org/inventory#bolt'))
inf(iri('http://example.org/inventory#s1'), iri('http://example.org/inventory#location'), iri('http://example.org/inventory#ghent'))
inf(iri('http://example.org/inventory#s1'), iri('http://example.org/inventory#quantity'), 120)
inf(iri('http://example.org/inventory#s2'), iri('http://example.org/inventory#product'), iri('http://example.org/inventory#bolt'))
inf(iri('http://example.org/inventory#s2'), iri('http://example.org/inventory#location'), iri('http://example.org/inventory#lyon'))
inf(iri('http://example.org/inventory#s2'), iri('http://example.org/inventory#quantity'), 80)
inf(iri('http://example.org/inventory#s3'), iri('http://example.org/inventory#product'), iri('http://example.org/inventory#nut'))
inf(iri('http://example.org/inventory#s3'), iri('http://example.org/inventory#location'), iri('http://example.org/inventory#ghent'))
inf(iri('http://example.org/inventory#s3'), iri('http://example.org/inventory#quantity'), 500)
What the proof establishes
The saved report ends with verdict('checked').
GROUP BY compiles to findall_sorted, so the aggregates do not depend on
the order in which the stock records were derived. When a firing is
replayed, each grouped list is taken from the proof’s call tables for the
search as it stood when the grouping ran; the sorted list of ground items
depends only on which records there are, which the listings establish.
The checker reads the clauses from the compiled program, not from the
proof, and never calls the reasoning engine. It answers the original
questions from the proof’s call tables, checking each table by expanding its
call one level against the program; it requires each derived listing to be
closed under the forward rules; and it replays the recorded firing of each
listed fact, computing primitives again. A step, table or listing the check
does not use fails it.
The verdict establishes that the displayed claims follow from the compiled program and are every answer of its original questions.
Try it
From the repository root:
node bin/eyemind.js examples/inventory-srl.srl
node bin/eyemind.js --proof examples/inventory-srl.srl
node bin/eyemind.js --check-proof examples/proof/inventory-srl.js examples/inventory-srl.srl
