libpetri 0.3.4 → 0.4.0

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.
@@ -0,0 +1,408 @@
1
+ import { a as PetriNet, b as Transition, T as Token } from '../petri-net-C3Jy5HCt.js';
2
+ import { E as EventStore, N as NetEvent } from '../event-store-Y8q_wapJ.js';
3
+
4
+ /**
5
+ * Commands sent from debug UI client to server via WebSocket.
6
+ * TypeScript port of Java's DebugCommand sealed interface.
7
+ */
8
+ type SubscriptionMode = 'live' | 'replay';
9
+ type BreakpointType = 'TRANSITION_ENABLED' | 'TRANSITION_START' | 'TRANSITION_COMPLETE' | 'TRANSITION_FAIL' | 'TOKEN_ADDED' | 'TOKEN_REMOVED';
10
+ interface BreakpointConfig {
11
+ readonly id: string;
12
+ readonly type: BreakpointType;
13
+ readonly target: string | null;
14
+ readonly enabled: boolean;
15
+ }
16
+ interface EventFilter {
17
+ readonly eventTypes: readonly string[] | null;
18
+ readonly transitionNames: readonly string[] | null;
19
+ readonly placeNames: readonly string[] | null;
20
+ }
21
+ type DebugCommand = {
22
+ readonly type: 'subscribe';
23
+ readonly sessionId: string;
24
+ readonly mode: SubscriptionMode;
25
+ readonly fromIndex?: number;
26
+ } | {
27
+ readonly type: 'unsubscribe';
28
+ readonly sessionId: string;
29
+ } | {
30
+ readonly type: 'listSessions';
31
+ readonly limit?: number;
32
+ readonly activeOnly?: boolean;
33
+ } | {
34
+ readonly type: 'seek';
35
+ readonly sessionId: string;
36
+ readonly timestamp: string;
37
+ } | {
38
+ readonly type: 'playbackSpeed';
39
+ readonly sessionId: string;
40
+ readonly speed: number;
41
+ } | {
42
+ readonly type: 'filter';
43
+ readonly sessionId: string;
44
+ readonly filter: EventFilter;
45
+ } | {
46
+ readonly type: 'pause';
47
+ readonly sessionId: string;
48
+ } | {
49
+ readonly type: 'resume';
50
+ readonly sessionId: string;
51
+ } | {
52
+ readonly type: 'stepForward';
53
+ readonly sessionId: string;
54
+ } | {
55
+ readonly type: 'stepBackward';
56
+ readonly sessionId: string;
57
+ } | {
58
+ readonly type: 'setBreakpoint';
59
+ readonly sessionId: string;
60
+ readonly breakpoint: BreakpointConfig;
61
+ } | {
62
+ readonly type: 'clearBreakpoint';
63
+ readonly sessionId: string;
64
+ readonly breakpointId: string;
65
+ } | {
66
+ readonly type: 'listBreakpoints';
67
+ readonly sessionId: string;
68
+ };
69
+ declare function eventFilterAll(): EventFilter;
70
+
71
+ /**
72
+ * Responses sent from server to debug UI client via WebSocket.
73
+ * TypeScript port of Java's DebugResponse sealed interface.
74
+ */
75
+
76
+ interface SessionSummary {
77
+ readonly sessionId: string;
78
+ readonly netName: string;
79
+ readonly startTime: string;
80
+ readonly active: boolean;
81
+ readonly eventCount: number;
82
+ }
83
+ interface TokenInfo {
84
+ readonly id: string | null;
85
+ readonly type: string;
86
+ readonly value: string | null;
87
+ readonly timestamp: string | null;
88
+ }
89
+ interface NetEventInfo {
90
+ readonly type: string;
91
+ readonly timestamp: string;
92
+ readonly transitionName: string | null;
93
+ readonly placeName: string | null;
94
+ readonly details: Record<string, unknown>;
95
+ }
96
+ interface PlaceInfo {
97
+ readonly name: string;
98
+ readonly graphId: string;
99
+ readonly tokenType: string;
100
+ readonly isStart: boolean;
101
+ readonly isEnd: boolean;
102
+ readonly isEnvironment: boolean;
103
+ }
104
+ interface TransitionInfo {
105
+ readonly name: string;
106
+ readonly graphId: string;
107
+ }
108
+ interface NetStructure {
109
+ readonly places: readonly PlaceInfo[];
110
+ readonly transitions: readonly TransitionInfo[];
111
+ }
112
+ type DebugResponse = {
113
+ readonly type: 'sessionList';
114
+ readonly sessions: readonly SessionSummary[];
115
+ } | {
116
+ readonly type: 'subscribed';
117
+ readonly sessionId: string;
118
+ readonly netName: string;
119
+ readonly dotDiagram: string;
120
+ readonly structure: NetStructure;
121
+ readonly currentMarking: Record<string, readonly TokenInfo[]>;
122
+ readonly enabledTransitions: readonly string[];
123
+ readonly inFlightTransitions: readonly string[];
124
+ readonly eventCount: number;
125
+ readonly mode: string;
126
+ } | {
127
+ readonly type: 'unsubscribed';
128
+ readonly sessionId: string;
129
+ } | {
130
+ readonly type: 'event';
131
+ readonly sessionId: string;
132
+ readonly index: number;
133
+ readonly event: NetEventInfo;
134
+ } | {
135
+ readonly type: 'eventBatch';
136
+ readonly sessionId: string;
137
+ readonly startIndex: number;
138
+ readonly events: readonly NetEventInfo[];
139
+ readonly hasMore: boolean;
140
+ } | {
141
+ readonly type: 'markingSnapshot';
142
+ readonly sessionId: string;
143
+ readonly marking: Record<string, readonly TokenInfo[]>;
144
+ readonly enabledTransitions: readonly string[];
145
+ readonly inFlightTransitions: readonly string[];
146
+ } | {
147
+ readonly type: 'playbackStateChanged';
148
+ readonly sessionId: string;
149
+ readonly paused: boolean;
150
+ readonly speed: number;
151
+ readonly currentIndex: number;
152
+ } | {
153
+ readonly type: 'filterApplied';
154
+ readonly sessionId: string;
155
+ readonly filter: EventFilter;
156
+ } | {
157
+ readonly type: 'breakpointHit';
158
+ readonly sessionId: string;
159
+ readonly breakpointId: string;
160
+ readonly event: NetEventInfo;
161
+ readonly eventIndex: number;
162
+ } | {
163
+ readonly type: 'breakpointList';
164
+ readonly sessionId: string;
165
+ readonly breakpoints: readonly BreakpointConfig[];
166
+ } | {
167
+ readonly type: 'breakpointSet';
168
+ readonly sessionId: string;
169
+ readonly breakpoint: BreakpointConfig;
170
+ } | {
171
+ readonly type: 'breakpointCleared';
172
+ readonly sessionId: string;
173
+ readonly breakpointId: string;
174
+ } | {
175
+ readonly type: 'error';
176
+ readonly code: string;
177
+ readonly message: string;
178
+ readonly sessionId: string | null;
179
+ };
180
+
181
+ /**
182
+ * Analyze places for start/end/environment classification.
183
+ * TypeScript port of Java's PlaceAnalysis.
184
+ */
185
+
186
+ interface PlaceAnalysisInfo {
187
+ readonly tokenType: string;
188
+ readonly hasIncoming: boolean;
189
+ readonly hasOutgoing: boolean;
190
+ }
191
+ declare class PlaceAnalysis {
192
+ private readonly _data;
193
+ constructor(data: ReadonlyMap<string, PlaceAnalysisInfo>);
194
+ get data(): ReadonlyMap<string, PlaceAnalysisInfo>;
195
+ isStart(placeName: string): boolean;
196
+ isEnd(placeName: string): boolean;
197
+ /** Build place analysis from a PetriNet. */
198
+ static from(net: PetriNet): PlaceAnalysis;
199
+ }
200
+
201
+ /**
202
+ * EventStore with live tailing and historical replay.
203
+ * TypeScript port of Java's DebugEventStore.
204
+ *
205
+ * Node.js simplifications: single-threaded, no locks needed,
206
+ * synchronous Set<Function> for subscribers, microtask broadcast.
207
+ */
208
+
209
+ /** Handle for managing a live event subscription. */
210
+ interface Subscription {
211
+ cancel(): void;
212
+ isActive(): boolean;
213
+ }
214
+ /** Default maximum events to retain before evicting oldest. */
215
+ declare const DEFAULT_MAX_EVENTS = 10000;
216
+ declare class DebugEventStore implements EventStore {
217
+ private readonly _events;
218
+ private readonly _subscribers;
219
+ private readonly _sessionId;
220
+ private readonly _maxEvents;
221
+ private _eventCount;
222
+ private _evictedCount;
223
+ constructor(sessionId: string, maxEvents?: number);
224
+ get sessionId(): string;
225
+ get maxEvents(): number;
226
+ /** Total events appended (including evicted). */
227
+ eventCount(): number;
228
+ /** Number of events evicted from the store. */
229
+ evictedCount(): number;
230
+ append(event: NetEvent): void;
231
+ events(): readonly NetEvent[];
232
+ isEnabled(): boolean;
233
+ size(): number;
234
+ isEmpty(): boolean;
235
+ /** Subscribe to receive events as they occur. */
236
+ subscribe(listener: (event: NetEvent) => void): Subscription;
237
+ /** Number of active subscribers. */
238
+ subscriberCount(): number;
239
+ /** Returns events starting from a specific index. */
240
+ eventsFrom(fromIndex: number): readonly NetEvent[];
241
+ /** Returns all events since the specified timestamp. */
242
+ eventsSince(from: number): readonly NetEvent[];
243
+ /** Returns events within a time range. */
244
+ eventsBetween(from: number, to: number): readonly NetEvent[];
245
+ /** Close the store (no-op in JS, but matches Java interface). */
246
+ close(): void;
247
+ }
248
+
249
+ /**
250
+ * Registry for managing Petri net debug sessions.
251
+ * TypeScript port of Java's DebugSessionRegistry.
252
+ */
253
+
254
+ interface DebugSession {
255
+ readonly sessionId: string;
256
+ readonly netName: string;
257
+ readonly dotDiagram: string;
258
+ readonly places: PlaceAnalysis;
259
+ readonly transitions: ReadonlySet<Transition>;
260
+ readonly eventStore: DebugEventStore;
261
+ readonly startTime: number;
262
+ readonly active: boolean;
263
+ }
264
+ type EventStoreFactory = (sessionId: string) => DebugEventStore;
265
+ declare class DebugSessionRegistry {
266
+ private readonly _sessions;
267
+ private readonly _maxSessions;
268
+ private readonly _eventStoreFactory;
269
+ constructor(maxSessions?: number, eventStoreFactory?: EventStoreFactory);
270
+ /**
271
+ * Registers a new debug session for the given Petri net.
272
+ * Generates DOT diagram and extracts net structure.
273
+ */
274
+ register(sessionId: string, net: PetriNet): DebugSession;
275
+ /** Marks a session as completed (no longer active). */
276
+ complete(sessionId: string): void;
277
+ /** Removes a session from the registry. */
278
+ remove(sessionId: string): DebugSession | undefined;
279
+ /** Returns a session by ID. */
280
+ getSession(sessionId: string): DebugSession | undefined;
281
+ /** Lists sessions, ordered by start time (most recent first). */
282
+ listSessions(limit: number): readonly DebugSession[];
283
+ /** Lists only active sessions. */
284
+ listActiveSessions(limit: number): readonly DebugSession[];
285
+ /** Total number of sessions. */
286
+ get size(): number;
287
+ /** Evicts oldest inactive sessions if at capacity. */
288
+ private evictIfNecessary;
289
+ }
290
+
291
+ /**
292
+ * Framework-agnostic handler for the Petri net debug protocol.
293
+ * TypeScript port of Java's DebugProtocolHandler.
294
+ *
295
+ * Manages debug subscriptions, event filtering, breakpoints, and replay
296
+ * for connected clients. Decoupled from any specific WebSocket framework
297
+ * via the ResponseSink type.
298
+ */
299
+
300
+ /** Callback for sending responses to a connected client. */
301
+ type ResponseSink = (response: DebugResponse) => void;
302
+ /** Computed state from replaying events. */
303
+ interface ComputedState {
304
+ readonly marking: ReadonlyMap<string, readonly TokenInfo[]>;
305
+ readonly enabledTransitions: readonly string[];
306
+ readonly inFlightTransitions: readonly string[];
307
+ }
308
+ declare class DebugProtocolHandler {
309
+ private readonly _sessionRegistry;
310
+ private readonly _clients;
311
+ constructor(sessionRegistry: DebugSessionRegistry);
312
+ /** Registers a new client connection. */
313
+ clientConnected(clientId: string, sink: ResponseSink): void;
314
+ /** Cleans up when a client disconnects. */
315
+ clientDisconnected(clientId: string): void;
316
+ /** Handles a command from a connected client. */
317
+ handleCommand(clientId: string, command: DebugCommand): void;
318
+ private handleListSessions;
319
+ private handleSubscribe;
320
+ private subscribeLive;
321
+ private subscribeReplay;
322
+ private handleUnsubscribe;
323
+ private handleSeek;
324
+ private handlePlaybackSpeed;
325
+ private handleSetFilter;
326
+ private handlePause;
327
+ private handleResume;
328
+ private handleStepForward;
329
+ private handleStepBackward;
330
+ private handleSetBreakpoint;
331
+ private handleClearBreakpoint;
332
+ private handleListBreakpoints;
333
+ private send;
334
+ private sendError;
335
+ private sendInBatches;
336
+ }
337
+
338
+ /**
339
+ * Caches computed state snapshots at periodic intervals for efficient seek/step.
340
+ * TypeScript port of Java's MarkingCache.
341
+ */
342
+
343
+ /** Number of events between cached snapshots. */
344
+ declare const SNAPSHOT_INTERVAL = 256;
345
+ declare class MarkingCache {
346
+ private readonly _snapshots;
347
+ /**
348
+ * Computes the state at the given event index, using cached snapshots
349
+ * to minimize the number of events that need to be replayed.
350
+ *
351
+ * @param events the full event list for the session
352
+ * @param targetIndex event index to compute state at (exclusive upper bound)
353
+ */
354
+ computeAt(events: readonly NetEvent[], targetIndex: number): ComputedState;
355
+ /** Invalidates the cache. */
356
+ invalidate(): void;
357
+ /** Extends the snapshot cache to cover at least up to the given event index. */
358
+ private ensureCachedUpTo;
359
+ }
360
+
361
+ /**
362
+ * Converts CTPN NetEvent instances to serializable DebugResponse types.
363
+ * TypeScript port of Java's NetEventConverter.
364
+ */
365
+
366
+ /** Converts a NetEvent to a serializable NetEventInfo. */
367
+ declare function toEventInfo(event: NetEvent, compact?: boolean): NetEventInfo;
368
+ /** Converts a Token to serializable TokenInfo with full value. */
369
+ declare function tokenInfo(token: Token<unknown>): TokenInfo;
370
+ /** Converts a Token to compact TokenInfo (type only, no value). */
371
+ declare function compactTokenInfo(token: Token<unknown>): TokenInfo;
372
+ /** Converts a marking map to serializable form. */
373
+ declare function convertMarking(marking: ReadonlyMap<string, readonly Token<unknown>[]>, compact?: boolean): Record<string, readonly TokenInfo[]>;
374
+
375
+ /**
376
+ * EventStore that delegates to both a primary store and a debug store.
377
+ * TypeScript port of Java's DebugAwareEventStore.
378
+ */
379
+
380
+ declare class DebugAwareEventStore implements EventStore {
381
+ private readonly _primary;
382
+ private readonly _debugStore;
383
+ constructor(primary: EventStore, debugStore: DebugEventStore);
384
+ append(event: NetEvent): void;
385
+ events(): readonly NetEvent[];
386
+ isEnabled(): boolean;
387
+ size(): number;
388
+ isEmpty(): boolean;
389
+ /** Returns the underlying debug store for subscription management. */
390
+ get debugStore(): DebugEventStore;
391
+ }
392
+
393
+ /**
394
+ * Debug infrastructure for Petri net execution visualization.
395
+ *
396
+ * Provides a framework-agnostic debug protocol handler, event store with
397
+ * live tailing, session registry, and marking cache for efficient replay.
398
+ *
399
+ * @module debug
400
+ */
401
+
402
+ /**
403
+ * Returns the path to the bundled debug UI assets directory.
404
+ * Requires Node.js — resolves relative to this module's location.
405
+ */
406
+ declare function debugUiAssetPath(): Promise<string>;
407
+
408
+ export { type BreakpointConfig, type BreakpointType, type ComputedState, DEFAULT_MAX_EVENTS, DebugAwareEventStore, type DebugCommand, DebugEventStore, DebugProtocolHandler, type DebugResponse, type DebugSession, DebugSessionRegistry, type EventFilter, type EventStoreFactory, MarkingCache, type NetEventInfo, type NetStructure, PlaceAnalysis, type PlaceAnalysisInfo, type PlaceInfo, type ResponseSink, SNAPSHOT_INTERVAL, type SessionSummary, type Subscription, type SubscriptionMode, type TokenInfo, type TransitionInfo, compactTokenInfo, convertMarking, debugUiAssetPath, eventFilterAll, toEventInfo, tokenInfo };