Places¶
Places hold tokens. Beyond the plain Place, several variants model resource
pools, pacing, thresholds, and sinks.
cpnx.Place
¶
An unbounded FIFO queue that holds tokens flowing through a Petri net.
CPN equivalent: a place with an unrestricted colour set (accepts any
colour) and no initial marking. Set color_set to restrict accepted
colours; set initial_marking to pre-fill with tokens at construction.
All operations are thread-safe via an internal threading.Lock.
Source code in src/cpnx/places.py
23 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 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 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 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 | |
tokens
property
¶
Snapshot of all current tokens (including not-yet-available ones) as an immutable tuple.
Does not filter by available_at and does not consume or remove any tokens.
__init__
¶
__init__(
name: str,
bound: int | None = None,
color_set: set[str] | None = None,
initial_marking: list[Token] | None = None,
) -> None
Create a new Place.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Unique identifier for this place within a |
required |
bound
|
int | None
|
Optional k-bound (capacity constraint). The engine will not
fire a transition whose unguarded output arc targets this place
if doing so would exceed the bound. |
None
|
color_set
|
set[str] | None
|
Set of accepted token colours. |
None
|
initial_marking
|
list[Token] | None
|
Tokens to deposit at construction (CPN I function). Deposited before any external code runs. |
None
|
Source code in src/cpnx/places.py
deposit
¶
Append token to the tail of the FIFO queue, enforcing the place's colour set.
Updates last_deposit_time (and last_deposit_time_model if model_time is given),
then calls _on_deposit.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
token
|
Token
|
The token to deposit. |
required |
model_time
|
float | None
|
Optional logical clock timestamp recorded alongside the deposit. Does not affect wall-clock availability checks. |
None
|
Raises:
| Type | Description |
|---|---|
TypeError
|
If |
Source code in src/cpnx/places.py
retrieve
¶
Remove and return count tokens from the head of the queue, in FIFO order.
A token is only eligible if its available_at timestamp is at or before the
effective time (model_time if given, else time.monotonic()); tokens still
in the future (e.g. cooling down) are skipped.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
count
|
int
|
Number of tokens to retrieve. Must be >= 1. |
1
|
model_time
|
float | None
|
Optional logical clock timestamp used instead of wall-clock time to determine which tokens are available. |
None
|
Returns:
| Type | Description |
|---|---|
list[Token]
|
List of retrieved tokens in FIFO order. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If fewer than count tokens are available. |
Source code in src/cpnx/places.py
retrieve_specific
¶
Remove and return exactly the given tokens, matched by id rather than FIFO order.
Used by the engine when an InputArc has an expression that
selects a specific subset of tokens to consume rather than the head of the queue.
Rebuilds the internal deque in O(n) rather than removing tokens one at a time.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tokens
|
list[Token]
|
Tokens to remove, identified by their |
required |
model_time
|
float | None
|
Optional logical clock timestamp used instead of wall-clock time to check token availability. |
None
|
Returns:
| Type | Description |
|---|---|
list[Token]
|
The removed tokens, in the same order as tokens (not necessarily FIFO order). |
Raises:
| Type | Description |
|---|---|
ValueError
|
If any token in tokens is not yet available (its |
Source code in src/cpnx/places.py
retrieve_all
¶
Remove and return every currently-available token, leaving not-yet-available tokens behind.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model_time
|
float | None
|
Optional logical clock timestamp used instead of wall-clock time to determine which tokens are available. |
None
|
Returns:
| Type | Description |
|---|---|
list[Token]
|
All available tokens in FIFO order; empty list if none are available. |
Source code in src/cpnx/places.py
peek
¶
Return up to count available tokens from the head without removing them.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
count
|
int
|
Maximum number of tokens to inspect. |
1
|
model_time
|
float | None
|
Optional logical clock timestamp used instead of wall-clock time to determine which tokens are available. |
None
|
Returns:
| Type | Description |
|---|---|
list[Token]
|
List of up to count tokens; may be shorter than requested if fewer |
list[Token]
|
are present or available. Does not modify the queue. |
Source code in src/cpnx/places.py
can_retrieve
¶
Return True if at least count tokens are currently available for retrieval.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
count
|
int
|
Number of tokens needed. Defaults to 1. |
1
|
model_time
|
float | None
|
Optional logical clock timestamp used instead of wall-clock time to determine which tokens are available. |
None
|
Returns:
| Type | Description |
|---|---|
bool
|
|
bool
|
effective time, |
Source code in src/cpnx/places.py
can_deposit
¶
Return True if the place can accept count more tokens without exceeding its bound.
Implements k-bounded place semantics: a place with bound=k blocks when
depositing would push the token count above k. Unbounded places
(bound=None) always return True. Ignores colour — use can_accept
to check colour compatibility.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
count
|
int
|
Number of tokens to be deposited. Defaults to 1. |
1
|
Returns:
| Type | Description |
|---|---|
bool
|
|
Source code in src/cpnx/places.py
can_accept
¶
Return True if token's colour is compatible with this place's colour set.
This is a non-mutating pre-flight check that does not modify the place's tokens
and does not consider capacity — use can_deposit for bound checks.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
token
|
Token
|
The token to check for colour compatibility. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
|
Source code in src/cpnx/places.py
__len__
¶
cpnx.ResourcePlace
¶
Bases: Place
A Place pre-filled with capacity resource tokens, for modelling finite permits.
CPN equivalent: Place(color_set={"resource"}, initial_marking=[Token(color="resource")] * capacity).
This class is a Python shorthand — it sets the colour set and initial marking
automatically and documents the resource-return invariant explicitly. It does not
otherwise change Place's behavior: all inherited methods (deposit,
retrieve, etc.) work exactly as on the base class.
Resource tokens (color="resource") are consumed when a transition fires
and must be returned via a matching output arc. This models finite resources
such as GPU slots, database connections, or thread-pool permits.
Source code in src/cpnx/places.py
__init__
¶
Create a ResourcePlace pre-filled with capacity resource tokens.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Unique identifier for this place within a |
required |
capacity
|
int
|
Number of resource permits in the pool. |
required |
Source code in src/cpnx/places.py
cpnx.PacedResourcePlace
¶
Bases: ResourcePlace
A ResourcePlace where returned tokens must cool down before becoming reusable.
CPN equivalent: a Timed CPN ResourcePlace where returned tokens
carry a timestamp that prevents re-use until pacing_secs have elapsed.
This is a pragmatic extension — standard Timed CPNs put timestamps on tokens,
not cooldown windows on places.
Useful for enforcing API rate limits or minimum inter-request intervals.
Tokens are available immediately at construction; after each return via
deposit, they are unavailable for pacing_secs seconds.
Example — 10 Serper requests per second:
Source code in src/cpnx/places.py
311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 | |
__init__
¶
Create a PacedResourcePlace.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Unique identifier for this place within a |
required |
capacity
|
int
|
Number of resource permits in the pool. |
required |
pacing_secs
|
float
|
Seconds a token must wait after being returned before it becomes available again. |
required |
Source code in src/cpnx/places.py
deposit
¶
Return a resource token to the pool, replacing its available_at to start a cooldown timer.
Differs from Place.deposit: instead of appending token unchanged,
this creates a copy of token with available_at set to the effective time plus
pacing_secs, so the token cannot be retrieved again until the cooldown elapses.
Does not validate color_set (unlike the base class).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
token
|
Token
|
The resource token being returned. Must have |
required |
model_time
|
float | None
|
Optional logical clock timestamp used instead of wall-clock time
as the cooldown's start reference, and recorded in
|
None
|
Source code in src/cpnx/places.py
can_retrieve
¶
Return True if at least count tokens have completed their cooldown and are usable.
Behaves identically to Place.can_retrieve; documented
separately here because "available" specifically means "cooldown has expired"
for this class.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
count
|
int
|
Number of cooled-down tokens needed. Defaults to 1. |
1
|
model_time
|
float | None
|
Optional logical clock timestamp used instead of wall-clock time to determine which tokens have finished cooling down. |
None
|
Returns:
| Type | Description |
|---|---|
bool
|
|
Source code in src/cpnx/places.py
retrieve
¶
Remove and return count tokens whose cooldown has expired, in expiry order.
Behaves like Place.retrieve but the error message reports
how many tokens are still cooling down, which is specific to this class's semantics.
Uses an O(n) rebuild rather than O(n^2) indexed deletion.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
count
|
int
|
Number of cooled-down tokens to retrieve. Defaults to 1. |
1
|
model_time
|
float | None
|
Optional logical clock timestamp used instead of wall-clock time to determine which tokens have finished cooling down. |
None
|
Returns:
| Type | Description |
|---|---|
list[Token]
|
List of retrieved resource tokens in cooldown-expiry order. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If fewer than count tokens are past their cooldown, with a message indicating how many are ready vs still cooling down. |
Source code in src/cpnx/places.py
peek
¶
Return up to count cooled-down tokens without removing them.
Behaves identically to Place.peek; "available" specifically
means "cooldown has expired" for this class.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
count
|
int
|
Maximum number of tokens to inspect. Defaults to 1. |
1
|
model_time
|
float | None
|
Optional logical clock timestamp used instead of wall-clock time to determine which tokens have finished cooling down. |
None
|
Returns:
| Type | Description |
|---|---|
list[Token]
|
List of up to count cooled-down tokens; may be shorter than requested. |
list[Token]
|
Does not modify the pool. |
Source code in src/cpnx/places.py
cpnx.ThresholdPlace
¶
Bases: Place
A Place where tokens are only retrievable once the queue depth reaches threshold.
CPN equivalent: a plain Place whose associated transition has a
guard requiring |M(p)| >= threshold before firing. This class is a Python
shorthand that encodes the threshold directly on the place rather than
duplicating it in every downstream transition's guard.
Useful for batch processing: tokens accumulate until enough are present,
then they are released in groups matching the transition's arc.count.
deposit and peek are inherited unchanged from Place.
Example — convene a committee once 6 validated leads are ready:
Source code in src/cpnx/places.py
441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 | |
__init__
¶
Create a ThresholdPlace.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Unique identifier for this place within a |
required |
threshold
|
int
|
Minimum queue depth required before any retrieval is permitted. Must be >= 1. |
required |
Source code in src/cpnx/places.py
can_retrieve
¶
Return True only if the batch threshold is met AND at least count tokens are present.
Differs from Place.can_retrieve: adds a gating condition
on top of the plain count check — the queue must have reached threshold regardless
of count, and separately contain at least count available tokens (count may
exceed threshold).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
count
|
int
|
Number of tokens needed by the requesting transition arc. Defaults to 1. |
1
|
model_time
|
float | None
|
Optional logical clock timestamp used instead of wall-clock time to determine which tokens are available. |
None
|
Returns:
| Type | Description |
|---|---|
bool
|
|
Source code in src/cpnx/places.py
retrieve
¶
Remove and return count tokens from the head of the queue, but only if the threshold is met.
Differs from Place.retrieve: first checks that the queue
has reached threshold available tokens (raising if not) before applying the
usual count check, gating retrieval behind the batch threshold.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
count
|
int
|
Number of tokens to retrieve. Defaults to 1. |
1
|
model_time
|
float | None
|
Optional logical clock timestamp used instead of wall-clock time to determine which tokens are available. |
None
|
Returns:
| Type | Description |
|---|---|
list[Token]
|
List of retrieved tokens in FIFO order. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the threshold is not yet met, with a message showing current depth vs required threshold. |
ValueError
|
If the threshold is met but fewer than count tokens are available. |
Source code in src/cpnx/places.py
retrieve_all
¶
Remove and return every available token, but only if the threshold has been met.
Differs from Place.retrieve_all: raises instead of
returning an empty list when fewer than threshold tokens are available.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model_time
|
float | None
|
Optional logical clock timestamp used instead of wall-clock time to determine which tokens are available. |
None
|
Returns:
| Type | Description |
|---|---|
list[Token]
|
All available tokens in FIFO order. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the threshold is not yet met, with a message showing current depth vs required threshold. |
Source code in src/cpnx/places.py
cpnx.SinkPlace
¶
Bases: Place
A terminal place that counts and optionally samples tokens but never retains them for retrieval.
Deposited tokens are absorbed: their colour and cumulative counts are recorded, and up
to keep_last of the most recent tokens are kept in a ring buffer purely for inspection
via tokens/stats/drain_stats — but they can never be consumed onward by a transition.
Useful for streaming pipelines (e.g. logging sinks, dead-letter/error places) to avoid
accumulating memory indefinitely while still exposing aggregate statistics.
Warning
Avoid setting a restrictive color_set if this place is used as an error_place.
Dead-lettered tokens preserve their original colours, and depositing a rejected
colour will raise a TypeError inside the locked transition failure branch,
causing the token to be lost rather than successfully dead-lettered.
Source code in src/cpnx/places.py
560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 | |
__init__
¶
Create a new SinkPlace.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Unique identifier for this place within a |
required |
keep_last
|
int
|
Number of most recent tokens to keep in a ring buffer for inspection. Default is 0 (retain nothing beyond the aggregate counters). |
0
|
color_set
|
set[str] | None
|
Set of accepted token colours. |
None
|
Source code in src/cpnx/places.py
deposit
¶
Absorb token: append it to the ring buffer and update cumulative counters and timestamps.
Differs from Place.deposit: the internal deque has
maxlen=keep_last, so once full, appending silently evicts the oldest kept
token — this is a sampling buffer, not the full token history. Also increments
the _absorbed count and the per-colour tally, and records _first_deposit_time
on the very first deposit.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
token
|
Token
|
The token to absorb. |
required |
model_time
|
float | None
|
Optional logical clock timestamp recorded in |
None
|
Raises:
| Type | Description |
|---|---|
TypeError
|
If |
Source code in src/cpnx/places.py
can_retrieve
¶
Always return False: a sink is a terminal place, so nothing is ever retrievable.
Differs from Place.can_retrieve: arriving tokens are
absorbed for inspection/counting only and can never be consumed onward by a
transition, so this unconditionally reports nothing is retrievable. count and
model_time are accepted for interface compatibility but ignored.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
count
|
int
|
Ignored. |
1
|
model_time
|
float | None
|
Ignored. |
None
|
Returns:
| Type | Description |
|---|---|
bool
|
|
Source code in src/cpnx/places.py
retrieve
¶
Always raise: a sink is terminal, so tokens cannot be retrieved by any means.
Differs from Place.retrieve: never returns tokens, since
absorbed tokens are only for inspection, not downstream consumption. count and
model_time are accepted for interface compatibility but ignored.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
count
|
int
|
Ignored. |
1
|
model_time
|
float | None
|
Ignored. |
None
|
Raises:
| Type | Description |
|---|---|
ValueError
|
Always, with message "SinkPlace is terminal — tokens are absorbed, not retrievable". |
Source code in src/cpnx/places.py
retrieve_specific
¶
Always raise: a sink is terminal, so no tokens — specific or otherwise — can be retrieved.
Differs from Place.retrieve_specific: never
returns tokens, since absorbed tokens are only for inspection. tokens and
model_time are accepted for interface compatibility but ignored.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tokens
|
list[Token]
|
Ignored. |
required |
model_time
|
float | None
|
Ignored. |
None
|
Raises:
| Type | Description |
|---|---|
ValueError
|
Always, with message "SinkPlace is terminal — tokens are absorbed, not retrievable". |
Source code in src/cpnx/places.py
retrieve_all
¶
Always raise: a sink is terminal, so its absorbed tokens can never be drained via retrieval.
Differs from Place.retrieve_all: never returns tokens.
Use drain_stats to reset the aggregate counters instead. model_time is accepted
for interface compatibility but ignored.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model_time
|
float | None
|
Ignored. |
None
|
Raises:
| Type | Description |
|---|---|
ValueError
|
Always, with message "SinkPlace is terminal — tokens are absorbed, not retrievable". |
Source code in src/cpnx/places.py
peek
¶
Always raise: use the tokens property to inspect a sink's ring buffer instead.
Differs from Place.peek: rather than returning a possibly-empty
list, this raises, since a sink's kept tokens are only meant to be read via tokens
or stats. count and model_time are accepted for interface compatibility but
ignored.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
count
|
int
|
Ignored. |
1
|
model_time
|
float | None
|
Ignored. |
None
|
Raises:
| Type | Description |
|---|---|
ValueError
|
Always, with message "SinkPlace is terminal — tokens are absorbed, not retrievable". |
Source code in src/cpnx/places.py
can_deposit
¶
Always return True: a sink has unbounded capacity and absorbs every token offered.
Differs from Place.can_deposit: ignores bound
entirely (a SinkPlace is constructed with bound=None and never rejects
on capacity grounds — only color_set can reject a deposit).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
count
|
int
|
Ignored. |
1
|
Returns:
| Type | Description |
|---|---|
bool
|
|
Source code in src/cpnx/places.py
stats
¶
Return a snapshot of cumulative statistics of absorbed tokens, without resetting any counters.
Example
Returns:
| Type | Description |
|---|---|
dict
|
Dictionary with keys |
dict
|
int), |
dict
|
currently in the ring buffer), |
dict
|
nothing has been deposited), and |
dict
|
nothing has been deposited). |
Source code in src/cpnx/places.py
drain_stats
¶
Atomically return the current stats snapshot and reset the cumulative counters to zero.
Differs from stats: after returning the snapshot, resets _absorbed to 0,
_by_color to an empty dict, and _first_deposit_time to None (the ring buffer
of kept tokens and last_deposit_time are left untouched). Useful for periodic
reporting where each report should cover only the interval since the last drain.
Returns:
| Type | Description |
|---|---|
dict
|
The stats dictionary as it was immediately before the reset — same shape as |
dict
|
|
dict
|
|