Draw your model
Goal: see how a model is put together and how its processes interact, before (or instead of) reading all of its code.
cimba.diagrams draws two complementary pictures of a configured model.
Neither compiles nor runs anything, and both work before you’ve bound input
data, so you can draw a model while you’re still building it.
The object tree: which model owns which (solid arrows) and which references which (dotted arrows). |
|
The interactions: which process puts into or gets from which store, acquires which resource, reads which input, signals which condition and spawns which model. |
Both return a Graph that renders to
Mermaid or Graphviz
DOT.
Process graphs
from cimba import diagrams
from tutorial.tut_1_1 import MM1
graph = diagrams.process_graph(MM1())
print(graph.to_mermaid())
flowchart TD
subgraph g0["mm1: MM1"]
n0[/"interarrival · exponential"/]
n1[/"service_time · exponential"/]
n2["queue · Container"]
n3(["arrival"])
n4(["service"])
end
n0 -->|next| n3
n3 -->|put| n2
n2 -->|get| n4
n1 -->|next| n4
Rendered:
---
config:
layout: elk
---
flowchart LR
subgraph g0["mm1: MM1"]
n0[/"interarrival · exponential"/]
n1[/"service_time · exponential"/]
n2["queue · Container"]
n3(["arrival"])
n4(["service"])
end
n0 -->|next| n3
n3 -->|put| n2
n2 -->|get| n4
n1 -->|next| n4
How to read it:
Shape |
Meaning |
|---|---|
rounded box |
a process ( |
rectangle |
an entity, with its type: |
parallelogram |
an input, with its bound source: |
hexagon |
an |
double-bordered box |
|
boxes around nodes |
one per model instance ( |
Edges point the way things flow. A process puts into a store; the store feeds the process that gets from it; a resource feeds the process that acquires it, and the process releases it back; an input feeds the process that calls next/now/at; a condition feeds the process that waits, and a process signals it; a process spawns a model and schedules an event; any process records into a dataset.
A bigger example
The harbor from tutorial chapter 4: ships are spawned by the traffic model
and, through their harbor reference, compete for the facilities.
---
config:
layout: elk
---
flowchart LR
subgraph g1["harbor.sea: SeaConditions"]
n0[/"wind_source · rayleigh"/]
n1[/"direction_source · pert"/]
n2(["weather"])
n3(["tide"])
end
subgraph g2["harbor.facilities: HarborFacilities"]
n4["harbormaster · Condition"]
n5["tugs · Resource"]
n6["berths_small · Resource"]
n7["berths_large · Resource"]
n8["comms · Resource"]
end
subgraph g3["harbor.traffic: ShipTraffic"]
n9[/"arrival_gap · exponential"/]
n10[/"radio_delay · gamma"/]
n11[/"movement · pert"/]
n12[/"unload_small · pert"/]
n13[/"unload_large · pert"/]
n14["departed · Store[Ship]"]
n15["time_small · Dataset"]
n16["time_large · Dataset"]
n17(["arrivals"])
n18(["departures"])
end
subgraph g4["Ship (spawned)"]
n19[["new Ship"]]
n20(["voyage"])
end
n0 -->|next| n2
n1 -->|next| n2
n3 -->|signal| n4
n9 -->|next| n17
n17 -->|spawn| n19
n14 -->|get| n18
n4 -->|wait| n20
n7 -->|acquire| n20
n6 -->|acquire| n20
n5 -->|acquire| n20
n8 -->|acquire| n20
n10 -->|next| n20
n20 -->|release| n8
n11 -->|next| n20
n20 -->|release| n5
n20 -->|signal| n4
n13 -->|next| n20
n12 -->|next| n20
n20 -->|release| n7
n20 -->|release| n6
n20 -->|record| n16
n20 -->|record| n15
n20 -->|put| n14
Options
process_graph(model, fields=True)Also draw writes to plain,
ParamandOutputfields, as cylinders. Useful to spot a field written from more places than expected.process_graph(model, hooks=True)Include
on_start/on_endhooks as actors.graph.to_mermaid(direction="LR")/graph.to_dot(rankdir="LR")Choose the layout direction.
The structure graph
print(diagrams.structure(model).to_mermaid())
# diagrams.mermaid(model) is a shortcut for the same text
---
config:
layout: elk
---
flowchart LR
n0["assemblyline: AssemblyLine"]
n1["assemblyline.finished_parts: FinishedParts"]
n2["assemblyline.station_1: Station"]
n3["assemblyline.station_2: Station"]
n4["assemblyline.station_3: Station"]
n0 -->|finished_parts| n1
n0 -->|station_1| n2
n0 -->|station_2| n3
n0 -->|station_3| n4
n1 -.->|line| n0
n2 -.->|downstream| n3
n3 -.->|downstream| n4
n4 -.->|finished| n1
Rendering
Documentation and Markdown. GitHub, GitLab, Notion, Obsidian and many wikis render fenced code blocks marked
mermaiddirectly. Sphinx sites can use sphinxcontrib-mermaid (these docs do).Notebooks. Paste into mermaid.live, or render DOT with the
graphvizPython package:graphviz.Source(graph.to_dot()).Files.
Path("model.mmd").write_text(graph.to_mermaid()), ordot -Tsvg model.dot -o model.svgfor Graphviz.
Working with the graph
The graph is plain data: graph.nodes (each with key, label,
kind and group), graph.edges (source, target, label,
style) and graph.groups. Keys are canonical paths such as
harbor.facilities.tugs, or Ship.voyage for a spawned model’s process.
That makes graphs easy to check in tests:
graph = diagrams.process_graph(Harbor())
edges = {(e.source, e.label, e.target) for e in graph.edges}
assert ("harbor.facilities.tugs", "acquire", "Ship.voyage") in edges
graph.topological_order() returns node keys in flow order, or raises
ValueError if the graph has a cycle. Many real models do: a process that
signals a condition it also waits on is an intentional cycle.
Review questions
A process graph is a good agenda for a model review:
Which processes create work, and which consume it? Is every store both filled and emptied?
Which entities are coordination points shared by several processes?
Does every condition that someone waits on also get signalled?
Are spawned models connected to the static models they need, and are they released somewhere?
Is every input read by the process you expect, and bound to the source you expect?
How it works, and its limits
The graph is inferred from the source code of each process, event and
(optionally) hook, resolved against the configured objects. Attribute
chains like self.harbor.facilities.tugs are followed through children,
references and list items. The analysis also follows:
local aliases (
berths = facilities.berths_small);list indices: a literal index hits one item, a computed index hits every item that could be meant;
helper functions called with the model (plain or
numba.njit);the keyword arguments of
cb.spawn, which connect a spawned model’s references and inputs to the objects the spawner passed in;models taken out of typed stores (
part = self.inbox.get()).
It is a map for humans, not a proof. It can’t see through process handles
stored as integers (cb.Process(pointer).interrupt()), values computed at
run time, or code whose source isn’t available. When in doubt, it leaves an
edge out rather than inventing one.