grampy brick lab

Brick Sorter

A sorting line run by grampy — the real library, executing in your browser. Every brick is a subject, every tray a node, and the line sorts bricks by colour. A TNT brick hides its colour: defused, it shows it and gets sorted; past saving, it goes to waste. A sorted brick sent back waits in the inbox lane before it runs again, and the packing station holds its bricks until five share a colour. See the code.

Clock
--:--:--
In the inbox
0
Sorted
0
TNT wasted
0
Loading Python and grampy…

The code behind the line

The whole workflow, read from scenario.py — the file running above. Eight nodes as data, one small class that teaches grampy to read a brick, and a handful of calls. Bricks come from two crates and take the same line: the factory ones are polished, the salvage ones are not, and that difference is stated once.

1 · Declare the graph
Loading scenario.py…
2 · Teach it to read your objects
Loading scenario.py…
3 · Run it on your storage
from quazardous.grampy import NodeJournal
from quazardous.grampy.drivers.sqlite import Query, SqliteDriver, schema
from quazardous.grampy.items import Items

for statement in schema():
    conn.execute(statement)
journal = NodeJournal(SqliteDriver(conn), GRAPH)
items = Items(journal, Bricks())      # the adapter from panel 2

# Bricks arrive. Their crate and their version are read off them.
items.admit(new_bricks)
items.arrive("inbox", new_bricks)

# WHICH bricks may be worked is YOUR sentence, in your storage's own terms:
# first column the subject, ORDER BY the priority. grampy reads it without
# understanding it, in the same transaction as the claim.
waiting = Query("SELECT id FROM bricks WHERE state <> 'shipped' ORDER BY dropped_at")

# A worker takes bricks. Candidates are bricks too, or a query like this one
# for a hot path. A salvage brick at the polisher is given up here, because
# the adapter said so.
lease = items.claim("polish", 10, candidates=waiting)
for brick in lease:
    ...  # your work, on your own object
items.conclude("polish", lease)

# A janitor, every few seconds: lanes, waits, timeouts, dead workers.
journal.settle(candidates=waiting)
journal.expire()

What you are looking at