finite state machine in lua

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

finite state machine in lua

murthy
Last discussion in this archive is very old hence wanted to check.
I need to implement a finite-state-machine in lua. Please let me know if http://lua-users.org/wiki/FiniteStateMachine is still the right way to go if I should use some libs. If so which one.

Thanks
Reply | Threaded
Open this post in threaded view
|

Re: finite state machine in lua

Sean Conner
It was thus said that the Great Srinivas Murthy once stated:
> Last discussion in this archive is very old hence wanted to check.
> I need to implement a finite-state-machine in lua. Please let me know if
> http://lua-users.org/wiki/FiniteStateMachine is still the right way to go
> if I should use some libs. If so which one.
>
> Thanks

  Personally, I tend to use mutually recursive functions to handle state
machines in Lua.  Since Lua has tall calls, stack overflows are not an
issue.

        function state1(state)
          local event = getevent(state)
          if event  == ACTION1 then
            return state2(state)
          elseif event == ACTION2 then
            return state3(state)
          else
            return state1(state)
          end
        end

        function state2(state)
          local event = getevent(state)
          if event == ACTION3 then
            return state1(state)
          elseif event == ACTION4 then
            return state3(state)
          else
            return state2(state)
          end
        end

        function state3(state)
          local event = getevent(state)
          if event == QUIT then
            return -- this leaves the state machine entirely
          elseif event == ACTION1 then
            return state1(state)
          elseif event == ACTION2 then
            return state2(state)
          else
            return state3(state)
          end
        end

  I've used this method to implement TFTP, using the same state machine for
client and server, but starting in a different state depending upon which
side it was.

  -spc


Reply | Threaded
Open this post in threaded view
|

Re: finite state machine in lua

Jorge Visca

On 28/4/19 17:32, Sean Conner wrote:
> Personally, I tend to use mutually recursive functions to handle state
> machines in Lua.

I've used that and it works great, very compact and efficient. An
explicit data structure with states, transitions, etc. is easier to
explain to others and to manipulate programmatically, tough.

If you need more sophisticated state machines, like hierarchical state
machines / statecharts, you can check this:

https://gitlab.fing.edu.uy/jvisca/ahsm

(it's a very light weight implementation, we use it on
microcontroller-based robots)


Jorge



Reply | Threaded
Open this post in threaded view
|

Re: finite state machine in lua

Soni "They/Them" L.


On 2019-04-28 9:44 p.m., Jorge wrote:

>
> On 28/4/19 17:32, Sean Conner wrote:
>> Personally, I tend to use mutually recursive functions to handle state
>> machines in Lua.
>
> I've used that and it works great, very compact and efficient. An
> explicit data structure with states, transitions, etc. is easier to
> explain to others and to manipulate programmatically, tough.
>
> If you need more sophisticated state machines, like hierarchical state
> machines / statecharts, you can check this:
>
> https://gitlab.fing.edu.uy/jvisca/ahsm
>
> (it's a very light weight implementation, we use it on
> microcontroller-based robots)
>
>
> Jorge
>
>
>

I sort of made my own thing I guess?
https://cybre.tech/SoniEx2/lua.cratera/ (this repo's a mess, but
basically look at parser.lua and luatokens.lua and I guess compiler.lua
too.)

I don't recommend it, it's just something to look at from a distance tbh...

... a great great distance.

Reply | Threaded
Open this post in threaded view
|

Re: finite state machine in lua

murthy
In reply to this post by Jorge Visca
I need to use this for a real-time event sampling use-case. 
https://gitlab.fing.edu.uy/jvisca/ahsm  does seem interesting but the cautionary note that its "very alpha quality and not tested ..." is worrisome.

Is there any other libs that you could recommend?
 

On Sun, Apr 28, 2019 at 5:53 PM Jorge <[hidden email]> wrote:

On 28/4/19 17:32, Sean Conner wrote:
> Personally, I tend to use mutually recursive functions to handle state
> machines in Lua.

I've used that and it works great, very compact and efficient. An
explicit data structure with states, transitions, etc. is easier to
explain to others and to manipulate programmatically, tough.

If you need more sophisticated state machines, like hierarchical state
machines / statecharts, you can check this:

https://gitlab.fing.edu.uy/jvisca/ahsm

(it's a very light weight implementation, we use it on
microcontroller-based robots)


Jorge



Reply | Threaded
Open this post in threaded view
|

Re: finite state machine in lua

Jorge Visca
On 28/4/19 23:39, Srinivas Murthy wrote:
> I need to use this for a real-time event sampling use-case.
> https://gitlab.fing.edu.uy/jvisca/ahsm does seem interesting but the
> cautionary note that its "very alpha quality and not tested ..." is
> worrisome.

I already considered removing that disclaimer. I should remove it. I
will. Any moment now :\

>
> Is there any other libs that you could recommend?

When I needed a plain state machine I just rolled my own following one
of two patterns, the one from
http://lua-users.org/wiki/FiniteStateMachine and the "functional" one 
(you can see an example of such machine parsing a protocol here:
https://github.com/xopxe/lua-bencode-push/blob/master/bencode-push.lua).
In these cases writing the machine felt pretty natural in the code
itself, and I did not feel the need to use an abstracted library.

When the SM is more complex a library makes sense. We tried to use rFSM
(https://github.com/kmarkus/rFSM), but run out of memory, and there
seemed to be too much complexity in there. I just wrote a implementation
based on its API. This is not the first time this happens to me, find a
library with an API I want to use, find the implementation too complex
or with lots of stuff I don't need, and then write my own library from
scratch inspired by that. I suspect that means that the API is actually
the hardest part of the library design.