Class: RGL::DOT::Graph

Inherits:
Element show all
Defined in:
lib/laser/third_party/rgl/rdot.rb

Overview

A graph representation. Whether or not it is rendered as directed or undirected depends on which of the programs dot or neato is used to process and render the graph.

Direct Known Subclasses

Digraph, Subgraph

Instance Attribute Summary

Attributes inherited from Element

#name, #options

Instance Method Summary collapse

Constructor Details

#initialize(params = {}, option_list = GRAPH_OPTS) ⇒ Graph

Creates a new Graph with the params Hash providing settings for all graph options. The option_list parameter restricts those options to the list of valid names it contains. The exception to this is the elements option which, if specified, must be an Enumerable containing a list of nodes, edges, and/or subgraphs.



290
291
292
293
294
# File 'lib/laser/third_party/rgl/rdot.rb', line 290

def initialize (params = {}, option_list = GRAPH_OPTS)
  super(params, option_list)
  @elements   = params['elements'] ? params['elements'] : []
  @dot_string = 'graph'
end

Instance Method Details

#<<(element) ⇒ Object Also known as: push

Adds a new node, edge, or subgraph to this graph.

:call-seq:

graph << element -> graph


312
313
314
315
# File 'lib/laser/third_party/rgl/rdot.rb', line 312

def << (element)
  @elements << element
  self
end

#each_element(&block) ⇒ Object

Calls block once for each node, edge, or subgraph contained by this graph, passing the node, edge, or subgraph to the block.

:call-seq:

graph.each_element {|element| block} -> graph


302
303
304
305
# File 'lib/laser/third_party/rgl/rdot.rb', line 302

def each_element (&block)
  @elements.each(&block)
  self
end

#popObject

Removes the most recently added node, edge, or subgraph from this graph and returns it.

:call-seq:

graph.pop -> element


324
325
326
# File 'lib/laser/third_party/rgl/rdot.rb', line 324

def pop
  @elements.pop
end

#to_s(leader = '', indent = ' ') ⇒ Object

Returns a string representation of this graph which is consumable by the graphviz tools dot and neato. The leader parameter is used to indent every line of the returned string, and the indent parameter is used to additionally indent nested items.



332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
# File 'lib/laser/third_party/rgl/rdot.rb', line 332

def to_s (leader = '', indent = '    ')
  hdr = leader + @dot_string + (@name.nil? ? '' : ' ' + quote_ID(@name)) + " {\n"

  options = @options.to_a.collect do |name, val|
    unless val.nil? then
      if name == 'label' then
        leader + indent + "#{quote_ID(name)} = #{quote_label(val)}"
      else
        leader + indent + "#{quote_ID(name)} = #{quote_ID(val)}"
      end
    end
  end.compact.join( "\n" )

  elements = @elements.collect do |element|
    element.to_s(leader + indent, indent)
  end.join("\n\n")
  hdr + (options.empty? ? '' : options + "\n\n") +
    (elements.empty? ? '' : elements + "\n") + leader + "}"
end