Class: MINT::Mapping
- Inherits:
-
Object
- Object
- MINT::Mapping
- Defined in:
- lib/MINT-core/mapping/mapping.rb
Overview
Basic abstract class to define a mapping, which specifies a communication between model agents. Therefore ia mapping listens to a source model of another agents’ model for actions like e.g. new elements, updates to elements or element removals.
Direct Known Subclasses
Instance Attribute Summary collapse
-
#state_callback ⇒ Object
informs mapping tool about state changes.
Instance Method Summary collapse
- #actions ⇒ Object
-
#actions_succeeded_callback(&block) ⇒ Object
This callback is used to inform that all actions have been finished.
-
#activated_callback(&block) ⇒ Object
This callback is used to inform that all observations have been successfully activated (subscribed).
- #call_actions_succeeded_callbacks(result) ⇒ Object
- #call_activated_callbacks ⇒ Object
-
#cb_activate_action(element, in_state, result, id) ⇒ Object
function is called every time an observation has been fulfilled or failed (in_state == :fail).
- #id ⇒ Object
-
#initialize(params) ⇒ Mapping
constructor
A new instance of Mapping.
- #mapping_name ⇒ Object
- #observations ⇒ Object
- #restart ⇒ Object
- #start ⇒ Object
- #startAction(observation_results) ⇒ Object
- #stop_observations ⇒ Object
Constructor Details
#initialize(params) ⇒ Mapping
Returns a new instance of Mapping.
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/MINT-core/mapping/mapping.rb', line 12 def initialize(params) @mapping = params # to fire callback if all observations have been successfully initialized @observation_init = 0 @activated_callback = nil @action_activated = {} # to fire callback to inform mapping state @state_callback = nil # stores variables assigned by observations @observation_results = {} @observation_state = {} @action_states = {} #overall action sucecces used for direct callback @actions_success = false end |
Instance Attribute Details
#state_callback ⇒ Object
informs mapping tool about state changes
10 11 12 |
# File 'lib/MINT-core/mapping/mapping.rb', line 10 def state_callback @state_callback end |
Instance Method Details
#actions ⇒ Object
49 50 51 |
# File 'lib/MINT-core/mapping/mapping.rb', line 49 def actions @mapping[:actions] end |
#actions_succeeded_callback(&block) ⇒ Object
This callback is used to inform that all actions have been finished
108 109 110 111 112 113 114 115 116 117 |
# File 'lib/MINT-core/mapping/mapping.rb', line 108 def actions_succeeded_callback(&block) return unless block if @action_activated block.call(self,@actions_success ) else @actions_succeeded_callbacks ||= [] @actions_succeeded_callbacks.unshift block # << block end end |
#activated_callback(&block) ⇒ Object
This callback is used to inform that all observations have been successfully activated (subscribed)
87 88 89 90 91 92 93 94 95 96 |
# File 'lib/MINT-core/mapping/mapping.rb', line 87 def activated_callback(&block) return unless block if @mapping_activated block.call(self) else @activated_callbacks ||= [] @activated_callbacks.unshift block # << block end end |
#call_actions_succeeded_callbacks(result) ⇒ Object
120 121 122 123 124 125 126 127 |
# File 'lib/MINT-core/mapping/mapping.rb', line 120 def call_actions_succeeded_callbacks(result) @actions_succeeded_callbacks ||= [] while cb = @actions_succeeded_callbacks.pop cb.call(self,result) end @actions_succeeded_callbacks.clear if @actions_succeeded_callbacks end |
#call_activated_callbacks ⇒ Object
98 99 100 101 102 103 104 105 |
# File 'lib/MINT-core/mapping/mapping.rb', line 98 def call_activated_callbacks @activated_callbacks ||= [] while cb = @activated_callbacks.pop cb.call(self) end @activated_callbacks.clear if @activated_callbacks end |
#cb_activate_action(element, in_state, result, id) ⇒ Object
function is called every time an observation has been fulfilled or failed (in_state == :fail)
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 |
# File 'lib/MINT-core/mapping/mapping.rb', line 130 def cb_activate_action(element,in_state,result,id) @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 if in_state == :fail stop_observations # unsubscribe observations @state_callback.call(@mapping[:name], {:id => @mapping[:id], :mapping_state => :failed}) if @state_callback restart # restart mapping return end if in_state @observation_results.merge! result # check if already all other observations have been matched if not @observation_state.values.include? false stop_observations # unsubscribe observations 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 # TODO -clean restart and move check_at_startup inside observation # - add callback for action if succeeded & implement parallel action execution m.restart } end end end |
#id ⇒ Object
41 42 43 |
# File 'lib/MINT-core/mapping/mapping.rb', line 41 def id @mapping[:id] end |
#mapping_name ⇒ Object
36 37 38 39 |
# File 'lib/MINT-core/mapping/mapping.rb', line 36 def mapping_name return @mapping[:name] if @mapping[:name] "unnamed" end |
#observations ⇒ Object
45 46 47 |
# File 'lib/MINT-core/mapping/mapping.rb', line 45 def observations @mapping[:observations] end |
#restart ⇒ Object
162 163 164 165 166 167 168 169 170 171 |
# File 'lib/MINT-core/mapping/mapping.rb', line 162 def restart @observation_state = {} @action_states = {} observations.each do |observation| @observation_state[observation.element] = false observation.start end end |
#start ⇒ Object
53 54 55 |
# File 'lib/MINT-core/mapping/mapping.rb', line 53 def start return self end |
#startAction(observation_results) ⇒ Object
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 |
# File 'lib/MINT-core/mapping/mapping.rb', line 57 def startAction(observation_results) @action_init = 0 @actions_success = false actions.each do |action| @action_init += 1 @state_callback.call(@mapping[:name], {:id => action.id, :state => :activated}) if @state_callback action.start(observation_results).finished_callback { |id,result| state = :failed state = :succeeded if result @action_states[id]=state @state_callback.call(@mapping[:name], {:id => action.id, :state => state}) if @state_callback @action_init -= 1 if @action_init == 0 @action_activated = true r = false r = true if not @action_states.values.include? :failed @actions_success = r call_actions_succeeded_callbacks r end } # pass observation variables end self end |
#stop_observations ⇒ Object
173 174 175 176 177 178 |
# File 'lib/MINT-core/mapping/mapping.rb', line 173 def stop_observations observations.each do |observation| observation.stop end end |