libpetri 0.3.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +121 -0
- package/dist/chunk-FN773SSE.js +87 -0
- package/dist/chunk-FN773SSE.js.map +1 -0
- package/dist/chunk-VQ4XMJTD.js +107 -0
- package/dist/chunk-VQ4XMJTD.js.map +1 -0
- package/dist/export/index.d.ts +153 -0
- package/dist/export/index.js +411 -0
- package/dist/export/index.js.map +1 -0
- package/dist/index.d.ts +498 -0
- package/dist/index.js +1972 -0
- package/dist/index.js.map +1 -0
- package/dist/petri-net-C3Jy5HCt.d.ts +543 -0
- package/dist/verification/index.d.ts +505 -0
- package/dist/verification/index.js +1201 -0
- package/dist/verification/index.js.map +1 -0
- package/package.json +48 -0
|
@@ -0,0 +1,543 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* An immutable token carrying a typed value through the Petri net.
|
|
3
|
+
*
|
|
4
|
+
* Tokens flow from place to place as transitions fire, carrying typed
|
|
5
|
+
* payloads that represent the state of a computation or workflow.
|
|
6
|
+
*/
|
|
7
|
+
interface Token<T> {
|
|
8
|
+
readonly value: T;
|
|
9
|
+
/** Epoch milliseconds when the token was created. */
|
|
10
|
+
readonly createdAt: number;
|
|
11
|
+
}
|
|
12
|
+
/** Creates a token with the given value and current timestamp. */
|
|
13
|
+
declare function tokenOf<T>(value: T): Token<T>;
|
|
14
|
+
/**
|
|
15
|
+
* Returns a unit token (marker with no meaningful value).
|
|
16
|
+
* Used for pure control flow where presence matters but data doesn't.
|
|
17
|
+
* Returns a cached singleton whose `value` is `null`.
|
|
18
|
+
*/
|
|
19
|
+
declare function unitToken(): Token<null>;
|
|
20
|
+
/** Creates a token with a specific timestamp (for testing/replay). */
|
|
21
|
+
declare function tokenAt<T>(value: T, createdAt: number): Token<T>;
|
|
22
|
+
/** Checks if this is the singleton unit token. */
|
|
23
|
+
declare function isUnit(token: Token<unknown>): boolean;
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* A typed place in the Petri Net that holds tokens of a specific type.
|
|
27
|
+
*
|
|
28
|
+
* Places are the "state containers" of a Petri net. They hold tokens that
|
|
29
|
+
* represent data or resources flowing through the net.
|
|
30
|
+
*
|
|
31
|
+
* Places use name-based equality (matching Java record semantics).
|
|
32
|
+
* Internally use `Map<string, ...>` keyed by `place.name` for O(1) lookups.
|
|
33
|
+
*/
|
|
34
|
+
interface Place<T> {
|
|
35
|
+
readonly name: string;
|
|
36
|
+
/** Phantom field to carry the type parameter. Never set at runtime. */
|
|
37
|
+
readonly _phantom?: T;
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* An environment place that accepts external token injection.
|
|
41
|
+
* Wraps a regular Place and marks it for external event injection.
|
|
42
|
+
*/
|
|
43
|
+
interface EnvironmentPlace<T> {
|
|
44
|
+
readonly place: Place<T>;
|
|
45
|
+
}
|
|
46
|
+
/** Creates a typed place. */
|
|
47
|
+
declare function place<T>(name: string): Place<T>;
|
|
48
|
+
/** Creates an environment place (external event injection point). */
|
|
49
|
+
declare function environmentPlace<T>(name: string): EnvironmentPlace<T>;
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Arc types connecting places to transitions in the Petri net.
|
|
53
|
+
*
|
|
54
|
+
* | Arc Type | Requires Token? | Consumes? | Effect |
|
|
55
|
+
* |-----------|-----------------|-----------|--------------------------|
|
|
56
|
+
* | Input | Yes | Yes | Token consumed on fire |
|
|
57
|
+
* | Output | No | No | Token produced on complete|
|
|
58
|
+
* | Inhibitor | No (blocks) | No | Disables transition |
|
|
59
|
+
* | Read | Yes | No | Token remains |
|
|
60
|
+
* | Reset | No | Yes (all) | All tokens removed |
|
|
61
|
+
*/
|
|
62
|
+
type Arc = ArcInput | ArcOutput | ArcInhibitor | ArcRead | ArcReset;
|
|
63
|
+
interface ArcInput<T = any> {
|
|
64
|
+
readonly type: 'input';
|
|
65
|
+
readonly place: Place<T>;
|
|
66
|
+
readonly guard?: (value: T) => boolean;
|
|
67
|
+
}
|
|
68
|
+
interface ArcOutput<T = any> {
|
|
69
|
+
readonly type: 'output';
|
|
70
|
+
readonly place: Place<T>;
|
|
71
|
+
}
|
|
72
|
+
interface ArcInhibitor<T = any> {
|
|
73
|
+
readonly type: 'inhibitor';
|
|
74
|
+
readonly place: Place<T>;
|
|
75
|
+
}
|
|
76
|
+
interface ArcRead<T = any> {
|
|
77
|
+
readonly type: 'read';
|
|
78
|
+
readonly place: Place<T>;
|
|
79
|
+
}
|
|
80
|
+
interface ArcReset<T = any> {
|
|
81
|
+
readonly type: 'reset';
|
|
82
|
+
readonly place: Place<T>;
|
|
83
|
+
}
|
|
84
|
+
/** Input arc: consumes token from place when transition fires. */
|
|
85
|
+
declare function inputArc<T>(place: Place<T>, guard?: (value: T) => boolean): ArcInput<T>;
|
|
86
|
+
/** Output arc: produces token to place when transition fires. */
|
|
87
|
+
declare function outputArc<T>(place: Place<T>): ArcOutput<T>;
|
|
88
|
+
/** Inhibitor arc: blocks transition if place has tokens. */
|
|
89
|
+
declare function inhibitorArc<T>(place: Place<T>): ArcInhibitor<T>;
|
|
90
|
+
/** Read arc: requires token without consuming. */
|
|
91
|
+
declare function readArc<T>(place: Place<T>): ArcRead<T>;
|
|
92
|
+
/** Reset arc: removes all tokens from place when firing. */
|
|
93
|
+
declare function resetArc<T>(place: Place<T>): ArcReset<T>;
|
|
94
|
+
/** Returns the place this arc connects to. */
|
|
95
|
+
declare function arcPlace(arc: Arc): Place<any>;
|
|
96
|
+
/** Checks if an input arc has a guard predicate. */
|
|
97
|
+
declare function hasGuard(arc: ArcInput): boolean;
|
|
98
|
+
/** Checks if a token value matches an input arc's guard. */
|
|
99
|
+
declare function matchesGuard<T>(arc: ArcInput<T>, value: T): boolean;
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* Input specification with cardinality and optional guard predicate.
|
|
103
|
+
* CPN-compliant: cardinality determines how many tokens to consume,
|
|
104
|
+
* guard filters which tokens are eligible.
|
|
105
|
+
*
|
|
106
|
+
* Inputs are always AND-joined (all must be satisfied to enable transition).
|
|
107
|
+
* XOR on inputs is modeled via multiple transitions (conflict).
|
|
108
|
+
*/
|
|
109
|
+
type In = InOne | InExactly | InAll | InAtLeast;
|
|
110
|
+
interface InOne<T = any> {
|
|
111
|
+
readonly type: 'one';
|
|
112
|
+
readonly place: Place<T>;
|
|
113
|
+
readonly guard?: (value: T) => boolean;
|
|
114
|
+
}
|
|
115
|
+
interface InExactly<T = any> {
|
|
116
|
+
readonly type: 'exactly';
|
|
117
|
+
readonly place: Place<T>;
|
|
118
|
+
readonly count: number;
|
|
119
|
+
readonly guard?: (value: T) => boolean;
|
|
120
|
+
}
|
|
121
|
+
interface InAll<T = any> {
|
|
122
|
+
readonly type: 'all';
|
|
123
|
+
readonly place: Place<T>;
|
|
124
|
+
readonly guard?: (value: T) => boolean;
|
|
125
|
+
}
|
|
126
|
+
interface InAtLeast<T = any> {
|
|
127
|
+
readonly type: 'at-least';
|
|
128
|
+
readonly place: Place<T>;
|
|
129
|
+
readonly minimum: number;
|
|
130
|
+
readonly guard?: (value: T) => boolean;
|
|
131
|
+
}
|
|
132
|
+
/** Consume exactly 1 token (standard CPN semantics). Optional guard filters eligible tokens. */
|
|
133
|
+
declare function one<T>(place: Place<T>, guard?: (value: T) => boolean): InOne<T>;
|
|
134
|
+
/** Consume exactly N tokens (batching). Optional guard filters eligible tokens. */
|
|
135
|
+
declare function exactly<T>(count: number, place: Place<T>, guard?: (value: T) => boolean): InExactly<T>;
|
|
136
|
+
/** Consume all available tokens (must be 1+). Optional guard filters eligible tokens. */
|
|
137
|
+
declare function all<T>(place: Place<T>, guard?: (value: T) => boolean): InAll<T>;
|
|
138
|
+
/** Wait for N+ tokens, consume all when enabled. Optional guard filters eligible tokens. */
|
|
139
|
+
declare function atLeast<T>(minimum: number, place: Place<T>, guard?: (value: T) => boolean): InAtLeast<T>;
|
|
140
|
+
/** Returns the minimum number of tokens required to enable. */
|
|
141
|
+
declare function requiredCount(spec: In): number;
|
|
142
|
+
/**
|
|
143
|
+
* Returns the actual number of tokens to consume given the available count.
|
|
144
|
+
* - One: always consumes 1
|
|
145
|
+
* - Exactly: always consumes exactly count
|
|
146
|
+
* - All: consumes all available
|
|
147
|
+
* - AtLeast: consumes all available (when enabled, i.e., >= minimum)
|
|
148
|
+
*/
|
|
149
|
+
declare function consumptionCount(spec: In, available: number): number;
|
|
150
|
+
|
|
151
|
+
/**
|
|
152
|
+
* Output specification with explicit split semantics.
|
|
153
|
+
* Supports composite structures (XOR of ANDs, AND of XORs, etc.)
|
|
154
|
+
*
|
|
155
|
+
* - And: ALL children must receive tokens
|
|
156
|
+
* - Xor: EXACTLY ONE child receives token
|
|
157
|
+
* - Place: Leaf node representing a single output place
|
|
158
|
+
* - Timeout: Timeout branch that activates if action exceeds duration
|
|
159
|
+
* - ForwardInput: Forward consumed input to output on timeout
|
|
160
|
+
*/
|
|
161
|
+
type Out = OutAnd | OutXor | OutPlace | OutTimeout | OutForwardInput;
|
|
162
|
+
interface OutAnd {
|
|
163
|
+
readonly type: 'and';
|
|
164
|
+
readonly children: readonly Out[];
|
|
165
|
+
}
|
|
166
|
+
interface OutXor {
|
|
167
|
+
readonly type: 'xor';
|
|
168
|
+
readonly children: readonly Out[];
|
|
169
|
+
}
|
|
170
|
+
interface OutPlace {
|
|
171
|
+
readonly type: 'place';
|
|
172
|
+
readonly place: Place<any>;
|
|
173
|
+
}
|
|
174
|
+
interface OutTimeout {
|
|
175
|
+
readonly type: 'timeout';
|
|
176
|
+
/** Timeout duration in milliseconds. */
|
|
177
|
+
readonly afterMs: number;
|
|
178
|
+
readonly child: Out;
|
|
179
|
+
}
|
|
180
|
+
interface OutForwardInput {
|
|
181
|
+
readonly type: 'forward-input';
|
|
182
|
+
readonly from: Place<any>;
|
|
183
|
+
readonly to: Place<any>;
|
|
184
|
+
}
|
|
185
|
+
/**
|
|
186
|
+
* AND-split: all children must receive tokens.
|
|
187
|
+
*
|
|
188
|
+
* @example
|
|
189
|
+
* ```ts
|
|
190
|
+
* // AND of XOR branches: one of (A,B) AND one of (C,D)
|
|
191
|
+
* and(xorPlaces(placeA, placeB), xorPlaces(placeC, placeD))
|
|
192
|
+
*
|
|
193
|
+
* // AND with a fixed place + XOR branch
|
|
194
|
+
* and(outPlace(always), xorPlaces(left, right))
|
|
195
|
+
* ```
|
|
196
|
+
*/
|
|
197
|
+
declare function and(...children: Out[]): OutAnd;
|
|
198
|
+
/** AND-split from places: all places must receive tokens. */
|
|
199
|
+
declare function andPlaces(...places: Place<any>[]): OutAnd;
|
|
200
|
+
/** XOR-split: exactly one child receives token. */
|
|
201
|
+
declare function xor(...children: Out[]): OutXor;
|
|
202
|
+
/** XOR-split from places: exactly one place receives token. */
|
|
203
|
+
declare function xorPlaces(...places: Place<any>[]): OutXor;
|
|
204
|
+
/** Leaf output spec for a single place. */
|
|
205
|
+
declare function outPlace(p: Place<any>): OutPlace;
|
|
206
|
+
/** Timeout output: activates if action exceeds duration. */
|
|
207
|
+
declare function timeout(afterMs: number, child: Out): OutTimeout;
|
|
208
|
+
/** Timeout output pointing to a single place. */
|
|
209
|
+
declare function timeoutPlace(afterMs: number, p: Place<any>): OutTimeout;
|
|
210
|
+
/** Forward consumed input value to output place on timeout. */
|
|
211
|
+
declare function forwardInput(from: Place<any>, to: Place<any>): OutForwardInput;
|
|
212
|
+
/** Collects all leaf places from this output spec (flattened). */
|
|
213
|
+
declare function allPlaces(out: Out): Set<Place<any>>;
|
|
214
|
+
/**
|
|
215
|
+
* Enumerates all possible output branches for structural analysis.
|
|
216
|
+
*
|
|
217
|
+
* - AND = single branch containing all child places (Cartesian product)
|
|
218
|
+
* - XOR = one branch per alternative child
|
|
219
|
+
* - Nested = Cartesian product for AND, union for XOR
|
|
220
|
+
*/
|
|
221
|
+
declare function enumerateBranches(out: Out): ReadonlyArray<ReadonlySet<Place<any>>>;
|
|
222
|
+
|
|
223
|
+
/**
|
|
224
|
+
* Firing timing specification for transitions.
|
|
225
|
+
*
|
|
226
|
+
* Based on classical Time Petri Net (TPN) semantics:
|
|
227
|
+
* - Transition CANNOT fire before earliest time (lower bound)
|
|
228
|
+
* - Transition MUST fire by deadline OR become disabled (upper bound)
|
|
229
|
+
*
|
|
230
|
+
* All durations are in milliseconds.
|
|
231
|
+
*/
|
|
232
|
+
type Timing = TimingImmediate | TimingDeadline | TimingDelayed | TimingWindow | TimingExact;
|
|
233
|
+
interface TimingImmediate {
|
|
234
|
+
readonly type: 'immediate';
|
|
235
|
+
}
|
|
236
|
+
interface TimingDeadline {
|
|
237
|
+
readonly type: 'deadline';
|
|
238
|
+
/** Deadline in milliseconds. Must be positive. */
|
|
239
|
+
readonly byMs: number;
|
|
240
|
+
}
|
|
241
|
+
interface TimingDelayed {
|
|
242
|
+
readonly type: 'delayed';
|
|
243
|
+
/** Minimum delay in milliseconds. Must be non-negative. */
|
|
244
|
+
readonly afterMs: number;
|
|
245
|
+
}
|
|
246
|
+
interface TimingWindow {
|
|
247
|
+
readonly type: 'window';
|
|
248
|
+
/** Earliest firing time in milliseconds. Must be non-negative. */
|
|
249
|
+
readonly earliestMs: number;
|
|
250
|
+
/** Latest firing time in milliseconds. Must be >= earliestMs. */
|
|
251
|
+
readonly latestMs: number;
|
|
252
|
+
}
|
|
253
|
+
interface TimingExact {
|
|
254
|
+
readonly type: 'exact';
|
|
255
|
+
/** Exact firing time in milliseconds. Must be non-negative. */
|
|
256
|
+
readonly atMs: number;
|
|
257
|
+
}
|
|
258
|
+
/** ~100 years in milliseconds, used for "unconstrained" intervals. */
|
|
259
|
+
declare const MAX_DURATION_MS: number;
|
|
260
|
+
/** Immediate firing: can fire as soon as enabled, no deadline. [0, inf) */
|
|
261
|
+
declare function immediate(): TimingImmediate;
|
|
262
|
+
/** Immediate with deadline: can fire immediately, must fire by deadline. [0, by] */
|
|
263
|
+
declare function deadline(byMs: number): TimingDeadline;
|
|
264
|
+
/** Delayed firing: must wait, then can fire anytime. [after, inf) */
|
|
265
|
+
declare function delayed(afterMs: number): TimingDelayed;
|
|
266
|
+
/** Time window: can fire within [earliest, latest]. */
|
|
267
|
+
declare function window(earliestMs: number, latestMs: number): TimingWindow;
|
|
268
|
+
/** Exact timing: fires at precisely the specified time. [at, at] */
|
|
269
|
+
declare function exact(atMs: number): TimingExact;
|
|
270
|
+
/** Returns the earliest time (ms) the transition can fire after enabling. */
|
|
271
|
+
declare function earliest(timing: Timing): number;
|
|
272
|
+
/** Returns the latest time (ms) by which the transition must fire. */
|
|
273
|
+
declare function latest(timing: Timing): number;
|
|
274
|
+
/** Returns true if this timing has a finite deadline. */
|
|
275
|
+
declare function hasDeadline(timing: Timing): boolean;
|
|
276
|
+
|
|
277
|
+
/**
|
|
278
|
+
* Consumed input tokens bound to their source places.
|
|
279
|
+
*
|
|
280
|
+
* Passed to TransitionAction as the `input` parameter, providing
|
|
281
|
+
* type-safe read access to tokens consumed from input places.
|
|
282
|
+
*/
|
|
283
|
+
declare class TokenInput {
|
|
284
|
+
private readonly tokens;
|
|
285
|
+
/** Add a token (used by executor when firing transition). */
|
|
286
|
+
add<T>(place: Place<T>, token: Token<T>): this;
|
|
287
|
+
/** Get all tokens for a place. */
|
|
288
|
+
getAll<T>(place: Place<T>): readonly Token<T>[];
|
|
289
|
+
/** Get the first token for a place. Throws if no tokens. */
|
|
290
|
+
get<T>(place: Place<T>): Token<T>;
|
|
291
|
+
/** Get the first token's value for a place. Throws if no tokens. */
|
|
292
|
+
value<T>(place: Place<T>): T;
|
|
293
|
+
/** Get all token values for a place. */
|
|
294
|
+
values<T>(place: Place<T>): readonly T[];
|
|
295
|
+
/** Get token count for a place. */
|
|
296
|
+
count(place: Place<any>): number;
|
|
297
|
+
/** Check if any tokens exist for a place. */
|
|
298
|
+
has(place: Place<any>): boolean;
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
/**
|
|
302
|
+
* An output entry: place + token pair.
|
|
303
|
+
*/
|
|
304
|
+
interface OutputEntry {
|
|
305
|
+
readonly place: Place<any>;
|
|
306
|
+
readonly token: Token<any>;
|
|
307
|
+
}
|
|
308
|
+
/**
|
|
309
|
+
* Collects output tokens produced by a transition action.
|
|
310
|
+
*/
|
|
311
|
+
declare class TokenOutput {
|
|
312
|
+
private readonly _entries;
|
|
313
|
+
/** Add a value to an output place (creates token with current timestamp). */
|
|
314
|
+
add<T>(place: Place<T>, value: T): this;
|
|
315
|
+
/** Add a pre-existing token to an output place. */
|
|
316
|
+
addToken<T>(place: Place<T>, token: Token<T>): this;
|
|
317
|
+
/** Returns all collected outputs. */
|
|
318
|
+
entries(): readonly OutputEntry[];
|
|
319
|
+
/** Check if any outputs were produced. */
|
|
320
|
+
isEmpty(): boolean;
|
|
321
|
+
/** Returns the set of place names that received tokens. */
|
|
322
|
+
placesWithTokens(): Set<string>;
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
/** Callback for emitting log messages from transition actions. */
|
|
326
|
+
type LogFn = (level: string, message: string, error?: Error) => void;
|
|
327
|
+
/**
|
|
328
|
+
* Context provided to transition actions.
|
|
329
|
+
*
|
|
330
|
+
* Provides filtered access based on structure:
|
|
331
|
+
* - Input places (consumed tokens)
|
|
332
|
+
* - Read places (context tokens, not consumed)
|
|
333
|
+
* - Output places (where to produce tokens)
|
|
334
|
+
*
|
|
335
|
+
* Enforces the structure contract — actions can only access places
|
|
336
|
+
* declared in the transition's structure.
|
|
337
|
+
*/
|
|
338
|
+
declare class TransitionContext {
|
|
339
|
+
private readonly rawInput;
|
|
340
|
+
private readonly _rawOutput;
|
|
341
|
+
private readonly allowedInputs;
|
|
342
|
+
private readonly allowedReads;
|
|
343
|
+
private readonly allowedOutputs;
|
|
344
|
+
private readonly _inputPlaces;
|
|
345
|
+
private readonly _readPlaces;
|
|
346
|
+
private readonly _outputPlaces;
|
|
347
|
+
private readonly _transitionName;
|
|
348
|
+
private readonly executionCtx;
|
|
349
|
+
private readonly _logFn?;
|
|
350
|
+
constructor(transitionName: string, rawInput: TokenInput, rawOutput: TokenOutput, inputPlaces: ReadonlySet<Place<any>>, readPlaces: ReadonlySet<Place<any>>, outputPlaces: ReadonlySet<Place<any>>, executionContext?: Map<string, unknown>, logFn?: LogFn);
|
|
351
|
+
/** Get single consumed input value. Throws if place not declared or multiple tokens. */
|
|
352
|
+
input<T>(place: Place<T>): T;
|
|
353
|
+
/** Get all consumed input values for a place. */
|
|
354
|
+
inputs<T>(place: Place<T>): readonly T[];
|
|
355
|
+
/** Get consumed input token with metadata. */
|
|
356
|
+
inputToken<T>(place: Place<T>): Token<T>;
|
|
357
|
+
/** Returns declared input places (consumed). */
|
|
358
|
+
inputPlaces(): ReadonlySet<Place<any>>;
|
|
359
|
+
private requireInput;
|
|
360
|
+
/** Get read-only context value. Throws if place not declared as read. */
|
|
361
|
+
read<T>(place: Place<T>): T;
|
|
362
|
+
/** Get all read-only context values for a place. */
|
|
363
|
+
reads<T>(place: Place<T>): readonly T[];
|
|
364
|
+
/** Returns declared read places (context, not consumed). */
|
|
365
|
+
readPlaces(): ReadonlySet<Place<any>>;
|
|
366
|
+
private requireRead;
|
|
367
|
+
/** Add output value. Throws if place not declared as output. */
|
|
368
|
+
output<T>(place: Place<T>, value: T): this;
|
|
369
|
+
/** Add output token with metadata. */
|
|
370
|
+
outputToken<T>(place: Place<T>, token: Token<T>): this;
|
|
371
|
+
/** Returns declared output places. */
|
|
372
|
+
outputPlaces(): ReadonlySet<Place<any>>;
|
|
373
|
+
private requireOutput;
|
|
374
|
+
/** Returns the transition name. */
|
|
375
|
+
transitionName(): string;
|
|
376
|
+
/** Retrieves an execution context object by key. */
|
|
377
|
+
executionContext<T>(key: string): T | undefined;
|
|
378
|
+
/** Checks if an execution context object of the given key is present. */
|
|
379
|
+
hasExecutionContext(key: string): boolean;
|
|
380
|
+
/** Emits a structured log message into the event store. */
|
|
381
|
+
log(level: string, message: string, error?: Error): void;
|
|
382
|
+
/** @internal Used by BitmapNetExecutor to collect outputs after action completion. */
|
|
383
|
+
rawOutput(): TokenOutput;
|
|
384
|
+
}
|
|
385
|
+
|
|
386
|
+
/**
|
|
387
|
+
* The action executed when a transition fires.
|
|
388
|
+
* Receives a TransitionContext providing filtered I/O and structure access.
|
|
389
|
+
*/
|
|
390
|
+
type TransitionAction = (ctx: TransitionContext) => Promise<void>;
|
|
391
|
+
/** Identity action: produces no outputs. */
|
|
392
|
+
declare function passthrough(): TransitionAction;
|
|
393
|
+
/**
|
|
394
|
+
* Transform action: applies function to context, copies result to ALL output places.
|
|
395
|
+
*
|
|
396
|
+
* @example
|
|
397
|
+
* ```ts
|
|
398
|
+
* const action = transform(ctx => ctx.input(inputPlace).toUpperCase());
|
|
399
|
+
* // Result is copied to every declared output place
|
|
400
|
+
* ```
|
|
401
|
+
*/
|
|
402
|
+
declare function transform(fn: (ctx: TransitionContext) => unknown): TransitionAction;
|
|
403
|
+
/**
|
|
404
|
+
* Fork action: copies single input token to all outputs.
|
|
405
|
+
* Requires exactly one input place (derived from structure).
|
|
406
|
+
*/
|
|
407
|
+
declare function fork(): TransitionAction;
|
|
408
|
+
/**
|
|
409
|
+
* Transform with explicit input place.
|
|
410
|
+
*/
|
|
411
|
+
declare function transformFrom<I>(inputPlace: Place<I>, fn: (value: I) => unknown): TransitionAction;
|
|
412
|
+
/**
|
|
413
|
+
* Async transform: applies async function, copies result to all outputs.
|
|
414
|
+
*/
|
|
415
|
+
declare function transformAsync(fn: (ctx: TransitionContext) => Promise<unknown>): TransitionAction;
|
|
416
|
+
/** Produce action: produces a single token with the given value to the specified place. */
|
|
417
|
+
declare function produce<T>(place: Place<T>, value: T): TransitionAction;
|
|
418
|
+
/**
|
|
419
|
+
* Wraps an action with timeout handling.
|
|
420
|
+
* If the action completes within the timeout, normal completion.
|
|
421
|
+
* If the timeout expires, the timeoutValue is produced to the timeoutPlace.
|
|
422
|
+
*
|
|
423
|
+
* @example
|
|
424
|
+
* ```ts
|
|
425
|
+
* const action = withTimeout(
|
|
426
|
+
* async (ctx) => { ctx.output(resultPlace, await fetchData()); },
|
|
427
|
+
* 5000,
|
|
428
|
+
* timeoutPlace,
|
|
429
|
+
* 'timed-out',
|
|
430
|
+
* );
|
|
431
|
+
* ```
|
|
432
|
+
*/
|
|
433
|
+
declare function withTimeout<T>(action: TransitionAction, timeoutMs: number, timeoutPlace: Place<T>, timeoutValue: T): TransitionAction;
|
|
434
|
+
|
|
435
|
+
/**
|
|
436
|
+
* A transition in the Time Petri Net that transforms tokens.
|
|
437
|
+
*
|
|
438
|
+
* Transitions use identity-based equality (===) — each instance is unique
|
|
439
|
+
* regardless of name. The name is purely a label for display/debugging/export.
|
|
440
|
+
*/
|
|
441
|
+
declare class Transition {
|
|
442
|
+
readonly name: string;
|
|
443
|
+
readonly inputSpecs: readonly In[];
|
|
444
|
+
readonly outputSpec: Out | null;
|
|
445
|
+
readonly inhibitors: readonly ArcInhibitor[];
|
|
446
|
+
readonly reads: readonly ArcRead[];
|
|
447
|
+
readonly resets: readonly ArcReset[];
|
|
448
|
+
readonly timing: Timing;
|
|
449
|
+
readonly actionTimeout: OutTimeout | null;
|
|
450
|
+
readonly action: TransitionAction;
|
|
451
|
+
readonly priority: number;
|
|
452
|
+
private readonly _inputPlaces;
|
|
453
|
+
private readonly _readPlaces;
|
|
454
|
+
private readonly _outputPlaces;
|
|
455
|
+
/** @internal Use {@link Transition.builder} to create instances. */
|
|
456
|
+
constructor(key: symbol, name: string, inputSpecs: readonly In[], outputSpec: Out | null, inhibitors: readonly ArcInhibitor[], reads: readonly ArcRead[], resets: readonly ArcReset[], timing: Timing, action: TransitionAction, priority: number);
|
|
457
|
+
/** Returns set of input places — consumed tokens. */
|
|
458
|
+
inputPlaces(): ReadonlySet<Place<any>>;
|
|
459
|
+
/** Returns set of read places — context tokens, not consumed. */
|
|
460
|
+
readPlaces(): ReadonlySet<Place<any>>;
|
|
461
|
+
/** Returns set of output places — where tokens are produced. */
|
|
462
|
+
outputPlaces(): ReadonlySet<Place<any>>;
|
|
463
|
+
/** Returns true if this transition has an action timeout. */
|
|
464
|
+
hasActionTimeout(): boolean;
|
|
465
|
+
toString(): string;
|
|
466
|
+
static builder(name: string): TransitionBuilder;
|
|
467
|
+
}
|
|
468
|
+
declare class TransitionBuilder {
|
|
469
|
+
private readonly _name;
|
|
470
|
+
private readonly _inputSpecs;
|
|
471
|
+
private _outputSpec;
|
|
472
|
+
private readonly _inhibitors;
|
|
473
|
+
private readonly _reads;
|
|
474
|
+
private readonly _resets;
|
|
475
|
+
private _timing;
|
|
476
|
+
private _action;
|
|
477
|
+
private _priority;
|
|
478
|
+
constructor(name: string);
|
|
479
|
+
/** Add input specifications with cardinality. */
|
|
480
|
+
inputs(...specs: In[]): this;
|
|
481
|
+
/** Set the output specification (composite AND/XOR structure). */
|
|
482
|
+
outputs(spec: Out): this;
|
|
483
|
+
/** Add inhibitor arc. */
|
|
484
|
+
inhibitor(place: Place<any>): this;
|
|
485
|
+
/** Add inhibitor arcs. */
|
|
486
|
+
inhibitors(...places: Place<any>[]): this;
|
|
487
|
+
/** Add read arc. */
|
|
488
|
+
read(place: Place<any>): this;
|
|
489
|
+
/** Add read arcs. */
|
|
490
|
+
reads(...places: Place<any>[]): this;
|
|
491
|
+
/** Add reset arc. */
|
|
492
|
+
reset(place: Place<any>): this;
|
|
493
|
+
/** Add reset arcs. */
|
|
494
|
+
resets(...places: Place<any>[]): this;
|
|
495
|
+
/** Set timing specification. */
|
|
496
|
+
timing(timing: Timing): this;
|
|
497
|
+
/** Set the transition action. */
|
|
498
|
+
action(action: TransitionAction): this;
|
|
499
|
+
/** Set the priority (higher fires first). */
|
|
500
|
+
priority(priority: number): this;
|
|
501
|
+
build(): Transition;
|
|
502
|
+
}
|
|
503
|
+
|
|
504
|
+
/**
|
|
505
|
+
* Immutable definition of a Time Petri Net structure.
|
|
506
|
+
*
|
|
507
|
+
* A PetriNet is a reusable definition that can be executed multiple times
|
|
508
|
+
* with different initial markings. Places are auto-collected from transitions.
|
|
509
|
+
*/
|
|
510
|
+
declare class PetriNet {
|
|
511
|
+
readonly name: string;
|
|
512
|
+
readonly places: ReadonlySet<Place<any>>;
|
|
513
|
+
readonly transitions: ReadonlySet<Transition>;
|
|
514
|
+
/** @internal Use {@link PetriNet.builder} to create instances. */
|
|
515
|
+
constructor(key: symbol, name: string, places: ReadonlySet<Place<any>>, transitions: ReadonlySet<Transition>);
|
|
516
|
+
/**
|
|
517
|
+
* Creates a new PetriNet with actions bound to transitions by name.
|
|
518
|
+
* Unbound transitions keep passthrough action.
|
|
519
|
+
*/
|
|
520
|
+
bindActions(actionBindings: Map<string, TransitionAction> | Record<string, TransitionAction>): PetriNet;
|
|
521
|
+
/**
|
|
522
|
+
* Creates a new PetriNet with actions bound via a resolver function.
|
|
523
|
+
*/
|
|
524
|
+
bindActionsWithResolver(actionResolver: (name: string) => TransitionAction): PetriNet;
|
|
525
|
+
static builder(name: string): PetriNetBuilder;
|
|
526
|
+
}
|
|
527
|
+
declare class PetriNetBuilder {
|
|
528
|
+
private readonly _name;
|
|
529
|
+
private readonly _places;
|
|
530
|
+
private readonly _transitions;
|
|
531
|
+
constructor(name: string);
|
|
532
|
+
/** Add an explicit place. */
|
|
533
|
+
place(place: Place<any>): this;
|
|
534
|
+
/** Add explicit places. */
|
|
535
|
+
places(...places: Place<any>[]): this;
|
|
536
|
+
/** Add a transition (auto-collects places from arcs). */
|
|
537
|
+
transition(transition: Transition): this;
|
|
538
|
+
/** Add transitions (auto-collects places from arcs). */
|
|
539
|
+
transitions(...transitions: Transition[]): this;
|
|
540
|
+
build(): PetriNet;
|
|
541
|
+
}
|
|
542
|
+
|
|
543
|
+
export { hasDeadline as $, type Arc as A, TokenOutput as B, type TransitionAction as C, TransitionBuilder as D, type EnvironmentPlace as E, all as F, allPlaces as G, and as H, type In as I, andPlaces as J, arcPlace as K, type LogFn as L, MAX_DURATION_MS as M, atLeast as N, type Out as O, type Place as P, consumptionCount as Q, deadline as R, delayed as S, type Token as T, earliest as U, enumerateBranches as V, environmentPlace as W, exact as X, exactly as Y, fork as Z, forwardInput as _, PetriNet as a, hasGuard as a0, immediate as a1, inhibitorArc as a2, inputArc as a3, isUnit as a4, latest as a5, matchesGuard as a6, one as a7, outPlace as a8, outputArc as a9, passthrough as aa, place as ab, produce as ac, readArc as ad, requiredCount as ae, resetArc as af, timeout as ag, timeoutPlace as ah, tokenAt as ai, tokenOf as aj, transform as ak, transformAsync as al, transformFrom as am, unitToken as an, window as ao, withTimeout as ap, xor as aq, xorPlaces as ar, Transition as b, TransitionContext as c, type ArcInhibitor as d, type ArcInput as e, type ArcOutput as f, type ArcRead as g, type ArcReset as h, type InAll as i, type InAtLeast as j, type InExactly as k, type InOne as l, type OutAnd as m, type OutForwardInput as n, type OutPlace as o, type OutTimeout as p, type OutXor as q, type OutputEntry as r, PetriNetBuilder as s, type Timing as t, type TimingDeadline as u, type TimingDelayed as v, type TimingExact as w, type TimingImmediate as x, type TimingWindow as y, TokenInput as z };
|