A day at Helix Biolabs¶
examples/helix/ is the access-control system of the tutorial
as a program: the site and its 26 people loaded from files, the door controller, the monitor, and
a simulated Tuesday.
The main process plays the card readers. It follows every badge’s itinerary for the day through the door controller.
A second process runs the monitor, which checks the rules no door can enforce every five simulated minutes.
The two processes share nothing but the database directory, as a real controller and monitor would. The day contains a case of every rule: badges, bans, grants and their hours, training, closing times, minimum and maximum occupancy, rooms that must never be empty, visitors’ escorts and the time limit in high containment.
Running it¶
From the repository root, with the package installed (pip install zypherdb) or built from source
(make python, then PYTHONPATH=python):
python -m examples.helix.simulate
Helix Biolabs, Tuesday 11 March 2025: 8 rooms, 26 people, 27 badges, 134 movements planned
06:55 refused Emeka Nwosu Sequencing Lab outside hours
07:10 refused Lars Becker Animal Holding training missing
07:55 refused Chen Wei Lobby badge not valid
08:30 refused Kemi Adeyemi BSL-3 Lab training missing
10:00 refused Tomás Novak Sequencing Lab room full
10:00 refused Marta Gil Lobby badge not valid
10:10 refused Sara Lindgren BSL-3 Lab room full
10:10 refused Yusuf Demir BSL-3 Lab room full
10:30 refused Ines Moreau Sequencing Lab no access
10:30 refused Gustavo Pérez Airlock barred from the zone
10:40 refused Hiro Tanaka Sequencing Lab no access
11:00 ALARM unescorted visitor Seminar Room Ines Moreau
11:45 cleared unescorted visitor Seminar Room
12:35 ALARM overstay BSL-3 Lab Dalia Haddad, Joon Park
12:40 ALARM alone BSL-3 Lab Nadia Petrova
12:40 cleared overstay BSL-3 Lab
12:50 cleared alone BSL-3 Lab
13:00 ALARM unstaffed Vivarium Control
13:30 cleared unstaffed Vivarium Control
14:10 refused Nadia Petrova BSL-3 Lab room full
15:00 site a sequencer is serviced: the Sequencing Lab takes 3
15:00 ALARM over capacity Sequencing Lab Aiko Sato, Chen Wei, Emeka Nwosu, Mia Schulz, Rafael Costa
15:40 refused Priya Raman Sequencing Lab room full
15:45 site the service is done: the Sequencing Lab takes 5 again
15:45 cleared over capacity Sequencing Lab
18:00 ALARM after closing BSL-3 Lab Sara Lindgren, Yusuf Demir
18:10 ALARM overstay BSL-3 Lab Sara Lindgren, Yusuf Demir
18:20 cleared after closing BSL-3 Lab
18:20 cleared overstay BSL-3 Lab
134 swipes; refused: room full 5, badge not valid 2, no access 2, training missing 2, barred from the zone 1, outside hours 1
Alarms:
11:00-11:45 unescorted visitor Seminar Room
12:35-12:40 overstay BSL-3 Lab
12:40-12:50 alone BSL-3 Lab
13:00-13:30 unstaffed Vivarium Control
15:00-15:45 over capacity Sequencing Lab
18:00-18:20 after closing BSL-3 Lab
18:10-18:20 overstay BSL-3 Lab
Where everyone was at 11:40, from the log:
BSL-3 Lab Ada Okafor, Dalia Haddad, Joon Park, Nadia Petrova
Genomics Office Bruno Lindqvist, Gustavo Pérez, Sofia Marín, Tomás Novak
Seminar Room Ines Moreau, Kemi Adeyemi, Sara Lindgren, Yusuf Demir
Sequencing Lab Aiko Sato, Chen Wei, Emeka Nwosu, Mia Schulz, Rafael Costa
Vivarium Control Fatima Rossi, Olga Ivanova
Still inside at 19:00:
Vivarium Control Pedro Alves
The output lists what needed attention, in time order:
the swipes a door refused, with the rule that refused them;
the alarms the monitor raised and cleared;
the changes the facilities team made to the site.
The end of the day gives the totals, the alarm history and two views of where people were. The first view is rebuilt from the log alone, for a moment in the past; the second is the positions the controllers keep current.
Options:
--database DIRkeeps the database inDIR, which must be new, for you to query afterwards;--every MINUTESchanges how often the monitor checks;--verbosealso prints every door that opened.
The pieces¶
File |
What it is |
|---|---|
|
the building: zones with their closing times, rooms with their rules, teams, team grants |
|
the people: employees with their teams, badges, certifications and bans; contractors; visitors and their escorts |
|
loads both files into an empty database |
|
|
|
the checks, the alarms, and |
|
the day as data: each badge’s itinerary, and the site operations |
|
runs the day and prints what happened |
Loading the site¶
The files are plain JSON: names, numbers, and times and dates as ISO 8601 text. Each kind of thing is one query over a list parameter, so the load is a dozen transactions however many people the files hold. The rooms, for example:
# Rooms, each with its door and the certifications it requires.
"""
UNWIND $site.rooms AS room
MATCH (z:Zone {name: room.zone})
CREATE (r:Room {name: room.name, bsl: room.bsl})-[:IN]->(z),
(:Door {id: 'D-' + room.name})-[:LEADS_TO]->(r)
SET r.min_occupancy = room.min_occupancy, r.max_occupancy = room.max_occupancy,
r.staffed = room.staffed
""",
SET with a missing value (room.min_occupancy for a room without one) sets nothing, so one
query serves rooms with and without each rule. Times and dates become temporal values inside the
queries: localtime(grant.from), date(c.valid_until).
The door controller¶
The decision is the one built in step 18, with one change: it
says why a swipe was refused. Each rule is a column, and refusal names the first one broken.
The team grants are gathered once, as a list, so the same list answers both “may they ever enter
this zone?” and “may they enter it now?”. That is what separates no access from outside hours.
ENTER = """
WITH $at AS at, localtime($at) AS clock, date($at) AS today
MATCH (b:Badge {number: $badge})-[:ISSUED_TO]->(p:Person),
(d:Door {id: $door})-[:LEADS_TO]->(r:Room)-[:IN]->(z:Zone)
WITH b, d, p, r, z, at, clock, today,
[(p)-[:MEMBER_OF]->(:Team)-[:PART_OF*0..]->(:Team)-[g:MAY_ENTER]->(z) | g]
+ [(p)-[:MANAGES]->(:Team)<-[:PART_OF*0..]-(:Team)-[g:MAY_ENTER]->(z) | g] AS grants,
coalesce(z.public, false) OR EXISTS { (p)-[:MAY_ENTER]->(r) } AS always_open
WITH b, d, p, r, at,
b.active AND (b.valid_until IS NULL OR b.valid_until >= today) AS badge_ok,
EXISTS { (p)-[:DENIED]->(z) } AS barred,
always_open OR size(grants) > 0 AS reaches_zone,
always_open OR any(g IN grants WHERE g.days IS NULL
OR (at.dayOfWeek IN g.days AND g.from <= clock < g.until)) AS in_hours,
EXISTS { (r)-[:REQUIRES]->(c:Certification)
WHERE NOT EXISTS { (p)-[held:CERTIFIED]->(c) WHERE held.expires >= today } } AS untrained,
z.closes_at IS NOT NULL AND clock >= z.closes_at AS closed,
r.max_occupancy IS NOT NULL
AND size([(other:Person)-[:IS_IN]->(r) WHERE other <> p | other]) >= r.max_occupancy AS full
WITH b, d, p, r, at,
CASE
WHEN NOT badge_ok THEN 'badge not valid'
WHEN barred THEN 'barred from the zone'
WHEN NOT reaches_zone THEN 'no access'
WHEN NOT in_hours THEN 'outside hours'
WHEN untrained THEN 'training missing'
WHEN closed THEN 'zone closed'
WHEN full THEN 'room full'
END AS refusal
CREATE (s:Swipe {at: at, direction: 'in', granted: refusal IS NULL, refusal: refusal}),
(s)-[:BY]->(b), (s)-[:AT]->(d)
WITH p, r, at WHERE refusal IS NULL
OPTIONAL MATCH (p)-[previous:IS_IN]->()
DELETE previous
CREATE (p)-[:IS_IN {since: at}]->(r)
RETURN r.name
"""
The refusal is stored on the swipe, so the security office can count refusals by reason (the
“refused” line at the end of the output). The query creates the swipe for every row, and only
continues to the writes that move the person when the door opens. A refused swipe therefore ends
the query with no rows, and DoorController.enter then reads the reason back from the log.
The monitor, in its own process¶
The checks are those of step 19. check(db, at) runs them and
returns what changed: the alarms raised in this run, and the ones cleared. serve is the
monitor’s process. It opens the database itself and runs a check for each time it receives:
def serve(path: str, times: multiprocessing.Queue, reports: multiprocessing.Queue) -> None:
"""Checks the database at `path` at each time that arrives on `times`, until None does.
The process opens the database itself: the controllers and the monitor
share the file, not a Python object.
"""
with zypherdb.Database(path, durability="deferred") as db:
while (at := times.get()) is not None:
reports.put(check(db, at))
The simulation starts it with multiprocessing (spawn, so the child shares no state with the
parent) and sends it the simulated time:
sequenceDiagram
participant R as readers (main process)
participant D as database directory
participant M as monitor (second process)
R->>D: ENTER / LEAVE, one transaction per swipe
R->>M: check at 12:40
M->>D: the six checks (read)
M->>D: RAISE / CLEAR (write)
M-->>R: report: raised alone, cleared overstay
R->>D: ENTER / LEAVE …
The controllers’ writes and the monitor’s take turns: the storage engine lets one write transaction run at a time, across processes. The monitor’s reads see a consistent snapshot, never half of a swipe. In production the two would run independently on the clock. Here the readers wait for each check, so every run prints the same day.
The database is opened with durability="deferred", because a simulation does not need each
commit on disk before going on. A real controller would keep the default, "full".
The day¶
day.py gives each badge an itinerary: the times its holder goes into a room, and when they go
home. The doors may refuse; a refused person stays where they were and goes on with their day.
Comments mark the moments that test a rule:
# Virology: the BSL-3 lab takes two to four people, for at most four hours.
70004: "08:15 Lobby, 08:25 Airlock, 08:30 BSL-3 Lab, 12:40 Seminar Room, "
"13:30 Airlock, 13:35 BSL-3 Lab, 16:30 home", # 12:35: in for over four hours
70008: "08:15 Lobby, 08:25 Airlock, 08:30 BSL-3 Lab, 12:40 Seminar Room, "
"13:30 Airlock, 13:35 BSL-3 Lab, 16:30 home",
70009: "08:15 Lobby, 08:25 Airlock, 08:30 BSL-3 Lab, 09:00 Seminar Room, 12:00 home", # not certified
70018: "08:50 Lobby, 09:00 Airlock, 09:05 BSL-3 Lab, 12:50 Seminar Room, "
"14:00 Airlock, 14:10 BSL-3 Lab, 14:15 home", # 12:40: left alone; 14:10: the lab is full
70019: "08:50 Lobby, 09:00 Airlock, 09:05 BSL-3 Lab, 11:30 home",
70020: "10:00 Lobby, 10:05 Airlock, 10:10 BSL-3 Lab, 10:15 Seminar Room, "
"14:00 Airlock, 14:05 BSL-3 Lab, 18:20 home", # 10:10: full; 18:00: past closing
70021: "10:00 Lobby, 10:05 Airlock, 10:10 BSL-3 Lab, 10:15 Seminar Room, "
"14:00 Airlock, 14:05 BSL-3 Lab, 18:20 home",
Reading the output against it:
08:30 Kemi has no BSL-3 certification: training missing.
10:10 Dalia, Joon, Nadia and Luca are in the BSL-3 lab, which takes four: Sara and Yusuf are refused, room full, and come back in the afternoon.
12:35 Dalia and Joon have been inside since 08:30: overstay.
12:40 they come out, which clears the overstay, and leaves Nadia alone: alone. Nothing at the door can prevent this, because a door always lets people out.
18:00 the suite closes with Sara and Yusuf inside: after closing. Ten minutes later they pass four hours too: overstay.
The other teams’ days test the remaining rules:
Emeka arrives before the genomics floor opens: outside hours.
Lars, on the night shift, is not trained for the animal rooms.
The head of research manages the research teams, which reaches every zone they may enter, but a ban on the containment suite overrides that.
Ada leaves her visitor alone in the seminar room.
Fatima and Olga leave the vivarium control room empty for half an hour.
The facilities team lowers the sequencing lab’s limit with five people inside.
Exploring the result¶
With --database, the day stays on disk to be queried:
# docs-test: skip
import zypherdb
with zypherdb.Database("helix-day") as db:
print(db.query("""
MATCH (s:Swipe)-[:BY]->(:Badge)-[:ISSUED_TO]->(p:Person {name: 'Nadia Petrova'}),
(s)-[:AT]->(d:Door)
RETURN s.at, d.id, s.direction, s.refusal ORDER BY s.at
"""))
Things to try:
change an itinerary in
day.py, or a rule insite.json, and run the day again;set the BSL-3 lab’s
min_occupancyto 1 and watch the alone alarm disappear;add the Sequencing Lab to Hiro Tanaka’s rooms in
staff.json, and the 10:40 refusal becomes the lab’s to decide by its occupancy.