How to express "StateMachine<TState" .... for hierarchical state machine

152 views
Skip to first unread message

mabra

unread,
Jun 24, 2013, 6:58:01 PM6/24/13
to dotnet-state-ma...@googlegroups.com
Hello !

I have just different statemachines to mange state for one object [computer]. I just learned the basics.
Now, I feel, this is too complex and a much better fit would be to use a real hierarchical statemachine.

But I dont find an expressive way to define the states. There is nothing like "hirerachical enums". The
PhoneCall sample is just very raw for this. My first attempt is to use a enmum like this:

{ New, Animating_PingOk, Animating_PingFailed, Connecting_ConnectOk, Connecting_ConnectFailed, ....Disabled }

where New, Animating_* [!!], Connecting_* express my toplevel states, where the identifier part after
the underscore shows the substate. I feel not good with this, but I am not finding an alternative.

I've tried classes and subsclasses and failed. My last attempt was this:

public class States
{
    public enum New {};
    public enum Animating { PingOk, PingFailed };
    public enum Connecting { ConnectionOk, ConnectionFailed };
    public enum Operating { Working, Failed };
    public enum Failed {};
}

but I failed to configure:

            this.states = new States();
            this.sm = new StateMachine<States, Trigger2>(this.states);

            this.sm.Configure(this.states)
                    .Permit(Trigger2.Animating_PingOk, this.states.Operating );  <== error [tried first: Operating.Working].

I really dont understand the statmachine definition. VS shows me:

class Stateless.StateMachine<TState

I am looking for samples, how to express this. Enums are really easy to understand.
The documentation says this:

"Generic support for states and triggers of any .NET type (numbers, strings, enums, etc.) "

I am stuck here. If someone probably has a little moment and can give me a hint, that
would be really great!

Thanks anyway and best regards,
++mabra

Grant BlahaErath

unread,
Jun 24, 2013, 7:38:47 PM6/24/13
to dotnet-state-ma...@googlegroups.com
Your state and triggers can be anything, not just enums.  I've used reference types as well as value types. However, you can't mix different types as you are trying below.  It's all based on .Equals to identify states or triggers, but the states have to be the same type, and the triggers have to be the same type.  

I've used Guids, strings, ints, and singleton objects depending on the application.   



--
 
---
You received this message because you are subscribed to the Google Groups "Stateless .NET State Machine Framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to dotnet-state-machine-...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

mabra

unread,
Jun 26, 2013, 4:01:39 PM6/26/13
to dotnet-state-ma...@googlegroups.com
Hi !

Ok, thanks.

What I was looking for, is a way to visibly express hierarchies in the state definition.
If you use enmus, there is no way. But subclasses will not wok.

I defined a Level class and TopLevel and SubLevels subclasses. This would
allow me to make it visible, which state I am talking about. Sad, this cannot
work.

Thinking ....

Oh, another compomis could be this:

public static class State
{
    public enum Top { New, Animating, Disabled };
    public enum Sub { PingFailed, Connectable };
}

private StateMachine<int, Trigger2> sm;

this.sm = new StateMachine<int, Trigger2>( (int) State.Top.Animating);

this.sm.Configure( (int) State.Top.New )
        .Permit(Trigger2.Animating_PingOk, (int) State.Sub.Connectable);

Not really good, but let me see, which state I am configuring.
I hope to have explained me right now.


Thanks anyway and
best regards,
++mabra


To unsubscribe from this group and stop receiving emails from it, send an email to dotnet-state-machine-framework+unsubscribe@googlegroups.com.

Grant BlahaErath

unread,
Jun 26, 2013, 4:57:03 PM6/26/13
to dotnet-state-ma...@googlegroups.com
This also works:

static class Foo
{
static class SubFoo1 {
static readonly object State1 = new Object();
static readonly object State2 = new Object();
}

static class SubFoo2 {
static readonly object State1 = new Object();
static readonly object State2 = new Object();
}
}

var sm = new StateMachine<object, TriggerType>(Foo.SubFoo1.State1);

it works because the objects are static and unique, so when the Equals operate is used, it defaults to comparing the singleton objects.

etc...

To unsubscribe from this group and stop receiving emails from it, send an email to dotnet-state-machine-...@googlegroups.com.

mabra

unread,
Jun 26, 2013, 6:08:40 PM6/26/13
to dotnet-state-ma...@googlegroups.com
Hi !

Ok, thats helps me really. I start recognizing the pattern.

Thanks!
++mabra
Reply all
Reply to author
Forward
0 new messages