File: GraphViz.pm

package info (click to toggle)
libgraphviz-perl 2.24-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, sid, trixie
  • size: 952 kB
  • sloc: perl: 2,174; makefile: 8
file content (1417 lines) | stat: -rw-r--r-- 39,233 bytes parent folder | download
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
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
package GraphViz;

use 5.006;
use strict;
use warnings;
our $AUTOLOAD;

use Carp;
use Config;
use IPC::Run qw(run binary);

our $VERSION = '2.24';

=pod

=head1 NAME

GraphViz - Interface to AT&T's GraphViz. Deprecated. See GraphViz2

=head1 SYNOPSIS

  use GraphViz;

  my $g = GraphViz->new();

  $g->add_node('London');
  $g->add_node('Paris', label => 'City of\nlurve');
  $g->add_node('New York');

  $g->add_edge('London' => 'Paris');
  $g->add_edge('London' => 'New York', label => 'Far');
  $g->add_edge('Paris' => 'London');

  print $g->as_png;


=head1 DESCRIPTION

This module provides an interface to layout and image generation of directed
and undirected graphs in a variety of formats (PostScript, PNG, etc.) using the
"dot", "neato", "twopi", "circo" and "fdp"  programs from the Graphviz project
(http://www.graphviz.org/ or http://www.research.att.com/sw/tools/graphviz/).

GraphViz is deprecated in favour of L<GraphViz2>.

=head1 Installation

Of course you need to install AT&T's Graphviz before using this module.
See L<http://www.graphviz.org/Download.php>.

You are strongly advised to download the stable version of Graphviz, because the
development snapshots (click on 'Source code'), are sometimes non-functional.

Install L<GraphViz> as you would for any C<Perl> module:

Run:

	cpanm GraphViz

	Note: cpanm ships in App::cpanminus. See also App::perlbrew.

or run:

	sudo cpan GraphViz

or unpack the distro, and then either:

	perl Build.PL
	./Build
	./Build test
	sudo ./Build install

or:

	perl Makefile.PL
	make (or dmake or nmake)
	make test
	make install

=head1 Overview

=head2 Modules in this distro

=over 4

=item o GraphViz

=item o GraphViz::No

=item o GraphViz::Small

=item o GraphViz::Regex

=item o GraphViz::XML

=item o GraphViz::Data::Grapher

=item o GraphViz::Parse::RecDescent

=item o GraphViz::Parse::Yacc

=item o GraphViz::Parse::Yapp

=back

=head2 What is a graph?

A (undirected) graph is a collection of nodes linked together with
edges.

A directed graph is the same as a graph, but the edges have a
direction.

=head2 What is GraphViz?

This module is an interface to the GraphViz toolset
(http://www.graphviz.org/). The GraphViz tools provide automatic graph
layout and drawing. This module simplifies the creation of graphs and
hides some of the complexity of the GraphViz module.

Laying out graphs in an aesthetically-pleasing way is a hard problem -
there may be multiple ways to lay out the same graph, each with their
own quirks. GraphViz luckily takes part of this hard problem and does
a pretty good job in a couple of seconds for most graphs.

=head2 Why should I use this module?

Observation aids comprehension. That is a fancy way of expressing
that popular faux-Chinese proverb: "a picture is worth a thousand
words".

Text is not always the best way to represent anything and everything
to do with a computer programs. Pictures and images are easier to
assimilate than text. The ability to show a particular thing
graphically can aid a great deal in comprehending what that thing
really represents.

Diagrams are computationally efficient, because information can be
indexed by location; they group related information in the same
area. They also allow relations to be expressed between elements
without labeling the elements.

A friend of mine used this to his advantage when trying to remember
important dates in computer history. Instead of sitting down and
trying to remember everything, he printed over a hundred posters (each
with a date and event) and plastered these throughout his house. His
spatial memory is still so good that asked last week (more than a year
since the experiment) when Lisp was invented, he replied that it was
upstairs, around the corner from the toilet, so must have been around
1958.

Spreadsheets are also a wonderfully simple graphical representation of
computational models.

=head2 Applications

Bundled with this module are several modules to help graph data
structures (GraphViz::Data::Dumper), XML (GraphViz::XML), and
Parse::RecDescent, Parse::Yapp, and yacc grammars
(GraphViz::Parse::RecDescent, GraphViz::Parse::Yapp, and
GraphViz::Parse::Yacc).

Note that Marcel Grunauer has released some modules on CPAN to graph
various other structures. See GraphViz::DBI and GraphViz::ISA for
example.

brian d foy has written an article about Devel::GraphVizProf for
Dr. Dobb's Journal:
http://www.ddj.com/columns/perl/2001/0104pl002/0104pl002.htm

=head2 Award winning!

I presented a paper and talk on "Graphing Perl" using GraphViz at the
3rd German Perl Workshop and received the "Best Knowledge Transfer"
prize.

    Talk: http://www.astray.com/graphing_perl/graphing_perl.pdf
  Slides: http://www.astray.com/graphing_perl/

=head1 METHODS

=head2 new

This is the constructor. It accepts several attributes.

  my $g = GraphViz->new();
  my $g = GraphViz->new(directed => 0);
  my $g = GraphViz->new(layout => 'neato', ratio => 'compress');
  my $g = GraphViz->new(rankdir  => 'BT');
  my $g = GraphViz->new(width => 8.5, height => 11);
  my $g = GraphViz->new(width => 30, height => 20,
			pagewidth => 8.5, pageheight => 11);

The most two important attributes are 'layout' and 'directed'.

=over

=item layout

The 'layout' attribute determines which layout algorithm GraphViz.pm will
use. Possible values are:

=over

=item dot

The default GraphViz layout for directed graph layouts

=item neato

For undirected graph layouts - spring model

=item twopi

For undirected graph layouts - radial

=item circo

For undirected graph layouts - circular

=item fdp

For undirected graph layouts - force directed spring model

=back

=item directed

The 'directed' attribute, which defaults to 1 (true) specifies
directed (edges have arrows) graphs. Setting this to zero produces
undirected graphs (edges do not have arrows).

=item rankdir

Another attribute 'rankdir' controls the direction in which the nodes are linked
together. The default is 'TB' (arrows from top to bottom). Other legal values
are 'BT' (bottom->top), 'LR' (left->right) and 'RL' (right->left).

=item width, height

The 'width' and 'height' attributes control the size of the bounding
box of the drawing in inches. This is more useful for PostScript
output as for raster graphic (such as PNG) the pixel dimensions
can not be set, although there are generally 96 pixels per inch.

=item pagewidth, pageheight

The 'pagewidth' and 'pageheight' attributes set the PostScript
pagination size in inches. That is, if the image is larger than the
page then the resulting PostScript image is a sequence of pages that
can be tiled or assembled into a mosaic of the full image. (This only
works for PostScript output).

=item concentrate

The 'concentrate' attribute controls enables an edge merging technique
to reduce clutter in dense layouts of directed graphs. The default is
not to merge edges.

=item orientation

This option controls the angle, in degrees, used to rotate polygon node shapes.

=item random_start

For undirected graphs, the 'random_start' attribute requests an
initial random placement for the graph, which may give a better
result. The default is not random.

=item epsilon

For undirected graphs, the 'epsilon' attribute decides how long the
graph solver tries before finding a graph layout. Lower numbers allow
the solver to fun longer and potentially give a better layout. Larger
values can decrease the running time but with a reduction in layout
quality. The default is 0.1.

=item overlap

The 'overlap' option allows you to set layout behavior for graph nodes
that overlap.  (From GraphViz documentation:)

Determines if and how node overlaps should be removed.

=over

=item true

(the default) overlaps are retained.

=item scale

overlaps are removed by uniformly scaling in x and y.

=item false

If the value converts to "false", node overlaps are removed by a Voronoi-based technique.

=item scalexy

x and y are separately scaled to remove overlaps.

=item orthoxy, orthxy

If the value is "orthoxy" or "orthoyx", overlaps are moved by optimizing two
constraint problems, one for the x axis and one for the y. The suffix indicates
which axis is processed first.

B<NOTE>: The methods related to "orthoxy" and "orthoyx" are still evolving. The
semantics of these may change, or these methods may disappear altogether.

=item compress

If the value is "compress", the layout will be scaled down as much as possible
without introducing any overlaps.

=back

Except for the Voronoi method, all of these transforms preserve the orthogonal
ordering of the original layout. That is, if the x coordinates of two nodes are
originally the same, they will remain the same, and if the x coordinate of one
node is originally less than the x coordinate of another, this relation will
still hold in the transformed layout. The similar properties hold for the y
coordinates.

=item no_overlap

The 'no_overlap' overlap option, if set, tells the graph solver to not
overlap the nodes.  Deprecated,  Use 'overlap' => 'false'.

=item ratio

The 'ratio' option sets the aspect ratio (drawing height/drawing width) for the
drawing. Note that this is adjusted before the size attribute constraints are
enforced.  Default value is C<fill>.

=over

=item numeric

If ratio is numeric, it is taken as the desired aspect ratio. Then, if the
actual aspect ratio is less than the desired ratio, the drawing height is
scaled up to achieve the desired ratio; if the actual ratio is greater than
that desired ratio, the drawing width is scaled up.

=item fill

If ratio = C<fill> and the size attribute is set, node positions are scaled,
separately in both x and y, so that the final drawing exactly fills the
specified size.

=item compress

If ratio = C<compress> and the size attribute is set, dot attempts to compress
the initial layout to fit in the given size. This achieves a tighter packing of
nodes but reduces the balance and symmetry. This feature only works in dot.

=item expand

If ratio = C<expand> the size attribute is set, and both the width and the
height of the graph are less than the value in size, node positions are scaled
uniformly until at least one dimension fits size exactly. Note that this is
distinct from using size as the desired size, as here the drawing is expanded
before edges are generated and all node and text sizes remain unchanged.

=item auto

If ratio = C<auto> the page attribute is set and the graph cannot be drawn on a
single page, then size is set to an ``ideal'' value. In particular, the size in
a given dimension will be the smallest integral multiple of the page size in
that dimension which is at least half the current size. The two dimensions are
then scaled independently to the new size. This feature only works in dot.

=back

=item bgcolor

The 'bgcolor' option sets the background colour. A colour value may be
"h,s,v" (hue, saturation, brightness) floating point numbers between 0
and 1, or an X11 color name such as 'white', 'black', 'red', 'green',
'blue', 'yellow', 'magenta', 'cyan', or 'burlywood'.

=item name

The 'name' option sets name of the graph. This option is useful in few
situations, like client side image map generation, see cmapx.
By default 'test' is used.

=item node,edge,graph

The 'node', 'edge' and 'graph' attributes allow you to specify global
node, edge and graph attributes (in addition to those controlled by
the special attributes described above). The value should be a hash
reference containing the corresponding key-value pairs. For example,
to make all nodes box-shaped (unless explicitly given another shape):

  my $g = GraphViz->new(node => {shape => 'box'});

=back

=cut

sub new {
    my $proto  = shift;
    my $config = shift;
    my $class  = ref($proto) || $proto;
    my $self   = {};

    # Cope with the old hashref format
    if ( ref($config) ne 'HASH' ) {
        my %config;
        %config = ( $config, @_ ) if @_;
        $config = \%config;
    }

    $self->{NODES}    = {};
    $self->{NODELIST} = [];
    $self->{EDGES}    = [];

    if ( exists $config->{directed} ) {
        $self->{DIRECTED} = $config->{directed};
    } else {
        $self->{DIRECTED} = 1;    # default to directed
    }

    if ( exists $config->{layout} ) {
        $self->{LAYOUT} = $config->{layout};
    } else {
        $self->{LAYOUT} = "dot";    # default layout
    }

    if ( exists $config->{name} ) {
        $self->{NAME} = $config->{name};
    } else {
        $self->{NAME} = 'test';
    }

    if ( exists $config->{bgcolor} ) {
        $self->{BGCOLOR} = $config->{bgcolor};
    }

    $self->{RANK_DIR} = $config->{rankdir} if ( exists $config->{rankdir} );

    $self->{WIDTH}  = $config->{width}  if ( exists $config->{width} );
    $self->{HEIGHT} = $config->{height} if ( exists $config->{height} );

    $self->{PAGEWIDTH} = $config->{pagewidth}
        if ( exists $config->{pagewidth} );
    $self->{PAGEHEIGHT} = $config->{pageheight}
        if ( exists $config->{pageheight} );

    $self->{CONCENTRATE} = $config->{concentrate}
        if ( exists $config->{concentrate} );

    $self->{ORIENTATION} = $config->{orientation}
        if ( exists $config->{orientation} );

    $self->{RANDOM_START} = $config->{random_start}
        if ( exists $config->{random_start} );

    $self->{EPSILON} = $config->{epsilon} if ( exists $config->{epsilon} );

    $self->{SORT} = $config->{sort} if ( exists $config->{sort} );

    $self->{OVERLAP} = $config->{overlap} if ( exists $config->{overlap} );

    # no_overlap overrides overlap setting.
    $self->{OVERLAP} = 'false' if ( exists $config->{no_overlap} );

    $self->{RATIO} = $config->{ratio} || 'fill';

    # Global node, edge and graph attributes
    $self->{NODE_ATTRS}  = $config->{node}  if ( exists $config->{node} );
    $self->{EDGE_ATTRS}  = $config->{edge}  if ( exists $config->{edge} );
    $self->{GRAPH_ATTRS} = $config->{graph} if ( exists $config->{graph} );

    bless( $self, $class );
    return $self;
}

=head2 add_node

A graph consists of at least one node. All nodes have a name attached
which uniquely represents that node.

The add_node method creates a new node and optionally assigns it
attributes.

The simplest form is used when no attributes are required, in which
the string represents the name of the node:

  $g->add_node('Paris');

Various attributes are possible: "label" provides a label for the node
(the label defaults to the name if none is specified). The label can
contain embedded newlines with '\n', as well as '\c', '\l', '\r' for
center, left, and right justified lines:

  $g->add_node('Paris', label => 'City of\nlurve');

Attributes need not all be specified in the one line: successive
declarations of the same node have a cumulative effect, in that any
later attributes are just added to the existing ones. For example, the
following two lines are equivalent to the one above:

  $g->add_node('Paris');
  $g->add_node('Paris', label => 'City of\nlurve');

Note that multiple attributes can be specified. Other attributes
include:

=over 4

=item height, width

sets the minimum height or width

=item shape

sets the node shape. This can be one of: 'record', 'plaintext',
'ellipse', 'circle', 'egg', 'triangle', 'box', 'diamond', 'trapezium',
'parallelogram', 'house', 'hexagon', 'octagon'

=item fontsize

sets the label size in points

=item fontname

sets the label font family name

=item color

sets the outline colour, and the default fill colour if the 'style' is
'filled' and 'fillcolor' is not specified

A colour value may be "h,s,v" (hue, saturation, brightness) floating
point numbers between 0 and 1, or an X11 color name such as 'white',
'black', 'red', 'green', 'blue', 'yellow', 'magenta', 'cyan', or
'burlywood'

=item fillcolor

sets the fill colour when the style is 'filled'. If not specified, the
'fillcolor' when the 'style' is 'filled' defaults to be the same as
the outline color

=item style

sets the style of the node. Can be one of: 'filled', 'solid',
'dashed', 'dotted', 'bold', 'invis'

=item URL

sets the url for the node in image map and PostScript files. The
string '\N' value will be replaced by the node name. In PostScript
files, URL information is embedded in such a way that Acrobat
Distiller creates PDF files with active hyperlinks

=back

If you wish to add an anonymous node, that is a node for which you do
not wish to generate a name, you may use the following form, where the
GraphViz module generates a name and returns it for you. You may then
use this name later on to refer to this node:

  my $nodename = $g->add_node('label' => 'Roman city');

Nodes can be clustered together with the "cluster" attribute, which is
drawn by having a labelled rectangle around all the nodes in a
cluster. An empty string means not clustered.

  $g->add_node('London', cluster => 'Europe');
  $g->add_node('Amsterdam', cluster => 'Europe');

Clusters can also take a hashref so that you can set attributes:

  my $eurocluster = {
    name      =>'Europe',
    style     =>'filled',
    fillcolor =>'lightgray',
    fontname  =>'arial',
    fontsize  =>'12',
  };
  $g->add_node('London', cluster => $eurocluster, @default_attrs);


Nodes can be located in the same rank (that is, at the same level in
the graph) with the "rank" attribute. Nodes with the same rank value
are ranked together.

  $g->add_node('Paris', rank => 'top');
  $g->add_node('Boston', rank => 'top');

Also, nodes can consist of multiple parts (known as ports). This is
implemented by passing an array reference as the label, and the parts
are displayed as a label. GraphViz has a much more complete port
system, this is just a simple interface to it. See the 'from_port' and
'to_port' attributes of add_edge:

  $g->add_node('London', label => ['Heathrow', 'Gatwick']);

=cut

sub add_node {
    my $self = shift;
    my $node = shift;

    # Cope with the new simple notation
    if ( ref($node) ne 'HASH' ) {
        my $name = $node;
        my %node;
        if ( @_ % 2 == 1 ) {

            # No name passed
            %node = ( $name, @_ );
        } else {

            # Name passed
            %node = ( @_, name => $name );
        }
        $node = \%node;
    }

    $self->add_node_munge($node) if $self->can('add_node_munge');

    # The _code attribute is our internal name for the node
    $node->{_code} = $self->_quote_name( $node->{name} );

    if ( not exists $node->{name} ) {
        $node->{name} = $node->{_code};
    }

    if ( not exists $node->{label} ) {
        if ( exists $self->{NODES}->{ $node->{name} }
            and defined $self->{NODES}->{ $node->{name} }->{label} )
        {

            # keep our old label if we already exist
            $node->{label} = $self->{NODES}->{ $node->{name} }->{label};
        } else {
            $node->{label} = $node->{name};
        }
    } else {
        $node->{label} =~ s#([|<>\[\]{}"])#\\$1#g
            unless $node->{shape}
                && ($node->{shape} eq 'record'
                    || (   $node->{label} =~ /^<</
                        && $node->{shape} eq 'plaintext' )
                );
    }

    delete $node->{cluster}
        if exists $node->{cluster} && !length $node->{cluster};

    $node->{_label} = $node->{label};

    # Deal with ports
    if ( ref( $node->{label} ) eq 'ARRAY' ) {
        $node->{shape} = 'record';    # force a record
        my $nports = 0;
        $node->{label} = join '|', map {
            $_ =~ s#([|<>\[\]{}"])#\\$1#g;
            '<port' . $nports++ . '>' . $_
        } ( @{ $node->{label} } );
    }

    # Save ourselves
    if ( !exists( $self->{NODES}->{ $node->{name} } ) ) {
        $self->{NODES}->{ $node->{name} } = $node;
    } else {

        # If the node already exists, add or overwrite attributes.
        foreach ( keys %$node ) {
            $self->{NODES}->{ $node->{name} }->{$_} = $node->{$_};
        }
    }

    $self->{CODES}->{ $node->{_code} } = $node->{name};

    # Add the node to the nodelist, which contains the names of
    # all the nodes in the order that they were inserted (but only
    # if it's not already there)
    push @{ $self->{NODELIST} }, $node->{name}
        unless grep { $_ eq $node->{name} } @{ $self->{NODELIST} };

    return $node->{name};
}

=head2 add_edge

Edges are directed (or undirected) links between nodes. This method
creates a new edge between two nodes and optionally assigns it
attributes.

The simplest form is when now attributes are required, in which case
the nodes from and to which the edge should be are specified. This
works well visually in the program code:

  $g->add_edge('London' => 'Paris');

Attributes such as 'label' can also be used. This specifies a label
for the edge.  The label can contain embedded newlines with '\n', as
well as '\c', '\l', '\r' for center, left, and right justified lines.

  $g->add_edge('London' => 'New York', label => 'Far');

Note that multiple attributes can be specified. Other attributes
include:

=over 4

=item minlen

sets an integer factor that applies to the edge length (ranks for
normal edges, or minimum node separation for flat edges)

=item weight

sets the integer cost of the edge. Values greater than 1 tend to
shorten the edge. Weight 0 flat edges are ignored for ordering
nodes

=item fontsize

sets the label type size in points

=item fontname

sets the label font family name

=item fontcolor

sets the label text colour

=item color

sets the line colour for the edge

A colour value may be "h,s,v" (hue, saturation, brightness) floating
point numbers between 0 and 1, or an X11 color name such as 'white',
'black', 'red', 'green', 'blue', 'yellow', 'magenta', 'cyan', or
'burlywood'

=item style

sets the style of the node. Can be one of: 'filled', 'solid',
'dashed', 'dotted', 'bold', 'invis'


=item dir

sets the arrow direction. Can be one of: 'forward', 'back', 'both',  'none'

=item tailclip, headclip

when set to false disables endpoint shape clipping

=item arrowhead, arrowtail

sets the type for the arrow head or tail. Can be one of: 'none',
'normal', 'inv', 'dot', 'odot', 'invdot', 'invodot.'

=item arrowsize

sets the arrow size: (norm_length=10,norm_width=5,
inv_length=6,inv_width=7,dot_radius=2)

=item headlabel, taillabel

sets the text for port labels. Note that labelfontcolor,
labelfontname, labelfontsize are also allowed

=item labeldistance, port_label_distance

sets the distance from the edge / port to the label. Also labelangle

=item decorate

if set, draws a line from the edge to the label

=item samehead, sametail

if set aim edges having the same value to the same port, using the
average landing point

=item constraint

if set to false causes an edge to be ignored for rank assignment

=back

Additionally, adding edges between ports of a node is done via the
'from_port' and 'to_port' parameters, which currently takes in the
offset of the port (ie 0, 1, 2...).

  $g->add_edge('London' => 'Paris', from_port => 0);

=cut

sub add_edge {
    my $self = shift;
    my $edge = shift;

    # Also cope with simple $from => $to
    if ( ref($edge) ne 'HASH' ) {
        my $from = $edge;
        my %edge = ( from => $from, to => shift, @_ );
        $edge = \%edge;
    }

    $self->add_edge_munge($edge) if $self->can('add_edge_munge');

    if ( not exists $edge->{from} or not exists $edge->{to} ) {
        carp("GraphViz add_edge: 'from' or 'to' parameter missing!");
        return;
    }

    my $from = $edge->{from};
    my $to   = $edge->{to};
    $self->add_node($from) unless exists $self->{NODES}->{$from};
    $self->add_node($to)   unless exists $self->{NODES}->{$to};

    push @{ $self->{EDGES} }, $edge;    # should remove!
}

=head2 as_canon, as_text, as_gif etc. methods

There are a number of methods which generate input for dot / neato /
twopi / circo / fdp or output the graph in a variety of formats.

Note that if you pass a filename, the data is written to that
filename. If you pass a filehandle, the data will be streamed to the
filehandle. If you pass a scalar reference, then the data will be
stored in that scalar. If you pass it a code reference, then it is
called with the data (note that the coderef may be called multiple
times if the image is large). Otherwise, the data is returned:

B<Win32 Note:> you will probably want to binmode any filehandles you write
the output to if you want your application to be portable to Win32.

  my $png_image = $g->as_png;
  # or
  $g->as_png("pretty.png"); # save image
  # or
  $g->as_png(\*STDOUT); # stream image to a filehandle
  # or
  #g->as_png(\$text); # save data in a scalar
  # or
  $g->as_png(sub { $png_image .= shift });

=over 4

=item as_debug

The as_debug method returns the dot file which we pass to GraphViz. It
does not lay out the graph. This is mostly useful for debugging.

  print $g->as_debug;

=item as_canon

The as_canon method returns the canonical dot / neato / twopi / circo / fdp  file
which corresponds to the graph. It does not layout the graph - every
other as_* method does.

  print $g->as_canon;


  # prints out something like:
  digraph test {
      node [	label = "\N" ];
      London [label=London];
      Paris [label="City of\nlurve"];
      New_York [label="New York"];
      London -> Paris;
      London -> New_York [label=Far];
      Paris -> London;
  }

=item as_text

The as_text method returns text which is a layed-out dot / neato /
twopi / circo / fdp format file.

  print $g->as_text;

  # prints out something like:
  digraph test {
      node [	label = "\N" ];
      graph [bb= "0,0,162,134"];
      London [label=London, pos="33,116", width="0.89", height="0.50"];
      Paris [label="City of\nlurve", pos="33,23", width="0.92", height="0.62"];
      New_York [label="New York", pos="123,23", width="1.08", height="0.50"];
      London -> Paris [pos="e,27,45 28,98 26,86 26,70 27,55"];
      London -> New_York [label=Far, pos="e,107,40 49,100 63,85 84,63 101,46", lp="99,72"];
      Paris -> London [pos="s,38,98 39,92 40,78 40,60 39,45"];
  }

=item as_ps

Returns a string which contains a layed-out PostScript-format file.

  print $g->as_ps;

=item as_hpgl

Returns a string which contains a layed-out HP pen plotter-format file.

  print $g->as_hpgl;

=item as_pcl

Returns a string which contains a layed-out Laserjet printer-format file.

  print $g->as_pcl;

=item as_mif

Returns a string which contains a layed-out FrameMaker graphics-format file.

  print $g->as_mif;

=item as_pic

Returns a string which contains a layed-out PIC-format file.

  print $g->as_pic;

=item as_gd

Returns a string which contains a layed-out GD-format file.

  print $g->as_gd;

=item as_gd2

Returns a string which contains a layed-out GD2-format file.

  print $g->as_gd2;

=item as_gif

Returns a string which contains a layed-out GIF-format file.

  print $g->as_gif;

=item as_jpeg

Returns a string which contains a layed-out JPEG-format file.

  print $g->as_jpeg;

=item as_png

Returns a string which contains a layed-out PNG-format file.

  print $g->as_png;
  $g->as_png("pretty.png"); # save image


=item as_wbmp

Returns a string which contains a layed-out Windows BMP-format file.

  print $g->as_wbmp;

=item as_cmap  (deprecated)

Returns a string which contains a layed-out HTML client-side image map
format file.   Use as_cmapx instead.

  print $g->as_cmap;

=item as_cmapx

Returns a string which contains a layed-out HTML HTML/X client-side image map
format file. Name and id attributes of map element are set to name of the graph.

  print $g->as_cmapx;

=item as_ismap (deprecated)

Returns a string which contains a layed-out old-style server-side
image map format file.  Use as_imap instead.

  print $g->as_ismap;

=item as_imap

Returns a string which contains a layed-out HTML new-style server-side
image map format file.

  print $g->as_imap;

=item as_vdx

Returns a string which contains a VDX-format (Microsoft Visio) file.

  print $g->as_vdx;

=item as_vrml

Returns a string which contains a layed-out VRML-format file.

  print $g->as_vrml;

=item as_vtx

Returns a string which contains a layed-out VTX (Visual Thought)
format file.

  print $g->as_vtx;

=item as_mp

Returns a string which contains a layed-out MetaPost-format file.

  print $g->as_mp;

=item as_fig

Returns a string which contains a layed-out FIG-format file.

  print $g->as_fig;

=item as_svg

Returns a string which contains a layed-out SVG-format file.

  print $g->as_svg;

=item as_svgz

Returns a string which contains a layed-out SVG-format file
that is compressed.

  print $g->as_svgz;

=item as_plain

Returns a string which contains a layed-out simple-format file.

  print $g->as_plain;

=back

=cut

# Generate magic methods to save typing

sub AUTOLOAD {
    my $self = shift;
    my $type = ref($self)
        or croak("$self is not an object");
    my $output = shift;

    my $name = $AUTOLOAD;
    $name =~ s/.*://;    # strip fully-qualified portion

    return if $name =~ /DESTROY/;

    if ( $name eq 'as_text' ) {
        $name = "as_dot";
    }

    if ( $name
        =~ /^as_(ps|hpgl|pcl|mif|pic|gd|gd2|gif|jpeg|png|wbmp|cmapx?|ismap|imap|vdx|vrml|vtx|mp|fig|svgz?|dot|canon|plain)$/
        )
    {
        my $data = $self->_as_generic( '-T' . $1, $self->_as_debug, $output );
        return $data;
    }

    croak "Method $name not defined!";
}

# Return the main dot text
sub as_debug {
    my $self = shift;
    return $self->_as_debug(@_);
}

sub _as_debug {
    my $self = shift;

    my $dot;

    my $graph_type = $self->{DIRECTED} ? 'digraph' : 'graph';

    $dot .= $graph_type . " " . $self->{NAME} . " {\n";

    # the direction of the graph
    if ($self->{RANK_DIR}) {
        $self->{RANK_DIR} = uc $self->{RANK_DIR};
        my(%valid)        = (BT => 1, LR => 1, RL => 1, TB => 1);
        $self->{RANK_DIR} = 'LR' if (! $valid{$self->{RANK_DIR} });
        $dot              .= "\trankdir=" . $self->{RANK_DIR} . ";\n";
    }

    # the size of the graph
    $dot .= "\tsize=\"" . $self->{WIDTH} . "," . $self->{HEIGHT} . "\";\n"
        if $self->{WIDTH} && $self->{HEIGHT};
    $dot
        .= "\tpage=\""
        . $self->{PAGEWIDTH} . ","
        . $self->{PAGEHEIGHT} . "\";\n"
        if $self->{PAGEWIDTH} && $self->{PAGEHEIGHT};

    # Ratio setting
    $dot .= "\tratio=\"" . $self->{RATIO} . "\";\n";

    # edge merging
    $dot .= "\tconcentrate=true;\n" if $self->{CONCENTRATE};

    # Orientation
    $dot .= "\torientation=$self->{ORIENTATION};\n" if $self->{ORIENTATION};

    # epsilon
    $dot .= "\tepsilon=" . $self->{EPSILON} . ";\n" if $self->{EPSILON};

    # random start
    $dot .= "\tstart=rand;\n" if $self->{RANDOM_START};

    # overlap
    $dot .= "\toverlap=\"" . $self->{OVERLAP} . "\";\n" if $self->{OVERLAP};

    # color, bgcolor
    $dot .= "\tbgcolor=\"" . $self->{BGCOLOR} . "\";\n" if $self->{BGCOLOR};

    # Global node, edge and graph attributes
    $dot .= "\tnode" . _attributes( $self->{NODE_ATTRS} ) . ";\n"
        if exists( $self->{NODE_ATTRS} );
    $dot .= "\tedge" . _attributes( $self->{EDGE_ATTRS} ) . ";\n"
        if exists( $self->{EDGE_ATTRS} );
    $dot .= "\tgraph" . _attributes( $self->{GRAPH_ATTRS} ) . ";\n"
        if exists( $self->{GRAPH_ATTRS} );

    my %clusters      = ();
    my %cluster_nodes = ();
    my %clusters_edge = ();

    my $arrow = $self->{DIRECTED} ? ' -> ' : ' -- ';

    # Add all the nodes
    my @nodelist = @{ $self->{NODELIST} };
    @nodelist = sort @nodelist if $self->{SORT};

    foreach my $name (@nodelist) {
        my $node = $self->{NODES}->{$name};

        # Note all the clusters
        if ( exists $node->{cluster} && $node->{cluster} ) {

        # map "name" to value in case cluster attribute is not a simple string
            $clusters{ $node->{cluster} } = $node->{cluster};
            push @{ $cluster_nodes{ $node->{cluster} } }, $name;
            next;
        }

        $dot .= "\t" . $node->{_code} . _attributes($node) . ";\n";
    }

    # Add all the edges
    foreach
        my $edge ( sort { $a->{from} cmp $b->{from} || $a->{to} cmp $b->{to} }
        @{ $self->{EDGES} } )
    {

        my $from = $self->{NODES}->{ $edge->{from} }->{_code};
        my $to   = $self->{NODES}->{ $edge->{to} }->{_code};

        # Deal with ports
        if ( exists $edge->{from_port} ) {
            $from = '"' . $from . '"' . ':port' . $edge->{from_port};
        }
        if ( exists $edge->{to_port} ) {
            $to = '"' . $to . '"' . ':port' . $edge->{to_port};
        }

        if (   exists $self->{NODES}->{$from}
            && exists $self->{NODES}->{$from}->{cluster}
            && exists $self->{NODES}->{$to}
            && exists $self->{NODES}->{$to}->{cluster}
            && $self->{NODES}->{$from}->{cluster} eq
            $self->{NODES}->{$to}->{cluster} )
        {

            $clusters_edge{ $self->{NODES}->{$from}->{cluster} }
                .= "\t\t" . $from . $arrow . $to . _attributes($edge) . ";\n";
        } else {
            $dot .= "\t" . $from . $arrow . $to . _attributes($edge) . ";\n";
        }
    }

    foreach my $clustername ( sort keys %cluster_nodes ) {
        my $cluster = $clusters{$clustername};
        my $attrs;
        my $name;
        if ( ref($cluster) eq 'HASH' ) {
            if ( exists $cluster->{label} ) {
                $name = $cluster->{label};
            } elsif ( exists $cluster->{name} ) {

                # "coerce" name attribute into label attribute
                $name = $cluster->{name};
                $cluster->{label} = $name;
                delete $cluster->{name};
            }
            $attrs = _attributes($cluster);
        } else {
            $name = $cluster;
            $attrs = _attributes( { label => $cluster } );
        }

        # rewrite attributes string slightly
        $attrs =~ s/^\s\[//o;
        $attrs =~ s/,/;/go;
        $attrs =~ s/\]$//o;

        $dot .= "\tsubgraph cluster_" . $self->_quote_name($name) . " {\n";
        $dot .= "\t\t$attrs;\n";
        $dot .= join "", map {
            "\t\t"
                . $self->{NODES}->{$_}->{_code}
                . _attributes( $self->{NODES}->{$_} ) . ";\n";
        } ( @{ $cluster_nodes{$cluster} } );
        $dot .= $clusters_edge{$cluster} if exists $clusters_edge{$cluster};
        $dot .= "\t}\n";
    }

    # Deal with ranks
    my %ranks;
    foreach my $name (@nodelist) {
        my $node = $self->{NODES}->{$name};
        next unless exists $node->{rank};
        push @{ $ranks{ $node->{rank} } }, $name;
    }

    foreach my $rank ( keys %ranks ) {
        $dot .= qq|\t{rank=same; |;
        $dot .= join '; ', map { $self->_quote_name($_) } @{ $ranks{$rank} };
        $dot .= qq|}\n|;
    }

    # {rank=same; Paris; Boston}

    $dot .= "}\n";

    return $dot;
}

# Call dot / neato / twopi / circo / fdp with the input text and any parameters

sub _as_generic {
    my ( $self, $type, $dot, $output ) = @_;

    my $buffer;
    my $out;
    if ( ref $output || UNIVERSAL::isa( \$output, 'GLOB' ) ) {

        # $output is a filehandle or a scalar reference or something.
        # have to take a reference to a bare filehandle or run will
        # complain
        $out = ref $output ? $output : \$output;
    } elsif ( defined $output ) {

        # if it's defined it must be a filename so we'll write to it.
        $out = $output;
    } else {

        # but otherwise we capture output in a scalar
        $out = \$buffer;
    }

    my $program = $self->{LAYOUT};

    run [ $program, $type ], \$dot, ">", binary(), $out;

    return $buffer unless defined $output;
}

# Quote a node/edge name using dot / neato / circo / fdp / twopi's quoting rules

sub _quote_name {
    my ( $self, $name ) = @_;
    my $realname = $name;

    return $self->{_QUOTE_NAME_CACHE}->{$name}
        if $name && exists $self->{_QUOTE_NAME_CACHE}->{$name};

    if ( defined $name && $name =~ /^[a-zA-Z]\w*$/ && $name ne "graph" ) {

        # name is fine
    } elsif ( defined $name && $name =~ /^[a-zA-Z](\w| )*$/ ) {

        # name contains spaces, so quote it
        $name = '"' . $name . '"';
    } else {

        # name contains weird characters - let's make up a name for it
        $name = 'node' . ++$self->{_NAME_COUNTER};
    }

    $self->{_QUOTE_NAME_CACHE}->{$realname} = $name if defined $realname;

    #  warn "# $realname -> $name\n";

    return $name;
}

# Return the attributes of a node or edge as a dot / neato / circo / fdp / twopi attribute
# string

sub _attributes {
    my $thing = shift;

    my @attributes;

    foreach my $key ( keys %$thing ) {
        next if $key =~ /^_/;
        next if $key =~ /^(to|from|name|cluster|from_port|to_port)$/;

        my $value = $thing->{$key} || '';

		if ( $key ne 'label' || $value !~ /^<</ )
		{
	        $value =~ s|"|\\"|g;
   		    $value = '"' . $value . '"'
		}
       	$value =~ s|\n|\\n|g;
        $value = '""' if ( ($value eq '') && ($value ne '""') );

        push @attributes, "$key=$value";
    }

    if (@attributes) {
        return ' [' . ( join ', ', sort @attributes ) . "]";
    } else {
        return "";
    }
}

=head1 FAQ

=head2 Why do I get error messages like the following?

	Error: <stdin>:1: syntax error near line 1
	context: digraph >>>  Graph <<<  {

Graphviz reserves some words as keywords, meaning they can't be used as an ID, e.g. for the name of the graph.
So, don't do this:

	strict graph graph{...}
	strict graph Graph{...}
	strict graph strict{...}
	etc...

Likewise for non-strict graphs, and digraphs. You can however add double-quotes around such reserved words:

	strict graph "graph"{...}

Even better, use a more meaningful name for your graph...

The keywords are: node, edge, graph, digraph, subgraph and strict. Compass points are not keywords.

See L<keywords|http://www.graphviz.org/content/dot-language> in the discussion of the syntax of DOT
for details.

=head2 How do you handle XXE within XML?

Due to security risks with XXE in XML, Graphviz does not support XML that contains XXE. Thus it
automatically prevents external entities being parsed by using the no_xxe option in L<XML::Twig>
when calling XML::Twig -> new(). And for this reason also the pre-reqs in Makefile.PL specify
XML::Twig V 3.52.

See L<https://metacpan.org/pod/release/MIROD/XML-Twig-3.52/Twig.pm#no_xxe>

=head1 NOTES

Older versions of GraphViz used a slightly different syntax for node
and edge adding (with hash references). The new format is slightly
clearer, although for the moment we support both. Use the new, clear
syntax, please.

=head1 SEE ALSO

GraphViz is deprecated in favour of L<GraphViz2>.

=head1 Machine-Readable Change Log

The file Changes was converted into Changelog.ini by L<Module::Metadata::Changes>.

=head1 Repository

L<https://github.com/ronsavage/GraphViz>

=head1 AUTHOR

Leon Brocard: E<lt>F<acme@astray.com>E<gt>.

Current maintainer: Ron Savage I<E<lt>ron@savage.net.auE<gt>>.

My homepage: L<http://savage.net.au/>.

=head1 COPYRIGHT

Copyright (C) 2000-4, Leon Brocard

=head1 LICENSE

This module is free software; you can redistribute it or modify it under the Perl License,
a copy of which is available at L<http://dev.perl.org/licenses/>.

=cut

1;