graphed-to-graphlet 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  1. #!/usr/local/bin/perl -n
  2. #
  3. # This script converts graphs in GraphEd format to GraphEdPro Format.
  4. # Note: it relies heavily on the way GraphEd writes files !
  5. #
  6. # Note: this script needs perl version 5.
  7. #
  8. # (C) Michael Himsolt / Graphlet Project,
  9. # University of Passau, 1995-1996
  10. #
  11. BEGIN {
  12. $true= (42 == 42);
  13. $false= (42 != 42);
  14. $includegraphed=$false;
  15. $LastNodeId=-1;
  16. $GraphicsStarted=$false;
  17. }
  18. ##########################################
  19. #
  20. # Convert header line
  21. #
  22. ##########################################
  23. if (/^GRAPH\b\s*=*\s*(".*")/) {
  24. print "graph [\n";
  25. if ($1 ne "") {
  26. print "label $1\n";
  27. }
  28. if (/\bDIRECTED\b/) {
  29. print "directed 1\n";
  30. } elsif (/\bUNDIRECTED\b/) {
  31. print "directed 0\n";
  32. } else {
  33. print "directed 1\n";
  34. };
  35. }
  36. ##########################################
  37. #
  38. # Convert footer line (final END)
  39. #
  40. ##########################################
  41. if (/^END$/) {
  42. print "]\n";
  43. }
  44. ##########################################
  45. #
  46. # Cleanup operations ...
  47. #
  48. ##########################################
  49. #
  50. # remove all lines with just a ';' in them
  51. #
  52. if (/^\s*;\s*$/) {
  53. s/\s*;\s*//;
  54. }
  55. #
  56. # remove all lines with just a "{$" or "$}" in them
  57. # (thay are the delimeters for the graph attributes)
  58. #
  59. s/^\s*\{\$\s*$//;
  60. s/^\s*\$\}\s*$//;
  61. ##########################################
  62. #
  63. # Convert fonts, node- and edgetypes, node- and edgestyles
  64. #
  65. ##########################################
  66. #
  67. # Font
  68. #
  69. if (/^Font:\s*(\d+)\s*(".*")\s*(".*")/) {
  70. if ($includegraphed) {
  71. print "Font [\nid $1\ngraphedname $2\nexample $3\n]\n";
  72. }
  73. }
  74. #
  75. # Nodetype
  76. #
  77. if (/^nodetype\s*(\d+)\s*(".*")/) {
  78. if ($includegraphed) {
  79. print "Nodetype [\nid $1\nname $2\n]\n";
  80. }
  81. }
  82. #
  83. # Edgetype
  84. #
  85. if (/^edgetype\s*(\d+)\s*(".*")/) {
  86. if ($includegraphed) {
  87. print "Edgetype [\nid $1\nname $2\n]\n";
  88. }
  89. }
  90. #
  91. # Nodestyle
  92. #
  93. if (/^NSTYL\s*(".*")\s*\[/) {
  94. if ($includegraphed) {
  95. print "Nodestyle [\nlabel $1\n";
  96. }
  97. }
  98. #
  99. # Edgestyle
  100. #
  101. if (/^ESTYL\s*(".*")\s*\[/) {
  102. if ($includegraphed) {
  103. print "Edgestyle [\nlabel $1\n";
  104. }
  105. }
  106. ##########################################
  107. #
  108. # Convert the adjacency list structure
  109. #
  110. ##########################################
  111. #
  112. # Convert nodes
  113. #
  114. if (/^(\d+)\s*\{\$/) {
  115. print "node [\nid $1\n";
  116. $LastNodeId=$1;
  117. }
  118. #
  119. # Convert Edges
  120. #
  121. if (/^\s+(\d+)\s*\{\$/) {
  122. print "edge [\nsource $LastNodeId\ntarget $1\n";
  123. }
  124. ##########################################
  125. #
  126. # Convert standard attributes
  127. #
  128. ##########################################
  129. $GraphicsStarted=$false;
  130. # NS x y
  131. if (/\bNS\b\s+(\d+)\s+(\d+)/ && $LastNodeId > 0) {
  132. print "graphics [\n";
  133. $GraphicsStarted=$true;
  134. print "w $1.0\nh $2.0\n";
  135. }
  136. # NP x y
  137. if (/\bNP\b\s+(\d+)\s+(\d+)/ && $LastNodeId > 0) {
  138. print "x $1.0\ny $2.0\n";
  139. if ($GraphicsStarted == $true) {
  140. print "]\n";
  141. $GraphicsStarted =$false;
  142. }
  143. }
  144. # NTI nodetypeindex
  145. # NFI nodefontindex
  146. # NLP nodelabelplacement
  147. # NEI nodeedgeinterface
  148. # NLV bool
  149. if (/\bNTI\b\s+(\d+)/) {
  150. if ($includegraphed) {
  151. print "Typeid $1\n";
  152. }
  153. };
  154. if (/\bNFI\b\s+(\d+)/) {
  155. if ($includegraphed) {
  156. print "Fontid $1\n";
  157. }
  158. };
  159. if (/\bNLP\b\s+(\d+)/) {
  160. if ($includegraphed) {
  161. print "Labelplacement $1\n";
  162. }
  163. };
  164. if (/\bNEI\b\s+(\d+)/) {
  165. if ($includegraphed) {
  166. print "Nodeedgeinterface $1\n";
  167. }
  168. };
  169. if (/\bNLV\b\s+(\d+)/) {
  170. if ($includegraphed) {
  171. print "Labelvisible $1\n";
  172. }
  173. }
  174. # ETI edgetypeid
  175. # EFI edgefontid
  176. # ELV bool
  177. # AL length AA angle
  178. if (/\bETI\b\s+(\d+)/) {
  179. if ($includegraphed) {
  180. print "Typeid $1\n";
  181. }
  182. };
  183. if (/\bEFI\b\s+(\d+)/) {
  184. if ($includegraphed) {
  185. print "Fontid $1\n";
  186. }
  187. };
  188. if (/\bELV\b\s+(\d+)/) {
  189. if ($includegraphed) {
  190. print "Labelvisible $1\n";
  191. }
  192. };
  193. if (/\bAL\b\s+(\d+)\s+\bAA\b\s+(\d+)/) {
  194. if ($includegraphed) {
  195. print "Arrow [\nlength $1.0\nangle $2.0\n]\n";
  196. }
  197. };
  198. # Col colorid
  199. if (/\bCOL\b\s+(\d+)/) {
  200. if ($includegraphed) {
  201. print "Colorid $1\n";
  202. }
  203. };
  204. #
  205. # GraphEd's default graph parameters ...
  206. #
  207. # DIR defaultdirected
  208. # GRAGRA nr string string
  209. # GEMBED string
  210. # EMBMATCH boolean
  211. # ELS defaultedgelabelwidth defaultedgelabelheight
  212. # WA defaultworkingareawidth defaultworkingareaheight
  213. # WAWIN workingareawindowwidth tworkingareawindowheight
  214. # SC scrolloffsetx scrolloffsety
  215. # GR gridwidth
  216. if (/\bDIR\b\s+(\d+)/) {
  217. if ($includegraphed) {
  218. print "GraphedGraphStyle [\ndirected $1";
  219. }
  220. };
  221. if (/\bGRAGRA\b\s+(\d+)\s+(".*")\s*(".*")/) {
  222. if ($includegraphed) {
  223. print "Gragra [\nstyle $1\nterminals $2\nnonterminals $2\n";
  224. }
  225. };
  226. if (/\bGEMBED\b\s+(".*")/) {
  227. if ($includegraphed) {
  228. print "Embeddingidentifier $1\n";
  229. }
  230. };
  231. if (/\bEMBMATCH\b\s+(\d+)/) {
  232. if ($includegraphed) {
  233. print "Embeddingmatch $1\n";
  234. }
  235. }
  236. if (/\bELS\b\s+(\d+)\s+(\d+)/) {
  237. if ($includegraphed) {
  238. print "Edgelabelsize [\nw $1.0\nh $2.0\n]\n";
  239. }
  240. }
  241. if (/\bWA\b\s+(\d+)\s+(\d+)/) {
  242. if ($includegraphed) {
  243. print "Workingarea [\nw $1.0\nh $2.0\n]\n";
  244. }
  245. }
  246. if (/\bWAWIN\b\s+(\d+)\s+(\d+)/) {
  247. if ($includegraphed) {
  248. print "Windowsize [\nw $1.0\nh $2.0\n]\n";
  249. }
  250. }
  251. if (/\bSC\b\s+(\d+)\s+(\d+)/) {
  252. if ($includegraphed) {
  253. print "Scrolloffset [\n x $1.0\ny $2.0\n]\n";
  254. }
  255. }
  256. if (/\bGR\b\s+(\d+)/) {
  257. if ($includegraphed) {
  258. print "Gridwidth $1.0\n]\n";
  259. }
  260. }
  261. # EL x y x y x ty ...
  262. if (/\bEL((\b\s+\d+\s+\d+)+)/) {
  263. if ($GraphicsStarted == $false) {
  264. print "graphics [\n";
  265. $GraphicsStarted=$true;
  266. }
  267. $coords = $1;
  268. $coords =~ s/\s+(\d+)\s+(\d+)/point [\nx $1.0\ny $2.0\n]\n/g;
  269. print "Line [\n$coords]\n";
  270. # Last in edge graphics
  271. if ($GraphicsStarted == $true) {
  272. print "]\n";
  273. $GraphicsStarted =$false;
  274. }
  275. };
  276. #
  277. # The next substitutions come here because they close the lists ...
  278. #
  279. #
  280. # Convert node/edge labels (one substitution fits all)
  281. #
  282. if (/\$\}\s*(".*")\s*$/) {
  283. print "\nlabel $1\n]\n";
  284. }
  285. #
  286. # Close node/edge styles
  287. #
  288. if (/]/) {
  289. if ($includegraphed) {
  290. print "]\n";
  291. }
  292. }
  293. #
  294. # clean up ...
  295. #
  296. s/^\s//;
  297. ##########################################
  298. #
  299. # Set emacs variables
  300. #
  301. ##########################################
  302. # ;;; Local Variables: ***
  303. # ;;; tab-width: 4 ***
  304. # ;;; tab-stop-list: (4 8 12 16 20 24 28 32 36 40 44 48 52 56 60 64 68 72) ***
  305. # ;;; End: ***