Class: DotGraphPrinter

Inherits:
Object
  • Object
show all
Defined in:
lib/graphr/graphviz_dot.rb

Constant Summary collapse

@@default_node_shaper =

A node shaper maps each node to a string describing its shape. Valid shapes are:

"ellipse"   (default)
"box"
"circle"
"plaintext" (no outline)
"doublecircle"
"diamond"

Not yet supported or untested once are:

"polygon", "record", "epsf"
proc{|n| "box"}
@@default_node_labeler =
proc{|n| 
  if Symbol===n
    n.id2name 
  elsif String===n
    n
  else
    n.inspect
  end
}
proc{|info| info ? info.inspect : nil}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(links = [], nodes = []) ⇒ DotGraphPrinter

links is either array of

arrays [fromNode, toNode [, infoOnLink]], or
objects with attributes :from, :to, :info

nodes is array of node objects All nodes used in the links are used as nodes even if they are not in the “nodes” parameters.



38
39
40
41
42
# File 'lib/graphr/graphviz_dot.rb', line 38

def initialize(links = [], nodes = [])
  @links, @nodes = links, add_nodes_in_links(links, nodes)
  @node_attributes, @edge_attributes = Hash.new, Hash.new
  set_default_values
end

Instance Attribute Details

#colorObject

Returns the value of attribute color.



2
3
4
# File 'lib/graphr/graphviz_dot.rb', line 2

def color
  @color
end

The following can be set to blocks of code that gives a default value for the node shapes, node labels and link labels, respectively.



6
7
8
# File 'lib/graphr/graphviz_dot.rb', line 6

def link_labeler
  @link_labeler
end

#node_labelerObject

The following can be set to blocks of code that gives a default value for the node shapes, node labels and link labels, respectively.



6
7
8
# File 'lib/graphr/graphviz_dot.rb', line 6

def node_labeler
  @node_labeler
end

#node_shaperObject

The following can be set to blocks of code that gives a default value for the node shapes, node labels and link labels, respectively.



6
7
8
# File 'lib/graphr/graphviz_dot.rb', line 6

def node_shaper
  @node_shaper
end

#orientationObject

Returns the value of attribute orientation.



2
3
4
# File 'lib/graphr/graphviz_dot.rb', line 2

def orientation
  @orientation
end

#sizeObject

Returns the value of attribute size.



2
3
4
# File 'lib/graphr/graphviz_dot.rb', line 2

def size
  @size
end

Instance Method Details

#set_default_valuesObject



44
45
46
47
48
49
50
51
# File 'lib/graphr/graphviz_dot.rb', line 44

def set_default_values
  @color = "black"
  @size = "9,11"
  @orientation = "portrait"
  @node_shaper = @@default_node_shaper
  @node_labeler = @@default_node_labeler
  @link_labeler = @@default_link_labeler
end

#set_edge_attributes(anEdge, aHash) ⇒ Object



60
61
62
63
64
# File 'lib/graphr/graphviz_dot.rb', line 60

def set_edge_attributes(anEdge, aHash)
  # TODO check if attributes are valid dot edge attributes
  edge = find_edge(anEdge)
  set_attributes(edge, @edge_attributes, true, aHash)
end

#set_node_attributes(aNode, aHash) ⇒ Object



66
67
68
69
# File 'lib/graphr/graphviz_dot.rb', line 66

def set_node_attributes(aNode, aHash)
  # TODO check if attributes are valid dot node attributes
  set_attributes(aNode, @node_attributes, true, aHash)
end

#to_dot_specificationObject



71
72
73
74
75
76
77
78
79
# File 'lib/graphr/graphviz_dot.rb', line 71

def to_dot_specification
  set_edge_labels(@links)
  set_node_labels_and_shape(@nodes)
  "digraph G {\n" +
    graph_parameters_to_dot_specification +
    @nodes.uniq.map {|n| format_node(n)}.join(";\n") + ";\n" +
    @links.uniq.map {|l| format_link(l)}.join(";\n") + ";\n" +
    "}"
end

#write_to_file(filename, fileType = "ps") ⇒ Object



53
54
55
56
57
58
# File 'lib/graphr/graphviz_dot.rb', line 53

def write_to_file(filename, fileType = "ps")
  dotfile = temp_filename(filename)
  File.open(dotfile, "w") {|f| f.write to_dot_specification}
  system "dot -T#{fileType} -o #{filename} #{dotfile}"
  File.delete(dotfile)
end