Class: Rviz::Graph

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Helper

#attrs_to_s, #quote, #set

Constructor Details

#initialize(attrs = {}) ⇒ Graph

Returns a new instance of Graph.



5
6
7
8
9
10
11
# File 'lib/rviz/graph.rb', line 5

def initialize attrs = {}
  @attrs = attrs
  @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

Instance Method Details

#add(name, shape = 'box', attrs = {}) ⇒ Object

add a node or create a node with name, shae and attributes



39
40
41
42
43
44
45
46
47
48
49
# File 'lib/rviz/graph.rb', line 39

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



57
58
59
60
# File 'lib/rviz/graph.rb', line 57

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

#edge_attrObject



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

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

#graph_attrObject



20
21
22
23
# File 'lib/rviz/graph.rb', line 20

def graph_attr
  @attrs["charset"] = "UTF-8" unless @attrs["charset"]
  sprintf("  graph [%s]", self.attrs_to_s)
end

#graph_endObject



17
# File 'lib/rviz/graph.rb', line 17

def graph_end; "}" end

#graph_start(type = "digraph", name = "G") ⇒ Object



13
14
15
# File 'lib/rviz/graph.rb', line 13

def graph_start type = "digraph", name = "G"
  "#{type} #{name} {"
end

create edge between two nodes



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

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



34
35
36
# File 'lib/rviz/graph.rb', line 34

def node name
  @nodes[name]
end

#node_attrObject



25
26
27
# File 'lib/rviz/graph.rb', line 25

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



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/rviz/graph.rb', line 63

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
  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
    if %w[Mrecord record].include?(@nodes[ary[0]].shape)
      ary[1] = @nodes[ary[0]].get_anchor(ary[1])
      ary[3] = @nodes[ary[2]].get_anchor(ary[3])
    end
    @edges << Edge.new(*ary)
  end

  @edges.each {|e| fh.puts "  " + e.to_s}

  fh.puts self.graph_end
  fh.close
end