Module: Philiprehberger::StateMachine::Validation
- Defined in:
- lib/philiprehberger/state_machine/validation.rb
Overview
Validates state machine definitions for unreachable states and other issues.
Class Method Summary collapse
-
.predecessors(definition, state) ⇒ Array<Symbol>
Find all states that can transition to the given state.
-
.reachable_states(definition) ⇒ Array<Symbol>
Find all states reachable from the initial state via BFS.
-
.unreachable_states(definition) ⇒ Array<Symbol>
Find states that can never be reached from the initial state.
Class Method Details
.predecessors(definition, state) ⇒ Array<Symbol>
Find all states that can transition to the given state.
43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/philiprehberger/state_machine/validation.rb', line 43 def self.predecessors(definition, state) result = [] definition.events.each_value do |transitions| transitions.each do |t| next unless t.to == state Array(t.from).each do |from_state| result << from_state unless result.include?(from_state) end end end result end |
.reachable_states(definition) ⇒ Array<Symbol>
Find all states reachable from the initial state via BFS.
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/philiprehberger/state_machine/validation.rb', line 20 def self.reachable_states(definition) visited = Set.new([definition.initial]) queue = [definition.initial] until queue.empty? current = queue.shift targets = targets_from(definition, current) targets.each do |target| unless visited.include?(target) visited << target queue << target end end end visited.to_a end |
.unreachable_states(definition) ⇒ Array<Symbol>
Find states that can never be reached from the initial state.
11 12 13 14 |
# File 'lib/philiprehberger/state_machine/validation.rb', line 11 def self.unreachable_states(definition) reachable = reachable_states(definition) definition.all_states - reachable end |