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
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 439 440 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 558 559 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 783 784 | |
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,
schema: type | Callable[[Any], bool] | 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
|
schema
|
type | Callable[[Any], bool] | None
|
Optional schema or type constraint for token payloads. Can be a
type (checked via |
None
|
Note
Token payloads are always coerced to an immutable FrozenDict
(a Mapping, not a dict subclass), so a type schema is only useful as
dict/Mapping (always satisfied) or an ABC that FrozenDict registers
under. To validate payload contents, pass a callable predicate.
Source code in src/cpnx/places.py
validate_schema
¶
Validate whether payload conforms to this place's schema.
Thin boolean wrapper over _schema_failure_reason (which carries the diagnostic detail).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
payload
|
Any
|
The token payload to check against |
required |
Returns:
| Type | Description |
|---|---|
bool
|
|
bool
|
(when |
bool
|
(when |
bool
|
schema raises an exception. |
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 a key/filter that
selects a specific subset of tokens to consume rather than the head of the queue.
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
register_key_index
¶
Maintain a persistent ascending-key index over this place's ready tokens.
Registered by the engine at net-build time for each InputArc
whose key is certified (see [cpnx.certification]) — an uncertified key is not
indexed, because keying happens on the deposit path where an unbounded callable
cannot be allowed to run. index_id identifies the registering arc; re-registering
the same id with a different function rebuilds the index, so reassigning arc.key
after construction stays correct. A disabled index can also be re-armed in place,
without changing the function, via rearm_key_index
(issue #34).
Registration back-fills from the tokens already present, so it is safe on a
non-empty place. This is a pure optimisation: every read through
peek_by_key can decline, and the engine then computes
the same answer the slow way.
Source code in src/cpnx/places.py
rearm_key_index
¶
Re-enable a disabled key-index for index_id and repopulate it from the ready set.
Returns whether the index is serving afterwards. Called by the engine when selection
succeeds again for an arc whose index disabled itself (issue #34): a keying failure is
no longer permanent — once the offending token is gone the fast path is restored,
rebuilt at most once per _KEY_INDEX_REARM_MIN_DEPOSITS deposits to bound the cost of a
flapping key.
Source code in src/cpnx/places.py
key_index_disabled
¶
Whether index_id's index exists and has disabled itself.
Distinct from "cannot answer right now" (see
peek_by_key, which also declines while the place holds
cooling tokens): this is specifically the state a keying failure leaves behind, which
is reversible via rearm_key_index (issue #34). The
engine reads it when reporting a selection fault, so the report mentions the lost
index only when one was actually lost — a raising filter never touches the index,
and an uncertified key never has one.
Source code in src/cpnx/places.py
peek_by_key
¶
peek_by_key(
index_id: int,
count: int,
predicate: Callable[[Token], bool] | None = None,
) -> list[Token] | None
Return up to count available tokens in ascending key order, or None.
None means the index cannot answer and the caller must fall back to its own
ordering — no index registered, the index disabled itself after a keying failure,
or the place currently holds timed (cooling) tokens the index does not cover.
predicate is applied at pop: a rejected token is skipped but stays indexed, and
the scan continues past it, so a filter never causes a short read.
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
earliest_available_boundary
¶
Smallest future available_at (strictly greater than now) held here, or None.
Backs the engine's logical-clock advance. O(1) for an untimed place (nothing is cooling), and O(log n) amortized otherwise — via the store's cooling heap — so the clock advance no longer scans the whole marking of every place on every tick.
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 is compatible with this place's colour set and schema.
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 and schema validation. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
|
bool
|
|
bool
|
tokens are exempt from the schema check (see |
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 |
schema
|
type | Callable[[Any], bool] | None
|
Optional schema or type constraint for token payloads. Resource
permit tokens are exempt (they carry no payload). See |
None
|
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
830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 | |
__init__
¶
__init__(
name: str,
capacity: int,
pacing_secs: float,
schema: type | Callable[[Any], bool] | None = None,
) -> None
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 |
schema
|
type | Callable[[Any], bool] | None
|
Optional schema or type constraint for token payloads. Resource
permit tokens are exempt (they carry no payload). See |
None
|
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
|
Raises:
| Type | Description |
|---|---|
TypeError
|
If |
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.
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
968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 | |
__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 |
schema
|
type | Callable[[Any], bool] | None
|
Optional schema or type constraint for token payloads. Resource
permit tokens are exempt (they carry no payload). See |
None
|
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.
Note
A sink is a terminal ring buffer, not a retrievable queue, so it does NOT use
_TokenStore internally — it keeps its own bounded
collections.deque(maxlen=keep_last) and overrides every method that would
otherwise touch the base class's store.
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
1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 | |
tokens
property
¶
Snapshot of the ring buffer's currently-kept tokens (most recent keep_last), as a tuple.
Differs from Place.tokens: reads the sink's own bounded
deque ring buffer rather than a _TokenStore (a sink never routes deposits
through the store).
__init__
¶
__init__(
name: str,
*,
keep_last: int = 0,
color_set: set[str] | None = None,
schema: type | Callable[[Any], bool] | None = None,
) -> None
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
|
schema
|
type | Callable[[Any], bool] | None
|
Optional schema or type constraint for token payloads. Resource
permit tokens are exempt (they carry no payload). See |
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
__len__
¶
Return the number of tokens currently kept in the ring buffer (at most keep_last).
Differs from Place.__len__: reflects the ring buffer's
size, not a _TokenStore's — a sink's "length" is its sample size, not its
(unbounded) absorbed count (see stats()["absorbed"] for that).
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
|
|