1
0

Fsm_From_Dia.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. <?php
  2. /**
  3. * Input converter for FSM from Dia
  4. *
  5. * This class converts an UML diagram from Dia 0.9.6
  6. * into an abstract FSM graph which can then be output by another class
  7. * to the FSM XML format used by the OSInet FSM 1.6.
  8. *
  9. * @copyright 2007 Ouest Systèmes Informatiques
  10. * @author Frederic G. MARAND
  11. * @license CeCILL 2.0
  12. * @version CVS: $Id: Fsm_From_Dia.php,v 1.2 2007-06-10 19:38:14 marand Exp $
  13. * @link http://wiki.audean.com/fsm/fsm
  14. * @since FSM 1.6
  15. * @package fsm
  16. * @subpackage fsm.ui
  17. */
  18. $erFsmFromDia = error_reporting(E_ALL|E_STRICT);
  19. /**
  20. * This class converts an UML diagram from Dia 0.9.6
  21. * into an abstract FSM graph.
  22. *
  23. * @package fsm
  24. * @subpackage fsm.ui
  25. * @todo Validate the diagram: currently it will just choke on non-accepted diagrams
  26. */
  27. class Fsm_From_Dia
  28. {
  29. const DIA_NAMESPACE = "http://www.lysator.liu.se/~alla/dia/";
  30. /**
  31. * The DOM for the source Dia diagram
  32. *
  33. * @var DOMDocument
  34. */
  35. protected $dom;
  36. /**
  37. * The FSM writer instance to save to.
  38. *
  39. * @var FSM_Writer
  40. */
  41. protected $fsm;
  42. /**
  43. * Sequence generator for unnamed event results (guards)
  44. *
  45. * @var integer
  46. */
  47. protected $result_generator = 0;
  48. /**
  49. * Extract the initial/final status from a Dia "UML - State Term"
  50. *
  51. * @param DOMElement $element
  52. * @return array
  53. */
  54. protected function getStateTermInfo($element)
  55. {
  56. $query = 'dia:attribute[@name="is_final"]/dia:boolean';
  57. $xpath = new DOMXPath($this->dom);
  58. $xpath->registerNamespace("dia", Fsm_From_Dia::DIA_NAMESPACE);
  59. $id = $element->getAttribute('id');
  60. $dia_attributes = $xpath->query($query, $element);
  61. if ($dia_attributes->length == 1) // Normal case
  62. {
  63. $dia_boolean = $dia_attributes->item(0); // "is_final" is a Dia boolean
  64. $val = $dia_boolean->getAttribute('val');
  65. switch ($val)
  66. {
  67. case 'true': $ret = Finite_State_Machine::FINAL_STATE; break;
  68. case 'false': $ret = Finite_State_Machine::INIT_STATE; break;
  69. default: $ret = "anomalous($val)"; break;
  70. }
  71. }
  72. else
  73. {
  74. echo "Initial/final state #$id does not bear the is_final attribute: anomaly.\n";
  75. }
  76. return array('type' => $ret, 'name' => $ret);
  77. }
  78. /**
  79. * Extract the name from a Dia "UML - State"
  80. *
  81. * @param DOMElement $element
  82. * @return array
  83. */
  84. protected function getStateInfo($element)
  85. {
  86. $query = 'dia:attribute[@name="text"]/dia:composite/dia:attribute[@name="string"]/dia:string';
  87. $xpath = new DOMXPath($this->dom);
  88. $xpath->registerNamespace("dia", Fsm_From_Dia::DIA_NAMESPACE);
  89. $id = $element->getAttribute('id');
  90. $dia_string = $xpath->query($query, $element);
  91. if ($dia_string->length == 1) // Normal case
  92. {
  93. $dia_text = $dia_string->item(0);
  94. $ret = trim($dia_text->textContent, '#');
  95. }
  96. else
  97. {
  98. echo "Standard state #$id does not contain the expected content: anomaly.\n";
  99. }
  100. return array('type' => 'standard', 'name' => $ret);
  101. }
  102. /**
  103. * Extract the actual text content from a query for a Dia string element.
  104. * This is to be used over XPath query results
  105. *
  106. * @param DOMNodeList $nodes
  107. * @return string
  108. */
  109. private function getTextFromDiaString ($nodes)
  110. {
  111. if ($nodes->length == 1)
  112. {
  113. $ret = $nodes->item(0);
  114. $ret = trim($ret->textContent, '#');
  115. }
  116. else
  117. {
  118. $ret = NULL;
  119. }
  120. return $ret;
  121. }
  122. /**
  123. * Extract the various information fields from a Dia "UML - Connection"
  124. *
  125. * @param DOMElement $element
  126. * @return array
  127. */
  128. protected function getTransitionInfo($element)
  129. {
  130. $xpath = new DOMXPath($this->dom);
  131. $xpath->registerNamespace("dia", Fsm_From_Dia::DIA_NAMESPACE );
  132. $id = $element->getAttribute('id');
  133. $baseQuery = 'dia:attribute[@name="%s"]/dia:string';
  134. foreach (array('trigger', 'action', 'guard') as $parameter)
  135. {
  136. $query = sprintf($baseQuery, $parameter);
  137. $nodes = $xpath->query($query, $element);
  138. $ret[$parameter] = $this->getTextFromDiaString($nodes);
  139. if ($parameter <> 'guard') // triggers and actions
  140. {
  141. $ret[$parameter] = ucfirst($ret[$parameter]);
  142. }
  143. }
  144. $query = 'dia:connections/dia:connection';
  145. $dia_connections = $xpath->query($query, $element);
  146. // echo "Connections: $dia_connections->length\n";
  147. if ($dia_connections->length == 2)
  148. {
  149. // echo "Transition $id links" ;
  150. foreach ($dia_connections as $end)
  151. {
  152. $handle = $end->getAttribute('handle');
  153. $link = $end->getAttribute('to');
  154. switch ($handle)
  155. {
  156. case '0' : $kind = 'from'; $ret['from'] = $link ; break;
  157. case '1' : $kind = 'to'; $ret['to'] = $link ; break;
  158. default: $kind = "anomaly($handle)"; break;
  159. }
  160. // echo " $kind $link";
  161. }
  162. }
  163. else
  164. {
  165. echo "Anomaly detected on the connection properties of transiition #$id\n";
  166. }
  167. // echo " on ${trigger}[$guard]/$action.\n";
  168. return $ret;
  169. }
  170. /**
  171. * Load a Dia file into the DOM
  172. *
  173. * @param string $filePath
  174. * @return void
  175. */
  176. public function __construct($filePath)
  177. {
  178. $this->dom = new DOMDocument();
  179. $this->dom->load($filePath);
  180. $this->fsm = new Fsm_Writer();
  181. }
  182. /**
  183. * Parse the DOM to extract the various UML elements to an abstract FSM array
  184. * @return array
  185. */
  186. public function parse()
  187. {
  188. $query = '//dia:object';
  189. $xpath = new DOMXPath($this->dom);
  190. $xpath->registerNamespace("dia", Fsm_From_Dia::DIA_NAMESPACE);
  191. $result = $xpath->query($query);
  192. foreach ($result as $object)
  193. {
  194. $type = $object->getAttribute('type');
  195. $id = $object->getAttribute('id');
  196. switch ($type)
  197. {
  198. case 'UML - State Term':
  199. $state = $this->getStateTermInfo($object);
  200. $this->fsm->addState($state['name']);
  201. $this->states[$id] = $state; // needed to match transitions
  202. if ( ($state['type'] == Finite_State_Machine::INIT_STATE)
  203. || ($state['type'] == Finite_State_Machine::FINAL_STATE)
  204. )
  205. {
  206. $this->fsm->setSpecialState($state['type'], $state['name']);
  207. }
  208. break;
  209. case 'UML - State':
  210. $state = $this->getStateInfo($object);
  211. $this->fsm->addState($state['name']);
  212. $this->states[$id] = $state; // needed to match transitions
  213. break;
  214. case 'UML - Transition':
  215. $transition = $this->getTransitionInfo($object);
  216. $state_name = $this->states[$transition['from']]['name'];
  217. $next_state_name = $this->states[$transition['to']]['name'];
  218. $event_name = $transition['trigger'];
  219. $result_name = $transition['guard'];
  220. $action_name = $transition['action'];
  221. if (empty($result_name)) // Not allowed
  222. {
  223. $result_name = "unnnamed_result_" . $this->result_generator++;
  224. }
  225. /**
  226. * This add will fail when adding outcomes to existing events,
  227. * but this is as designed
  228. */
  229. $this->fsm->addEvent($state_name, $event_name);
  230. $this->fsm->addOutcome($state_name, $event_name, $result_name,
  231. $next_state_name, $action_name);
  232. break;
  233. default:
  234. echo "Object #$id is of unknown type $type: ignored.\n";
  235. break;
  236. }
  237. }
  238. }
  239. /**
  240. * Facade for FSM_Writer
  241. *
  242. * @param string $prefix
  243. * @param boolean $php
  244. * @param boolean $overwrite
  245. */
  246. public function save_fsm($prefix = 'fsm', $php = FALSE, $overwrite = FALSE)
  247. {
  248. $ret = $this->fsm->save_fsm($prefix = 'fsm', $php = FALSE, $overwrite = FALSE);
  249. return $ret;
  250. }
  251. }
  252. error_reporting($erFsmFromDia);
  253. unset ($erFsmFromDia);