Visualization¶
Introspect a running net or export it to Graphviz DOT.
cpnx.snapshot
¶
Capture a JSON-serialisable snapshot of a PetriNet's current marking.
Acquires the net's internal lock while reading, so the snapshot reflects a
consistent point-in-time view. For each place, records each token's id,
payload (as a plain dict), created_at, and color. For a
SinkPlace, the place's entry is instead a dict with a
"tokens" list (from its ring buffer, if any) and an "absorbed" count
(the cumulative number of tokens ever absorbed).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
net
|
PetriNet
|
The |
required |
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
A dict with two keys: |
dict[str, Any]
|
list of token dicts, or (for sink places) a dict with |
dict[str, Any]
|
|
dict[str, Any]
|
mid-firing. |
Source code in src/cpnx/visualization.py
cpnx.to_dot
¶
Render a PetriNet's structure and current token counts as Graphviz DOT.
The rendering is built around a token's valid paths, so places carry a count
only when they actually hold tokens (a bare name otherwise). Transitions are boxes;
places are circles, with these role-distinct renderings — several because their tokens
arrive or leave off-arc and would otherwise mislead:
- a resource pool (
ResourcePlace/PacedResourcePlace) is a double circle filled#dbe9ff— seeded with permits at construction (the CPN initial marking) and cycling them via resource arcs; - a sink / terminal (
SinkPlace) is a radial-gradient well (light centre, dark rim) — an inset-shadow "inverted cone" a token falls into and is absorbed, so it does not read as an ordinary holding place; - the net's error place is the same well tinted salmon — it is fed off-arc by the engine's dead-lettering, not by a customer, so it is a drain, not a source;
- an external source — a place no transition produces into (arc in-degree 0,
excluding pools, sinks, and the error place) — has a dashed border; its tokens
can only come from an external
deposit(...)(or an initial marking).
Each input arc is an edge from its place to the transition, labelled with its count
and, when applicable, consume_all / settle=<settle_secs>s; each output arc an edge
to its place. Two off-arc token paths are drawn dashed so no place looks isolated
and no path is hidden: a dead-letter side channel (pale red, thin, unlabelled —
named once in the legend; drawn constraint=true, weight=0 so it ranks the error place
downstream of its feeders and fans in cleanly, yet exerts no pull that would warp the main
flow) from every transition with a finite max_retries to the error place — the engine's
failure routing; and a return (implicit) edge from any transition that borrows a
resource permit without a matching output arc back to its pool (i.e. relying on the
engine's implicit leftover-return rather than a structural self-loop).
Terminal places — those with incoming tokens but no outgoing arc (nothing consumes
from them: a SinkPlace, or the dead-letter bin whose only edges are the off-arc,
rank-less dead-letter channels) — are pinned to the sink rank via { rank=sink; ... }, so
the flow reads left-to-right and endpoints land at the far right instead of floating at
rank 0. Inferred from the arc structure, so no place name is special-cased.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
net
|
PetriNet
|
The |
required |
highlight_impact_from
|
str | None
|
If given, the name of a transition whose forward
colour-domain blast radius (see |
None
|
Returns:
| Type | Description |
|---|---|
str
|
A string containing the full |
str
|
suitable for rendering with Graphviz (e.g. |
Raises:
| Type | Description |
|---|---|
KeyError
|
if |
Source code in src/cpnx/visualization.py
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 | |
Highlighting a blast radius¶
Both to_dot and PetriNet.to_dot accept an optional
highlight_impact_from=<transition name> argument. When given, the transition's forward
colour-domain blast radius (see trace_impact) is shaded into the
rendering:
| Element | Fill | Meaning |
|---|---|---|
| Impacted places and transitions | #ffd9d9 (light red) |
reachable within the traced colour domain |
The seed transition (origin) |
#ff8080 (heavier red) + thicker border |
the transition the trace started from |
highlight_impact_from=None (the default) leaves the rendering exactly as it was — no styling
is added. See ADR 0008
for the underlying impact-analysis model.