Created
August 1, 2014 03:23
-
-
Save leeonix/9569e66f1e9dcd79be8d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# coding: gbk | |
<%! | |
from textwrap import wrap | |
import re | |
%><% | |
def replacer(s): | |
try: | |
return '(event == %s)' % (machine.event(s.group()).name) | |
except KeyError: | |
return '%s(param)' % machine.input(s.group()).name | |
def parseCondition(c): | |
c = re.sub(r'\b(\w+)\b', replacer, c) | |
c = c.replace('&&', 'and') | |
c = c.replace('||', 'or') | |
c = c.replace('!', 'not ') | |
return c | |
def formatAction(a): | |
if a.type == 'output': | |
return '%s(param);' % a.name | |
else: | |
return '%s.process_event(%s, param);' % (machine.name, a.name,) | |
%> | |
-- State | |
% for s in machine.states: | |
local ${s.name} = ${loop.index + 1} | |
% endfor | |
-- Event | |
% for e in machine.events: | |
% for l in wrap(e.comment, 80): | |
-- ${l} | |
% endfor | |
local ${e.name} = ${loop.index + 1} | |
% endfor | |
local _state_name = { | |
% for e in machine.events: | |
"${e.name}", | |
% endfor | |
} | |
-- delegate declare | |
-- input | |
% for i in machine.inputs: | |
local ${i.name} | |
% endfor | |
-- output | |
% for o in machine.outputs: | |
local ${o.name} | |
% endfor | |
local _current_state = ${machine.initialState} | |
local _event_queue = {} | |
-- machine | |
local ${machine.name} = { | |
-- State | |
% for s in machine.states: | |
${s.name} = ${loop.index + 1}, | |
% endfor | |
-- Event | |
% for e in machine.events: | |
${e.name} = ${loop.index + 1}, | |
% endfor | |
} | |
function ${machine.name}.current_state() | |
return _current_state | |
end -- end function | |
function ${machine.name}.state_name(state) | |
return _state_name[state] | |
end -- end function | |
local state_func = { | |
% for s in machine.states: | |
-- case ${s.name} | |
function (event, param) | |
% for t in s.transitions: | |
-- ${t.name} | |
if ${parseCondition(t.condition)} then | |
% if t.destination: | |
% for a in s.exitingActions: | |
${formatAction(a)} | |
% endfor | |
% endif | |
% for a in t.actions: | |
${formatAction(a)} | |
% endfor | |
% if t.destination: | |
_current_state = ${t.destination.name}; | |
% for a in t.destination.incomeActions: | |
${formatAction(a)} | |
% endfor | |
% endif | |
return | |
end | |
% endfor | |
end, -- end function | |
% endfor | |
} | |
function ${machine.name}.process_event(event, param) | |
local count = #_event_queue; | |
table.insert(_event_queue, event) | |
table.insert(_event_queue, param) | |
if count ~= 0 then | |
return | |
end -- end if | |
for i = 1, #_event_queue, 2 do | |
state_func[_current_state](_event_queue[i], _event_queue[i + 1]) | |
end -- end for | |
_event_queue = {} | |
end -- end function | |
function ${machine.name}.set_delegate(d) | |
assert(d) | |
-- inputs | |
% for i in machine.inputs: | |
${i.name} = d.${i.name} | |
% endfor | |
-- outputs | |
% for o in machine.outputs: | |
${o.name} = d.${o.name} | |
% endfor | |
end -- end function | |
return ${machine.name} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment