Skip to main content

libpetri_event/
net_event.rs

1use std::collections::HashMap;
2use std::sync::Arc;
3
4/// All observable events during Petri net execution.
5///
6/// 13 event types as a discriminated union matching the TypeScript/Java implementations.
7#[derive(Debug, Clone)]
8pub enum NetEvent {
9    /// Net execution started.
10    ExecutionStarted { net_name: Arc<str>, timestamp: u64 },
11    /// Net execution completed (no more enabled transitions, no in-flight actions).
12    ExecutionCompleted { net_name: Arc<str>, timestamp: u64 },
13    /// Transition became enabled (all input/inhibitor/read conditions satisfied).
14    TransitionEnabled {
15        transition_name: Arc<str>,
16        timestamp: u64,
17    },
18    /// Transition's enabling clock restarted (input place tokens changed while enabled).
19    TransitionClockRestarted {
20        transition_name: Arc<str>,
21        timestamp: u64,
22    },
23    /// Transition started firing (tokens consumed, action dispatched).
24    TransitionStarted {
25        transition_name: Arc<str>,
26        timestamp: u64,
27    },
28    /// Transition completed successfully (outputs produced).
29    TransitionCompleted {
30        transition_name: Arc<str>,
31        timestamp: u64,
32    },
33    /// Transition action failed with error.
34    TransitionFailed {
35        transition_name: Arc<str>,
36        error: String,
37        timestamp: u64,
38    },
39    /// Transition exceeded its timing deadline and was force-disabled.
40    TransitionTimedOut {
41        transition_name: Arc<str>,
42        timestamp: u64,
43    },
44    /// Transition action exceeded its action timeout.
45    ActionTimedOut {
46        transition_name: Arc<str>,
47        timeout_ms: u64,
48        timestamp: u64,
49    },
50    /// Token added to a place.
51    TokenAdded {
52        place_name: Arc<str>,
53        timestamp: u64,
54    },
55    /// Token removed from a place.
56    TokenRemoved {
57        place_name: Arc<str>,
58        timestamp: u64,
59    },
60    /// Log message emitted by a transition action.
61    LogMessage {
62        transition_name: Arc<str>,
63        level: String,
64        message: String,
65        timestamp: u64,
66    },
67    /// Snapshot of the current marking (token counts per place).
68    MarkingSnapshot {
69        marking: HashMap<Arc<str>, usize>,
70        timestamp: u64,
71    },
72}
73
74impl NetEvent {
75    /// Returns the timestamp of this event.
76    pub fn timestamp(&self) -> u64 {
77        match self {
78            NetEvent::ExecutionStarted { timestamp, .. }
79            | NetEvent::ExecutionCompleted { timestamp, .. }
80            | NetEvent::TransitionEnabled { timestamp, .. }
81            | NetEvent::TransitionClockRestarted { timestamp, .. }
82            | NetEvent::TransitionStarted { timestamp, .. }
83            | NetEvent::TransitionCompleted { timestamp, .. }
84            | NetEvent::TransitionFailed { timestamp, .. }
85            | NetEvent::TransitionTimedOut { timestamp, .. }
86            | NetEvent::ActionTimedOut { timestamp, .. }
87            | NetEvent::TokenAdded { timestamp, .. }
88            | NetEvent::TokenRemoved { timestamp, .. }
89            | NetEvent::LogMessage { timestamp, .. }
90            | NetEvent::MarkingSnapshot { timestamp, .. } => *timestamp,
91        }
92    }
93
94    /// Returns the transition name if this event is transition-related.
95    pub fn transition_name(&self) -> Option<&str> {
96        match self {
97            NetEvent::TransitionEnabled {
98                transition_name, ..
99            }
100            | NetEvent::TransitionClockRestarted {
101                transition_name, ..
102            }
103            | NetEvent::TransitionStarted {
104                transition_name, ..
105            }
106            | NetEvent::TransitionCompleted {
107                transition_name, ..
108            }
109            | NetEvent::TransitionFailed {
110                transition_name, ..
111            }
112            | NetEvent::TransitionTimedOut {
113                transition_name, ..
114            }
115            | NetEvent::ActionTimedOut {
116                transition_name, ..
117            }
118            | NetEvent::LogMessage {
119                transition_name, ..
120            } => Some(transition_name),
121            _ => None,
122        }
123    }
124
125    /// Returns true if this is a failure event.
126    pub fn is_failure(&self) -> bool {
127        matches!(
128            self,
129            NetEvent::TransitionFailed { .. }
130                | NetEvent::TransitionTimedOut { .. }
131                | NetEvent::ActionTimedOut { .. }
132        )
133    }
134}