My favorites | Sign in
Project Home Downloads Wiki Issues Source
Search
for
usingXML  

Featured
Updated Jun 28, 2009 by cjcat2266

Using XML

Generating XML Representations

The XML representation of a finite state machine can be generated through the FSMachine.toXML() method.

import idv.cjcat.fenofsm.*;

var fsm:FSMachine = new FSMachine("s0");
fsm.createState("s1");
fsm.createState("s2");

fsm.createTransition(s0, s1, "forward", "t01", 0);
fsm.createTransition(s1, s2, "forward", "t12", 0);
fsm.createTransition(s2, s0, "forward", "t20", 0);
fsm.createTransition(s0, s2, "backward", "t02", 0);
fsm.createTransition(s2, s1, "backward", "t21", 0);
fsm.createTransition(s1, s0, "backward", "t10", 0);

trace(fsm.toXML().toXMLString());

Here's the output:

<machine initState="s0">
  <states>
    <state name="s0"/>
    <state name="s1"/>
    <state name="s2"/>
  </states>
  <transitions>
    <transition name="t01" from="s0" to="s1" input="forward" inputType="string" delay="0"/>
    <transition name="t12" from="s1" to="s2" input="forward" inputType="string" delay="0"/>
    <transition name="t20" from="s2" to="s0" input="forward" inputType="string" delay="0"/>
    <transition name="t02" from="s0" to="s2" input="backward" inputType="string" delay="0"/>
    <transition name="t21" from="s2" to="s1" input="backward" inputType="string" delay="0"/>
    <transition name="t10" from="s1" to="s0" input="backward" inputType="string" delay="0"/>
  </transitions>
</machine>

Parsing XML Representations

A finite state machine's states and transitions structure can be constructed from XML representations.

This can be done by calling the FSMachine.parseXML() method.

var xml:XML = 
<machine initState="s0">
  <states>
    <state name="s0"/>
    <state name="s1"/>
    <state name="s2"/>
  </states>
  <transitions>
    <transition name="t01" from="s0" to="s1" input="forward" inputType="string" delay="0"/>
    <transition name="t12" from="s1" to="s2" input="forward" inputType="string" delay="0"/>
    <transition name="t20" from="s2" to="s0" input="forward" inputType="string" delay="0"/>
    <transition name="t02" from="s0" to="s2" input="backward" inputType="string" delay="0"/>
    <transition name="t21" from="s2" to="s1" input="backward" inputType="string" delay="0"/>
    <transition name="t10" from="s1" to="s0" input="backward" inputType="string" delay="0"/>
  </transitions>
</machine>
;

This is how to parse the XML representation:

import idv.cjcat.fenofsm.*;

var fsm:FSMachine = new FSMachine();
fsm.parseXML(xml);

trace(fsm.toXML().toXMLString());

And you'll get the exact same XML representation output, which means the finite state machine has successfully parsed the original XML representation and constructed the correct states and transitions structure.

Recall that to obtain state and transition references, you can use the FSMachine.getStateByName() and FSMachine.getTransitionByName() methods.

var s0:State = fsm.getStateByName("s0");
var s1:State = fsm.getStateByName("s1");
var s2:State = fsm.getStateByName("s2");

var t01:Transition = fsm.getTransitionByName("t01");
var t12:Transition = fsm.getTransitionByName("t12");
var t20:Transition = fsm.getTransitionByName("t20");
var t02:Transition = fsm.getTransitionByName("t02");
var t21:Transition = fsm.getTransitionByName("t21");
var t10:Transition = fsm.getTransitionByName("t10");

Sign in to add a comment
Powered by Google Project Hosting