libpetri 1.1.0 → 1.3.1
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/dist/index.d.ts +205 -1
- package/dist/index.js +1211 -0
- package/dist/index.js.map +1 -1
- package/package.json +7 -7
package/dist/index.d.ts
CHANGED
|
@@ -304,6 +304,210 @@ declare class BitmapNetExecutor implements PetriNetExecutor {
|
|
|
304
304
|
private emitEvent;
|
|
305
305
|
}
|
|
306
306
|
|
|
307
|
+
/**
|
|
308
|
+
* @module precompiled-net
|
|
309
|
+
*
|
|
310
|
+
* Flat-array precompiled representation of a PetriNet for high-performance execution.
|
|
311
|
+
*
|
|
312
|
+
* **Design**: Compiles from a `CompiledNet`, converting all per-transition data into
|
|
313
|
+
* flat typed arrays indexed by transition ID (tid) or place ID (pid). Eliminates Map
|
|
314
|
+
* lookups and object traversals from the hot path.
|
|
315
|
+
*
|
|
316
|
+
* **Sparse enablement**: Each transition's needs/inhibitor masks are classified as
|
|
317
|
+
* empty (-2), single-word (>=0), or multi-word (-1). Single-word masks avoid inner
|
|
318
|
+
* loops entirely; multi-word uses precomputed sparse indices to skip zero words.
|
|
319
|
+
*
|
|
320
|
+
* **Opcode programs**: Input consumption is compiled to compact opcode sequences
|
|
321
|
+
* (CONSUME_ONE, CONSUME_N, CONSUME_ALL, CONSUME_ATLEAST, RESET) that the executor
|
|
322
|
+
* dispatches without inspecting In spec objects.
|
|
323
|
+
*
|
|
324
|
+
* @see CompiledNet for the base bitmap representation
|
|
325
|
+
* @see PrecompiledNetExecutor for the executor that uses this representation
|
|
326
|
+
*/
|
|
327
|
+
|
|
328
|
+
/**
|
|
329
|
+
* Immutable precompiled representation of a Petri net.
|
|
330
|
+
*
|
|
331
|
+
* All data is stored in flat typed arrays indexed by tid/pid for cache-friendly,
|
|
332
|
+
* branch-free access on the hot path. Compiled once, reused across executions.
|
|
333
|
+
*/
|
|
334
|
+
declare class PrecompiledNet {
|
|
335
|
+
readonly compiled: CompiledNet;
|
|
336
|
+
readonly placeCount: number;
|
|
337
|
+
readonly transitionCount: number;
|
|
338
|
+
readonly wordCount: number;
|
|
339
|
+
/** Places indexed by pid — avoids compiled.place(pid) indirection on hot path. */
|
|
340
|
+
readonly places: readonly Place<any>[];
|
|
341
|
+
/** Per-transition consume opcode sequences. */
|
|
342
|
+
readonly consumeOps: readonly (readonly number[])[];
|
|
343
|
+
/** Per-transition read-arc place IDs. */
|
|
344
|
+
readonly readOps: readonly (readonly number[])[];
|
|
345
|
+
readonly needsMask: readonly Uint32Array[];
|
|
346
|
+
readonly inhibitorMask: readonly Uint32Array[];
|
|
347
|
+
readonly needsSingleWordIndex: Int8Array;
|
|
348
|
+
readonly needsSingleWordMask: Uint32Array;
|
|
349
|
+
readonly needsSparseIndices: readonly (readonly number[])[];
|
|
350
|
+
readonly needsSparseMasks: readonly (readonly number[])[];
|
|
351
|
+
readonly inhibitorSingleWordIndex: Int8Array;
|
|
352
|
+
readonly inhibitorSingleWordMask: Uint32Array;
|
|
353
|
+
readonly inhibitorSparseIndices: readonly (readonly number[])[];
|
|
354
|
+
readonly inhibitorSparseMasks: readonly (readonly number[])[];
|
|
355
|
+
readonly earliestMs: Float64Array;
|
|
356
|
+
readonly latestMs: Float64Array;
|
|
357
|
+
readonly hasDeadline: Uint8Array;
|
|
358
|
+
readonly priorities: Int32Array;
|
|
359
|
+
readonly transitionToPriorityIndex: Uint32Array;
|
|
360
|
+
readonly priorityLevels: readonly number[];
|
|
361
|
+
readonly distinctPriorityCount: number;
|
|
362
|
+
/** -2=no spec, -1=complex, >=0=single output place ID. */
|
|
363
|
+
readonly simpleOutputPlaceId: Int32Array;
|
|
364
|
+
readonly inputPlaceCount: Uint32Array;
|
|
365
|
+
readonly inputPlaceMaskWords: readonly Uint32Array[];
|
|
366
|
+
readonly placeToTransitions: readonly (readonly number[])[];
|
|
367
|
+
readonly consumptionPlaceIds: readonly (readonly number[])[];
|
|
368
|
+
readonly cardinalityChecks: readonly (CardinalityCheck | null)[];
|
|
369
|
+
readonly hasGuards: readonly boolean[];
|
|
370
|
+
readonly allImmediate: boolean;
|
|
371
|
+
readonly allSamePriority: boolean;
|
|
372
|
+
readonly anyDeadlines: boolean;
|
|
373
|
+
private constructor();
|
|
374
|
+
static compile(net: PetriNet): PrecompiledNet;
|
|
375
|
+
static compileFrom(compiled: CompiledNet): PrecompiledNet;
|
|
376
|
+
/**
|
|
377
|
+
* Three-case sparse enablement check:
|
|
378
|
+
* 1. Empty mask (needsSingleWordIndex == -2): always passes
|
|
379
|
+
* 2. Single-word mask (>=0): one comparison
|
|
380
|
+
* 3. Multi-word mask (-1): iterate precomputed sparse indices
|
|
381
|
+
*/
|
|
382
|
+
canEnableSparse(tid: number, snapshot: Uint32Array): boolean;
|
|
383
|
+
}
|
|
384
|
+
|
|
385
|
+
/**
|
|
386
|
+
* @module precompiled-net-executor
|
|
387
|
+
*
|
|
388
|
+
* High-performance executor for Typed Coloured Time Petri Nets.
|
|
389
|
+
*
|
|
390
|
+
* **Architecture**: Uses `PrecompiledNet` for all transition/place data, replacing
|
|
391
|
+
* Map lookups and object traversals with typed-array indexing. Token storage uses
|
|
392
|
+
* simple per-place arrays, leveraging V8's optimized small-array shift/push.
|
|
393
|
+
*
|
|
394
|
+
* **Execution loop**: Same 5-phase structure as `BitmapNetExecutor`:
|
|
395
|
+
* 1. Process completed transitions — drain completionQueue, validate outputs
|
|
396
|
+
* 2. Process external events — inject tokens from EnvironmentPlaces
|
|
397
|
+
* 3. Update dirty transitions — sparse enablement via `canEnableSparse()`
|
|
398
|
+
* 4. Enforce deadlines — gated by `anyDeadlines` flag
|
|
399
|
+
* 5. Fire ready transitions — opcode dispatch or priority-queue path
|
|
400
|
+
*
|
|
401
|
+
* **Key optimizations over BitmapNetExecutor**:
|
|
402
|
+
* - Opcode-based consumption (switch on int vs object dispatch)
|
|
403
|
+
* - Cached place references (avoids compiled.place(pid) indirection)
|
|
404
|
+
* - Sparse enablement masks (skip zero words)
|
|
405
|
+
* - Lazy Marking sync (arrays are sole source of truth during execution)
|
|
406
|
+
* - Flat-array in-flight tracking (no Map overhead)
|
|
407
|
+
*
|
|
408
|
+
* @see PrecompiledNet for the precompiled data representation
|
|
409
|
+
* @see BitmapNetExecutor for the reference implementation
|
|
410
|
+
*/
|
|
411
|
+
|
|
412
|
+
interface PrecompiledNetExecutorOptions {
|
|
413
|
+
eventStore?: EventStore;
|
|
414
|
+
environmentPlaces?: Set<EnvironmentPlace<any>>;
|
|
415
|
+
longRunning?: boolean;
|
|
416
|
+
executionContextProvider?: (transitionName: string, consumed: Token<any>[]) => Map<string, unknown>;
|
|
417
|
+
/** Skip output spec validation for trusted actions (CONC-026). */
|
|
418
|
+
skipOutputValidation?: boolean;
|
|
419
|
+
/** Reuse a precompiled program (avoids recompilation). */
|
|
420
|
+
program?: PrecompiledNet;
|
|
421
|
+
}
|
|
422
|
+
/**
|
|
423
|
+
* High-performance executor using `PrecompiledNet`.
|
|
424
|
+
*
|
|
425
|
+
* Implements `PetriNetExecutor` with the same semantics as `BitmapNetExecutor`
|
|
426
|
+
* but with flat-array optimizations for lower per-transition overhead.
|
|
427
|
+
*/
|
|
428
|
+
declare class PrecompiledNetExecutor implements PetriNetExecutor {
|
|
429
|
+
private readonly program;
|
|
430
|
+
private readonly eventStore;
|
|
431
|
+
private readonly environmentPlaces;
|
|
432
|
+
private readonly longRunning;
|
|
433
|
+
private readonly executionContextProvider?;
|
|
434
|
+
private readonly skipOutputValidation;
|
|
435
|
+
private readonly startMs;
|
|
436
|
+
private readonly eventStoreEnabled;
|
|
437
|
+
/** Per-place token arrays, indexed by pid. */
|
|
438
|
+
private readonly tokenQueues;
|
|
439
|
+
private readonly markingBitmap;
|
|
440
|
+
private readonly dirtyBitmap;
|
|
441
|
+
private readonly dirtyScanBuffer;
|
|
442
|
+
private readonly enabledAtMs;
|
|
443
|
+
private readonly inFlightFlags;
|
|
444
|
+
private readonly enabledFlags;
|
|
445
|
+
private readonly transitionWords;
|
|
446
|
+
private enabledTransitionCount;
|
|
447
|
+
private readonly inFlightPromises;
|
|
448
|
+
private readonly inFlightContexts;
|
|
449
|
+
private readonly inFlightConsumed;
|
|
450
|
+
private readonly inFlightStartMs;
|
|
451
|
+
private readonly inFlightResolves;
|
|
452
|
+
private readonly inFlightErrors;
|
|
453
|
+
private inFlightCount;
|
|
454
|
+
private readonly pendingResetWords;
|
|
455
|
+
private hasPendingResets;
|
|
456
|
+
private readonly completionQueue;
|
|
457
|
+
private readonly externalQueue;
|
|
458
|
+
private wakeUpResolve;
|
|
459
|
+
private readonly markingSnapBuffer;
|
|
460
|
+
private readonly firingSnapBuffer;
|
|
461
|
+
private readonly awaitPromises;
|
|
462
|
+
private readonly racePromises;
|
|
463
|
+
private readonly readyBuffer;
|
|
464
|
+
private running;
|
|
465
|
+
private closed;
|
|
466
|
+
private marking;
|
|
467
|
+
constructor(net: PetriNet, initialTokens: Map<Place<any>, Token<any>[]>, options?: PrecompiledNetExecutorOptions);
|
|
468
|
+
private markTransitionDirty;
|
|
469
|
+
private markDirty;
|
|
470
|
+
private setMarkingBit;
|
|
471
|
+
private clearMarkingBit;
|
|
472
|
+
run(timeoutMs?: number): Promise<Marking>;
|
|
473
|
+
private executeLoop;
|
|
474
|
+
inject<T>(envPlace: EnvironmentPlace<T>, token: Token<T>): Promise<boolean>;
|
|
475
|
+
injectValue<T>(envPlace: EnvironmentPlace<T>, value: T): Promise<boolean>;
|
|
476
|
+
private initializeMarkingBitmap;
|
|
477
|
+
private markAllDirty;
|
|
478
|
+
private shouldTerminate;
|
|
479
|
+
private updateDirtyTransitions;
|
|
480
|
+
private enforceDeadlines;
|
|
481
|
+
private canEnable;
|
|
482
|
+
private countMatching;
|
|
483
|
+
private removeFirstMatching;
|
|
484
|
+
private hasInputFromResetPlace;
|
|
485
|
+
private fireReadyTransitions;
|
|
486
|
+
/**
|
|
487
|
+
* Fast path for nets where all transitions are immediate and same priority.
|
|
488
|
+
* Simple linear scan matching BitmapNetExecutor's pattern.
|
|
489
|
+
*/
|
|
490
|
+
private fireReadyImmediate;
|
|
491
|
+
private fireReadyGeneral;
|
|
492
|
+
private fireTransition;
|
|
493
|
+
private fireTransitionGuarded;
|
|
494
|
+
private updateBitmapAfterConsumption;
|
|
495
|
+
private processCompletedTransitions;
|
|
496
|
+
private processExternalEvents;
|
|
497
|
+
private drainPendingExternalEvents;
|
|
498
|
+
private awaitWork;
|
|
499
|
+
private millisUntilNextTimedTransition;
|
|
500
|
+
private wakeUp;
|
|
501
|
+
private hasDirtyBits;
|
|
502
|
+
private syncMarkingFromQueues;
|
|
503
|
+
getMarking(): Marking;
|
|
504
|
+
private snapshotMarking;
|
|
505
|
+
isQuiescent(): boolean;
|
|
506
|
+
executionId(): string;
|
|
507
|
+
close(): void;
|
|
508
|
+
private emitEvent;
|
|
509
|
+
}
|
|
510
|
+
|
|
307
511
|
/**
|
|
308
512
|
* Error thrown when a transition's output doesn't satisfy its declared Out spec.
|
|
309
513
|
*/
|
|
@@ -348,4 +552,4 @@ declare function validateOutSpec(tName: string, spec: Out, producedPlaceNames: S
|
|
|
348
552
|
*/
|
|
349
553
|
declare function produceTimeoutOutput(context: TransitionContext, timeoutChild: Out): void;
|
|
350
554
|
|
|
351
|
-
export { BitmapNetExecutor, type BitmapNetExecutorOptions, type CardinalityCheck, CompiledNet, EnvironmentPlace, EventStore, type GuardSpec, Marking, Out, OutViolationError, PetriNet, type PetriNetExecutor, Place, Token, Transition, TransitionContext, clearBit, containsAll, intersects, produceTimeoutOutput, setBit, testBit, validateOutSpec };
|
|
555
|
+
export { BitmapNetExecutor, type BitmapNetExecutorOptions, type CardinalityCheck, CompiledNet, EnvironmentPlace, EventStore, type GuardSpec, Marking, Out, OutViolationError, PetriNet, type PetriNetExecutor, Place, PrecompiledNet, PrecompiledNetExecutor, type PrecompiledNetExecutorOptions, Token, Transition, TransitionContext, clearBit, containsAll, intersects, produceTimeoutOutput, setBit, testBit, validateOutSpec };
|