FiniteStateMachine.jl icon indicating copy to clipboard operation
FiniteStateMachine.jl copied to clipboard

Use macros as a DSL to create state machine

Open IainNZ opened this issue 11 years ago • 5 comments

Just saw this on METADATA.jl, cool idea. It'd be need to use macros to make the state machine, something like:

@build_state_machine fsm
    "initial" => "first"
    "events" => [
       "name" => "hop"
...
    "final" => "fourth"
end

anything thats parseable by Julia will work, doesn't have to be valid Julia syntax. Just a thought :)

IainNZ avatar Aug 26 '14 06:08 IainNZ

Cool idea! I was just thinking how the Dict{String,String} and (String=>Any) syntax is a bit awkward, in the context of "just sketch a map of your FSM here". (Or, I could settle for Dict{Any,Any}, and gain a nice Python-style syntax...)

tinybike avatar Aug 26 '14 06:08 tinybike

Yeah why do you need the types?

IainNZ avatar Aug 26 '14 07:08 IainNZ

Or maybe deeper, why index by strings at all? It'd more Julia to use symbols, e.g.

julia> fsm = state_machine([
    :initial => :first,
    :final => :fourth,
    :events => [
        [
            :name => :hop,
            :from => :first,
            :to => :second,
        ],
        [
            :name => :skip,
            :from => :second,
            :to => :third,
        ],
        [
            :name => :jump,
            :from => :third,
            :to => :fourth,
        ],
    ],
    :callbacks => [
        :onbeforeevent => (fsm::StateMachine, args...) -> 10,
    ],
])

IainNZ avatar Aug 26 '14 07:08 IainNZ

With macros you could even get away without the :, I think... Check out https://github.com/JuliaOpt/JuMP.jl for inspiration, we kinda have a DSL going there

IainNZ avatar Aug 26 '14 07:08 IainNZ

I removed the type info for a nicer-looking syntax. Leaving this open for now though, as I do find the idea of a DSL to be very appealing, just don't quite have the focus to write it right now :)

tinybike avatar Aug 26 '14 09:08 tinybike