Class: DR::Node

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/dr/base/graph.rb

Constant Summary collapse

NodeError =
Class.new(RuntimeError)
STEP =
4

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, attributes: {}, graph: nil) ⇒ Node

Returns a new instance of Node.



9
10
11
12
13
14
15
16
# 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

Instance Attribute Details

#attributesObject

Returns the value of attribute attributes.



8
9
10
# File 'lib/dr/base/graph.rb', line 8

def attributes
  @attributes
end

#childrenObject

Returns the value of attribute children.



8
9
10
# File 'lib/dr/base/graph.rb', line 8

def children
  @children
end

#graphObject (readonly)

Returns the value of attribute graph.



7
8
9
# File 'lib/dr/base/graph.rb', line 7

def graph
  @graph
end

#nameObject

Returns the value of attribute name.



8
9
10
# File 'lib/dr/base/graph.rb', line 8

def name
  @name
end

#parentsObject

Returns the value of attribute parents.



8
9
10
# File 'lib/dr/base/graph.rb', line 8

def parents
  @parents
end

Instance Method Details

#<=>(other) ⇒ Object



20
21
22
# File 'lib/dr/base/graph.rb', line 20

def <=>(other)
	return @name <=> other.name
end

#add_child(*nodes) ⇒ Object

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



42
43
44
45
46
47
48
49
50
# 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) ⇒ Object



60
61
62
63
64
65
66
67
68
# 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

#ancestorsObject



24
25
26
# File 'lib/dr/base/graph.rb', line 24

def ancestors
	self.graph.ancestors(self, ourselves: false)
end

#check_node(node) ⇒ Object

Raises:



32
33
34
35
# 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

#descendantsObject



27
28
29
# File 'lib/dr/base/graph.rb', line 27

def descendants
	self.graph.descendants(self, ourselves: false)
end

#each(&b) ⇒ Object



17
18
19
# File 'lib/dr/base/graph.rb', line 17

def each(&b)
	@children.each(&b)
end

#inspectObject



83
84
85
# File 'lib/dr/base/graph.rb', line 83

def inspect
	"#{self.class}: #{to_s(show_attr: true)}"+(graph.nil? ? "" : " (#{graph})")
end

#rm_child(*nodes) ⇒ Object



51
52
53
54
55
56
57
58
59
# 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) ⇒ Object



69
70
71
72
73
74
75
76
77
# 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: []) ⇒ Object



97
98
99
100
101
102
103
104
# 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: []) ⇒ Object

output like a graph



87
88
89
90
91
92
93
94
95
96
# 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) ⇒ Object



80
81
82
# 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) ⇒ Object



37
38
39
# File 'lib/dr/base/graph.rb', line 37

def update_attributes(new_attr)
	@attributes.merge!(new_attr)
end