File: graphviz.rb

package info (click to toggle)
ruby-graphviz 1.2.3-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 1,156 kB
  • sloc: ruby: 7,682; xml: 26; makefile: 17
file content (969 lines) | stat: -rw-r--r-- 24,635 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
# Copyright (C) 2003 - 2012 Gregoire Lejeune <gregoire.lejeune@free.fr>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA


IS_JRUBY = (defined?( JRUBY_VERSION ) != nil)
IS_CYGWIN = ((RUBY_PLATFORM =~ /cygwin/) != nil)

require 'tempfile'

require 'graphviz/utils'
require 'graphviz/node'
require 'graphviz/edge'
require 'graphviz/attrs'
require 'graphviz/constants'
require 'graphviz/elements'
require 'graphviz/dot_script'

require 'graphviz/dot2ruby'
require 'graphviz/types'
require 'graphviz/core_ext'

if /^1.8/.match RUBY_VERSION
  $KCODE = "UTF8"
end

class GraphViz
  include GraphViz::Constants
  include GraphViz::Utils

  public

  ## Var: Output format (dot, png, jpeg, ...)
  @@format = nil #"canon"
  @format = nil
  ## Var: Output file name
  @filename = nil
  ## Var: Output format and file
  @output = nil
  ## Var: program to use (dot|twopi)
  @@prog = "dot"
  @prog = nil
  ## Var: program path
  @@path = []
  @path = nil
  ## Var: Error level
  @@errors = 1
  @errors = nil
  ## Var: External libraries
  @@extlibs = []
  @extlibs = nil

  ## Var: Graph name
  @name = nil

  ## Var: defined attributes
  @graph = nil
  @node = nil
  @edge = nil

  # This accessor allow you to set global graph attributes
  attr_accessor :graph
  alias_method :graph_attrs, :graph

  # This accessor allow you to set global nodes attributes
  attr_accessor :node
  alias_method :node_attrs, :node

  # This accessor allow you to set global edges attributes
  attr_accessor :edge
  alias_method :edge_attrs, :edge


  @elements_order = nil

  def add_node( xNodeName, hOpts = {} )
    if xNodeName.kind_of? Array
      raise ArgumentError, "use `add_nodes' to add several nodes at the same time"
    end
    return add_nodes(xNodeName, hOpts)
  end

  # Create a new node
  #
  # In:
  # * xNodeName : Name of the new node
  # * hOpts : Node attributes
  #
  # Return the GraphViz::Node object created
  def add_nodes(node_name, options = {})
     if node_name.kind_of? Array
        node_name.each { |n| add_nodes(n, options.clone) }
     else
        node = @hoNodes[node_name]

        if node.nil?
           @hoNodes[node_name] = GraphViz::Node::new( node_name, self )
           @hoNodes[node_name].index = @elements_order.size_of( "node" )

           unless options.keys.include?(:label) or options.keys.include?("label")
              options[:label] = node_name
           end

           @elements_order.push( {
              "type" => "node",
              "name" => node_name,
              "value" => @hoNodes[node_name]
           } )

           node = @hoNodes[node_name]
        end

        options.each do |xKey, xValue|
           @hoNodes[node_name][xKey.to_s] = xValue
        end

        return node
     end
  end

  # Return the node object for the given name (or nil) in the current graph
  def get_node( xNodeName, &block )
    node = @hoNodes[xNodeName] || nil

    yield( node ) if( block and node )

    return node
  end

  # Returns the first node found in the entire graph, starting from the root graph
  def find_node(name)
     root = root_graph
     return root.search_node(name)
  end

  # Return the first node found in the current graph, and it subgraphs
  def search_node(name)
     n = get_node(name)
     return n unless n.nil?
     each_graph { |_, g|
        n = g.search_node(name)
        return n unless n.nil?
     }
     return nil
  end

  #
  # Return the node object for the given index
  #
  def get_node_at_index( index )
    element = @elements_order[index, "node"]
    (element.nil?) ? nil : element["value"]
  end

  #
  # Allow you to traverse nodes
  #
  def each_node( &block )
    if block_given?
      @hoNodes.each do |name, node|
        yield( name, node )
      end
    else
      return( @hoNodes )
    end
  end

  # Get the number of nodes
  def node_count
    @hoNodes.size
  end

  def add_edge( oNodeOne, oNodeTwo, hOpts = {} )
    if oNodeTwo.kind_of? Array or oNodeOne.kind_of? Array 
      raise ArgumentError, "use `add_edges' to add several edges at the same time"
    end
    add_edges(oNodeOne, oNodeTwo, hOpts)
  end

  # Create a new edge
  #
  # In:
  # * node_one : First node (or node list)
  # * node_two : Second Node (or node list)
  # * options : Edge attributes
  def add_edges( node_one, node_two, options = {} )

    if( node_one.class == Array )
      node_one.each do |no|
        add_edges( no, node_two, options )
      end
    else
      if( node_two.class == Array )
        node_two.each do |nt|
          add_edges( node_one, nt, options )
        end
      else
        edge = GraphViz::Edge::new( node_one, node_two, self )
        edge.index = @elements_order.size_of( "edge" )

        options.each do |xKey, xValue|
          edge[xKey.to_s] = xValue
        end

        @elements_order.push( {
          "type" => "edge",
          "value" => edge
        } )
        @loEdges.push( edge )

        return( edge )
      end
    end
  end

  #
  # Allow you to traverse edges
  #
  def each_edge( &block )
    if block_given?
      @loEdges.each do |edge|
        yield(edge)
      end
    else
      return @loEdges
    end
  end

  #
  # Get the number of edges
  #
  def edge_count
    @loEdges.size
  end

  #
  # Return the edge object for the given index
  #
  def get_edge_at_index( index )
    element = @elements_order[index, "edge"]
    (element.nil?) ? nil : element["value"]
  end

  #
  # Create a new graph
  #
  # In:
  # * xGraphName : Graph name
  # * hOpts : Graph attributes
  #
  def add_graph( xGraphName = nil, hOpts = {}, &block )
     if xGraphName.kind_of?(GraphViz)
        xGraphID = xGraphName.id
        @hoGraphs[xGraphID] = xGraphName.clone
        @hoGraphs[xGraphID].type = @oGraphType
        @hoGraphs[xGraphID].pg = self
        xGraphName = xGraphID
     else
        if xGraphName.kind_of?(Hash)
           hOpts = xGraphName
           xGraphName = nil
        end

        if xGraphName.nil?
           xGraphID = String.random(11)
           xGraphName = ""
        else
           xGraphID = xGraphName
        end

        @hoGraphs[xGraphID] = GraphViz::new( xGraphName, {:parent => self, :type => @oGraphType}, &block )

        hOpts.each do |xKey, xValue|
           @hoGraphs[xGraphID][xKey.to_s] = xValue
        end
     end

     @elements_order.push( {
        "type" => "graph",
        "name" => xGraphName,
        "value" => @hoGraphs[xGraphID]
     } )

     return( @hoGraphs[xGraphID] )
  end
  alias :subgraph :add_graph
  #
  # Return the graph object for the given name (or nil)
  #
  def get_graph( xGraphName, &block )
    graph = @hoGraphs[xGraphName] || nil

    yield( graph ) if( block and graph )

    return graph
  end

  #
  # Allow you to traverse graphs
  #
  def each_graph( &block )
    if block_given?
      @hoGraphs.each do |name, graph|
        yield( name, graph )
      end
    else
      return @hoGraphs
    end
  end

  #
  # Add nodes and edges defined by a Hash
  #
  def add(h)
    if h.kind_of? Hash
      h.each do |node, data|
        add_hash_edge(node, data)
      end
    end
  end

  #
  # Return the graph type (graph digraph)
  #
  def type
    @oGraphType
  end
  def type=(x) #:nodoc:
    @oGraphType = x
  end

  #
  # Get the number of graphs
  #
  def graph_count
    @hoGraphs.size
  end

  def method_missing( idName, *args, &block ) #:nodoc:
    xName = idName.id2name

    rCod = nil

    if block
      # Creating a cluster named '#{xName}'
      rCod = add_graph( xName, args[0]||{} )
      yield( rCod )
    else
      # Create a node named '#{xName}' or search for a node, edge or cluster
      if @hoNodes.keys.include?( xName )
        if( args[0] )
          return { xName => args[0] }
        else
          return( @hoNodes[xName] )
        end
      end
      return( @hoGraphs[xName] ) if @hoGraphs.keys.include?( xName )

      rCod = add_nodes( xName, args[0]||{} )
    end

    return rCod
  end

  #
  # Set value +xValue+ to the graph attribute +xAttrName+
  #
  def []=( xAttrName, xValue )
    xValue = xValue.to_s if xValue.class == Symbol
    @graph[xAttrName] = xValue
  end

  #
  # Get the value of the graph attribute +xAttrName+
  #
  def []( xAttrName )
    if Hash === xAttrName
      xAttrName.each do |key, value|
        self[key] = value
      end
    else
      attr = @graph[xAttrName]
      if attr.nil?
        return nil
      else
        return( @graph[xAttrName].clone )
      end
    end
  end

  #
  # Calls block once for each attribute of the graph, passing the name and value to the
  # block as a two-element array.
  #
  def each_attribute(&b)
    @graph.each do |k,v|
      yield(k,v)
    end
  end
  def each_attribut(&b)
     warn "`GraphViz#each_attribut` is deprecated, please use `GraphViz#each_attribute`"
     each_attribute(&b)
  end

  # Create a new graph from the current subgraph
  def to_graph
    graph = self.clone
    graph.pg = nil
    return graph
  end

  #
  # Generate the graph
  #
  # Options :
  # * :output : Output format (GraphViz::Constants::FORMATS)
  # * :file : Output file name
  # * :use : Program to use (GraphViz::Constants::PROGRAMS)
  # * :path : Program PATH
  # * :<format> => <file> : <file> can be
  #   * a file name
  #   * nil, then the output will be printed to STDOUT
  #   * String, then the output will be returned as a String
  # * :errors : DOT error level (default 1)
  #   * 0 = Error + Warning
  #   * 1 = Error
  #   * 2 = none
  #
  def output( hOpts = {} )
    xDOTScript = DOTScript.new
    lNotHugly = []

    append_attributes_and_types(xDOTScript)

    xDOTScript << "}"

    if has_parent_graph?
      xDOTScript.make_subgraph("#{GraphViz.escape(@name, :unquote_empty => true)}")
    else
      hOutput = {}

      hOpts.each do |xKey, xValue|
        xValue = xValue.to_s unless xValue.nil? or [Class, TrueClass, FalseClass].include?(xValue.class)
        case xKey.to_s
          when "use"
            if PROGRAMS.index( xValue ).nil?
              raise ArgumentError, "can't use '#{xValue}'"
            end
            @prog = xValue
          when "path"
            @path = xValue && xValue.split( "," ).map{ |x| x.strip }
          when "errors"
            @errors = xValue
          when "extlib"
            @extlibs = xValue.split( "," ).map{ |x| x.strip }
          when "scale"
            # Scale input by 'v' (=72)
            @scale = xValue
          when "inverty"
            # Invert y coordinate in output
            @inverty = xValue
          when "no_layout"
            # No layout mode 'v' (=1)
            @no_layout = xValue
          when "reduce"
            # Reduce graph
            @reduce_graph = xValue
          when "Lg"
            # Don't use grid
            @Lg = xValue
          when "LO"
            # Use old attractive force
            @LO = xValue
          when "Ln"
            # Set number of iterations to i
            @Ln = xValue
          when "LU"
            # Set unscaled factor to i
            @LU = xValue
          when "LC"
            # Set overlap expansion factor to v
            @LC = xValue
          when "LT"
            # Set temperature (temperature factor) to v
            @LT = xValue
          when "nothugly"
            begin
              require 'graphviz/nothugly'
              @nothugly = true
            rescue LoadError
              warn "You must install ruby-xslt or libxslt-ruby to use nothugly option!"
              @nothugly = false
            end
          else
            if FORMATS.index( xKey.to_s ).nil?
              raise ArgumentError, "output format '#{xValue}' invalid"
            end
            hOutput[xKey.to_s] = xValue
        end
      end

      @output = hOutput if hOutput.size > 0

      xStict = ((@strict && @oGraphType == "digraph") ? "strict " : "")
      xDOTScript.prepend(
        "#{xStict}#{@oGraphType} #{GraphViz.escape(@name, :unquote_empty => true)} {"
      )

      xOutputString = (@filename == String ||
        @output.any? {|format, file| file == String })

      xOutput = ""
      if @format.to_s == "none" or @output.any? {|fmt, fn| fmt.to_s == "none"}
        if xOutputString
          xOutput << xDOTScript
        else
          xFileName = @output["none"] || @filename
          open( xFileName, "w" ) do |h|
            h.puts xDOTScript
          end
        end
      end

      if (@format.to_s != "none" and not @format.nil?) or (@output.any? {|format, file| format != "none" } and @output.size > 0)
        ## Act: Save script and send it to dot
        t = Tempfile::open( File.basename(__FILE__) )
        t.print( xDOTScript )
        t.close

        cmd = find_executable( @prog, @path )
        if cmd == nil
          raise StandardError, "GraphViz not installed or #{@prog} not in PATH. Install GraphViz or use the 'path' option"
        end

        xOutputWithFile = []
        xOutputWithoutFile = []
        unless @format.nil? or @format == "none"
          lNotHugly << @filename if @format.to_s == "svg" and @nothugly
          if @filename.nil? or @filename == String
            xOutputWithoutFile = ["-T#{@format}"]
          else
            xOutputWithFile = ["-T#{@format}", "-o#{@filename}"]
          end
        end
        @output.each_except( :key => ["none"] ) do |format, file|
          lNotHugly << file if format.to_s == "svg" and @nothugly
          if file.nil? or file == String
            xOutputWithoutFile += ["-T#{format}"]
          else
            xOutputWithFile += ["-T#{format}", "-o#{file}"]
          end
        end

        xExternalLibraries = @extlibs.map { |lib| "-l#{lib}" }

        xOtherOptions = []
        xOtherOptions << "-s#{@scale}" if @scale
        xOtherOptions << "-y" if @inverty
        xOtherOptions << "-n#{@no_layout}" if @no_layout
        xOtherOptions << "-x" if @reduce_graph
        xOtherOptions << "-Lg" if @Lg
        xOtherOptions << "-LO" if @LO
        xOtherOptions << "-Ln#{@Ln}" if @Ln
        xOtherOptions << "-LU#{@LU}" if @LU
        xOtherOptions << "-LC#{@LC}" if @LC
        xOtherOptions << "-LT#{@LT}" if @LT

        tmpPath = if IS_JRUBY
          t.path
        elsif IS_CYGWIN
          begin
            IO.popen("cygpath", "-w", t.path).chomp
          rescue
            warn "cygpath is not installed!"
            t.path
          end
        else
          t.path
        end

        xCmd = [cmd, "-q#{@errors}"] +
               xExternalLibraries +
               xOtherOptions +
               xOutputWithFile +
               xOutputWithoutFile +
               [tmpPath]

        xOutput << output_from_command( xCmd )
      end

      # Not Hugly
      lNotHugly.each do |f|
        if f.nil? or f == String
          xOutput = GraphViz.nothugly( xOutput, false )
        else
          GraphViz.nothugly( f, true )
        end
      end

      if xOutputString
        xOutput
      else
        print xOutput
      end
    end
  end
  alias :save :output

  def append_attributes_and_types(script)
    script_data = DOTScriptData.new

    @elements_order.each { |kElement|
      is_new_type = script_data.type != kElement["type"]
      if is_new_type 
        script << script_data unless script_data.type.nil? or script_data.empty?
        script_data = DOTScriptData.new(kElement["type"])
      end

      # Modified by Brandon Coleman verify value is NOT NULL
      kElement["value"] or raise ArgumentError, "#{kElement["name"]} is nil!"

      case kElement["type"]
        when "graph_attr", "node_attr", "edge_attr"
          script_data.add_attribute(kElement["name"], kElement["value"].to_gv)
        when "node", "graph"
          script << kElement["value"].output()
        when "edge"
          script << "  " + kElement["value"].output( @oGraphType )
        else
          raise ArgumentError,
            "Don't know what to do with element type '#{kElement['type']}'"
      end
    }
    script << script_data unless script_data.type.nil? or script_data.empty?
  end

  def to_s
    self.output(:none => String)
  end

  #
  # Get the graph name
  #
  def name
    @name.clone
  end
  alias :id :name

  #
  # Create an edge between the current cluster and the node or cluster +oNode+
  #
  def <<( oNode )
    raise( ArgumentError, "Edge between root graph and node or cluster not allowed!" ) if self.pg.nil?

    if( oNode.class == Array )
      oNode.each do |no|
        self << no
      end
    else
      return GraphViz::commonGraph( oNode, self ).add_edges( self, oNode )
    end
  end
  alias :> :<<
  alias :- :<<
  alias :>> :<<

  def pg #:nodoc:
    @oParentGraph
  end
  def pg=(x) #:nodoc:
    @oParentGraph = x
  end

  #
  # Return the root graph
  #
  def root_graph
    return( (self.pg.nil?) ? self : self.pg.root_graph )
  end

  def self.commonGraph( o1, o2 ) #:nodoc:
    g1 = o1.pg
    g2 = o2.pg

    return o1 if g1.nil?
    return o2 if g2.nil?

    return g1 if g1.object_id == g2.object_id

    return GraphViz::commonGraph( g1, g2 )
  end

  def set_position( xType, xKey, xValue ) #:nodoc:
    @elements_order.push( {
      "type" => "#{xType}_attr",
      "name" => xKey,
      "value" => xValue
    } )
  end

## ----------------------------------------------------------------------------

  #
  # Change default options (:use, :path, :errors and :output)
  #
  def self.default( hOpts )
    hOpts.each do |k, v|
      case k.to_s
        when "use"
          @@prog = v
        when "path"
          @@path = v.split( "," ).map{ |x| x.strip }
        when "errors"
          @@errors = v
        when "extlibs"
          @@extlibs = v.split( "," ).map{ |x| x.strip }
        else
          warn "Invalid option #{k}!"
      end
    end
  end

  def self.options( hOpts )
    GraphViz::default( hOpts )
  end

## ----------------------------------------------------------------------------

  # Create a new graph from a GraphViz File
  #
  # Options :
  # * :output : Output format (GraphViz::Constants::FORMATS) (default : dot)
  # * :file : Output file name (default : none)
  # * :use : Program to use (GraphViz::Constants::PROGRAMS) (default : dot)
  # * :path : Program PATH
  #
  def self.parse( xFile, hOpts = {}, &block )
    graph = Dot2Ruby::new( hOpts[:path], nil, nil ).eval( xFile )
    yield( graph ) if( block and graph )
    return graph
  end

  # Create a new graph from a GraphViz File
  #
  # Options :
  # * :output : Output format (GraphViz::Constants::FORMATS) (default : dot)
  # * :file : Output file name (default : none)
  # * :use : Program to use (GraphViz::Constants::PROGRAMS) (default : dot)
  # * :path : Program PATH
  #
  def self.parse_string( str, hOpts = {}, &block )
    graph = Dot2Ruby::new(hOpts[:path], nil, nil).eval_string(str)
    yield(graph) if(block and graph)
    return graph
  end

  # Return a new completed graph
  def complete
    GraphViz.parse_string( root_graph.output( :dot => String ) )
  end

  # Complete the current graph
  def complete!
    # TODO: Keep options
    complete
  end

  # Return true if the graph is directed.
  def directed?
    not((/digraph/ =~ "bla digraph bla").nil?)
  end

  def has_parent_graph?
     not @oParentGraph.nil?
  end
## ----------------------------------------------------------------------------

  private

  ## Var: Nodes, Edges and Graphs tables
  @hoNodes = nil
  @loEdges = nil
  @hoGraphs = nil

  ## Var: Parent graph
  @oParentGraph = nil

  ## Var: Type de graphe (orienté ou non)
  @oGraphType = nil

  #
  # Create a new graph object
  #
  # Options :
  # * :output : Output format (GraphViz::Constants::FORMATS) (default : dot)
  # * :file : Output file name (default : nil)
  # * :use : Program to use (GraphViz::Constants::PROGRAMS) (default : dot)
  # * :path : Program PATH
  # * :parent : Parent graph (default : nil)
  # * :type : Graph type (GraphViz::Constants::GRAPHTYPE) (default : digraph)
  # * :errors : DOT error level (default 1)
  #   * 0 = Error + Warning
  #   * 1 = Error
  #   * 2 = none
  #
  def initialize( xGraphName, hOpts = {}, &block )
    @filename = nil
    @name     = xGraphName.to_s
    @format   = @@format
    @prog     = @@prog
    @path     = @@path
    @errors   = @@errors
    @extlibs  = @@extlibs
    @output   = {}
    @nothugly = false
    @strict   = false

    @scale        = nil
    @inverty      = nil
    @no_layout    = nil
    @reduce_graph = nil
    @Lg           = nil
    @LO           = nil
    @Ln           = nil
    @LU           = nil
    @LC           = nil
    @LT           = nil

    @elements_order = GraphViz::Elements::new()

    @oParentGraph = nil
    @oGraphType   = "digraph"

    @hoNodes  = Hash::new()
    @loEdges  = Array::new()
    @hoGraphs = Hash::new()

    @node  = GraphViz::Attrs::new( self, "node",  NODESATTRS  )
    @edge  = GraphViz::Attrs::new( self, "edge",  EDGESATTRS  )
    @graph = GraphViz::Attrs::new( self, "graph", GRAPHSATTRS )

    hOpts.each do |xKey, xValue|
      case xKey.to_s
        when "use"
          if PROGRAMS.index( xValue.to_s ).nil?
            raise ArgumentError, "can't use '#{xValue}'"
          end
          @prog = xValue.to_s
        when "parent"
          @oParentGraph = xValue
        when "type"
          if GRAPHTYPE.index( xValue.to_s ).nil?
            raise ArgumentError, "graph type '#{xValue}' unknow"
          end
          @oGraphType = xValue.to_s
        when "path"
          @path = xValue.split( "," ).map{ |x| x.strip }
        when "strict"
          @strict = (xValue ? true : false)
        when "errors"
          @errors = xValue
        when "extlibs"
          @extlibs = xValue.split( "," ).map{ |x| x.strip }
        else
          self[xKey.to_s] = xValue.to_s
      end
    end

    yield( self ) if( block )
  end

  # Edge between a node and a Hash
  # Used by GraphViz#add
  def add_hash_edge(node, hash)
    if hash.kind_of? Hash
      hash.each do |nt, data|
        add_edges(node, nt)
        add_hash_edge(nt, data)
      end
    else
      add_edges(node, hash)
    end
  end

  #
  # Create a new undirected graph
  #
  # See also GraphViz::new
  #
  def self.graph( xGraphName, hOpts = {}, &block )
    new( xGraphName, hOpts.symbolize_keys.merge( {:type => "graph"} ), &block )
  end

  #
  # Create a new directed graph
  #
  # See also GraphViz::new
  #
  def self.digraph( xGraphName, hOpts = {}, &block )
    new( xGraphName, hOpts.symbolize_keys.merge( {:type => "digraph"} ), &block )
  end

  # Create a new strict directed graph
  #
  # See also GraphViz::new
  def self.strict_digraph( xGraphName, hOpts = {}, &block )
    new( xGraphName, hOpts.symbolize_keys.merge( {:type => "digraph", :strict => true} ), &block )
  end

  # Create a random graph.
  def self.generate(num_nodes, num_edges, directed = false, weight_range = (1..1))
     g = nil
     if directed
        g = GraphViz.digraph(:G)
     else
        g = GraphViz.graph(:G)
     end

     nodes = (1..num_nodes).map{ |e| e.to_s }
     g.add_nodes(nodes)

     edges = []
     nodes.each do |head|
        nodes.each do |tail|
           if (directed and head != tail) or (head < tail)
              edges << {:head => head, :tail => tail, :weight => weight_range.to_a.shuffle[0]}
           end
        end
     end
     edges.shuffle!

     (num_edges - 1).times do |i|
        g.add_edges(edges[i][:head], edges[i][:tail], :label => edges[i][:weight].to_s, :weight => edges[i][:weight])
     end

     return g
  end

  #
  # Escape a string to be acceptable as a node name in a graphviz input file
  #
  def self.escape(str, opts = {} ) #:nodoc:
    options = {
      :force => false,
      :unquote_empty => false,
    }.merge(opts)

    reserved_words = %w{node edge graph digraph subgraph strict}
    if (options[:force] or str.match( /\A[a-zA-Z_]+[a-zA-Z0-9_]*\Z/ ).nil? or reserved_words.include?(str.downcase))
      unless options[:unquote_empty] and str.size == 0
        '"' + str.gsub('"', '\\"').gsub("\n", '\\\\n') + '"'
      end
    else
      str
    end
  end
end