Class: Ellington::Route

Inherits:
SimpleDelegator
  • Object
show all
Extended by:
HasTargets, Observable
Defined in:
lib/ellington/route.rb

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from HasTargets

error_target, fail_target, state, state_names

Constructor Details

#initializeRoute

Returns a new instance of Route.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/ellington/route.rb', line 8

def initialize
  super self.class

  if lines.empty?
    message = "#{self.class.name} has no lines!"
    raise Ellington::NoLinesDeclared.new(message)
  end

  if goal.empty?
    message = "#{self.class.name} has no lines!"
    raise Ellington::NoGoalDeclared.new(message)
  end

  initialize_lines
  states
end

Class Attribute Details

.initializedObject (readonly)

Returns the value of attribute initialized.



39
40
41
# File 'lib/ellington/route.rb', line 39

def initialized
  @initialized
end

Class Method Details

.board(passenger, options = {}) ⇒ Object



57
58
59
# File 'lib/ellington/route.rb', line 57

def board(passenger, options={})
  lines.first.board passenger, options
end

.connect_to(line, options) ⇒ Object



102
103
104
105
106
# File 'lib/ellington/route.rb', line 102

def connect_to(line, options)
  type = options.keys.first
  states = options[type]
  connections << Ellington::Connection.new(line, type, states)
end

.connectionsObject



98
99
100
# File 'lib/ellington/route.rb', line 98

def connections
  @connections ||= Ellington::ConnectionList.new
end

.generate_graphs(dir, options = {}) ⇒ Object



45
46
47
48
49
50
51
# File 'lib/ellington/route.rb', line 45

def generate_graphs(dir, options={})
  options[:format] ||= :svg
  FileUtils.mkdir_p(dir)
  @subclasses.each do |subclass|
    Ellington::Visualizer.new(subclass.new, dir, options[:format]).graph_all
  end
end

.inherited(subclass) ⇒ Object



41
42
43
# File 'lib/ellington/route.rb', line 41

def inherited(subclass)
  (@subclasses ||= []) << subclass
end

.initial_stateObject



88
89
90
# File 'lib/ellington/route.rb', line 88

def initial_state
  @initial_state ||= "PRE #{name}"
end

.initialized?Boolean

Returns:

  • (Boolean)


53
54
55
# File 'lib/ellington/route.rb', line 53

def initialized?
  @initialized
end

.line_completed(line_info) ⇒ Object



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/ellington/route.rb', line 116

def line_completed(line_info)
  route_info = Ellington::RouteInfo.new(self, line_info)

  required_connections = connections.select do |connection|
    connection.required?(route_info.passenger)
  end

  if required_connections.empty?
    if passed.satisfied?(route_info.passenger) || failed.satisfied?(route_info.passenger)
      log route_info
      changed
      notify_observers route_info
    end
    Ellington.logger.info "\n" if Ellington.logger
  end

  required_connections.each do |connection|
    connection.line.board route_info.passenger, route_info.options
  end
end

.linesObject



61
62
63
# File 'lib/ellington/route.rb', line 61

def lines
  @lines ||= Ellington::LineList.new(self)
end

.log_options(options = {}) ⇒ Object



108
109
110
111
112
113
114
# File 'lib/ellington/route.rb', line 108

def log_options(options={})
  @log_options ||= begin
    options[:passenger] ||= []
    options[:options] ||= []
    options
  end
end

.pass_target(*line_goals) ⇒ Object Also known as: passed, goal



92
93
94
# File 'lib/ellington/route.rb', line 92

def pass_target(*line_goals)
  @goal ||= Ellington::Target.new(*line_goals.flatten)
end

.statesObject



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/ellington/route.rb', line 65

def states
  @states ||= begin
    catalog = StateJacket::Catalog.new
    catalog.add initial_state => lines.first.stations.first.states.keys

    lines.each do |line|
      line.states.each do |state, transitions|
        catalog[state] = transitions.nil? ? nil : transitions.dup
      end
    end

    connections.each do |connection|
      connection.states.each do |state|
        catalog[state] ||= []
        catalog[state].concat connection.line.stations.first.states.keys
      end
    end

    catalog.lock
    catalog
  end
end

Instance Method Details

#initialize_linesObject



25
26
27
28
29
30
31
32
33
34
# File 'lib/ellington/route.rb', line 25

def initialize_lines
  lines.each do |line|
    line.route = self
    line.add_observer self, :line_completed
    line.stations.each do |station|
      station.line = line
      station.add_observer line, :station_completed
    end
  end
end