1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
use std::collections::{BTreeMap, BTreeSet, VecDeque};
use std::fmt::Debug;
use std::hash::Hash;
use crate::error::ValidationError;
/// Classifies how an event behaves in the FSM.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, serde::Serialize, serde::Deserialize)]
pub enum EventKind {
/// Standard event — requires explicit triggering.
Normal,
/// Hard event (suffix `!`) — auto-fires immediately after entering the source state.
Hard,
/// Soft event (suffix `?`) — silently ignored if the current state doesn't respond.
Soft,
}
/// A single transition definition in the FSM graph.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Transition<S, E> {
/// Source state.
pub from: S,
/// Allowed target states (on_transition picks one).
pub to: Vec<S>,
/// The event that triggers this transition.
pub event: E,
/// Classification of the event.
pub kind: EventKind,
}
/// The validated transition graph for a finite state machine.
///
/// Holds all states, events, and transitions, with an index for fast lookup.
/// Created at compile time by the `#[finitomata]` macro or at runtime via
/// [`TransitionGraph::new`].
#[derive(Debug, Clone)]
pub struct TransitionGraph<S: Ord + Clone, E: Ord + Clone> {
initial: S,
finals: BTreeSet<S>,
transitions: Vec<Transition<S, E>>,
index: BTreeMap<S, Vec<usize>>,
}
impl<S, E> TransitionGraph<S, E>
where
S: Ord + Clone + Hash + Debug + Eq,
E: Ord + Clone + Hash + Debug + Eq,
{
/// Constructs a new transition graph from an initial state, final states, and transitions.
pub fn new(initial: S, finals: BTreeSet<S>, transitions: Vec<Transition<S, E>>) -> Self {
let mut index: BTreeMap<S, Vec<usize>> = BTreeMap::new();
for (i, t) in transitions.iter().enumerate() {
index.entry(t.from.clone()).or_default().push(i);
}
Self {
initial,
finals,
transitions,
index,
}
}
/// Returns the initial (starting) state of the FSM.
pub fn initial_state(&self) -> &S {
&self.initial
}
/// Returns the set of final (terminal) states.
pub fn final_states(&self) -> &BTreeSet<S> {
&self.finals
}
/// Returns `true` if the given state is a final state.
pub fn is_final(&self, state: &S) -> bool {
self.finals.contains(state)
}
/// Returns the full list of transitions.
pub fn transitions(&self) -> &[Transition<S, E>] {
&self.transitions
}
/// Checks if a transition from `from` via `event` is allowed.
/// Returns the allowed target states and the event kind, or `None`.
pub fn allowed(&self, from: &S, event: &E) -> Option<(&[S], EventKind)> {
self.index.get(from).and_then(|indices| {
indices.iter().find_map(|&i| {
let t = &self.transitions[i];
if &t.event == event {
Some((t.to.as_slice(), t.kind))
} else {
None
}
})
})
}
/// Returns `true` if the given state responds to the given event.
pub fn responds(&self, state: &S, event: &E) -> bool {
self.allowed(state, event).is_some()
}
/// Returns all events available from the given state, with their kinds.
pub fn events_for(&self, state: &S) -> Vec<(&E, EventKind)> {
self.index
.get(state)
.map(|indices| {
indices
.iter()
.map(|&i| (&self.transitions[i].event, self.transitions[i].kind))
.collect()
})
.unwrap_or_default()
}
/// Returns all states mentioned in the graph (initial, final, and transition participants).
pub fn all_states(&self) -> BTreeSet<S> {
let mut states = BTreeSet::new();
states.insert(self.initial.clone());
for s in &self.finals {
states.insert(s.clone());
}
for t in &self.transitions {
states.insert(t.from.clone());
for to in &t.to {
states.insert(to.clone());
}
}
states
}
/// Returns all distinct events in the graph.
pub fn all_events(&self) -> BTreeSet<&E> {
self.transitions.iter().map(|t| &t.event).collect()
}
/// Finds the shortest path (sequence of events and intermediate states)
/// from `from` to `to` using BFS. Returns `None` if unreachable.
pub fn shortest_path(&self, from: &S, to: &S) -> Option<Vec<(E, S)>> {
if from == to {
return Some(vec![]);
}
let mut visited = BTreeSet::new();
let mut queue: VecDeque<(S, Vec<(E, S)>)> = VecDeque::new();
queue.push_back((from.clone(), vec![]));
visited.insert(from.clone());
while let Some((current, path)) = queue.pop_front() {
if let Some(indices) = self.index.get(¤t) {
for &i in indices {
let t = &self.transitions[i];
for target in &t.to {
if target == to {
let mut result = path.clone();
result.push((t.event.clone(), target.clone()));
return Some(result);
}
if !visited.contains(target) {
visited.insert(target.clone());
let mut new_path = path.clone();
new_path.push((t.event.clone(), target.clone()));
queue.push_back((target.clone(), new_path));
}
}
}
}
}
None
}
/// Validates the graph for structural correctness.
/// Returns `Ok(())` if valid, or a list of validation errors.
pub fn validate(&self) -> Result<(), Vec<ValidationError>> {
let mut errors = Vec::new();
let all_states = self.all_states();
if self.finals.is_empty() {
errors.push(ValidationError::NoFinalState);
}
let reachable = self.reachable_from(&self.initial);
for state in &all_states {
if state != &self.initial && !reachable.contains(state) {
errors.push(ValidationError::UnreachableState(format!("{state:?}")));
}
}
if errors.is_empty() {
Ok(())
} else {
Err(errors)
}
}
fn reachable_from(&self, start: &S) -> BTreeSet<S> {
let mut visited = BTreeSet::new();
let mut queue = VecDeque::new();
queue.push_back(start.clone());
while let Some(current) = queue.pop_front() {
if visited.contains(¤t) {
continue;
}
visited.insert(current.clone());
if let Some(indices) = self.index.get(¤t) {
for &i in indices {
for target in &self.transitions[i].to {
if !visited.contains(target) {
queue.push_back(target.clone());
}
}
}
}
}
visited
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_basic_graph() {
let graph: TransitionGraph<&str, &str> = TransitionGraph::new(
"idle",
BTreeSet::from(["done"]),
vec![
Transition {
from: "idle",
to: vec!["running"],
event: "start",
kind: EventKind::Normal,
},
Transition {
from: "running",
to: vec!["done"],
event: "finish",
kind: EventKind::Normal,
},
],
);
assert_eq!(graph.initial_state(), &"idle");
assert!(graph.is_final(&"done"));
assert!(graph.responds(&"idle", &"start"));
assert!(!graph.responds(&"idle", &"finish"));
assert!(graph.validate().is_ok());
}
#[test]
fn test_shortest_path() {
let graph: TransitionGraph<&str, &str> = TransitionGraph::new(
"a",
BTreeSet::from(["c"]),
vec![
Transition {
from: "a",
to: vec!["b"],
event: "go",
kind: EventKind::Normal,
},
Transition {
from: "b",
to: vec!["c"],
event: "end",
kind: EventKind::Normal,
},
],
);
let path = graph.shortest_path(&"a", &"c").unwrap();
assert_eq!(path, vec![("go", "b"), ("end", "c")]);
}
#[test]
fn test_unreachable_state() {
let graph: TransitionGraph<&str, &str> = TransitionGraph::new(
"a",
BTreeSet::from(["c"]),
vec![
Transition {
from: "a",
to: vec!["b"],
event: "go",
kind: EventKind::Normal,
},
Transition {
from: "c",
to: vec!["a"],
event: "loop",
kind: EventKind::Normal,
},
],
);
let errors = graph.validate().unwrap_err();
assert!(
errors
.iter()
.any(|e| matches!(e, ValidationError::UnreachableState(_)))
);
}
#[test]
fn test_no_final_state() {
let graph: TransitionGraph<&str, &str> = TransitionGraph::new(
"a",
BTreeSet::new(),
vec![Transition {
from: "a",
to: vec!["b"],
event: "go",
kind: EventKind::Normal,
}],
);
let errors = graph.validate().unwrap_err();
assert!(
errors
.iter()
.any(|e| matches!(e, ValidationError::NoFinalState))
);
}
}