Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re^6: a State machine with Roles - possible? (class or instance)

by mascip (Pilgrim)
on May 31, 2013 at 16:58 UTC ( [id://1036269]=note: print w/replies, xml ) Need Help??


in reply to Re^5: a State machine with Roles - possible? (class or instance)
in thread a State machine with Roles - possible?

Thank you Salva, i think that's a very good point: if you need to do this, you probably need to redesign.

I think your first point is wrong though: you can have "non-linear" behavior, for example a "Person" turned into a "Zombie", could become stronger when they are "Poisoned" (instead of becoming weaker like a non-Zombie person).

And transitions can be implemented in the Roles, so that you can also have complex transitions.

So, i think you'd get a truly multidimensional State Machine (is that right, or did i misunderstand something?). It might just get a bit messy in terms of the code, which shows you might want to redesign.

It's still nice to know that it's possible though. In the meantime i'll try and use Class::StateMachine.

Replies are listed 'Best First'.
Re^7: a State machine with Roles - possible? (class or instance)
by salva (Canon) on Jun 01, 2013 at 10:16 UTC
    I think your first point is wrong though: you can have "non-linear" behavior, for example a "Person" turned into a "Zombie", could become stronger when they are "Poisoned" (instead of becoming weaker like a non-Zombie person).

    You are right in the sense that it can be done. But my point is that you loose the advantages of considering the state as multidimensional.

    For me, the real issue with complex state machines (mutidimensional or not) is not how to implement them but how to express them in a way that is easy to understand for humans. How to avoid ending with a big ball of code.

      And that's where Class::StateMachine::Declarative shines: it's concise, and makes it easy for humans to apprehend the whole transitions set.

      I realise now that objects, if they have several attributes, ARE multidimensional state machines anyway. My question was thus mostly about whether it is possible to unapply a Role; thinking that it could be useful some day.

      Just one last quesiton: Is it possible to use Class::StateMachine in a Role?

        Class::StateMachine::Declarative is the result of my experience developing complex state machines, but for my current kind of problems that may be quite specific, I don't know how that could bend to the problems of other people.

        I am still not completely happy with it, but it is true that the current iteration is much better than its predecessors.

        Is it possible to use Class::StateMachine in a Role

        No, because Class::StateMachine is a metaclass, it plays with the method resolution order (mro) to attain the specific state machine method resolution.

        When I designed Class::StateMachine I have a hard time figuring how support the state machine while retaining the full perl object oriented semantics (specifically inheritance and multiple inheritance). But then, when I got to use it for practical matters, I discover that inheritance and state machines do not mix well.

        Thinks like defining some states in one class, and others in a subclass, or refining them in the subclass is not a great idea.

        It is ok to reuse (via inheritance or roles) the code that implements simple actions, but the state machine definition should be a coherent entity.

      I agree with you, that an intuitive API is the more important than implementation.

      But do you have a real world example of a multidimensional state machine?

      This "door" thing is just to shallow to justify the efforts and clarify advantages of different approaches.

      Cheers Rolf

      ( addicted to the Perl Programming Language)

        Yes, a real common example is, on an event-based application, an object that has to do two (or more) things at the same time. For instance accessing a network service while tracking and external process or accessing a database.

        You may be able to delegate most of the work needed to perform the subtasks to other objects (which would be unidimensional state machines). But there would be a point where you have to handle the interaction between them using a multidimensional state machine.

        In any case, note that a multidimensional state machine can be converted into a unidimensional one if you flatten the state matrix using the cartesian product, or in practice, if you are able to organize the states in a tree (usually not all the multidimensional states may happen).

        I don't have an example: it's just me playing with fire, and learning.

        The closest example that i found is here. It's a video game character that can be turned into a Zombie, poisoned (which could make her stronger if she's a zombie), turned into a Chipmunk, an Angel (which turns it into a Dead-Angel if it's also a Zombie), break a leg, or any combination of the above.

        Maybe i'll never need that in real life. But sometimes it's when you know that something is possible, that it gets you out of a complex situation elegantly.

        And... i like learning and experimenting new ways.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1036269]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (2)
As of 2024-03-19 10:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found