Browse Source

API change: now produces a FSM directly, using the Fsm_Writer class.

Frederic G. Marand 17 years ago
parent
commit
a3e0c7b918
1 changed files with 67 additions and 7 deletions
  1. 67 7
      Fsm_From_Dia.php

+ 67 - 7
Fsm_From_Dia.php

@@ -9,7 +9,7 @@
  * @copyright  2007 Ouest Systèmes Informatiques
  * @author     Frederic G. MARAND
  * @license    CeCILL 2.0
- * @version    CVS: $Id: Fsm_From_Dia.php,v 1.1 2007-06-10 19:37:09 marand Exp $
+ * @version    CVS: $Id: Fsm_From_Dia.php,v 1.2 2007-06-10 19:38:14 marand Exp $
  * @link       http://wiki.audean.com/fsm/fsm
  * @since      FSM 1.6
  * @package    fsm
@@ -22,6 +22,8 @@ $erFsmFromDia = error_reporting(E_ALL|E_STRICT);
  * This class converts an UML diagram from Dia 0.9.6
  * into an abstract FSM graph.
  *
+ * @package    fsm
+ * @subpackage fsm.ui
  * @todo Validate the diagram: currently it will just choke on non-accepted diagrams
  */
 class Fsm_From_Dia
@@ -35,6 +37,20 @@ class Fsm_From_Dia
    */
   protected $dom;
 
+  /**
+   * The FSM writer instance to save to.
+   *
+   * @var FSM_Writer
+   */
+  protected $fsm;
+
+  /**
+   * Sequence generator for unnamed event results (guards)
+   *
+   * @var integer
+   */
+  protected $result_generator = 0;
+
   /**
    * Extract the initial/final status from a Dia "UML - State Term"
    *
@@ -56,8 +72,8 @@ class Fsm_From_Dia
       $val = $dia_boolean->getAttribute('val');
       switch ($val)
         {
-        case 'true':  $ret = 'final';   break;
-        case 'false': $ret = 'initial'; break;
+        case 'true':  $ret = Finite_State_Machine::FINAL_STATE; break;
+        case 'false': $ret = Finite_State_Machine::INIT_STATE;  break;
         default:      $ret = "anomalous($val)"; break;
         }
       }
@@ -178,6 +194,7 @@ class Fsm_From_Dia
     {
     $this->dom = new DOMDocument();
     $this->dom->load($filePath);
+    $this->fsm = new Fsm_Writer();
     }
 
   /**
@@ -198,15 +215,45 @@ class Fsm_From_Dia
       switch ($type)
         {
         case 'UML - State Term':
-          $states[$id] = $this->getStateTermInfo($object);
+          $state = $this->getStateTermInfo($object);
+          $this->fsm->addState($state['name']);
+          $this->states[$id] = $state; // needed to match transitions
+          if ( ($state['type'] == Finite_State_Machine::INIT_STATE)
+            || ($state['type'] == Finite_State_Machine::FINAL_STATE)
+            )
+            {
+            $this->fsm->setSpecialState($state['type'], $state['name']);
+            }
           break;
 
         case 'UML - State':
-          $states[$id] = $this->getStateInfo($object);
+          $state = $this->getStateInfo($object);
+          $this->fsm->addState($state['name']);
+          $this->states[$id] = $state; // needed to match transitions
           break;
 
         case 'UML - Transition':
-          $transitions[$id] = $this->getTransitionInfo($object);
+          $transition  = $this->getTransitionInfo($object);
+          $state_name      = $this->states[$transition['from']]['name'];
+          $next_state_name = $this->states[$transition['to']]['name'];
+          $event_name  = $transition['trigger'];
+          $result_name = $transition['guard'];
+          $action_name = $transition['action'];
+
+          if (empty($result_name)) // Not allowed
+            {
+            $result_name = "unnnamed_result_" . $this->result_generator++;
+            }
+
+          /**
+           * This add will fail when adding outcomes to existing events,
+           * but this is as designed
+           */
+          $this->fsm->addEvent($state_name, $event_name);
+
+
+          $this->fsm->addOutcome($state_name, $event_name, $result_name,
+            $next_state_name, $action_name);
           break;
 
         default:
@@ -214,7 +261,20 @@ class Fsm_From_Dia
           break;
         }
       }
-    return array('states' => $states, 'transitions' => $transitions);
+
+    }
+
+  /**
+   * Facade for FSM_Writer
+   *
+   * @param string $prefix
+   * @param boolean $php
+   * @param boolean $overwrite
+   */
+  public function save_fsm($prefix = 'fsm', $php = FALSE, $overwrite = FALSE)
+    {
+    $ret = $this->fsm->save_fsm($prefix = 'fsm', $php = FALSE, $overwrite = FALSE);
+    return $ret;
     }
   }