Class: ProjectSimulator::Controller

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(s, time: Time.now, debug: false) ⇒ Controller

Returns a new instance of Controller.



506
507
508
509
510
511
512
513
514
515
# File 'lib/projectsimulator.rb', line 506

def initialize(s, time: Time.now, debug: false)
  
  @time, @debug = time, debug

  doc = Rexle.new(RowX.new(s).to_xml)

  @events = doc.root.xpath('item')\
      .map {|e| Event.new(e, time: @time, debug: debug) }

end

Instance Attribute Details

#eventsObject (readonly)

Returns the value of attribute events.



503
504
505
# File 'lib/projectsimulator.rb', line 503

def events
  @events
end

#timeObject

Returns the value of attribute time.



504
505
506
# File 'lib/projectsimulator.rb', line 504

def time
  @time
end

Instance Method Details

#to_docObject



522
523
524
525
526
527
528
# File 'lib/projectsimulator.rb', line 522

def to_doc()
  
  doc = Rexle.new('<events/>')      
  @events.each {|event| doc.root.add event.to_node }
  return doc
  
end

#to_rowxObject



530
531
532
# File 'lib/projectsimulator.rb', line 530

def to_rowx()
  @events.collect(&:to_rowx).join("\n\n#{'-'*50}\n\n")
end

#trigger(name, location: '') ⇒ Object



534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
# File 'lib/projectsimulator.rb', line 534

def trigger(name, location: '')
  
  events = @events.select do |event|
    
    puts 'event: '  + event.inspect if @debug

    event.match(trigger: 'motion', location: location)
    
  end
  
  puts 'events: ' + events.inspect if @debug
  
  events.each do |event|
    c = event.constraints.select {|x| x.is_a? FrequencyConstraint }
    puts 'c:' + c.inspect
    c.each(&:increment)
  end
  
  events.flat_map(&:messages)
  
end