-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathahsm.lua
418 lines (371 loc) · 13.4 KB
/
ahsm.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
--- ahsm Hierarchical State Machine.
-- ahsm is a very small implementation of Hierararchical State Machines,
-- also known as Statecharts. It's written in Lua, with no external
-- dependencies, and in a single file. Can be run on platforms as small as
-- a microcontroler.
-- @module ahsm
-- @usage local ahsm = require 'ahsm'
-- @alias M
local pairs, ipairs, type, rawset, rawget, tonumber = pairs, ipairs, type, rawset, rawget, tonumber
local math_huge = math.huge
local M = {}
local EV_ANY = {}
local EV_TIMEOUT = {_name='EV_TIMEOUT'}
local function init ( composite )
--initialize debug name for states and events
if M.debug then
for ne, e in pairs(composite.events or {}) do
M.debug('event', e, ne)
end
for ns, s in pairs(composite.states) do
s.container = composite
M.debug('state', s, ns)
end
for nt, t in pairs(composite.transitions) do
M.debug('trans', t, nt)
end
end
for _, s in pairs(composite.states) do
s.container = composite
for nt, t in pairs(composite.transitions or {}) do
if t.src == s then
for _, e in pairs(t.events or {}) do
if M.debug then
M.debug('trsel', s, t, e)
end
s.out_trans[e] = s.out_trans[e] or {}
s.out_trans[e][t] = true
end
end
end
if s.states then init( s ) end --recursion
end
end
--- Function used to get current time.
-- Replace with whatever your app uses. Must return a number. This is used when
-- computing timeouts for transitions.
-- Defaults to os.time.
-- @function get_time
M.get_time = os.time
--- Initialize a state.
-- Converts a state specfification into a state.
-- The state has a EV\_DONE field which is an event triggered on state
-- completion.
-- @param state_s state specificatios (see @{state_s}).
-- @return the initilized state
M.state = function (state_s)
state_s = state_s or {}
state_s.EV_DONE = {} --singleton, trigered on state completion
state_s.out_trans = {}
return state_s
end
--- Debug print function.
-- If provided, this function will be called to print debug information.
-- It must be set before calling @{init}. The debug will try to get friendly
-- names for events, transitions and states from the exported names (see `states`,
-- `transitions` and `events` fields from @{state_s}), or a `_name` field.
-- @usage ahsm.debug = print
M.debug = nil
-- metatable to maintain state.out_trans structure for transition timeouts
local to_key = {}
local mt_transition = {
__index = function (t, k)
if k=='timeout' then
return rawget(t, to_key)
else
return rawget(t, k)
end
end,
__newindex = function(t, k, v)
if k=='timeout' then
local src_out_trans = t.src.out_trans
local number_v = tonumber(v)
if number_v then -- add a timeout
if M.debug then M.debug('sched', t, v) end
src_out_trans[EV_TIMEOUT] = src_out_trans[EV_TIMEOUT] or {}
src_out_trans[EV_TIMEOUT][t] = true
elseif src_out_trans[EV_TIMEOUT] then -- remove a timeout
if M.debug then M.debug('sched', t, 'unset') end
src_out_trans[EV_TIMEOUT][t] = nil
end
rawset(t, to_key, number_v)
else
rawset(t, k, v)
end
end
}
local mt_state_gc = {
__gc = function (s)
if s.exit then s.exit(s) end
end
}
--- Initialize a transition.
-- Converts a transition specification into a transition table.
-- @param transition_s transition specificatios (see @{transition_s}).
-- @return the initilized transition
M.transition = function (transition_s)
transition_s = transition_s or {}
assert(transition_s.src, 'missing source state in transition')
assert(transition_s.tgt, 'missing target state in transition')
local timeout = transition_s.timeout
transition_s.timeout = nil
setmetatable(transition_s, mt_transition)
transition_s.timeout = timeout
return transition_s
end
--- When used in the @{transition_s}`.events` field will match any event.
M.EV_ANY = EV_ANY --singleton, event matches any event
--- Event reported to @{transition_s}`.effect` when a transition is made due
-- to a timeout.
M.EV_TIMEOUT = EV_TIMEOUT
--- Create a hsm.
-- Constructs and initializes an hsm from a root state.
-- @param root the root state, must be a composite.
-- @return initialized hsm
M.init = function ( root )
local hsm = {
--- Callback for pulling events.
-- If provided, this function will be called from inside the `step` call
-- so new events can be added.
-- @param evqueue an array where new events can be added.
-- @function hsm.get_events
get_events = nil, --function (evqueue) end,
root = root,
}
root.container = {_name='.'} -- fake container for root state
if M.debug then M.debug('state', root, '') end
init( root )
if root.exit then -- use gc to trigger exit() function on root state
setmetatable(root, mt_state_gc)
end
local evqueue = { n=0 } -- array, will hold events for step() to process
local current_states = {} -- states being active
local active_trans = {} --must be balanced (enter and leave step() empty)
local function enter_state (hsm, s, now)
if s.entry then s.entry(s) end
s.container.current_substate = s
s.done = nil
current_states[s] = true
local out_trans_timeout = s.out_trans[EV_TIMEOUT]
if out_trans_timeout then
local timeout, t = math_huge, nil
for tt in pairs(out_trans_timeout) do
local t_timeout = tt.timeout
if t_timeout<timeout then timeout, t = t_timeout, tt end
end
--if M.debug then M.debug('sched', now, now + timeout, s, t) end
s.expiration = now + timeout
end
if s.initial then
if M.debug then M.debug('init', s.initial) end
enter_state(hsm, s.initial, now) -- recurse into embedded hsm
end
end
local function exit_state (hsm, s)
if s.exit then s.exit(s) end
current_states[s] = nil
if s.current_substate then
exit_state (hsm, s.current_substate) --FIXME call or not call?
end
end
enter_state (hsm, root, M.get_time()) -- activate root state
local stepping = false -- do not allow recursion whil stepping
local function step ()
if stepping then return true end
stepping = true
local next_expiration = math_huge
local now = M.get_time()
--queue new events
if hsm.get_events then
hsm.get_events( evqueue )
end
--find active transitions
for s, _ in pairs( current_states ) do
local transited = false
-- check for matching transitions for events
--for _, e in ipairs(evqueue) do
for i = 1, evqueue.n do
local e=evqueue[i]
local out_trans_e = s.out_trans[e]
if out_trans_e then
for t in pairs(out_trans_e) do -- search through transitions on event
if t.guard==nil or t.guard(e) then --TODO pcall?
transited = true
active_trans[t] = e
break
end
end
if transited then break end
end
end
--check if event is * and there is anything queued
if not transited then -- priority down if already found listed event
local out_trans_any = s.out_trans[EV_ANY]
local e = evqueue[1]
if e~=nil and out_trans_any then
for t in pairs(out_trans_any) do
if t.guard==nil or t.guard(e) then
transited = true
active_trans[t] = e
break
end
end
end
end
--check timeouts
if not transited then
local out_trans_timeout = s.out_trans[EV_TIMEOUT]
if out_trans_timeout then
for t in pairs(out_trans_timeout) do -- search through transitions on tmout
local expiration = s.expiration
if now>=expiration then
if (t.guard==nil or t.guard(EV_TIMEOUT)) then
transited = true
--active_trans[s.out_trans[EV_TIMEOUT]] = EV_TIMEOUT
active_trans[t] = EV_TIMEOUT
end
else
if expiration<next_expiration then
next_expiration = expiration
end
end
end
end
end
end
-- purge current events
for i=1, evqueue.n do
rawset(evqueue, i, nil)
end
evqueue.n = 0
local idle = true
--call leave_state, traverse transition, and enter_state
for t, e in pairs(active_trans) do
if current_states[t.src] then --src state could've been left
if M.debug then
M.debug('step', t, e)
end
idle = false
exit_state(hsm, t.src)
if t.effect then t.effect(e) end --FIXME pcall
enter_state(hsm, t.tgt, now)
end
active_trans[t] = nil
end
--call doo on active_states
for s, _ in pairs(current_states) do
if not s.done then
if type(s.doo)=='nil' then
evqueue.n = evqueue.n + 1
evqueue[evqueue.n] = s.EV_DONE
s.done = true
idle = false -- let step again for new event
elseif type(s.doo)=='function' then
local poll_flag = s.doo(s) --TODO pcall
if not poll_flag then
evqueue.n = evqueue.n + 1
evqueue[evqueue.n] = s.EV_DONE
s.done = true
idle = false -- let step again for new EV_DONE event
end
end
end
end
stepping = false
if next_expiration==math_huge then
return idle, nil
else
return idle, next_expiration
end
end
--- Push new event to the hsm.
-- The event will be queued, and then the machine will be looped using
-- @{loop}.
-- BEWARE: if this is called from a callback from C side, will cause a core
-- panic. In this scenario you must use @{queue_event}.
-- @param ev an event. Can be of any type except nil.
hsm.send_event = function (ev)
evqueue.n = evqueue.n + 1
evqueue[evqueue.n] = ev
hsm.loop()
end
--- Queue new event to the hsm.
-- The queued messages will be processed when machine is stepped using
-- @{step} or @{loop}. Also, see @{send_event}
-- @param ev an event. Can be of any type except nil.
hsm.queue_event = function (ev)
evqueue.n = evqueue.n + 1
evqueue[evqueue.n] = ev
end
--- Step trough the hsm.
-- A single step will consume all pending events, and do a round evaluating
-- available doo() functions on all active states. This call finishes as soon
-- as the cycle count is reached or the hsm becomes idle.
-- @param count maximum number of cycles to perform. Defaults to 1
-- @return the idle status, and the next impending expiration time if
-- available. Being idle means that all events have been consumed and no
-- doo() function is pending to be run. The expiration time indicates there
-- is a transition with timeout waiting.
hsm.step = function ( count )
count = count or 1
for i=1, count do
local idle, expiration = step()
if idle then return true, expiration end
end
return false
end
--- Loop trough the hsm.
-- Will step the machine until it becomes idle. When this call returns means
-- there's no actions to be taken immediatelly.
-- @return If available, the time of the closests pending timeout
-- on a transition
hsm.loop = function ()
local idle, expiration
repeat
idle, expiration = step()
until idle
return expiration
end
return hsm
end
--- Data structures.
-- Main structures used to describe a hsm.
-- @section structures
------
-- State specification.
-- A state can be either leaf or composite. A composite state has a hsm
-- embedded, defined by the `states`, `transitions` and `initial` fields. When a
-- compodite state is activated the embedded hsm is started from the `initial`
-- state. The activity of a state must be provided in the `entry`, `exit` and `doo`
-- fields.
-- @field entry an optional function to be called on entering the state.
-- @field exit an optional function to be called on leaving the state.
-- @field doo an optional function that will be called when the state is
-- active. If this function returns true, it will be polled again. If
-- returns false, it is considered as completed.
-- @field EV_DONE This field is created when calling @{state}, and is an
-- event emitted when the `doo()` function is completed, or immediatelly if
-- no `doo()` function is provided.
-- @field states When the state is a composite this table's values are the
-- states of the embedded hsm. Keys can be used to provide a name.
-- @field transitions When the state is a composite this table's values are
-- the transitions of the embedded hsm. Keys can be used to provide a name.
-- @field initial This is the initial state of the embedded.
-- @table state_s
------
-- Transition specification.
-- @field src source state.
-- @field dst destination state.
-- @field events table where the values are the events that trigger the
-- transition. Can be supressed by the guard function
-- @field guard if provided, when the transition is triggered this function
-- will be evaluated with the event as parameter. If returns a true value
-- the transition is made.
-- @field effect this funcion of transition traversal, with the triggering
-- event as parameter.
-- @field timeout If provided, this number is used as timeout for time
-- traversal. After timeout time units spent in the souce state the transition
-- will be triggered with the @{EV_TIMEOUT} event as parameter. Uses the
-- @{get_time} to read the system's time.
-- @table transition_s
return M