Class: DR::Node

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

Constant Summary collapse

STEP =
4

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Node.



9
10
11
12
13
14
15
16
# File 'lib/drain/base/graph.rb', line 9

def initialize(name, attributes: nil, 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/drain/base/graph.rb', line 8

def attributes
  @attributes
end

#childrenObject

Returns the value of attribute children.



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

def children
  @children
end

#graphObject (readonly)

Returns the value of attribute graph.



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

def graph
  @graph
end

#nameObject

Returns the value of attribute name.



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

def name
  @name
end

#parentsObject

Returns the value of attribute parents.



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

def parents
  @parents
end

Instance Method Details

#<=>(other) ⇒ Object



20
21
22
# File 'lib/drain/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)



24
25
26
27
28
29
30
31
# File 'lib/drain/base/graph.rb', line 24

def add_child(*nodes)
  nodes.each do |node|
    if not @children.include?(node)
      @children << node
      node.parents << self
    end
  end
end

#add_parent(*nodes) ⇒ Object



40
41
42
43
44
45
46
47
# File 'lib/drain/base/graph.rb', line 40

def add_parent(*nodes)
  nodes.each do |node|
    if not @parents.include?(node)
      @parents << node
      node.children << self
    end
  end
end

#eachObject



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

def each
  @children.each
end

#rm_child(*nodes) ⇒ Object



32
33
34
35
36
37
38
39
# File 'lib/drain/base/graph.rb', line 32

def rm_child(*nodes)
  nodes.each do |node|
    if @children.include?(node)
      @children.delete(node)
      node.parents.delete(self)
    end
  end
end

#rm_parent(*nodes) ⇒ Object



48
49
50
51
52
53
54
55
# File 'lib/drain/base/graph.rb', line 48

def rm_parent(*nodes)
  nodes.each do |node|
    if @parents.include?(node)
      @parents.delete(node)
      node.children.delete(self)
    end
  end
end

#to_dotObject



72
73
74
75
76
77
78
79
# File 'lib/drain/base/graph.rb', line 72

def to_dot
  sout=["\""+name+"\""]
  @children.each do |child|
    sout.push  "\"#{@name}\" -> \"#{child.name}\""
    sout += child.to_dot
  end
  return sout
end

#to_graph(indent_level: 0) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
# File 'lib/drain/base/graph.rb', line 61

def to_graph(indent_level: 0)
  sout = ""
  margin = ''
  0.upto(indent_level/STEP-1) { |p| margin += (p==0 ? ' ' : '|') + ' '*(STEP - 1) }
  margin += '|' + '-'*(STEP - 2)
  sout += margin + "#{@name}\n"
  @children.each do |child|
    sout += child.to_graph(indent_level: indent_level+STEP)
  end
  return sout
end

#to_sObject



58
59
60
# File 'lib/drain/base/graph.rb', line 58

def to_s
  return @name
end