/tags/dev-19990920/FreeSpeech/data-flow/src/xml2net.pl

# · Perl · 267 lines · 240 code · 18 blank · 9 comment · 52 complexity · 5e36dc0311026a5a946e0e47af63b73b MD5 · raw file

  1. #!/usr/bin/perl
  2. while (<>)
  3. {
  4. if (/<dia:layer name="(.*)" visible="true">/)
  5. {
  6. $net_name = $1;
  7. parse_net($net_name);
  8. }
  9. }
  10. sub parse_net {
  11. $name = $_[0];
  12. $defined = 0;
  13. $nb_nodes=0;
  14. $nb_connect=0;
  15. $curr_net=0;
  16. $net_input=0;
  17. $net_output=0;
  18. $net_condition=0;
  19. while (<>)
  20. {
  21. if (/<\/dia:layer>/)
  22. {
  23. goto EndOfNet;
  24. }
  25. if (/<dia:object type.*Function.*id="(.*)">/)
  26. {
  27. parse_node($1);
  28. }
  29. if (/<dia:object type.*Flow.*id="(.*)">/)
  30. {
  31. parse_connexion($1);
  32. }
  33. }
  34. EndOfNet:
  35. set_connect();
  36. if ($net_condition)
  37. {
  38. print "Iterator: $name\n{\n";
  39. } else {
  40. print "Network: $name\n{\n";
  41. }
  42. for ($i=0;$i<=$#curr_net;$i++)
  43. {
  44. $name = $curr_net[$i]{"name"};
  45. $type = $curr_net[$i]{"type"};
  46. print " <node: $name> <type: $type>\n";
  47. for ($j=0;$j<$curr_net[$i]{"nb_inputs"};$j++)
  48. {
  49. print " ", $curr_net[$i]{"inputs"}[$j], "\n";
  50. }
  51. for ($j=0;$j<$curr_net[$i]{"nb_params"};$j++)
  52. {
  53. $pname = $curr_net[$i]{"param"}[$j][0];
  54. $pval = $curr_net[$i]{"param"}[$j][1];
  55. print " <param: $pname, $pval>\n";;
  56. }
  57. print "\n";
  58. }
  59. for ($i=0;$i<$nb_nodes;$i++)
  60. {
  61. if ($curr_net[$i]{"id"} eq $net_input)
  62. {
  63. $name = $curr_net[$i]{"name"};
  64. printf " <netInput: $name>\n";
  65. }
  66. }
  67. for ($i=0;$i<$nb_nodes;$i++)
  68. {
  69. if ($curr_net[$i]{"id"} eq $net_output)
  70. {
  71. $name = $curr_net[$i]{"name"};
  72. printf " <netOutput: $name>\n";
  73. }
  74. }
  75. if ($net_condition)
  76. {
  77. for ($i=0;$i<$nb_nodes;$i++)
  78. {
  79. if ($curr_net[$i]{"id"} eq $net_condition)
  80. {
  81. $name = $curr_net[$i]{"name"};
  82. printf " <netCondition: $name>\n";
  83. }
  84. }
  85. }
  86. print "}\n\n";
  87. }
  88. sub parse_node {
  89. $curr_node_id = $_[0];
  90. #print "parsing node id $id\n";
  91. while (<>)
  92. {
  93. if (/<\/dia:object>/)
  94. {
  95. goto EndOfNode;
  96. }
  97. if (/<dia:string>\#(.*)/)
  98. {
  99. $curr_net[$nb_nodes]{"name"}=$1;
  100. parse_node_indo();
  101. $nb_nodes++;
  102. }
  103. }
  104. EndOfNode:
  105. #if (/<dia:string>\#(.*)/)
  106. }
  107. sub parse_connexion {
  108. $id = $_[0];
  109. #print "parsing connexion id $id\n";
  110. while (<>)
  111. {
  112. if (/<\/dia:object>/)
  113. {
  114. goto EndOfConnexion;
  115. }
  116. if (/<dia:string>\#netInput\#<\/dia:string>/)
  117. {
  118. while (<>)
  119. {
  120. if (/<dia:connection handle="1" to="(.*)" connection.*>/)
  121. {$net_input=$1;}
  122. if (/<\/dia:object>/)
  123. {goto EndOfConnexion;}
  124. }
  125. return;
  126. }
  127. if (/<dia:string>\#netOutput\#<\/dia:string>/)
  128. {
  129. while (<>)
  130. {
  131. if (/<dia:connection handle="0" to="(.*)" connection.*>/)
  132. {$net_output=$1;}
  133. if (/<\/dia:object>/)
  134. {goto EndOfConnexion;}
  135. }
  136. return;
  137. }
  138. if (/<dia:string>\#netCondition\#<\/dia:string>/)
  139. {
  140. while (<>)
  141. {
  142. if (/<dia:connection handle="0" to="(.*)" connection.*>/)
  143. {$net_condition=$1;}
  144. if (/<\/dia:object>/)
  145. {goto EndOfConnexion;}
  146. }
  147. return;
  148. }
  149. if (/<dia:string>\#\#<\/dia:string>/)
  150. {
  151. $net_connect[$nb_connect]{"input"}="INPUT";
  152. $net_connect[$nb_connect]{"output"}="OUTPUT";
  153. goto next_connect;
  154. }
  155. if (/<dia:string>\#(.*)/)
  156. {
  157. $net_connect[$nb_connect]{"output"}=$1;
  158. }
  159. if (/(.*)\#<\/dia:string>/)
  160. {
  161. $net_connect[$nb_connect]{"input"}=$1;
  162. }
  163. if (/<dia:connection handle="0" to="(.*)" connection.*>/)
  164. {
  165. $net_connect[$nb_connect]{"from_node_id"}=$1;
  166. }
  167. if (/<dia:connection handle="1" to="(.*)" connection.*>/)
  168. {
  169. $net_connect[$nb_connect]{"to_node_id"}=$1;
  170. }
  171. next_connect:
  172. }
  173. EndOfConnexion:
  174. $nb_connect++;
  175. #print "INPUT = $to_node_id, OUTPUT = $from_node_id\n";
  176. #print "INPUT = $input, OUTPUT = $output\n";
  177. }
  178. sub set_connect {
  179. for ($conn=0;$conn < $nb_connect; $conn++)
  180. {
  181. $input=$net_connect[$conn]{"input"};
  182. $output=$net_connect[$conn]{"output"};
  183. $to_node_id = $net_connect[$conn]{"to_node_id"};
  184. $from_node_id = $net_connect[$conn]{"from_node_id"};
  185. for ($i=0;$i<$nb_nodes;$i++)
  186. {
  187. if ($curr_net[$i]{"id"} eq $to_node_id)
  188. {
  189. for ($j=0;$j<$nb_nodes;$j++)
  190. {
  191. if ($curr_net[$j]{"id"} eq $from_node_id)
  192. {
  193. $nb_inputs = $curr_net[$i]{"nb_inputs"};
  194. $in_name = $curr_net[$j]{"name"};
  195. $curr_net[$i]{"inputs"}[$nb_inputs]="<input: $input, $in_name, $output>";
  196. #print $curr_net[$i]{"inputs"}[$nb_inputs], "\n";
  197. $curr_net[$i]{"nb_inputs"}++;
  198. goto connect_find_out;
  199. }
  200. }
  201. }
  202. }
  203. connect_find_out:
  204. }
  205. }
  206. sub parse_node_indo {
  207. $istype=1;
  208. $param_count=0;
  209. $curr_net[$nb_nodes]{"nb_params"}=0;
  210. $curr_net[$nb_nodes]{"nb_inputs"}=0;
  211. $curr_net[$nb_nodes]{"id"}=$curr_node_id;
  212. while (<>)
  213. {
  214. if (/(.*)\#<\/dia:string>/)
  215. {
  216. if ($istype)
  217. {
  218. $curr_net[$nb_nodes]{"type"} = $1;
  219. $istype=0;
  220. } else {
  221. s/&quot;/\"/g;
  222. s/sp:/subnet_param:/;
  223. /(.*)=(.*)\#<\/dia:string>/;
  224. $curr_net[$nb_nodes]{"param"}[$param_count][0]=$1;
  225. $curr_net[$nb_nodes]{"param"}[$param_count][1]=$2;
  226. #print " <param: $1, $2>\n";
  227. $param_count++;
  228. $curr_net[$nb_nodes]{"nb_params"}++;
  229. }
  230. return;
  231. } else {
  232. chop;
  233. if ($istype)
  234. {
  235. $curr_net[$nb_nodes]{"type"} = $_;
  236. $istype=0;
  237. } else {
  238. s/&quot;/\"/g;
  239. s/sp:/subnet_param:/;
  240. /(.*)=(.*)/;
  241. $curr_net[$nb_nodes]{"param"}[$param_count][0]=$1;
  242. $curr_net[$nb_nodes]{"param"}[$param_count][1]=$2;
  243. #print " <param: $1, $2>\n";
  244. $param_count++;
  245. $curr_net[$nb_nodes]{"nb_params"}++;
  246. }
  247. }
  248. }
  249. }