8 # options for node declaration
26 # options for edge declaration
44 # options for graph declaration
74 # a root class for any element in dot notation
78 def initialize( params = {} )
79 @label = params['name'] ? params['name'] : ''
87 # an element that has options ( node, edge or graph )
88 class Element < SimpleElement
90 attr_accessor :name, :options
92 def initialize( params = {}, option_list = [] )
94 @name = params['name'] ? params['name'] : nil
95 @parent = params['parent'] ? params['parent'] : nil
98 @options[i] = params[i] if params[i]
100 @options['label'] ||= @name if @name != 'node'
104 @options.each{ |i| yield i }
108 @options.each_pair{ |key, val| yield key, val }
111 #def parent=( thing )
112 # @parent.delete( self ) if defined?( @parent ) and @parent
118 # this is used when we build nodes that have shape=record
119 # ports don't have options :)
120 class Port < SimpleElement
123 def initialize( params = {} )
125 @name = params['label'] ? params['label'] : ''
128 ( @name && @name != "" ? "<#{@name}>" : "" ) + "#{@label}"
135 def initialize( params = {}, option_list = NODE_OPTS )
136 super( params, option_list )
137 @ports = params['ports'] ? params['ports'] : []
141 @ports.each{ |i| yield i }
158 label = @options['shape'] != 'record' && @ports.length == 0 ?
160 t + TAB + "label = \"#{@options['label']}\"\n" :
162 t + TAB + 'label = "' + " \\\n" +
163 t + TAB2 + "#{@options['label']}| \\\n" +
166 }.join( "| \\\n" ) + " \\\n" +
170 @options.to_a.collect{ |i|
171 i[1] && i[0] != 'label' ?
172 t + TAB + "#{i[0]} = #{i[1]}" : nil
173 }.compact.join( ",\n" ) + ( label != '' ? ",\n" : "\n" ) +
179 # subgraph element is the same to graph, but has another header in dot
181 class Subgraph < Element
183 def initialize( params = {}, option_list = GRAPH_OPTS )
184 super( params, option_list )
185 @nodes = params['nodes'] ? params['nodes'] : []
186 @dot_string = 'subgraph'
190 @nodes.each{ |i| yield i }
206 hdr = t + "#{@dot_string} #{@name} {\n"
208 options = @options.to_a.collect{ |name, val|
209 val && name != 'label' ?
210 t + TAB + "#{name} = #{val}" :
211 name ? t + TAB + "#{name} = \"#{val}\"" : nil
212 }.compact.join( "\n" ) + "\n"
214 nodes = @nodes.collect{ |i|
216 }.join( "\n" ) + "\n"
217 hdr + options + nodes + t + "}\n"
222 class Digraph < Subgraph
223 def initialize( params = {}, option_list = GRAPH_OPTS )
224 super( params, option_list )
225 @dot_string = 'digraph'
231 attr_accessor :from, :to
232 def initialize( params = {}, option_list = EDGE_OPTS )
233 super( params, option_list )
234 @from = params['from'] ? params['from'] : nil
235 @to = params['to'] ? params['to'] : nil
239 t + "#{@from} -> #{to} [\n" +
240 @options.to_a.collect{ |i|
241 i[1] && i[0] != 'label' ?
242 t + TAB + "#{i[0]} = #{i[1]}" :
243 i[1] ? t + TAB + "#{i[0]} = \"#{i[1]}\"" : nil
244 }.compact.join( "\n" ) + "\n" + t + "]\n"