Class: GraphViz::Edge

Inherits:
Object show all
Includes:
Constants
Defined in:
lib/graphviz/edge.rb

Constant Summary

Constant Summary

Constants included from Constants

Constants::EDGESATTRS, Constants::FORMATS, Constants::GENCS_ATTRS, Constants::GRAPHSATTRS, Constants::GRAPHTYPE, Constants::NODESATTRS, Constants::PROGRAMS, Constants::RGV_VERSION

Instance Method Summary (collapse)

Methods included from Constants

getAttrsFor

Constructor Details

- (Edge) initialize(vNodeOne, vNodeTwo, parent_graph)

Create a new edge

In:

  • vNodeOne : First node (can be a GraphViz::Node or a node ID)

  • vNodeTwo : Second node (can be a GraphViz::Node or a node ID)

  • parent_graph : Graph



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/graphviz/edge.rb', line 30

def initialize( vNodeOne, vNodeTwo, parent_graph )
   @node_one_id, @node_one_port = getNodeNameAndPort( vNodeOne )
   @node_two_id, @node_two_port = getNodeNameAndPort( vNodeTwo )

   @parent_graph = parent_graph
   @edge_attributes = GraphViz::Attrs::new( nil, "edge", EDGESATTRS )
   @index = nil

   unless @parent_graph.directed?
      (@parent_graph.find_node(@node_one_id) || @parent_graph.add_nodes(@node_one_id)).incidents << (@parent_graph.find_node(@node_two_id) || @parent_graph.add_nodes(@node_two_id))
      (@parent_graph.find_node(@node_two_id) || @parent_graph.add_nodes(@node_two_id)).neighbors << (@parent_graph.find_node(@node_one_id) || @parent_graph.add_nodes(@node_one_id))
   end
   (@parent_graph.find_node(@node_one_id) || @parent_graph.add_nodes(@node_one_id)).neighbors << (@parent_graph.find_node(@node_two_id) || @parent_graph.add_nodes(@node_two_id))
   (@parent_graph.find_node(@node_two_id) || @parent_graph.add_nodes(@node_two_id)).incidents << (@parent_graph.find_node(@node_one_id) || @parent_graph.add_nodes(@node_one_id))
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

- (Object) method_missing(idName, *args, &block)

Add edge options use edge.<option>=<value> or edge.<option>( <value> )



152
153
154
155
156
157
# File 'lib/graphviz/edge.rb', line 152

def method_missing( idName, *args, &block ) #:nodoc:
   return if idName == :to_ary # ruby 1.9.2 fix
   xName = idName.id2name

   self[xName.gsub( /=$/, "" )]=args[0]
end

Instance Method Details

- (Object) <<(node) Also known as: >, -, >>

:nodoc:



117
118
119
120
121
# File 'lib/graphviz/edge.rb', line 117

def <<( node ) #:nodoc:
   n = @parent_graph.get_node(@node_two_id)

   GraphViz::commonGraph( node, n ).add_edges( n, node )
end

- (Object) [](attribute_name)

Set values for edge attributes or get the value of the given edge attribute attribute_name



82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/graphviz/edge.rb', line 82

def []( attribute_name )
   # Modification by axgle (http://github.com/axgle)
   if Hash === attribute_name
      attribute_name.each do |key, value|
         self[key] = value
      end
   else
      if @edge_attributes[attribute_name.to_s]
         @edge_attributes[attribute_name.to_s].clone
      else
         nil
      end
   end
end

- (Object) []=(attribute_name, attribute_value)

Set value attribute_value to the edge attribute attribute_name



75
76
77
78
# File 'lib/graphviz/edge.rb', line 75

def []=( attribute_name, attribute_value )
   attribute_value = attribute_value.to_s if attribute_value.class == Symbol
   @edge_attributes[attribute_name.to_s] = attribute_value
end

- (Object) each_attribut(global = true, &b)



112
113
114
115
# File 'lib/graphviz/edge.rb', line 112

def each_attribut(global = true, &b)
   warn "`GraphViz::Edge#each_attribut` is deprecated, please use `GraphViz::Edge#each_attribute`"
   each_attribute(global, &b)
end

- (Object) each_attribute(global = true, &b)

Calls block once for each attribute of the edge, passing the name and value to the block as a two-element array.

If global is set to false, the block does not receive the attributes set globally



103
104
105
106
107
108
109
110
111
# File 'lib/graphviz/edge.rb', line 103

def each_attribute(global = true, &b)
   attrs = @edge_attributes.to_h
   if global
      attrs = pg.edge.to_h.merge attrs
   end
   attrs.each do |k,v|
      yield(k,v)
   end
end

- (Object) index

Return the index of the edge



67
68
69
# File 'lib/graphviz/edge.rb', line 67

def index
   @index
end

- (Object) index=(i)

:nodoc:



70
71
72
# File 'lib/graphviz/edge.rb', line 70

def index=(i) #:nodoc:
   @index = i if @index == nil
end

- (Object) node_one(with_port = true) Also known as: tail_node

Return the node one as string (so with port if any)



47
48
49
50
51
52
53
# File 'lib/graphviz/edge.rb', line 47

def node_one( with_port = true )
   if not(@node_one_port and with_port)
      GraphViz.escape(@node_one_id)
   else
      GraphViz.escape(@node_one_id, :force => true) + ":#{@node_one_port}"
   end
end

- (Object) node_two(with_port = true) Also known as: head_node

Return the node two as string (so with port if any)



57
58
59
60
61
62
63
# File 'lib/graphviz/edge.rb', line 57

def node_two( with_port = true )
   if not(@node_two_port and with_port)
      GraphViz.escape(@node_two_id)
   else
      GraphViz.escape(@node_two_id, :force => true) + ":#{@node_two_port}"
   end
end

- (Object) output(oGraphType)

:nodoc:



159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
# File 'lib/graphviz/edge.rb', line 159

def output( oGraphType ) #:nodoc:
   xLink = " -> "
   if oGraphType == "graph"
      xLink = " -- "
   end

   xOut = self.node_one + xLink + self.node_two
   xAttr = ""
   xSeparator = ""
   @edge_attributes.data.each do |k, v|
      xAttr << xSeparator + k + " = " + v.to_gv
      xSeparator = ", "
   end
   if xAttr.length > 0
      xOut << " [" + xAttr + "]"
   end
   xOut << ";"

   return( xOut )
end

- (Object) pg

:nodoc:



133
134
135
# File 'lib/graphviz/edge.rb', line 133

def pg #:nodoc:
   @parent_graph
end

- (Object) root_graph

Return the root graph



129
130
131
# File 'lib/graphviz/edge.rb', line 129

def root_graph
   return( (self.pg.nil?) ? nil : self.pg.root_graph )
end

- (Object) set {|_self| ... }

Set edge attributes

Example :

e = graph.add_edges( ... )
...
e.set { |_e|
  _e.color = "blue"
  _e.fontcolor = "red"
}

Yields:

  • (_self)

Yield Parameters:



146
147
148
# File 'lib/graphviz/edge.rb', line 146

def set( &b )
   yield( self )
end