class Rbgraph::Node

Attributes

data[RW]
edges[RW]
graph[RW]
id[RW]
neighbors[RW]

Public Class Methods

new(graph, id, data = {}) click to toggle source
# File lib/rbgraph/node.rb, line 11
def initialize(graph, id, data = {})
  raise "Node should have a non-nil id!" if id.nil?
  self.graph      = graph
  self.id         = id
  self.neighbors  = {}
  self.edges      = {}
  self.data       = data || {}
end

Public Instance Methods

==(node) click to toggle source
# File lib/rbgraph/node.rb, line 20
def ==(node)
  self.id == node.id
end
[](key) click to toggle source
# File lib/rbgraph/node.rb, line 33
def [](key)
  attributes.fetch(key.to_sym)
end
ancestors() click to toggle source
# File lib/rbgraph/node.rb, line 88
def ancestors
  if graph.directed?
    nodes = {}
    up = parent
    if up.nil?
      return {}
    else
      nodes[up.id] = up
    end
    while !up.parent.nil?
      up = up.parent
      if nodes[up.id].nil?
        nodes[up.id] = up
      else
        raise "Cycle detected while getting ancestors of #{id}!"
      end
    end
    nodes
  else
    {}
  end
end
as_json(options = {}) click to toggle source
# File lib/rbgraph/node.rb, line 128
def as_json(options = {})
  attributes
end
attributes() click to toggle source
# File lib/rbgraph/node.rb, line 29
def attributes
  {id: id, data: data}
end
connect_to(node, edge) click to toggle source
# File lib/rbgraph/node.rb, line 37
def connect_to(node, edge)
  neighbors[node.id] ||= node
  edges[edge.id] ||= edge
  self
end
hash() click to toggle source
# File lib/rbgraph/node.rb, line 25
def hash
  id.hash
end
in_degree() click to toggle source
# File lib/rbgraph/node.rb, line 119
def in_degree
  incoming_edges.size
end
incoming_edges() click to toggle source
# File lib/rbgraph/node.rb, line 71
def incoming_edges
  edges.select { |eid, edge| edge.in_for?(self) }
end
inspect() click to toggle source
# File lib/rbgraph/node.rb, line 123
def inspect
  "<Rbgraph::Node:##{id} #{data.inspect}>"
end
merge!(node, options = {}, &block) click to toggle source
# File lib/rbgraph/node.rb, line 43
def merge!(node, options = {}, &block)
  if options[:keep_other_data]
    data.merge!(node.data)
  end
  node.edges.values.group_by(&:kind).each do |kind, edges|
    edges.each do |edge|
      other_node = edge.other_node(node)
      edge_kind = edge.kind || options[:edge_kind]
      unless other_node == self
        if edge.out_for?(node)
          graph.add_edge!(self, other_node, edge.weight, edge_kind, edge.data, &block)
        elsif edge.in_for?(node)
          graph.add_edge!(other_node, self, edge.weight, edge_kind, edge.data, &block)
        end
      end
    end
  end
  graph.remove_node!(node)
end
merge_data!(node) click to toggle source
# File lib/rbgraph/node.rb, line 63
def merge_data!(node)
  data.merge!(node.data)
end
out_degree() click to toggle source
# File lib/rbgraph/node.rb, line 115
def out_degree
  outgoing_edges.size
end
outgoing_edges() click to toggle source
# File lib/rbgraph/node.rb, line 67
def outgoing_edges
  edges.select { |eid, edge| edge.out_for?(self) }
end
parent() click to toggle source
# File lib/rbgraph/node.rb, line 75
def parent
  if graph.directed?
    incoming_edges_arr = incoming_edges.values
    case incoming_edges_arr.size
    when 0 then nil
    when 1 then incoming_edges_arr.first.different_node(self) ||
        raise("Node #{id} is connected to self!")
    else
      raise "Node #{id} has more than 1 incoming edges!"
    end
  end
end
root() click to toggle source
# File lib/rbgraph/node.rb, line 111
def root
  ancestors.values.last || self
end
to_json(options = {}) click to toggle source
# File lib/rbgraph/node.rb, line 132
def to_json(options = {})
  JSON.generate(attributes)
end