-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstate.rb
More file actions
executable file
·47 lines (36 loc) · 1.09 KB
/
state.rb
File metadata and controls
executable file
·47 lines (36 loc) · 1.09 KB
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
require 'transition'
class State
attr_reader :name, :transitions
def initialize(name, initial)
@name = name
@initial = initial
@transitions = Hash.new
end
def initial?
@initial == true
end
def add_transition(to_state, input, action)
initialize_transitions(input)
add_transition_for_input(action, input, to_state)
end
def get_reachable_states(state_machine)
reachable_states = Array.new
all_transitions = Array.new
all_transitions.push(@transitions.values)
all_transitions.flatten.each do |transition|
to_state = state_machine.get_state(transition.to)
reachable_states.push(to_state) if not to_state.nil?
end
return reachable_states
end
private
def add_transition_for_input(action, input, to_state)
@transitions[input].push(Transition.new(self.name, sanitize_to_state(to_state), input, action))
end
def sanitize_to_state(to_state)
return (to_state.nil? or to_state.empty?) ? self.name : to_state;
end
def initialize_transitions(input)
@transitions[input] = Array.new if @transitions[input].nil?
end
end