Class: Fabulator::Core::Structurals::StateMachine

Inherits:
Structural
  • Object
show all
Defined in:
lib/fabulator/core/structurals/state_machine.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Structural

accepts_structural?, #accepts_structural?, contained_in, contains, element, structurals

Constructor Details

#initializeStateMachine

Returns a new instance of StateMachine.



21
22
23
24
25
# File 'lib/fabulator/core/structurals/state_machine.rb', line 21

def initialize
  @states = { }
  @context = Fabulator::Expr::Context.new
  @state = 'start'
end

Instance Attribute Details

#errorsObject

Returns the value of attribute errors.



12
13
14
# File 'lib/fabulator/core/structurals/state_machine.rb', line 12

def errors
  @errors
end

#missing_paramsObject

Returns the value of attribute missing_params.



12
13
14
# File 'lib/fabulator/core/structurals/state_machine.rb', line 12

def missing_params
  @missing_params
end

#namespacesObject

Returns the value of attribute namespaces.



12
13
14
# File 'lib/fabulator/core/structurals/state_machine.rb', line 12

def namespaces
  @namespaces
end

#stateObject

Returns the value of attribute state.



13
14
15
# File 'lib/fabulator/core/structurals/state_machine.rb', line 13

def state
  @state
end

#statesObject

Returns the value of attribute states.



12
13
14
# File 'lib/fabulator/core/structurals/state_machine.rb', line 12

def states
  @states
end

#updated_atObject

Returns the value of attribute updated_at.



12
13
14
# File 'lib/fabulator/core/structurals/state_machine.rb', line 12

def updated_at
  @updated_at
end

Instance Method Details

#cloneObject



37
38
39
# File 'lib/fabulator/core/structurals/state_machine.rb', line 37

def clone
  YAML::load( YAML::dump( self ) )
end

#compile_xml(xml, context = nil) ⇒ Object



27
28
29
30
31
32
33
34
35
# File 'lib/fabulator/core/structurals/state_machine.rb', line 27

def compile_xml(xml, context = nil)
  super

  if @states.empty?
    s = State.new
    s.name = 'start'
    @states = { 'start' => s }
  end
end

#contextObject



54
55
56
# File 'lib/fabulator/core/structurals/state_machine.rb', line 54

def context
  { :data => @context.root, :state => @state }
end

#context=(c) ⇒ Object



62
63
64
65
66
67
68
69
70
71
# File 'lib/fabulator/core/structurals/state_machine.rb', line 62

def context=(c)
  if c.is_a?(Fabulator::Expr::Context)
    @context = c
  elsif c.is_a?(Fabulator::Expr::Node)
    @context.root = c
  elsif c.is_a?(Hash)
    @context.root = c[:data]
    @state = c[:state]
  end
end

#dataObject



121
122
123
# File 'lib/fabulator/core/structurals/state_machine.rb', line 121

def data
  @context.root
end

#fabulator_contextObject



58
59
60
# File 'lib/fabulator/core/structurals/state_machine.rb', line 58

def fabulator_context
  @context
end

#init_context(c) ⇒ Object



45
46
47
48
49
50
51
52
# File 'lib/fabulator/core/structurals/state_machine.rb', line 45

def init_context(c)
  @context.root = c.root
  begin
    @actions.run(@context)
  rescue Fabulator::StateChangeException => e
    @state = e
  end
end

#run(params) ⇒ Object



73
74
75
76
77
78
79
80
81
82
# File 'lib/fabulator/core/structurals/state_machine.rb', line 73

def run(params)
  current_state = @states[@state]
  return if current_state.nil?
  # select transition
  # possible get some errors
  # run transition, and move to new state as needed
  @context.in_context do |ctx|
    self.run_transition(current_state.select_transition(@context, params))
  end
end

#run_transition(best_transition) ⇒ Object



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
# File 'lib/fabulator/core/structurals/state_machine.rb', line 84

def run_transition(best_transition)
  return if best_transition.nil? || best_transition.empty?
  current_state = @states[@state]
  t = best_transition[:transition]
  @missing_params = best_transition[:missing]
  @errors = best_transition[:messages]
  if @missing_params.empty? && @errors.empty?
    @state = t.state
    # merge valid and context
    best_transition[:valid].sort_by { |a| a.path.length }.each do |item|
      p = item.path.gsub(/^[^:]+::/, '').split('/') - [ '' ]
      n = @context.traverse_path(p, true).first
      n.prune
      n.copy(item)
    end
    # run_post of state we're leaving
    begin
      current_state.run_post(@context)
      t.run(@context)
      # run_pre for the state we're going to
      new_state = @states[@state]
      new_state.run_pre(@context) if !new_state.nil?
    rescue Fabulator::StateChangeException => e # catch state change
      new_state = @states[e]
      begin
        if !new_state.nil?
          @state = new_state.name
          new_state.run_pre(@context)
        end
      rescue Fabulator::StateChangeException => e
        new_state = @states[e] 
        retry
      end
    end
  end
end

#state_namesObject



125
126
127
# File 'lib/fabulator/core/structurals/state_machine.rb', line 125

def state_names
  (@states.keys.map{ |k| @states[k].states }.flatten + @states.keys).uniq
end