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
use std::sync::Arc;

/// Actions are just boxed immutable functions that take an argument of the event that triggered them
pub type Action<E> = Box<Fn(E)>;
/// StoredActions just enable structures holding the Action to be cloned
type StoredAction<E> = Arc<Action<E>>;

/// The Transition records, for a given current state, what event type triggers it to move to
/// what state, performing a specific action on the transition
#[derive(Clone)]
struct Transition<S: Into<u32> + Clone, E: Into<u32> + Clone> {
	event_type: E,
	next_state: S,
	action: StoredAction<E>,
}

/// The StateTransition records all Transitions for a given state
#[derive(Clone)]
struct StateTransitions<S: Into<u32> + Clone, E: Into<u32> + Clone> {
	start_state: S,
	edges: Vec<Option<Transition<S, E>>>,
}

/// The Machine is the Finite State Machine, which has a current state and set of all valid
/// transitions
#[derive(Clone)]
pub struct Machine<S: Into<u32> + Clone, E: Into<u32> + Clone> {
	state: S,
	transitions: Vec<Option<StateTransitions<S, E>>>,
}

impl<S: Into<u32> + Clone, E: Into<u32> + Clone> Machine<S, E> {
	/// Constructs a new FSM with a given initial state
	pub fn new(initial_state: S) -> Machine<S, E> {
		Machine {
			state: initial_state,
			transitions: Vec::new(),
		}
	}

	/// Registers a new valid transition with the FSM
	pub fn add_transition(&mut self, in_state: S, on_event: E, next_state: S, action: Action<E>) -> bool {
		let state_index: u32 = in_state.clone().into();
		let event_index: u32 = on_event.clone().into();
		let mut result = false;

		if state_index as usize >= self.transitions.capacity() {
			let new_len = self.transitions.len() * 2;
			self.transitions.reserve(new_len);
		}
		while state_index as usize >= self.transitions.len() {
			self.transitions.push(None);
		}

		let create_state_entry = match self.transitions[state_index as usize] {
			Some(_) => false,
			None => true,
		};

		if create_state_entry {
			self.transitions[state_index as usize] = Some(StateTransitions {
				start_state: in_state,
				edges: Vec::new(),
			});
		}

		if let &mut Some(ref mut transitions) = &mut self.transitions[state_index as usize] {
			if event_index as usize >= transitions.edges.len() {
				let new_len = transitions.edges.len() * 2;
				transitions.edges.reserve(new_len);
			}
			while event_index as usize >= transitions.edges.len() {
				transitions.edges.push(None);
			}

			let transition = &mut transitions.edges[event_index as usize];

			*transition = match &*transition {
				&Some(ref t) => Some((*t).clone()),
				&None => {
					result = true;
					Some(Transition {
						event_type: on_event,
						next_state: next_state,
						action: Arc::new(action),
					})
				}
			};
		}

		result
	}

	/// Retrieves a reference to the current state
	pub fn current_state(&self) -> &S {
		&self.state
	}

	/// Tick the State Machine with an Event
	pub fn on_event(&mut self, event_type: E) {
		let event_index: u32 = event_type.clone().into();
		let state_index: u32 = self.state.clone().into();
		
		if state_index as usize >= self.transitions.len() {
			return;
		}

		if let &Some(ref state_transitions) = &self.transitions[state_index as usize] {
			if event_index as usize >= state_transitions.edges.len() {
				return;
			}

			if let &Some(ref transition) = &state_transitions.edges[event_index as usize] {
				self.state = transition.next_state.clone();
				(*(*transition).action)(event_type);
			}
		}
	}
}

#[cfg(test)]
mod test {
	use super::*;

	#[derive(Clone, Debug, Eq, PartialEq)]
	enum TurnStyleState {
		Locked,
		Unlocked,
	}

	#[derive(Clone, Debug, Eq, PartialEq)]
	enum TurnStyleEvent {
		Push,
		InsertCoin,
	}

	impl Into<u32> for TurnStyleState {
		fn into(self) -> u32 {
			self as u32
		}
	}

	impl Into<u32> for TurnStyleEvent {
		fn into(self) -> u32 {
			self as u32
		}
	}

	#[test]
	fn test_machine() {
		let mut machine = Machine::new(TurnStyleState::Locked);
		machine.add_transition(
			TurnStyleState::Locked, TurnStyleEvent::InsertCoin,
			TurnStyleState::Unlocked, Box::new(|_| println!("unlocked"))
		);
		machine.add_transition(
			TurnStyleState::Unlocked, TurnStyleEvent::Push,
			TurnStyleState::Locked, Box::new(|_| println!("locked"))
		);
		assert!(*machine.current_state() == TurnStyleState::Locked);
		machine.on_event(TurnStyleEvent::Push);
		assert!(*machine.current_state() == TurnStyleState::Locked);
		machine.on_event(TurnStyleEvent::InsertCoin);
		assert!(*machine.current_state() == TurnStyleState::Unlocked);
		machine.on_event(TurnStyleEvent::InsertCoin);
		assert!(*machine.current_state() == TurnStyleState::Unlocked);
		machine.on_event(TurnStyleEvent::Push);
		assert!(*machine.current_state() == TurnStyleState::Locked);
	}
}