Tokens¶
Immutable values that flow through the net, plus the sentinels and frozen mapping used to describe them.
cpnx.Token
dataclass
¶
An immutable, hashable-by-identity token that flows through a Petri net.
In Coloured Petri Net (CPN) theory, a token is a value drawn from its
place's colour set. Here, color is that colour: None for uncoloured
data tokens, "resource" for permit/resource tokens, or any user-defined
string for domain-specific colours. Being a frozen dataclass, a Token is
never mutated in place; state changes are made by creating a new instance
via evolve.
Attributes:
| Name | Type | Description |
|---|---|---|
id |
str
|
Unique identifier (16-char hex string). Auto-generated from |
payload |
FrozenDict
|
Immutable dict accumulating enrichment data as the token traverses
the net. Coerced to a |
created_at |
float
|
Monotonic timestamp (from |
color |
str | None
|
CPN colour. |
available_at |
float
|
Monotonic timestamp after which the token is available (timed CPNs).
Defaults to |
attempts |
int
|
Number of failed firings this token has been rolled back from. |
Source code in src/cpnx/tokens.py
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 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 | |
is_resource
property
¶
Report whether this token's colour is "resource".
Python-friendly shorthand for token.color == "resource".
Returns:
| Type | Description |
|---|---|
bool
|
|
evolve
¶
Create a new Token by merging payload updates and overriding fields.
Since Token is immutable, this is the standard way to derive an updated
token as it moves through the net. A fresh id is generated for the new
token unless id is explicitly passed in field_updates.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
payload_updates
|
dict[str, Any] | None
|
Optional dict merged on top of the existing |
None
|
**field_updates
|
Any
|
Additional dataclass field values (e.g. |
{}
|
Returns:
| Type | Description |
|---|---|
Token
|
A new |
Token
|
the original token is left unchanged. |
Example
Source code in src/cpnx/tokens.py
cpnx.FrozenDict
¶
Bases: Mapping
Immutable, hashable mapping that recursively freezes nested dicts and lists.
Implements collections.abc.Mapping over an internal types.MappingProxyType,
so instances support read-only dict-like access (d[key], len(d), iteration)
but no in-place mutation. Nested dict/Mapping values are recursively wrapped
in FrozenDict, and nested list values are recursively converted to tuple
(with any FrozenDict/Mapping items inside also frozen). The hash is computed
eagerly at construction time from frozenset(self._data.items()), so all values
(including nested ones) must be hashable.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
Mapping | None
|
Optional mapping to freeze. Entries are copied and recursively frozen. |
None
|
**kwargs
|
Any
|
Additional key/value pairs to include, applied after |
{}
|
Raises:
| Type | Description |
|---|---|
TypeError
|
If any value (at any nesting depth) is unhashable, since the hash is computed eagerly during construction. |
Source code in src/cpnx/tokens.py
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 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 | |
as_dict
¶
Recursively convert this FrozenDict back to a plain, mutable dict.
Nested FrozenDict values are recursively converted to dict, and tuple
values (frozen from list) are converted back to list, with any nested
FrozenDict items also unwrapped. Useful for JSON serialisation.
Returns:
| Type | Description |
|---|---|
dict
|
A new plain |
Example
Source code in src/cpnx/tokens.py
set
¶
Return a new FrozenDict with key set to value, leaving this instance unchanged.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
Any
|
The key to set (or overwrite) in the returned copy. |
required |
value
|
Any
|
The value to associate with |
required |
Returns:
| Type | Description |
|---|---|
FrozenDict
|
A new |
FrozenDict
|
given |
Example
Source code in src/cpnx/tokens.py
cpnx.AVAILABLE_NOW
module-attribute
¶
Sentinel value for Token.available_at meaning "available immediately".
Used as the default for available_at so that untimed tokens are eligible for firing
as soon as they are created, rather than after some future monotonic timestamp.
cpnx.ERROR_COLOR
module-attribute
¶
Reserved token colour conventionally used to mark dead-lettered/error tokens.
Not enforced by Token itself; consumers (e.g. error-handling
transitions or sink places) may use this string as the color value for tokens
that represent a failure, so they can be routed or filtered distinctly.