Class: MINT::SequentialMapping

Inherits:
ComplementaryMapping show all
Defined in:
lib/MINT-core/mapping/sequential_mapping.rb

Instance Attribute Summary

Attributes inherited from Mapping

#state_callback

Instance Method Summary collapse

Methods inherited from ComplementaryMapping

#start

Methods inherited from Mapping

#actions, #actions_succeeded_callback, #activated_callback, #call_actions_succeeded_callbacks, #call_activated_callbacks, #id, #initialize, #mapping_name, #observations, #start, #startAction, #stop_observations

Constructor Details

This class inherits a constructor from MINT::Mapping

Instance Method Details

#cb_activate_action(element, in_state, result, id) ⇒ Object

function is called every time an observation has been fulfilled



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
# File 'lib/MINT-core/mapping/sequential_mapping.rb', line 17

def cb_activate_action(element,in_state,result,id)
#      @state_callback.call(@mapping[:name], {:id => id, :state => in_state.to_s.to_sym}) if @state_callback
  @state_callback.call(@mapping[:name], {:id => id, :element => element, :result => result, :state => in_state.to_s.to_sym}) if @state_callback

  @observation_state[element] = in_state

  # ignore incoming updates of a previous observation that is still subscribed
  return if not observations[@active_observation].element.eql? element

  if in_state == :fail
        observations[@active_observation].stop # unsubscribe observation
         restart # restart mapping
         return
       end
  if in_state
    @observation_results.merge! result
    observations[@active_observation].stop # unsubscribe observation
    @active_observation += 1

    # check if already all other observations have been matched
    if observations.length <= @active_observation
      startAction(@observation_results).actions_succeeded_callback { |m,result|
                  state = :failed
                  state = :succeeded if result

                  @state_callback.call(@mapping[:name], {:id => @mapping[:id], :mapping_state => state}) if @state_callback

        # add callback for action if succeeded & implement parallel action execution
        m.restart

      }
    else # start next observation
      observations[@active_observation].start(@observation_results,self.method(:cb_activate_action))
    end
  end
end

#restartObject



54
55
56
57
58
# File 'lib/MINT-core/mapping/sequential_mapping.rb', line 54

def restart
  @active_observation = 0
  super
  self
end

#start_observationsObject



6
7
8
9
10
11
12
13
14
# File 'lib/MINT-core/mapping/sequential_mapping.rb', line 6

def start_observations
  @active_observation = 0

  observations[@active_observation].start(@observation_results,self.method(:cb_activate_action)).is_subscribed_callback { |observation|
    @observation_state[observation.element] = false
    @state_callback.call(@mapping[:name], {:id => observation.id, :state => :activated}) if @state_callback
    call_activated_callbacks
  }
end