FAQ
I needed a FSM for a project and had previously used Fysom for Python,
but couldn't find one with the same ease of use for Go.

The result is a simple state machine that can be used to replace several
(and often incompatible) boolean state flags. It's probably not
mathematically correct in the true sense of a FSM, but does the job of
being an advanced boolean.

You can find the project at github.com/looplab/fsm

API docs and examples are available at godoc.org/github.com/looplab/fsm

I have also written a short notice at my Tumblr
http://max-persson.tumblr.com/post/58929804350/finite-state-machine-in-go

Enjoy,
Max Persson

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Search Discussions

  • Ondřej Kupka at Aug 23, 2013 at 10:02 am
    Hi,

    I've been playing with state machines for my projects as well, although
    they are absolutely not the formal FSMs.
    You can check it at https://github.com/tchap/go-statemachine. It's pretty
    small but I find it quite useful. It can make your code much easier to read
    and understand.

    Regards,
    Ondra Kupka
    On Thursday, August 22, 2013 9:33:13 AM UTC+2, Max Persson wrote:

    I needed a FSM for a project and had previously used Fysom for Python,
    but couldn't find one with the same ease of use for Go.

    The result is a simple state machine that can be used to replace several
    (and often incompatible) boolean state flags. It's probably not
    mathematically correct in the true sense of a FSM, but does the job of
    being an advanced boolean.

    You can find the project at github.com/looplab/fsm

    API docs and examples are available at godoc.org/github.com/looplab/fsm

    I have also written a short notice at my Tumblr
    http://max-persson.tumblr.com/post/58929804350/finite-state-machine-in-go

    Enjoy,
    Max Persson
    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Peter Kleiweg at Aug 23, 2013 at 10:45 pm

    Op vrijdag 23 augustus 2013 12:02:51 UTC+2 schreef Ondřej Kupka:

    Hi,

    I've been playing with state machines for my projects as well, although
    they are absolutely not the formal FSMs.
    You can check it at https://github.com/tchap/go-statemachine. It's pretty
    small but I find it quite useful. It can make your code much easier to read
    and understand.
    I am not sure, but I think this looks interesting. I can't see me using
    now. You probably need some large software with complex behaviour to have a
    use for such a thing as this.

    By the way, there's also Ragel http://www.complang.org/ragel/
    That works on input strings. Perhaps you can use it to run on commands
    coming from a channel, instead?


    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Ondrej Kupka at Aug 24, 2013 at 7:13 am
    Hi,
    On Aug 24, 2013, at 12:45 AM, Peter Kleiweg wrote:

    Op vrijdag 23 augustus 2013 12:02:51 UTC+2 schreef Ondřej Kupka:
    Hi,

    I've been playing with state machines for my projects as well, although they are absolutely not the formal FSMs.
    You can check it at https://github.com/tchap/go-statemachine. It's pretty small but I find it quite useful. It can make your code much easier to read and understand.

    I am not sure, but I think this looks interesting. I can't see me using now. You probably need some large software with complex behaviour to have a use for such a thing as this.
    go-statemachine is in fact not really supposed to be used as you would use typical FSM. It's just for structuring your Go code and doing synchronization, not for using FSM from your code, so it's really something completely different. But the spirit of state machines is there :-) Sorry if I confused you :-)

    Regards,
    Ondra Kupka
    By the way, there's also Ragel http://www.complang.org/ragel/
    That works on input strings. Perhaps you can use it to run on commands coming from a channel, instead?



    --
    You received this message because you are subscribed to a topic in the Google Groups "golang-nuts" group.
    To unsubscribe from this topic, visit https://groups.google.com/d/topic/golang-nuts/ClHzw92lq9g/unsubscribe.
    To unsubscribe from this group and all its topics, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Max Persson at Aug 27, 2013 at 8:17 am
    Hi,

    Your FSM looks interesting for certain use cases, although I think channels
    are the way to go to make thins thread safe in go (passing the structs over
    a channel, which is thread safe by nature.)

    My FSM is not at all meant to be used in the same cases. It's primary use
    is to replace several dependent boolean state variables, for example
    open/closed and locked/unlocked for a door. These variables is much better
    combined into a simple FSM to track valid combinations (locked and closed
    is a strange combination for example.) Here is some sample code on how to
    use it:

    fsm := NewFSM(
         "locked",
         Events{
             {Name: "unlock", Src: []string{"locked"}, Dst: "unlocked"},
             {Name: "open", Src: []string{"unlocked"}, Dst: "open"},
             {Name: "close", Src: []string{"open"}, Dst: "unlocked"},
             {Name: "lock", Src: []string{"unlocked"}, Dst: "locked"},
         },
         Callbacks{},
    )

    fsm.Event("unlock")
    fsm.Event("open")
    fsm.Event("close")
    fsm.Event("lock")

    Proper error checking needs to be done on the Event() method to see if the
    transition was valid, but I removed it hear to make it easier to read the
    example.

    I believe both our FSMs have use cases, but I started this thread as an
    announcement of my new package so please keep the discussions of
    go-statemachine in another thread.

    Best Regards,
    Max

    On Friday, August 23, 2013 12:02:51 PM UTC+2, Ondřej Kupka wrote:

    Hi,

    I've been playing with state machines for my projects as well, although
    they are absolutely not the formal FSMs.
    You can check it at https://github.com/tchap/go-statemachine. It's pretty
    small but I find it quite useful. It can make your code much easier to read
    and understand.

    Regards,
    Ondra Kupka
    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Ondřej Kupka at Aug 27, 2013 at 8:42 am
    Hi,
    On Tuesday, August 27, 2013 10:16:57 AM UTC+2, Max Persson wrote:

    Hi,

    Your FSM looks interesting for certain use cases, although I think
    channels are the way to go to make thins thread safe in go (passing the
    structs over a channel, which is thread safe by nature.)

    My FSM is not at all meant to be used in the same cases. It's primary use
    is to replace several dependent boolean state variables, for example
    open/closed and locked/unlocked for a door. These variables is much better
    combined into a simple FSM to track valid combinations (locked and closed
    is a strange combination for example.) Here is some sample code on how to
    use it:

    fsm := NewFSM(
    "locked",
    Events{
    {Name: "unlock", Src: []string{"locked"}, Dst: "unlocked"},
    {Name: "open", Src: []string{"unlocked"}, Dst: "open"},
    {Name: "close", Src: []string{"open"}, Dst: "unlocked"},
    {Name: "lock", Src: []string{"unlocked"}, Dst: "locked"},
    },
    Callbacks{},
    )

    fsm.Event("unlock")
    fsm.Event("open")
    fsm.Event("close")
    fsm.Event("lock")

    Proper error checking needs to be done on the Event() method to see if the
    transition was valid, but I removed it hear to make it easier to read the
    example.

    I believe both our FSMs have use cases, but I started this thread as an
    announcement of my new package so please keep the discussions of
    go-statemachine in another thread.
    Yeah, sorry if I spammed this ANN, I guess I should have used another
    thread.

    Anyway, I see that you just return an error when there is a transition in
    progress (correct me if I am mistaken).
    How would you use this in Go, as a private field in a struct and then you
    do synchronization on top of it?
    Because it is not thread-safe as far as I understand. Or do you have a
    completely different use case
    where you don't need to synchronize?

    Regards,
    Ondra Kupka

    Best Regards,
    Max

    On Friday, August 23, 2013 12:02:51 PM UTC+2, Ondřej Kupka wrote:

    Hi,

    I've been playing with state machines for my projects as well, although
    they are absolutely not the formal FSMs.
    You can check it at https://github.com/tchap/go-statemachine. It's
    pretty small but I find it quite useful. It can make your code much easier
    to read and understand.

    Regards,
    Ondra Kupka
    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Max Persson at Aug 29, 2013 at 9:42 am
    No problem with that, just wanted to make sure this thread was kept on
    topic. :)

    As far a synchronization goes it's an area I haven't put much thought into.
    This version is meant to be used in a single thread, but I would be
    interested to hear about ideas on making it thread safe!

    Can you describe a use case of my FSM in a threaded situation, or provide
    some example code of it?

    Best,
    Max

    On Tuesday, August 27, 2013 10:42:33 AM UTC+2, Ondřej Kupka wrote:

    Hi,
    On Tuesday, August 27, 2013 10:16:57 AM UTC+2, Max Persson wrote:

    Hi,

    Your FSM looks interesting for certain use cases, although I think
    channels are the way to go to make thins thread safe in go (passing the
    structs over a channel, which is thread safe by nature.)

    My FSM is not at all meant to be used in the same cases. It's primary use
    is to replace several dependent boolean state variables, for example
    open/closed and locked/unlocked for a door. These variables is much better
    combined into a simple FSM to track valid combinations (locked and closed
    is a strange combination for example.) Here is some sample code on how to
    use it:

    fsm := NewFSM(
    "locked",
    Events{
    {Name: "unlock", Src: []string{"locked"}, Dst: "unlocked"},
    {Name: "open", Src: []string{"unlocked"}, Dst: "open"},
    {Name: "close", Src: []string{"open"}, Dst: "unlocked"},
    {Name: "lock", Src: []string{"unlocked"}, Dst: "locked"},
    },
    Callbacks{},
    )

    fsm.Event("unlock")
    fsm.Event("open")
    fsm.Event("close")
    fsm.Event("lock")

    Proper error checking needs to be done on the Event() method to see if
    the transition was valid, but I removed it hear to make it easier to read
    the example.

    I believe both our FSMs have use cases, but I started this thread as an
    announcement of my new package so please keep the discussions of
    go-statemachine in another thread.
    Yeah, sorry if I spammed this ANN, I guess I should have used another
    thread.

    Anyway, I see that you just return an error when there is a transition in
    progress (correct me if I am mistaken).
    How would you use this in Go, as a private field in a struct and then you
    do synchronization on top of it?
    Because it is not thread-safe as far as I understand. Or do you have a
    completely different use case
    where you don't need to synchronize?

    Regards,
    Ondra Kupka

    Best Regards,
    Max

    On Friday, August 23, 2013 12:02:51 PM UTC+2, Ondřej Kupka wrote:

    Hi,

    I've been playing with state machines for my projects as well, although
    they are absolutely not the formal FSMs.
    You can check it at https://github.com/tchap/go-statemachine. It's
    pretty small but I find it quite useful. It can make your code much easier
    to read and understand.

    Regards,
    Ondra Kupka
    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Davmaz at May 5, 2015 at 8:49 pm
    Hi Max,
       I've been using your looplab go fsm and it works great. However, I'm
    stuck on what makes sense to use your code in a continuous loop. Something
    like:
    for {
         switch fsm.Current() {
         case "InitS":
              // Do initS things...
         case "WorkingS":
              // Do working steps
        }
    }
    It seems like you've already got that "inside" and fsm.Current() doesn't
    seem necessary. But I want it to keep looping through states and responding
    accordingly.

    I'd love any suggestions you might have.

    Thanks,
    Dave
    On Tuesday, August 27, 2013 at 4:16:57 AM UTC-4, Max Ekman wrote:

    Hi,

    Your FSM looks interesting for certain use cases, although I think
    channels are the way to go to make thins thread safe in go (passing the
    structs over a channel, which is thread safe by nature.)

    My FSM is not at all meant to be used in the same cases. It's primary use
    is to replace several dependent boolean state variables, for example
    open/closed and locked/unlocked for a door. These variables is much better
    combined into a simple FSM to track valid combinations (locked and closed
    is a strange combination for example.) Here is some sample code on how to
    use it:

    fsm := NewFSM(
    "locked",
    Events{
    {Name: "unlock", Src: []string{"locked"}, Dst: "unlocked"},
    {Name: "open", Src: []string{"unlocked"}, Dst: "open"},
    {Name: "close", Src: []string{"open"}, Dst: "unlocked"},
    {Name: "lock", Src: []string{"unlocked"}, Dst: "locked"},
    },
    Callbacks{},
    )

    fsm.Event("unlock")
    fsm.Event("open")
    fsm.Event("close")
    fsm.Event("lock")

    Proper error checking needs to be done on the Event() method to see if the
    transition was valid, but I removed it hear to make it easier to read the
    example.

    I believe both our FSMs have use cases, but I started this thread as an
    announcement of my new package so please keep the discussions of
    go-statemachine in another thread.

    Best Regards,
    Max

    On Friday, August 23, 2013 12:02:51 PM UTC+2, Ondřej Kupka wrote:

    Hi,

    I've been playing with state machines for my projects as well, although
    they are absolutely not the formal FSMs.
    You can check it at https://github.com/tchap/go-statemachine. It's
    pretty small but I find it quite useful. It can make your code much easier
    to read and understand.

    Regards,
    Ondra Kupka
    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Max Persson at Aug 29, 2013 at 9:44 am
    The project was just added to the Go projects wiki page under data
    structures - other: http://code.google.com/p/go-wiki/wiki/Projects

    I have also added continuous integration at
    drone.io: https://drone.io/github.com/looplab/fsm

    /Max
    On Thursday, August 22, 2013 9:33:13 AM UTC+2, Max Persson wrote:

    I needed a FSM for a project and had previously used Fysom for Python,
    but couldn't find one with the same ease of use for Go.

    The result is a simple state machine that can be used to replace several
    (and often incompatible) boolean state flags. It's probably not
    mathematically correct in the true sense of a FSM, but does the job of
    being an advanced boolean.

    You can find the project at github.com/looplab/fsm

    API docs and examples are available at godoc.org/github.com/looplab/fsm

    I have also written a short notice at my Tumblr
    http://max-persson.tumblr.com/post/58929804350/finite-state-machine-in-go

    Enjoy,
    Max Persson
    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedAug 22, '13 at 2:39p
activeMay 5, '15 at 8:49p
posts9
users4
websitegolang.org

People

Translate

site design / logo © 2022 Grokbase