Class: ProjectSimulator::Event

Inherits:
Object
  • Object
show all
Defined in:
lib/projectsimulator.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(e, time: nil, title: '', debug: false) ⇒ Event

Returns a new instance of Event.



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/projectsimulator.rb', line 137

def initialize(e, time: nil, title: '', debug: false)

  @time, @debug = time, debug
  @title = e.text('event')
  @actions = []
  @triggers = []
  @constraints = []

  e.xpath('trigger').each do |x| 
    @triggers << Trigger.new(x.text().strip, time: time, debug: debug)\
        .to_type
  end

  e.xpath('action').each do |x|
    @actions << Action.new(x.text().strip, debug: debug).to_type
  end

  e.xpath('constraint').each do |x|
    puts 'before Constraints.new'
    @constraints << Constraint.new(x.text().strip, \
                                   time: time, debug: debug).to_type
  end

end

Instance Attribute Details

#actionsObject (readonly)

Returns the value of attribute actions.



135
136
137
# File 'lib/projectsimulator.rb', line 135

def actions
  @actions
end

#constraintsObject (readonly)

Returns the value of attribute constraints.



135
136
137
# File 'lib/projectsimulator.rb', line 135

def constraints
  @constraints
end

#messagesObject (readonly)

Returns the value of attribute messages.



135
136
137
# File 'lib/projectsimulator.rb', line 135

def messages
  @messages
end

#titleObject

Returns the value of attribute title.



134
135
136
# File 'lib/projectsimulator.rb', line 134

def title
  @title
end

#triggersObject (readonly)

Returns the value of attribute triggers.



135
136
137
# File 'lib/projectsimulator.rb', line 135

def triggers
  @triggers
end

Instance Method Details

#match(trigger: nil, location: '') ⇒ Object



162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
# File 'lib/projectsimulator.rb', line 162

def match(trigger: nil, location: '')
  
  @messages = []
  
  h = {motion: MotionTrigger}
  
  if @triggers.any? {|x| x.is_a? h[trigger.to_sym] and x.match } then
    
    if @constraints.all?(&:match) then
    
      @messages = @actions.map(&:call)
      
    else
      
      puts 'else reset?' if @debug
      a = @constraints.select {|x| x.is_a? FrequencyConstraint }
      puts 'a:' + a.inspect if @debug
      a.each {|x| x.reset if x.counter > 0 }
      return false
    end
    
  end
  
end

#time=(val) ⇒ Object



187
188
189
190
# File 'lib/projectsimulator.rb', line 187

def time=(val)
  @time = val
  @constraints.each {|x| x.time = val if x.is_a? TimeConstraint }
end

#to_nodeObject



192
193
194
195
196
197
198
199
200
201
# File 'lib/projectsimulator.rb', line 192

def to_node()
  
  e = Rexle::Element.new(:event, attributes: {title: @title})
  
  e.add node_collection(:triggers, @triggers)
  e.add node_collection(:actions, @actions)
  e.add node_collection(:constraints, @constraints)
  
  return e
end

#to_rowxObject



203
204
205
206
207
208
# File 'lib/projectsimulator.rb', line 203

def to_rowx()
  
  s = "event: %s\n\n" % @title
  s + [@triggers, @actions, @constraints]\
      .map {|x| x.collect(&:to_rowx).join("\n")}.join("\n")
end