stetre / moonsc

Harel Statecharts in Lua
Other
7 stars 0 forks source link
fsm harel-statechart lua scxml statecharts

MoonSC: Harel Statecharts in Lua

MoonSC is an execution environment for Harel statecharts following the W3C SCXML Recommendation, capable of running multiple concurrent SCXML sessions defined by statecharts written as Lua tables.

It runs on GNU/Linux and requires Lua (>=5.3). It should compile and run also on Windows (MSYS2/MinGW) and on MacOSX, but I've not tested this yet (any feedback is appreciated).

Author: Stefano Trettel

Lua logo

License

MIT/X11 license (same as Lua). See LICENSE.

Documentation

See the Reference Manual.

Getting and installing

Setup the build environment as described here, then:

$ git clone https://github.com/stetre/moonsc
$ cd moonsc
moonsc$ make
moonsc$ sudo make install

Example

The example below defines a simple statechart and executes it.

Other examples can be found in the examples/ and the tests/ directories.

-- MoonSC 'Hello World' example - hello.lua
local moonsc = require("moonsc")

-- Import the element constructors:
moonsc.import_tags()

-- Define a statechart using them:
local helloworld = _scxml{ name="hello", initial="s1",
   _script{ text=[[print("Hello, World!")]] },
   _state{ id="s1",
      _onentry{ _log{ expr="'entering state s1'" } },
      _transition{ event="go", target="s2",
         _log{ expr=[["received event '".._event.name.."'"]] }
      },
      _onexit{ _log{ expr="'exiting state s1'" } },
   },
   _final{ id="s2",
      _onentry{ _log{ expr="'entering state s2'" } },
      _onexit{ _log{ expr="'exiting'" } },
   },
}

-- Set a callback to redirect <log> executions to stdout:
moonsc.set_log_callback(function(...) print(...) end)

-- Create a session with the statechart, start it, and send it an event:
moonsc.create("mysession", helloworld)
moonsc.start("mysession")
moonsc.send("mysession", "go")

-- Enter the main event loop:
while true do
   if not moonsc.trigger() then break end
end

The script can be executed at the shell prompt with the standard Lua interpreter:

$ lua hello.lua

See also