Class: Ellington::Visualizer

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

Defined Under Namespace

Classes: Node

Constant Summary collapse

FONTNAME =
"Helvetica"
RANKSEP =
0.4
NODE_COLOR =
"white"
NODE_COLOR_VIRTUAL =
"gray50"
NODE_COLOR_LINE_GOAL =
"green2"
NODE_COLOR_ROUTE_GOAL =
"gold"
NODE_COLOR_PASSENGER_HIT =
"royalblue1"
NODE_FILLCOLOR =
"white"
NODE_FILLCOLOR_LINE_GOAL =
"green2"
NODE_FILLCOLOR_ROUTE_GOAL =
"gold"
NODE_FONTCOLOR_VIRTUAL =
"gray40"
NODE_SHAPE =
"box"
NODE_STYLE =
"filled,rounded"
NODE_STYLE_VIRTUAL =
"rounded"
NODE_PENWIDTH_PASSENGER_HIT =
2
EDGE_PENWIDTH_PASSENGER_HIT =
2
EDGE_COLOR_PASSENGER_HIT =
"royalblue1"
EDGE_STYLE_PASSENGER_MISS =
"dotted"
CLUSTER_STYLE =
"filled"
CLUSTER_COLOR =
"gray70"
CLUSTER_FILLCOLOR =
"gray70"
CLUSTER_PENCOLOR =
"gray50"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(route, format: :svg, short_names: true) ⇒ Visualizer

Returns a new instance of Visualizer.



38
39
40
41
42
# File 'lib/ellington/visualizer.rb', line 38

def initialize(route, format: :svg, short_names: true)
  @route = route
  @format = format
  @short_names = short_names
end

Instance Attribute Details

#formatObject (readonly)

Returns the value of attribute format.



36
37
38
# File 'lib/ellington/visualizer.rb', line 36

def format
  @format
end

#routeObject (readonly)

Returns the value of attribute route.



36
37
38
# File 'lib/ellington/visualizer.rb', line 36

def route
  @route
end

#short_namesObject (readonly)

Returns the value of attribute short_names.



36
37
38
# File 'lib/ellington/visualizer.rb', line 36

def short_names
  @short_names
end

Instance Method Details

#class_label(obj) ⇒ Object



67
68
69
70
71
72
# File 'lib/ellington/visualizer.rb', line 67

def class_label(obj)
  klass = obj
  klass = klass.class unless klass.is_a?(Class)
  return klass.name unless short_names
  klass.name.split("::").last
end

#graph_route(passenger = nil) ⇒ Object



141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
# File 'lib/ellington/visualizer.rb', line 141

def graph_route(passenger=nil)
  g = Node.new(nil, GraphViz.new("GraphRoute"))
  set_graph_defaults g.viz
  g.viz[:label] = "#{class_label(route)} Lines"
  g.viz[:ranksep] = 0.8

  route.lines.each_with_index do |line, index|
    line_cluster = g.add(Node.new(line, g.viz.add_graph("cluster#{index}")))
    set_cluster_defaults line_cluster.viz
    line_cluster.viz[:label] = class_label(line)
    add_state_nodes_for_line line_cluster, line, passenger
  end

  viz = g.viz.add_nodes(route.initial_state, :label => state_label(route.initial_state))
  rendered_edges = {}

  if passenger
    passenger_nodes = g.reduce([]) do |memo, line_cluster|
      line_cluster.children.each do |node|
        if node.viz[:color].to_s.gsub(/\W/, "") == NODE_COLOR_PASSENGER_HIT
          memo << node
        end
      end
      memo
    end
    previous_node = nil
    passenger_nodes.each do |node|
      from_viz = previous_node.nil? ? viz : previous_node.viz
      rendered_edges[from_viz.id + node.viz.id] = true
      edge = g.viz.add_edges(from_viz, node.viz)
      edge["color"] = EDGE_COLOR_PASSENGER_HIT
      edge["penwidth"] = EDGE_PENWIDTH_PASSENGER_HIT
      previous_node = node
    end
  end

  route.states.each do |from_state, to_states|
    (to_states || []).each do |to_state|
      from_line = route.lines.to_a.select{ |l| l.states.keys.include?(from_state) }.first
      from_node = g.find(from_line) if from_line
      from_viz = from_node.viz.get_node(from_state) if from_node
      from_viz ||= g.viz.get_node(from_state)
      to_line = route.lines.to_a.select{ |l| l.states.keys.include?(to_state) }.first
      to_node = g.find(to_line) if to_line
      to_viz = to_node.viz.get_node(to_state) if to_node
      to_viz ||= g.viz.get_node(to_state)

      if from_viz && to_viz && !rendered_edges[from_viz.id + to_viz.id]
        rendered_edges[from_viz.id + to_viz.id] = true
        edge = g.viz.add_edges(
          from_viz,
          to_viz
        )
        edge[:style] = EDGE_STYLE_PASSENGER_MISS if passenger
      end
    end
  end

  g.viz.output(format => String)
end

#graph_route_basic(passenger = nil) ⇒ Object



79
80
81
82
83
84
85
86
87
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/ellington/visualizer.rb', line 79

def graph_route_basic(passenger=nil)
  g = Node.new(nil, GraphViz.new("GraphRouteBasic"))
  set_graph_defaults g.viz
  g.viz[:ranksep] = 1
  g.viz[:label] = "#{class_label(route)} Route - basic"

  route.lines.each_with_index do |line, index|
    line_cluster = g.add(Node.new(line, g.viz.add_graph("cluster#{index}")))
    set_cluster_defaults line_cluster.viz
    line_cluster.viz[:label] = class_label(line)

    %w{PASS FAIL ERROR}.each do |state|
      state_node = line_cluster.add(Node.new(state, line_cluster.viz.add_nodes("#{line.class.name}#{state}", "label" => state)))
      states = line.stations.map{ |s| "#{state} #{s.name}" }
      style_node_for_line(state_node, line, *states)
      style_node_for_route(state_node, route, *states)
      style_node_for_passenger(state_node, passenger, *line.send("#{state.downcase}ed"))
    end
  end

  route.connections.each do |connection|
    to_node = g.find(connection.line)
    to_line = to_node.base

    combos = {}
    g.to_a.each do |node|
      states = node.base.state_names(connection.states)
      states.each do |state|
        (combos[state] ||= []) << node
      end
    end

    if connection.type == :if_any
      combos.each do |state, nodes|
        nodes.each do |node|
          from_line = node.base
          g.viz.add_edges(
            node.viz.get_node("#{from_line.class.name}#{state}"),
            to_node.viz.get_node("#{to_line.class.name}PASS"),
            "lhead" => to_node.viz.id
          )
        end
      end
    end

    if connection.type == :if_all
      combos.each do |state, nodes|
        node_name = nodes.map{ |n| n.base.class.name }.join + state
        node_label = nodes.map{ |n| state_label(state) }.join("\n")
        viz = g.viz.add_nodes(node_name, :label => node_label)
        g.viz.add_edges(
          viz,
          to_node.viz.get_node("#{connection.line.class.name}#{state}"),
          "lhead" => to_node.viz.id
        )
      end
    end
  end

  g.viz.output(format => String)
end

#state_label(state) ⇒ Object



74
75
76
77
# File 'lib/ellington/visualizer.rb', line 74

def state_label(state)
  return state unless short_names
  state.split(" ").map { |part| part.split("::").last }.join(" | ")
end