Class: ProjectSimulator::Controller

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(mcs, model = nil, time: Time.now, speed: 1, debug: false) ⇒ Controller

Returns a new instance of Controller.



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/projectsimulator/controller.rb', line 13

def initialize(mcs, model=nil, time: Time.now, speed: 1, debug: false)
  
  @debug = debug
  @syslog = []
        
  @macros = mcs.macros
  
  if model then
    @model = Model.new(model)
  end
  
  @state = :stop
  @speed = speed
  
  @qt = UnichronUtils::Quicktime.new time: time, speed: @speed
  @qt.start
  @qt.pause

end

Instance Attribute Details

#macrosObject (readonly)

Returns the value of attribute macros.



10
11
12
# File 'lib/projectsimulator/controller.rb', line 10

def macros
  @macros
end

#modelObject (readonly)

Returns the value of attribute model.



10
11
12
# File 'lib/projectsimulator/controller.rb', line 10

def model
  @model
end

#speedObject

Returns the value of attribute speed.



11
12
13
# File 'lib/projectsimulator/controller.rb', line 11

def speed
  @speed
end

#titleObject

Returns the value of attribute title.



11
12
13
# File 'lib/projectsimulator/controller.rb', line 11

def title
  @title
end

Instance Method Details

#opObject

Object Property (op) Helpful for accessing properites in dot notation e.g. op.livingroom.light.switch = ‘off’



37
38
39
# File 'lib/projectsimulator/controller.rb', line 37

def op()
  @model.op if @model
end

#pauseObject



41
42
43
44
# File 'lib/projectsimulator/controller.rb', line 41

def pause()
  @state = :pause
  @qt.pause
end

#playObject



46
47
48
49
# File 'lib/projectsimulator/controller.rb', line 46

def play()
  @state = :play
  @qt.play
end

#request(s) ⇒ Object



51
52
53
# File 'lib/projectsimulator/controller.rb', line 51

def request(s)
  @model.request s
end

#startObject



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/projectsimulator/controller.rb', line 55

def start()
  

  Thread.new do
    
    old_time = @qt.time - 1
    
    loop do
      
      interval = (1 / (2.0 * @speed))
      (sleep interval; next) if @state != :play or old_time == @qt.time
      #puts Time.now.inspect if @debug
      r = self.trigger :timer, {time: @qt.time}
      
      yield(r) if r.any?
      
      puts 'r: ' + r.inspect if @debug and r.any?
      sleep interval
      old_time  = @qt.time
    end
    
  end
end

#stopObject



79
80
81
82
# File 'lib/projectsimulator/controller.rb', line 79

def stop()
  @state = :stop
  @qt.pause
end

#timeObject



84
85
86
# File 'lib/projectsimulator/controller.rb', line 84

def time()
  @qt.time
end

#trigger(name, detail = {}) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/projectsimulator/controller.rb', line 88

def trigger(name, detail={})
  
  macros = @macros.select do |macro|
    
    #puts 'macro: '  + macro.inspect if @debug

    # fetch the associated properties from the model if possible and 
    # merge them into the detail.
    #
    valid_trigger = macro.match?(name, detail, op())
    
    #puts 'valid_trigger: ' + valid_trigger.inspect if @debug
    
    if valid_trigger then
      @syslog << [Time.now, :trigger, name] 
      @syslog << [Time.now, :macro, macro.title]
    end
                 
    valid_trigger
    
  end
  
  #puts 'macros: ' + macros.inspect if @debug
  
  macros.flat_map(&:run)
end