Class: Rviz::Graph

Inherits:
Object
  • Object
show all
Includes:
Helper
Defined in:
lib/rviz/graph.rb

Direct Known Subclasses

Subgraph

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Helper

#attrs_to_s, #quote, #set

Constructor Details

#initialize(title = '', attrs = {}) ⇒ Graph

Returns a new instance of 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

#edgesObject

Returns the value of attribute edges.



3
4
5
# File 'lib/rviz/graph.rb', line 3

def edges
  @edges
end

#nodesObject

Returns the value of attribute nodes.



3
4
5
# File 'lib/rviz/graph.rb', line 3

def nodes
  @nodes
end

#subgraphsObject

Returns the value of attribute subgraphs.



3
4
5
# File 'lib/rviz/graph.rb', line 3

def subgraphs
  @subgraphs
end

#titleObject

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_subgraphObject



52
53
54
# File 'lib/rviz/graph.rb', line 52

def add_subgraph
  # todo
end

#edge_attrObject



30
31
32
# File 'lib/rviz/graph.rb', line 30

def edge_attr
  '  edge [fontname="Arial Unicode MS", fontsize=7];'
end

#graph_attrObject



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_endObject



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

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_attrObject



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]] # from
    raise "#{ary[2]} not found as a node" unless @nodes[ary[2]] # to
    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