Class: OMF::OML::OmlNetwork

Inherits:
Base::LObject
  • Object
show all
Includes:
MonitorMixin
Defined in:
lib/omf_oml/network.rb

Overview

This class represents a network consisting of nodes and links with their respective attributes.

Constant Summary collapse

@@name2network =
{}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name = nil, attributes = {}) ⇒ OmlNetwork

name - Name of table opts -



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/omf_oml/network.rb', line 45

def initialize(name = nil, attributes = {})
  super name
  @name = name || "nw_#{object_id}"
  @attributes = attributes
  @nodes = {}
  @name2node = {}
  @links = {}
  @name2link = {}
  @epoch = 0 # increment whenever an element is being updated
  @updateListeners = {}
  if name
    synchronize do
      if @@name2network[name]
        raise OmlNetworkAlreadyExistException.new(name)
      end
      @@name2network[name] = self
    end
  end
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



39
40
41
# File 'lib/omf_oml/network.rb', line 39

def name
  @name
end

Class Method Details

.[](name) ⇒ Object

Return a named network



35
36
37
# File 'lib/omf_oml/network.rb', line 35

def self.[](name)
  @@name2network[name]
end

Instance Method Details

opts

:from - fromNode if +fromNode+ is nil
:to - toNode if +toNode+ is nil
...  - rest of options passed on to +NetworkLink+ constructor


133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/omf_oml/network.rb', line 133

def create_link(name = nil, fromNode = nil, toNode = nil, attributes = {})
  name = name.to_sym if name
  fromNode = attributes.delete(:from) unless fromNode
  toNode = attributes.delete(:to) unless toNode

  synchronize do
    if name && @name2link[name]
      raise OmlLinkAlreadyExistException.new(name)
    end
    if fromNode
      fromNode = node(fromNode) || (raise UnknownOmlNodeException.new(fromNode))
    end
    if toNode
      toNode = node(toNode) || (raise UnknownOmlNodeException.new(toNode))
    end
    link = NetworkLink.new(name, fromNode, toNode, attributes, self)
    @links[link.el_id] = link
    @name2link[name] = link if name
    link
  end
end

#create_node(name = nil, attributes = {}) ⇒ Object

NOTE: May need a monitor if used in multi-threaded environments



114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/omf_oml/network.rb', line 114

def create_node(name = nil, attributes = {})
  name = name.to_sym if name
  synchronize do
    if name && @name2node[name]
      raise OmlNodeAlreadyExistException.new(name)
    end
    node = NetworkNode.new(name, attributes, self)
    @nodes[node.el_id] = node
    @name2node[name] = node if name
    node
  end
end

#describeObject



196
197
198
199
200
201
202
# File 'lib/omf_oml/network.rb', line 196

def describe
  nh = {}
  @nodes.each do |id, node| nh[id] = node.describe end
  lh = {}
  @links.each do |id, link| lh[id] = link.describe end
  {:nodes => nh, :links => lh}
end

Return the link named name. If the link doesn’t exist and new_opts is a Hash, create a new one and return that.



88
89
90
91
92
93
94
95
# File 'lib/omf_oml/network.rb', line 88

def link(name, new_opts = nil)
  return name if name.kind_of? NetworkLink
  link = @name2link[name.to_sym]
  if link.nil? && !new_opts.nil?
    link = create_link(name, nil, nil, new_opts)
  end
  link
end


184
185
186
187
188
189
190
191
192
193
# File 'lib/omf_oml/network.rb', line 184

def link_schema(schema = nil)
  if schema
    @link_schema = OmlSchema.create(schema)
    @link_schema.insert_column_at(0, :id)
    @link_schema.insert_column_at(1, :name)
    @link_schema.insert_column_at(2, :from_id)
    @link_schema.insert_column_at(3, :to_id)
  end
  @link_schema
end


81
82
83
# File 'lib/omf_oml/network.rb', line 81

def links()
  @links.values
end

#node(name, new_opts = nil) ⇒ Object

Return the node named name. If the node doesn’t exist and new_opts is a Hash, create a new one and return that.



72
73
74
75
76
77
78
79
# File 'lib/omf_oml/network.rb', line 72

def node(name, new_opts = nil)
  return name if name.kind_of? NetworkNode
  node = @name2node[name.to_sym]
  if node.nil? && !new_opts.nil?
    node = create_node(name, new_opts)
  end
  node
end

#node_schema(schema = nil) ⇒ Object



175
176
177
178
179
180
181
182
# File 'lib/omf_oml/network.rb', line 175

def node_schema(schema = nil)
  if schema
    @node_schema = OmlSchema.create(schema)
    @node_schema.insert_column_at(0, :id)
    @node_schema.insert_column_at(1, :name)
  end
  @node_schema
end

#nodesObject



65
66
67
# File 'lib/omf_oml/network.rb', line 65

def nodes()
  @nodes.values
end

#on_update(name = :_, &callback) ⇒ Object

Register a callback to be called every time network elements change The callback is provided with an arrach of changed elements.



101
102
103
104
105
106
107
108
109
110
# File 'lib/omf_oml/network.rb', line 101

def on_update(name = :_, &callback)
  if (callback)
    if @updateListeners[name]
      throw SameNameOnUpdateException.new(name)
    end
    @updateListeners[name] = callback
  else
    @updateListeners.delete(name)
  end
end

#to_jsonObject



286
287
288
# File 'lib/omf_oml/network.rb', line 286

def to_json
  describe.to_json
end

#to_table(aspect, table_opts = {}) ⇒ Object

Create a table to track an aspect of this network.

aspect - Either :nodes or :links



235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
# File 'lib/omf_oml/network.rb', line 235

def to_table(aspect, table_opts = {})
  aspect = aspect.to_sym
  table_opts[:supress_index] = true
  case aspect
  when :nodes
    #puts "TABLE SCHEME 1 >>>> #{@node_schema}"
    table = OmlTable.create @name + '/nodes', @node_schema, table_opts
    #puts "TABLE SCHEME 2 >>>> #{@node_schema} - #{table.schema}"
    #table.add_rows(@nodes)
    table.add_rows(@nodes.map do |id, n|
      @node_schema.hash_to_row(n.attributes)
    end)
    on_update "__to_tables_nodes_#{table.object_id}" do |a|
      nodes = a.map do |e|
        e.kind_of?(NetworkNode) ? @node_schema.hash_to_row(e.attributes) : nil
      end.compact
      table.add_rows(nodes) unless nodes.empty?
    end

  when :links
    table = OmlTable.create @name + '/links', @link_schema, table_opts
    table.add_rows(@links.map do |id, n|
       @link_schema.hash_to_row(n.attributes)
    end)
    #puts table.rows.inspect
    on_update "__to_tables_links_#{table.object_id}" do |a|
      links = a.map do |e|
        e.kind_of?(NetworkLink) ? @link_schema.hash_to_row(e.attributes) : nil
      end.compact
      table.add_rows(links) unless links.empty?
    end

  else
    raise "Unknown aspect '#{aspect}'. Should be either 'nodes' or 'links'."
  end

  # on_update "__to_tables_#{table.object_id}" do |a|
    # a.each do |e|
      # if aspect == :nodes && e.kind_of?(NetworkNode)
        # table.add_row @node_schema.hash_to_row(e.attributes)
      # end
      # if aspect == :links && e.kind_of?(NetworkLink)
        # table.add_row @link_schema.hash_to_row(e.attributes)
      # end
    # end
  # end

  table
end

#transaction(&block) ⇒ Object

To have the update listeners only called once when multiple elements are changed at once, perform the changes within a transaction block. The listeners are then called once with an array containing all updated elements.



159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'lib/omf_oml/network.rb', line 159

def transaction(&block)
  updated = UpdateSet.new
  synchronize do
    @updated = updated

    @in_transaction = true
    block.call
    @in_transaction = true
  end
  unless updated.empty?
    @updateListeners.values.each do |l|
      l.call(updated)
    end
  end
end

#updated(element) ⇒ Object



292
293
294
295
296
297
298
299
300
301
302
303
304
# File 'lib/omf_oml/network.rb', line 292

def updated(element)
  synchronize do
    if @in_transaction
      @updated << element
      return
    end
  end
  uset = UpdateSet.new
  uset << element
  @updateListeners.each do |l|
    l.call(uset)
  end
end