Class: MINT::Sequential

Inherits:
Mapping show all
Defined in:
lib/MINT-core/mapping/sequential.rb

Overview

click_select_mapping = Sequential.new(HWButton,:pressed,HWButton,:released, 300, { AIChoiceElement =>“focused”},:choose)

Instance Attribute Summary

Attributes inherited from Mapping

#state_callback

Instance Method Summary collapse

Methods inherited from Mapping

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

Constructor Details

#initialize(source_model_1, state_1, source_model_2, state_2, state_2_conditions, min_time, max_time, conditions, error_recovery = nil) ⇒ Sequential

Returns a new instance of Sequential.



6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/MINT-core/mapping/sequential.rb', line 6

def initialize(source_model_1,state_1,source_model_2,state_2, state_2_conditions, min_time, max_time, conditions, error_recovery = nil)
  super(source_model_1,"write",nil,nil)
  @source_model_1= source_model_1
  @state_1= state_1
  @source_model_2= source_model_2
  @state_2= state_2
  @max_time = max_time
  @min_time = min_time
  @conditions = conditions
  @time_state_1_happened = nil
  @state_2_conditions = state_2_conditions
  @error_recovery = error_recovery
end

Instance Method Details

#check_sequence(result) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/MINT-core/mapping/sequential.rb', line 28

def check_sequence(result)
  ms = ((Time.now-@time_state_1_happened)*1000).to_i
  if @time_state_1_happened and ((@min_time<=ms and ms <=@max_time) or (@min_time == 0 and @max_time==0))
    if (check_state_2_conditions)
      p "Sequence #{@source_model_1}=#{@state_1} --> #{@source_model_2}= #{@state_2} PROCEEDED in #{ms} ms"

      publish_events @conditions

    else
      p "Sequence #{@source_model_1}=#{@state_1} --> #{@source_model_2}= #{@state_2} FAILED #{ms}ms because of state 2 conditions "

      publish_events @error_recovery if @error_recovery

    end
  else
    p "Sequence #{@source_model_1}=#{@state_1} --> #{@source_model_2}= #{@state_2} FAILED #{ms}ms instead of min/max #{@min_time}/#{@max_time}ms"

    publish_events @error_recovery if @error_recovery

  end
  @time_state_1_happened = nil
end

#check_state_2_conditionsObject



51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/MINT-core/mapping/sequential.rb', line 51

def check_state_2_conditions
  return true if not @state_2_conditions
  @state_2_conditions.each do |model,state|
#        state.each do |s|
    result=model.first( :abstract_states=> /(^|\|)#{Regexp.quote(state)}/)
    if result.nil?
      return false
    end
    #       end
  end
  true
end

#executeObject



20
21
22
23
24
25
26
# File 'lib/MINT-core/mapping/sequential.rb', line 20

def execute
  t1 = @source_model_1.notify("write", {  :new_states=> /(^|\|)#{Regexp.quote(@state_1)}/},self.method(:save_state_1_happened))
  #    t2 = @source_model_2.notify("write", {  :abstract_states=> /#{Regexp.quote(@state_2)}/},self.method(:check_sequence))
  #   p "registered for model #{@source_model_2} writes on state #{@state_2}"
  return t1
  #,t2
end

#save_state_1_happened(result) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/MINT-core/mapping/sequential.rb', line 64

def save_state_1_happened(result)
  p "Sequence #{@source_model_1}=#{@state_1} OK! --> #{@source_model_2}= #{@state_2} "

  @time_state_1_happened = Time.now
#      p "State #{@state_1} of #{@source_model_1} occurred!"
  if @max_time >0
    @source_model_2.wait("write", {  :new_states=> /(^|\|)#{Regexp.quote(@state_2)}/},self.method(:check_sequence))
    p "registered for model #{@source_model_2} writes on state #{@state_2}"
  else
    result = @source_model_2.first(:abstract_states=> /(^|\|)#{Regexp.quote(@state_2)}/)
    if (result)
      p "check seq"
      check_sequence(result)
    else
      p "Sequence #{@source_model_1}=#{@state_1} --> #{@source_model_2}= #{@state_2} without time slot FAILED because of state 2 conditions"

    end
  end
end