StateMachine in c# and xml, v3 (updated)

Updated:
 – New archive to download (v3)
 – XML updated
 – Code rewrite with delegates and helper methods

I added unittest for a switch in version 1. Leslie had some troubles with building his trafficsign. I now added a trafficsign as a unittest and it outputs the following:

<?xml version="1.0"?>
<Machine Id="Switch" InitialState="On" xmlns="Exyll.StateMachine">
  <SubStates>
    <State Id="On" InitialState="Red">
      <SubStates>
        <State Id="Red">
          <Transitions>
            <Transition EventName="TimeTrigger" Target="Green" Guard="TimerIs4" Action="TimerReset" />
            <Transition EventName="TimeTrigger" Target="Red" Action="TimerIncrement" />
          </Transitions>
        </State>
        <State Id="Yellow">
          <Transitions>
            <Transition EventName="TimeTrigger" Target="Red" Guard="TimerIs2" Action="TimerReset" />
            <Transition EventName="TimeTrigger" Target="Yellow" Action="TimerIncrement" />
          </Transitions>
        </State>
        <State Id="Green">
          <Transitions>
            <Transition EventName="TimeTrigger" Target="Yellow" Guard="TimerIs4" Action="TimerReset" />
            <Transition EventName="TimeTrigger" Target="Green" Action="TimerIncrement" />
          </Transitions>
        </State>
      </SubStates>
      <Transitions>
        <Transition EventName="TurnOff" Target="Off" />
      </Transitions>
    </State>
    <State Id="Off">
      <Transitions>
        <Transition EventName="TurnOn" Target="On" />
      </Transitions>
    </State>
  </SubStates>
  <Events>
    <Event>TimeTrigger</Event>
    <Event>TurnOff</Event>
    <Event>TurnOn</Event>
  </Events>
</Machine>

Download link : StateMachine version 3

StateMachine in c# and xml

Yesterday I browsed through the new articles at codeproject.com. Leslie Sanford StateMachine articles had my attention. He has designed and implemented it in c#. A job done pretty well I think. I took a look at his code that reads an xml definition to deserialize its state design. My thoughts were that it wasn’t really readable and logical so I made a class design that serializes to a "self-describing" xml document.

An example:
<?xml version="1.0"?>
<Machine Id="Switch" InitialState="On" xmlns="Exyll.StateMachine">
  <SubStates>
    <State Id="On">
      <Transitions>
        <Transition EventName="TurnOff" Target="Off" />
      </Transitions>
    </State>
    <State Id="Off">
      <Transitions>
        <Transition EventName="TurnOn" Target="On" />
      </Transitions>
    </State>
  </SubStates>
  <Events>
    <Event>TurnOff</Event>
    <Event>TurnOn</Event>
  </Events>
</Machine>

If you are interested in this class design and the xmlattributes I had to add to achieve this xml format then download StateMachine code.

LINQ features

September 14, 2005 / 1 Comment on LINQ features

I was just browsing a bit to get more information about LINQ. Our fellow blogger Joshua already posted something about it.

LINQ seems very usable. The old c omega project is transformed into LINQ so those guys that did that R&D project have a nice salary now 😉

So we have DLINQ for dataaccess to do easier selects etc. but I am wondering if I would want to do my DB access this way. I think that I don’t want to do this. The other dataaccess relating thing I noted is the O/R mapper attributes in the overview document. Haven’t seen good samples with this but if the LINQ library has logic like some O/R mappers like creating optimized dataaccess queries then this could become a hit. I really dive into this topic to get to know the details.

XLINQ is for the xml integration within your code. The samples that I’ve seen only show things about xml document creation. What would be a very interesting thing if is XLINQ has support for xsl-t within your code and to easily do thing within that xsl-t that require c# of vb.net code so that you don’t have to call another method but can merge xsl-t and c# code to walk through your xml document.

The bad thing is that I also saw afwul samples about anonymous types. I don’t think that you want to write those classes by hand. It is only interesting for run-time creation of certain object types. I think all options within LINQ rely heavily on this feature. Why on earth would a developer want to create such classes? Hard to understand, hard to maintain and the fucking code police will drop you from the 10th floor head down!

Barry Garving has created a nice LINQ Resources post.

  • Recent Posts
  • Recent Comments
  • Archives
  • Categories
  • Meta