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



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



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/rbgraph/node.rb', line 85

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



125
126
127
# File 'lib/rbgraph/node.rb', line 125

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



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

def in_degree
  incoming_edges.size
end

#incoming_edgesObject



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

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

#inspectObject



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

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
# File 'lib/rbgraph/node.rb', line 43

def merge!(node, options = {}, &block)
  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



60
61
62
# File 'lib/rbgraph/node.rb', line 60

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

#out_degreeObject



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

def out_degree
  outgoing_edges.size
end

#outgoing_edgesObject



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

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

#parentObject



72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/rbgraph/node.rb', line 72

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



108
109
110
# File 'lib/rbgraph/node.rb', line 108

def root
  ancestors.values.last || self
end

#to_json(options = {}) ⇒ Object



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

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