Class: SimpleModel

Inherits:
Object
  • Object
show all
Includes:
Observable
Defined in:
lib/app/simple_model.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeSimpleModel

Returns a new instance of SimpleModel.



17
18
19
# File 'lib/app/simple_model.rb', line 17

def initialize
  @graph_type = DirectedGraph
end

Instance Attribute Details

#graphObject (readonly)

Returns the value of attribute graph.



15
16
17
# File 'lib/app/simple_model.rb', line 15

def graph
  @graph
end

#layoutObject (readonly)

Returns the value of attribute layout.



15
16
17
# File 'lib/app/simple_model.rb', line 15

def layout
  @layout
end

Instance Method Details

#add_edge(source, target) ⇒ Object



97
98
99
# File 'lib/app/simple_model.rb', line 97

def add_edge(source, target)
  @graph.add_edge(source, target, true)
end

#add_node_at(x, y) ⇒ Object



76
77
78
79
80
81
# File 'lib/app/simple_model.rb', line 76

def add_node_at(x, y)
  $LOG.debug("add_node_at #{x}, #{y} called on #{self}")
  node = @graph.add_node(false)
  @layout.set_location(node, x, y, true)
  return node
end

#center_xObject



265
266
267
268
269
# File 'lib/app/simple_model.rb', line 265

def center_x
  @layout.center_x
  changed
  notify_observers(nil)
end

#center_xyObject



277
278
279
280
281
282
# File 'lib/app/simple_model.rb', line 277

def center_xy
  @layout.center_x
  @layout.center_y
  changed
  notify_observers(nil)
end

#center_yObject



271
272
273
274
275
# File 'lib/app/simple_model.rb', line 271

def center_y
  @layout.center_y
  changed
  notify_observers(nil)
end

#click(node) ⇒ Object



178
179
180
181
182
# File 'lib/app/simple_model.rb', line 178

def click(node)
  @graph.node_data(:is_clicked?)[node] = true
  changed
  notify_observers(nil)
end

#click_edge(edge) ⇒ Object



190
191
192
193
194
# File 'lib/app/simple_model.rb', line 190

def click_edge(edge)
  @graph.edge_data(:is_clicked?)[edge] = true
  changed
  notify_observers(nil)
end

#count_nodesObject



236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
# File 'lib/app/simple_model.rb', line 236

def count_nodes
  index = 0
  data = @graph.attach_node_data(:text)
  @graph.each_node do |node|
    data[node] = index.to_s
    index += 1
  end
  index = 0
  data = @graph.attach_edge_data(:text)
  @graph.each_edge do |edge|
    data[edge] = index.to_s
    index += 1
  end
  changed
  notify_observers(nil)
end

#del_edge(edge) ⇒ Object



113
114
115
# File 'lib/app/simple_model.rb', line 113

def del_edge(edge)
  @graph.del_edge(edge, true)
end

#del_node(node) ⇒ Object



92
93
94
95
# File 'lib/app/simple_model.rb', line 92

def del_node(node)
  @layout.delete(node)
  @graph.del_node(node, true)
end

#deselect(edge) ⇒ Object



126
127
# File 'lib/app/simple_model.rb', line 126

def deselect(edge)
end

#deselect_node(node) ⇒ Object



120
121
# File 'lib/app/simple_model.rb', line 120

def deselect_node(node)
end

#dsp(source, target) ⇒ Object



202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
# File 'lib/app/simple_model.rb', line 202

def dsp(source, target)
  stdata = @graph.attach_node_data(:start)
  stdata[source] = true
  endata = @graph.attach_node_data(:end)
  endata[target] = true

  dsp = DijkstraShortestPath.new(@graph, source)
  npath = dsp.node_path_to(target)
  epath = dsp.edge_path_to(target)

  edata = @graph.attach_edge_data(:path)
  epath.each do |edge|
    edata[edge] = true
  end
  ndata = @graph.attach_node_data(:path)
  npath.each do |node|
    ndata[node] = true
  end
end

#dspt(source) ⇒ Object



222
223
224
225
226
227
228
229
230
231
232
233
234
# File 'lib/app/simple_model.rb', line 222

def dspt(source)
  @graph.attach_node_data(:start)[source] = true
  path = []
  edata = @graph.attach_edge_data(:path)
  ndata = @graph.attach_node_data(:end)
  dijk = DijkstraShortestPath.new(@graph, source)
  @graph.each_node do |node|
    if (node != source)
      dijk.edge_path_to(node).each { |edge| edata[edge] = true }
      dijk.node_path_to(node).each { |node| ndata[node] = true }
    end
  end
end

#get_edge(source, target) ⇒ Object



101
102
103
104
105
106
107
108
109
110
111
# File 'lib/app/simple_model.rb', line 101

def get_edge(source, target)
  edge = nil
  @graph.each_adjacent_edge(source) do |e|
    if e.target == target
      edge = e
    elsif (not e.directed?) and e.source == target and e.target == source
      edge = e
    end
  end
  return edge
end

#get_nearest_node(x, y) ⇒ Object



83
84
85
86
# File 'lib/app/simple_model.rb', line 83

def get_nearest_node(x, y)
  $LOG.debug("get_nearest_node #{x}, #{y} called on #{self}")
  @layout.get_nearest_node(x, y)
end

#move_layout(x, y) ⇒ Object



259
260
261
262
263
# File 'lib/app/simple_model.rb', line 259

def move_layout(x, y)
  @layout.move(x, y)
  changed
  notify_observers(nil)
end

#new_binary_tree(height) ⇒ Object



57
58
59
60
61
# File 'lib/app/simple_model.rb', line 57

def new_binary_tree(height)
  $LOG.debug("new_bintree_graph called on #{self}")
  self.new_graph
  @graph.gen_binary_tree(height)
end

#new_connected_graph(size) ⇒ Object



39
40
41
42
43
# File 'lib/app/simple_model.rb', line 39

def new_connected_graph(size)
  $LOG.debug("new_connected_graph called on #{self}")
  self.new_graph
  @graph.gen_connected_graph(size)
end

#new_empty_graphObject



34
35
36
37
# File 'lib/app/simple_model.rb', line 34

def new_empty_graph
  new_graph
  @graph.notify_observers(nil)
end

#new_graphObject

Graph



26
27
28
29
30
31
32
# File 'lib/app/simple_model.rb', line 26

def new_graph
  @graph = @graph_type.new
  @graph.add_observer(self)
  @graph.attach_node_data(:is_clicked?, Hash.new(false))
  @graph.attach_edge_data(:is_clicked?, Hash.new(false))
  @layout.graph = @graph
end

#new_layout(layout) ⇒ Object

Layout



130
131
132
133
134
135
136
# File 'lib/app/simple_model.rb', line 130

def new_layout(layout)
  @layout = layout
  @layout.add_observer(self)
  if @graph
    @layout.graph = @graph
  end
end

#new_linear_graph(size) ⇒ Object



69
70
71
72
73
# File 'lib/app/simple_model.rb', line 69

def new_linear_graph(size)
  $LOG.debug("new_linear_graph called on #{self}")
  self.new_graph
  @graph.gen_linear_graph(size)
end

#new_mesh_graph(rows) ⇒ Object



63
64
65
66
67
# File 'lib/app/simple_model.rb', line 63

def new_mesh_graph(rows)
  $LOG.debug("new_mesh_graph called on #{self}")
  self.new_graph
  @graph.gen_mesh_graph(rows)
end

#new_random_graph(node_number) ⇒ Object



45
46
47
48
49
# File 'lib/app/simple_model.rb', line 45

def new_random_graph(node_number)
  $LOG.debug("new_random_graph called on #{self}")
  self.new_graph
  @graph.gen_random_graph(node_number)
end

#new_ring_graph(node_number) ⇒ Object



51
52
53
54
55
# File 'lib/app/simple_model.rb', line 51

def new_ring_graph(node_number)
  $LOG.debug("new_ring_graph called on #{self}")
  self.new_graph
  @graph.gen_ring_graph(node_number)
end

#rotate_layout(theta) ⇒ Object



253
254
255
256
257
# File 'lib/app/simple_model.rb', line 253

def rotate_layout(theta)
  @layout.rotate(theta)
  changed
  notify_observers(nil)
end

#select(edge) ⇒ Object



123
124
# File 'lib/app/simple_model.rb', line 123

def select(edge)
end

#select_node(node) ⇒ Object



117
118
# File 'lib/app/simple_model.rb', line 117

def select_node(node)
end

#set_circle_layoutObject



142
143
144
# File 'lib/app/simple_model.rb', line 142

def set_circle_layout
  self.new_layout(CircleLayout.new(@view.canvas))
end

#set_collapse_layoutObject



162
163
164
# File 'lib/app/simple_model.rb', line 162

def set_collapse_layout
  new_layout(CollapseLayout.new(@view.canvas))
end

#set_directedObject



166
167
168
# File 'lib/app/simple_model.rb', line 166

def set_directed
  @graph_type = DirectedGraph
end

#set_fr_layoutObject



146
147
148
# File 'lib/app/simple_model.rb', line 146

def set_fr_layout
 self.new_layout(FRLayout.new(@view.canvas))
end

#set_isom_layoutObject



174
175
176
# File 'lib/app/simple_model.rb', line 174

def set_isom_layout
  self.new_layout(ISOMLayout.new(@view.canvas))
end

#set_kk_layoutObject



150
151
152
# File 'lib/app/simple_model.rb', line 150

def set_kk_layout
  self.new_layout(KKLayout.new(@view.canvas))
end

#set_location(node, x, y) ⇒ Object



88
89
90
# File 'lib/app/simple_model.rb', line 88

def set_location(node, x, y)
  @layout.set_location(node, x, y, true)
end

#set_morph_layout(target_layout) ⇒ Object



154
155
156
157
158
159
160
# File 'lib/app/simple_model.rb', line 154

def set_morph_layout(target_layout)
  if target_layout
    self.new_layout(MorphLayout.new(@view.canvas, Kernel.const_get(target_layout)))
  else
    self.new_layout(MorphLayout.new(@view.canvas))
  end
end

#set_random_layoutObject



138
139
140
# File 'lib/app/simple_model.rb', line 138

def set_random_layout
  self.new_layout(RandomLayout.new(@view.canvas))
end

#set_undirectedObject



170
171
172
# File 'lib/app/simple_model.rb', line 170

def set_undirected
  @graph_type = UndirectedGraph
end

#unclick(node) ⇒ Object



184
185
186
187
188
# File 'lib/app/simple_model.rb', line 184

def unclick(node)
  @graph.node_data(:is_clicked?)[node] = false
  changed
  notify_observers(nil)
end

#unclick_edge(edge) ⇒ Object



196
197
198
199
200
# File 'lib/app/simple_model.rb', line 196

def unclick_edge(edge)
  @graph.edge_data(:is_clicked?)[edge] = false
  changed
  notify_observers(nil)
end

#update(observable) ⇒ Object

being a listener



285
286
287
288
289
290
# File 'lib/app/simple_model.rb', line 285

def update(observable)
  $LOG.debug("Observer #{self} being updated by #{observable}")
  observable = nil if observable.is_a?(Graph)
  changed
  notify_observers(observable)
end

#view=(view) ⇒ Object



21
22
23
# File 'lib/app/simple_model.rb', line 21

def view=(view)
  @view = view
end