Keeping track of states of a system



Consider a traffic light, which for the sake of this question, will be our system. This system can be in four states, OFF, RED, GREEN and AMBER. At any of these states, the system knows how more seconds it has to be in that state. It may not necessarily, always, know to which state it must next go to because the next state will be controlled externally.


Thus, I know, that



  • this system can have four possible states.

  • each state of this system has a known property - the time to 'live' in that state.

  • this system has one external input to control the state(s) of the system.


My question is, what tools do I have to model this system and its states ? My goal is to know how my system is performing under a set of control parameters.


Some searching later, I came across State Chart XML (SCXML) - also a W3C Working Draft - described as below.



State Chart XML (SCXML) is a general-purpose event-based state machine language that can be used in many ways.



The traffic light example, I mentioned above, can be encoded as shown below.



<?xml version="1.0"?>
<!--
Traffic light controller.
-->
<scxml initialstate="light">

<state id="light">
<initial>
<transition target="Red"/>
</initial>
<state id="Red">
<onentry>
<send target="Self" event="goGreen" delay="1s"/>
</onentry>
<transition event="goGreen" target="Green"/>
</state>
<state id="Yellow">
<onentry>
<send target="Self" event="goRed" delay="500ms"/>
</onentry>
<transition event="goRed" target="Red"/>
</state>
<state id="Green">
<onentry>
<send target="Self" event="goYellow" delay="1s"/>
</onentry>
<transition event="goYellow" target="Yellow"/>
</state>
</state >

</scxml>


For a simulation of the same, an example exists at Synergy SCXML Web Laboratory


I am also aware of the State pattern, described in Wikipedia as



This pattern is used in computer programming to encapsulate varying behavior for the same routine based on an object's state object.



But, I believe, it is more suitable when the states are defined in advance. I would like to have a control, on what states a system could be in, through external means and then (perhaps) pass it on to state pattern implementation.


No comments:

Post a Comment