Class: Rbgraph::Node

Inherits:
Object
  • Object
show all
Defined in:
lib/rbgraph/node.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(graph, id, data = {}) ⇒ Node

Returns a new instance of Node.



11
12
13
14
15
16
17
18
# 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

Instance Attribute Details

#dataObject

Returns the value of attribute data.



9
10
11
# File 'lib/rbgraph/node.rb', line 9

def data
  @data
end

#edgesObject

Returns the value of attribute edges.



8
9
10
# File 'lib/rbgraph/node.rb', line 8

def edges
  @edges
end

#graphObject

Returns the value of attribute graph.



6
7
8
# File 'lib/rbgraph/node.rb', line 6

def graph
  @graph
end

#idObject

Returns the value of attribute id.



5
6
7
# File 'lib/rbgraph/node.rb', line 5

def id
  @id
end

#neighborsObject

Returns the value of attribute neighbors.



7
8
9
# File 'lib/rbgraph/node.rb', line 7

def neighbors
  @neighbors
end

Instance Method Details

#[](key) ⇒ Object



33
34
35
# File 'lib/rbgraph/node.rb', line 33

def [](key)
  attributes.fetch(key.to_sym)
end

#ancestorsObject



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# 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 = {}) ⇒ Object



128
129
130
# File 'lib/rbgraph/node.rb', line 128

def as_json(options = {})
  attributes
end

#attributesObject



29
30
31
# File 'lib/rbgraph/node.rb', line 29

def attributes
  {id: id, data: data}
end

#connect_to(node, edge) ⇒ Object



37
38
39
40
41
# File 'lib/rbgraph/node.rb', line 37

def connect_to(node, edge)
  neighbors[node.id] ||= node
  edges[edge.id] ||= edge
  self
end

#hashObject



25
26
27
# File 'lib/rbgraph/node.rb', line 25

def hash
  id.hash
end

#in_degreeObject



119
120
121
# File 'lib/rbgraph/node.rb', line 119

def in_degree
  incoming_edges.size
end

#incoming_edgesObject



71
72
73
# File 'lib/rbgraph/node.rb', line 71

def incoming_edges
  edges.select { |eid, edge| edge.in_for?(self) }
end

#inspectObject



123
124
125
# File 'lib/rbgraph/node.rb', line 123

def inspect
  "<Rbgraph::Node:##{id} #{data.inspect}>"
end

#merge!(node, options = {}, &block) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# 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) ⇒ Object



63
64
65
# File 'lib/rbgraph/node.rb', line 63

def merge_data!(node)
  data.merge!(node.data)
end

#out_degreeObject



115
116
117
# File 'lib/rbgraph/node.rb', line 115

def out_degree
  outgoing_edges.size
end

#outgoing_edgesObject



67
68
69
# File 'lib/rbgraph/node.rb', line 67

def outgoing_edges
  edges.select { |eid, edge| edge.out_for?(self) }
end

#parentObject



75
76
77
78
79
80
81
82
83
84
85
86
# 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

#rootObject



111
112
113
# File 'lib/rbgraph/node.rb', line 111

def root
  ancestors.values.last || self
end

#to_json(options = {}) ⇒ Object



132
133
134
# File 'lib/rbgraph/node.rb', line 132

def to_json(options = {})
  JSON.generate(attributes)
end