Class: MINT::Interactor

Inherits:
Object
  • Object
show all
Includes:
DataMapper::Resource, InteractorHelpers
Defined in:
lib/MINT-core/model/interactor.rb

Overview

An Interactor is the basic abstract element of the complete MINT markup language. Nearly all other classes are derived from Interactor since early everything can be activated by #state and has a #name.

Direct Known Subclasses

AIO, CIO, HWButton, IR, Joypad, PTS, Task, Wheel

Constant Summary collapse

PUBLISH_ATTRIBUTES =
[:name,:states,:abstract_states,:new_states,:classtype, :mint_model]

Class Method Summary collapse

Instance Method Summary collapse

Methods included from InteractorHelpers

#restart_timeout, #start_timeout, #stop_timeout

Class Method Details

.class_from_channel_name(channel) ⇒ Object



74
75
76
# File 'lib/MINT-core/model/interactor.rb', line 74

def self.class_from_channel_name(channel)
  Object.const_get("MINT").const_get channel.split('.').last
end

.create_channel_nameObject



58
59
60
61
62
63
# File 'lib/MINT-core/model/interactor.rb', line 58

def self.create_channel_name
  a = [self]
  a.unshift a.first.superclass while (a.first!=MINT::Interactor)
  a.map!{|x| x.to_s.split('::').last}
  a.join(".")
end

.get(*name) ⇒ Object



18
19
20
21
22
23
24
# File 'lib/MINT-core/model/interactor.rb', line 18

def self.get(*name)
    if name.length == 1
      get_dm(getModel,name[0])
    else
      get_dm(*name)
    end
end

.get_dmObject



15
# File 'lib/MINT-core/model/interactor.rb', line 15

alias get_dm get

.getModelObject



9
10
11
# File 'lib/MINT-core/model/interactor.rb', line 9

def self.getModel
  "core"
end

.notify(action, query, callback, time = nil) ⇒ Object



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/MINT-core/model/interactor.rb', line 105

def self.notify(action,query,callback,time = nil)
  RedisConnector.sub.subscribe("#{self.create_channel_name}")

  RedisConnector.sub.on(:message) { |channel, message|
    found=MultiJson.decode message
    puts query.inspect
    query.keys.each do |k|
      if found[k.to_s]
        a = found[k.to_s]
        query[k].each do |e|
          puts "found #{e} a:#{a.inspect}"
          if a.include? e
            callback.call found
            break
          end
        end
      end
    end
  }
end

.wait(action, query, callback, time = nil) ⇒ Object



127
128
129
130
# File 'lib/MINT-core/model/interactor.rb', line 127

def self.wait(action,query,callback,time = nil)
  # q = scoped_query(query)
  # q.repository.notify(action,query,callback,self,q, time)
end

Instance Method Details

#create_attribute_channel_name(attribute) ⇒ Object



65
66
67
# File 'lib/MINT-core/model/interactor.rb', line 65

def create_attribute_channel_name(attribute)
  "#{attribute}:"+self.class.create_channel_name+".#{self.name}"
end

#create_channel_w_nameObject



69
70
71
# File 'lib/MINT-core/model/interactor.rb', line 69

def create_channel_w_name
  self.class.create_channel_name+".#{self.name}"
end

#getSCXMLObject



227
228
229
# File 'lib/MINT-core/model/interactor.rb', line 227

def getSCXML
  "#{File.dirname(__FILE__)}/interactor.scxml"
end

#init_statemachineObject



51
52
53
# File 'lib/MINT-core/model/interactor.rb', line 51

def init_statemachine
  initialize_statemachine true
end

#is_in?(state) ⇒ Boolean

Returns:

  • (Boolean)


214
215
216
217
218
219
220
221
222
223
224
# File 'lib/MINT-core/model/interactor.rb', line 214

def is_in?(state)
  if not @statemachine

    #return true if (attribute_get(:states).split('|').map &:intern).include? state.intern
    #return true if (attribute_get(:abstract_states).split('|').map &:intern).include? state.intern
    #return false
    initialize_statemachine
    recover_statemachine
  end
  @statemachine.In(state)
end

#new_statesObject



146
147
148
149
150
151
152
# File 'lib/MINT-core/model/interactor.rb', line 146

def new_states
  if attribute_get(:new_states)
    return attribute_get(:new_states).split("|").map &:to_sym if attribute_get(:new_states).class!=Array
    return attribute_get(:new_states)
  else return []
  end
end

#process_event(event, callback = nil, vars = nil) ⇒ Object



178
179
180
181
182
183
184
185
186
187
# File 'lib/MINT-core/model/interactor.rb', line 178

def process_event(event, callback=nil, vars = nil)

  states = process_event!(event,callback,vars)
  if states
    save_statemachine
    return states
  else
    return false
  end
end

#process_event!(event, callback = nil, vars = nil) ⇒ Object



189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
# File 'lib/MINT-core/model/interactor.rb', line 189

def process_event!(event, callback=nil,vars=nil)
  if not @statemachine
    initialize_statemachine
    recover_statemachine
  end
  if callback
    @statemachine.context = callback
  else
    @statemachine.context = self
  end
  begin
    old_states = @statemachine.states_id
    old_abstract_states = @statemachine.abstract_states
    @statemachine.process_event(event,*vars)
    calc_new_states = @statemachine.states_id-old_states
    calc_new_states = calc_new_states + (@statemachine.abstract_states - old_abstract_states)
    calc_new_states = @statemachine.states_id  if calc_new_states.length==0
    attribute_set(:new_states, calc_new_states.join('|'))
  rescue Statemachine::TransitionMissingException
    p "#{self.name} is in state #{self.states} and could not handle #{event}"
    return false
  end
  return @statemachine.states_id
end

#process_event_vars(event, *vars) ⇒ Object

allows to set variables that will be passed as parameters to the actions



174
175
176
# File 'lib/MINT-core/model/interactor.rb', line 174

def process_event_vars(event, *vars)
  process_event(event,nil,vars)
end

#publish_update(states, abstract_states, atomic_states) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/MINT-core/model/interactor.rb', line 78

def publish_update(states,abstract_states, atomic_states)


  as_copy = attribute_get(:abstract_states)
  new_copy = attribute_get(:new_states)
  states_copy = attribute_get(:states)

  attribute_set(:abstract_states, abstract_states.join('|'))
  attribute_set(:new_states, states.join('|'))
  attribute_set(:states, atomic_states.join('|'))

  RedisConnector.redis.publish self.create_channel_w_name, self.to_json(:only => self.class::PUBLISH_ATTRIBUTES)

  attribute_set(:abstract_states, as_copy)
  attribute_set(:new_states, new_copy)
  attribute_set(:states, states_copy)
end

#statesObject



138
139
140
141
142
143
144
# File 'lib/MINT-core/model/interactor.rb', line 138

def states
  if not @statemachine
    initialize_statemachine
    recover_statemachine
  end
  @statemachine.states_id
end

#states=(states) ⇒ Object



154
155
156
157
158
159
160
# File 'lib/MINT-core/model/interactor.rb', line 154

def states= states
  if not @statemachine # if called on element creation using the states hash!
    initialize_statemachine
  end
  @statemachine.states=states
  save_statemachine
end

#sync_event(event) ⇒ Object

The sync event method is overwritten in the derived classes to prevent synchronization cycles by setting an empty callback



169
170
171
# File 'lib/MINT-core/model/interactor.rb', line 169

def sync_event(event)
  process_event(event)
end

#sync_statesObject



96
97
98
99
100
101
102
103
# File 'lib/MINT-core/model/interactor.rb', line 96

def sync_states()
  values = RedisConnector.sync_redis.hmget "mint_interactors:#{self.class.getModel}#{name}","states","new_states","abstract_states"

  attribute_set(:abstract_states, values[2])
  attribute_set(:new_states, values[1])
  attribute_set(:states, values[0])
  recover_statemachine
end

#to_dot(filename) ⇒ Object



132
133
134
135
136
137
# File 'lib/MINT-core/model/interactor.rb', line 132

def to_dot(filename)
  if not @statemachine
    initialize_statemachine
  end
  @statemachine.to_dot(:output => filename)
end