class DR::Node

Constants

NodeError
STEP

Attributes

attributes[RW]
children[RW]
graph[R]
name[RW]
parents[RW]

Public Class Methods

new(name, attributes: {}, graph: nil) click to toggle source
# File lib/dr/base/graph.rb, line 9
def initialize(name, attributes: {}, graph: nil)
        @name = name
        @children = []
        @parents = []
        @attributes = attributes
        @graph=graph
        graph.nodes << self if @graph
end

Public Instance Methods

<=>(other) click to toggle source
# File lib/dr/base/graph.rb, line 20
def <=>(other)
        return @name <=> other.name
end
add_child(*nodes) click to toggle source

self.add_child(ploum) marks ploum as a child of self (ie ploum depends on self)

# File lib/dr/base/graph.rb, line 42
def add_child(*nodes)
        nodes.each do |node|
                check_node(node)
                if not @children.include?(node)
                        @children << node
                        node.parents << self
                end
        end
end
add_parent(*nodes) click to toggle source
# File lib/dr/base/graph.rb, line 60
def add_parent(*nodes)
        nodes.each do |node|
                check_node(node)
                if not @parents.include?(node)
                        @parents << node
                        node.children << self
                end
        end
end
ancestors() click to toggle source
# File lib/dr/base/graph.rb, line 24
def ancestors
        self.graph.ancestors(self, ourselves: false)
end
check_node(node) click to toggle source
# File lib/dr/base/graph.rb, line 32
def check_node(node)
        raise NodeError.new("wrong class: #{node.class}") unless node.is_a?(Node)
        raise NodeError.new("wrong graph: #{node.graph}") if self.graph != node.graph
end
descendants() click to toggle source
# File lib/dr/base/graph.rb, line 27
def descendants
        self.graph.descendants(self, ourselves: false)
end
each(&b) click to toggle source
# File lib/dr/base/graph.rb, line 17
def each(&b)
        @children.each(&b)
end
inspect() click to toggle source
# File lib/dr/base/graph.rb, line 83
def inspect
        "#{self.class}: #{to_s(show_attr: true)}"+(graph.nil? ? "" : " (#{graph})")
end
rm_child(*nodes) click to toggle source
# File lib/dr/base/graph.rb, line 51
def rm_child(*nodes)
        nodes.each do |node|
                check_node(node)
                if @children.include?(node)
                        @children.delete(node)
                        node.parents.delete(self)
                end
        end
end
rm_parent(*nodes) click to toggle source
# File lib/dr/base/graph.rb, line 69
def rm_parent(*nodes)
        nodes.each do |node|
                check_node(node)
                if @parents.include?(node)
                        @parents.delete(node)
                        node.children.delete(self)
                end
        end
end
to_dot(out: []) click to toggle source
# File lib/dr/base/graph.rb, line 97
def to_dot(out: [])
        out << "\""+name+"\""
        @children.each do |child|
                out <<  "\"#{@name}\" -> \"#{child.name}\""
                child.to_dot(out: out)
        end
        return out
end
to_graph(indent_level: 0, show_attr: true, out: []) click to toggle source

output like a graph

# File lib/dr/base/graph.rb, line 87
def to_graph(indent_level: 0, show_attr: true, out: [])
        margin = ''
        0.upto(indent_level/STEP-1) { |p| margin += (p==0 ? ' ' : '|') + ' '*(STEP - 1) }
        margin += '|' + '-'*(STEP - 2)
        out << margin + "#{to_s(show_attr: show_attr)}"
        @children.each do |child|
                child.to_graph(indent_level: indent_level+STEP, show_attr: show_attr, out: out)
        end
        return out
end
to_s(show_attr: true) click to toggle source
# File lib/dr/base/graph.rb, line 80
def to_s(show_attr: true)
        @name.to_s + (show_attr && ! attributes.empty? ? " #{attributes}" : "")
end
update_attributes(new_attr) click to toggle source
# File lib/dr/base/graph.rb, line 37
def update_attributes(new_attr)
        @attributes.merge!(new_attr)
end