gml2dot.pl 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. #!/usr/bin/perl
  2. #################################################################
  3. #
  4. # This script takes a graph in GML format and converts it to DOT
  5. # format. The latter can be visualized via the graphviz package.
  6. #
  7. #
  8. #################################################################
  9. #
  10. # Call possibilities are as follows.
  11. #
  12. # (a) within pipeline
  13. #
  14. # $ cat GRAPH.gml | perl gml2dot.pl > GRAPH.dot
  15. #
  16. # (b) via file input
  17. #
  18. # $ perl gml2dot.pl < GRAPH.gml > GRAPH.dot
  19. #
  20. # (c) direct conversion to PNG via dot from graphviz
  21. #
  22. # $ .. | perl gml2dot.pl | dot -Tpng -oGRAPH.png
  23. #
  24. # (d) direct conversion to SVG via dot from graphviz
  25. #
  26. # $ .. | perl gml2dot.pl | dot -Tsvg -oGRAPH.svg
  27. #
  28. #################################################################
  29. use strict;
  30. # read
  31. my $gml = "";
  32. while (my $line = <STDIN>) {
  33. $gml .= $line;
  34. }
  35. # DOT header
  36. print STDOUT "digraph \"graphFromGML\" {\n";
  37. # nodes
  38. while ($gml =~ /node\s*\[(.+)\]/g) {
  39. my $node = $1;
  40. # get node ID
  41. if ($node =~ /id\s+(\d+)\s+/) {
  42. my $nodeID = $1;
  43. # get node label
  44. if ($node =~ /label\s"(.*)"/) {
  45. my $nodeLabel = $1;
  46. # print node in DOT
  47. print STDOUT " N$nodeID [shape=oval label=\"$nodeLabel\"];\n"
  48. }
  49. }
  50. }
  51. # edges
  52. while ($gml =~ /edge\s*\[(.+)\]/g) {
  53. my $edge = $1;
  54. # get edge source
  55. if ($edge =~ /source\s+(\d+)\s+/) {
  56. my $source = $1;
  57. if ($edge =~ /target\s+(\d+)\s+/) {
  58. my $target = $1;
  59. # get edge label
  60. if ($edge =~ /label\s"(.*)"/) {
  61. my $label = $1;
  62. # print node in DOT
  63. print STDOUT " N$source -- N$target [label=\"$label\"];\n"
  64. }
  65. }
  66. }
  67. }
  68. # DOT footer
  69. print STDOUT "} // visualization: eg. ' | dot -Tpng -O GRAPHOUTPUTFILE'"
  70. # EOF