Class: Rviz::Graph
- Inherits:
-
Object
show all
- Includes:
- Helper
- Defined in:
- lib/rviz/graph.rb
Instance Attribute Summary collapse
Instance Method Summary
collapse
-
#add(name, shape = 'box', attrs = {}) ⇒ Object
add a node or create a node with name, shae and attributes.
-
#add_edge(from_name, from_anchor, to_name, to_anchor, attrs = {}) ⇒ Object
-
#add_mrecord(name, attr = {}) ⇒ Object
-
#add_record(name, attr = {}) ⇒ Object
-
#add_subgraph ⇒ Object
-
#edge_attr ⇒ Object
-
#graph_attr ⇒ Object
-
#graph_end ⇒ Object
-
#graph_start(type = "digraph", name = "G") ⇒ Object
-
#initialize(title = '', attrs = {}) ⇒ Graph
constructor
-
#link(from_name, from_anchor, to_name, to_anchor = "", attrs = {}) ⇒ Object
create edge between two nodes.
-
#node(name) ⇒ Object
return a specific node from the node list.
-
#node_attr ⇒ Object
-
#output(file = STDOUT) ⇒ Object
output dot language source to file or to stdout.
Methods included from Helper
#attrs_to_s, #quote, #set
Constructor Details
#initialize(title = '', attrs = {}) ⇒ Graph
5
6
7
8
9
10
11
12
|
# File 'lib/rviz/graph.rb', line 5
def initialize title = '', attrs = {}
@attrs = attrs
@title = title
@nodes = Hash.new
@edges = Array.new
@links = Array.new
@subgraphs = Hash.new
end
|
Instance Attribute Details
#edges ⇒ Object
Returns the value of attribute edges.
3
4
5
|
# File 'lib/rviz/graph.rb', line 3
def edges
@edges
end
|
#nodes ⇒ Object
Returns the value of attribute nodes.
3
4
5
|
# File 'lib/rviz/graph.rb', line 3
def nodes
@nodes
end
|
#subgraphs ⇒ Object
Returns the value of attribute subgraphs.
3
4
5
|
# File 'lib/rviz/graph.rb', line 3
def subgraphs
@subgraphs
end
|
#title ⇒ Object
Returns the value of attribute title.
3
4
5
|
# File 'lib/rviz/graph.rb', line 3
def title
@title
end
|
Instance Method Details
#add(name, shape = 'box', attrs = {}) ⇒ Object
add a node or create a node with name, shae and attributes
40
41
42
43
44
45
46
47
48
49
50
|
# File 'lib/rviz/graph.rb', line 40
def add name, shape='box', attrs = {}
if name.is_a?(Node)
node = name
name = name.name
else
node = Node.new(name, shape, attrs)
end
@nodes[name] = node
self
end
|
#add_edge(from_name, from_anchor, to_name, to_anchor, attrs = {}) ⇒ Object
72
73
74
75
|
# File 'lib/rviz/graph.rb', line 72
def add_edge from_name, from_anchor, to_name, to_anchor, attrs = {}
@edges << Edge.new(from_name, from_anchor, to_name, to_anchor, attrs)
self
end
|
#add_mrecord(name, attr = {}) ⇒ Object
61
62
63
64
|
# File 'lib/rviz/graph.rb', line 61
def add_mrecord name, attr = {}
@nodes[name] = Mrecord.new(name, attr)
self
end
|
#add_record(name, attr = {}) ⇒ Object
56
57
58
59
|
# File 'lib/rviz/graph.rb', line 56
def add_record name, attr = {}
@nodes[name] = Record.new(name, attr)
self
end
|
#add_subgraph ⇒ Object
52
53
54
|
# File 'lib/rviz/graph.rb', line 52
def add_subgraph
end
|
#edge_attr ⇒ Object
30
31
32
|
# File 'lib/rviz/graph.rb', line 30
def edge_attr
' edge [fontname="Arial Unicode MS", fontsize=7];'
end
|
#graph_attr ⇒ Object
21
22
23
24
|
# File 'lib/rviz/graph.rb', line 21
def graph_attr
@attrs["charset"] = "UTF-8" unless @attrs["charset"]
sprintf(" graph [%s]", self.attrs_to_s)
end
|
#graph_end ⇒ Object
18
|
# File 'lib/rviz/graph.rb', line 18
def graph_end; "}" end
|
#graph_start(type = "digraph", name = "G") ⇒ Object
14
15
16
|
# File 'lib/rviz/graph.rb', line 14
def graph_start type = "digraph", name = "G"
"#{type} #{name} {"
end
|
#link(from_name, from_anchor, to_name, to_anchor = "", attrs = {}) ⇒ Object
create edge between two nodes
67
68
69
70
|
# File 'lib/rviz/graph.rb', line 67
def link from_name, from_anchor, to_name, to_anchor = "", attrs = {}
@links << [from_name, from_anchor, to_name, to_anchor, attrs]
self
end
|
#node(name) ⇒ Object
return a specific node from the node list
35
36
37
|
# File 'lib/rviz/graph.rb', line 35
def node name
@nodes[name]
end
|
#node_attr ⇒ Object
26
27
28
|
# File 'lib/rviz/graph.rb', line 26
def node_attr
' node [fontname="Arial Unicode MS", fontsize=9, width=2.0];'
end
|
#output(file = STDOUT) ⇒ Object
output dot language source to file or to stdout
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
|
# File 'lib/rviz/graph.rb', line 78
def output file=STDOUT
fh = file
fh = File.open(file, "w:utf-8") if file.is_a?(String)
fh.puts self.graph_start
fh.puts self.graph_attr
if self.title and self.title.size > 0
fh.puts %Q{ labelloc = "t";}
fh.puts %Q{ label= "#{self.title}";}
end
fh.puts self.node_attr
fh.puts self.edge_attr
@nodes.each {|n,node| fh.puts " " + node.to_s}
@links.each do |ary|
raise "#{ary[0]} not found as a node" unless @nodes[ary[0]]
raise "#{ary[2]} not found as a node" unless @nodes[ary[2]]
ary[1] = @nodes[ary[0]].get_anchor(ary[1]) if @nodes[ary[0]].is_a?(Record)
ary[3] = @nodes[ary[2]].get_anchor(ary[3]) if @nodes[ary[2]].is_a?(Record)
@edges << Edge.new(*ary)
end
@edges.each {|e| fh.puts " " + e.to_s}
fh.puts self.graph_end
fh.close
end
|