Software /
code /
prosody
Annotate
util/fsm.lua @ 13107:9c4dc1e6d2c9
mod_http: Add way to retrieve internal URL instead of external
This could be of help when configuring reverse proxies, as it is the
internal URL the proxy must point at.
Argument treated as an enum "internal" "external"(default) to allow for
future extensibility.
author | Kim Alvefur <zash@zash.se> |
---|---|
date | Wed, 24 May 2023 14:43:45 +0200 |
parent | 13019:8a2f75e38eb2 |
child | 13165:9c13c11b199d |
rev | line source |
---|---|
13019
8a2f75e38eb2
util.fsm: New utility lib for finite state machines
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
1 local events = require "util.events"; |
8a2f75e38eb2
util.fsm: New utility lib for finite state machines
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
2 |
8a2f75e38eb2
util.fsm: New utility lib for finite state machines
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
3 local fsm_methods = {}; |
8a2f75e38eb2
util.fsm: New utility lib for finite state machines
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
4 local fsm_mt = { __index = fsm_methods }; |
8a2f75e38eb2
util.fsm: New utility lib for finite state machines
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
5 |
8a2f75e38eb2
util.fsm: New utility lib for finite state machines
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
6 local function is_fsm(o) |
8a2f75e38eb2
util.fsm: New utility lib for finite state machines
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
7 local mt = getmetatable(o); |
8a2f75e38eb2
util.fsm: New utility lib for finite state machines
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
8 return mt == fsm_mt; |
8a2f75e38eb2
util.fsm: New utility lib for finite state machines
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
9 end |
8a2f75e38eb2
util.fsm: New utility lib for finite state machines
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
10 |
8a2f75e38eb2
util.fsm: New utility lib for finite state machines
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
11 local function notify_transition(fire_event, transition_event) |
8a2f75e38eb2
util.fsm: New utility lib for finite state machines
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
12 local ret; |
8a2f75e38eb2
util.fsm: New utility lib for finite state machines
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
13 ret = fire_event("transition", transition_event); |
8a2f75e38eb2
util.fsm: New utility lib for finite state machines
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
14 if ret ~= nil then return ret; end |
8a2f75e38eb2
util.fsm: New utility lib for finite state machines
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
15 if transition_event.from ~= transition_event.to then |
8a2f75e38eb2
util.fsm: New utility lib for finite state machines
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
16 ret = fire_event("leave/"..transition_event.from, transition_event); |
8a2f75e38eb2
util.fsm: New utility lib for finite state machines
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
17 if ret ~= nil then return ret; end |
8a2f75e38eb2
util.fsm: New utility lib for finite state machines
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
18 end |
8a2f75e38eb2
util.fsm: New utility lib for finite state machines
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
19 ret = fire_event("transition/"..transition_event.name, transition_event); |
8a2f75e38eb2
util.fsm: New utility lib for finite state machines
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
20 if ret ~= nil then return ret; end |
8a2f75e38eb2
util.fsm: New utility lib for finite state machines
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
21 end |
8a2f75e38eb2
util.fsm: New utility lib for finite state machines
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
22 |
8a2f75e38eb2
util.fsm: New utility lib for finite state machines
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
23 local function notify_transitioned(fire_event, transition_event) |
8a2f75e38eb2
util.fsm: New utility lib for finite state machines
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
24 if transition_event.to ~= transition_event.from then |
8a2f75e38eb2
util.fsm: New utility lib for finite state machines
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
25 fire_event("enter/"..transition_event.to, transition_event); |
8a2f75e38eb2
util.fsm: New utility lib for finite state machines
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
26 end |
8a2f75e38eb2
util.fsm: New utility lib for finite state machines
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
27 if transition_event.name then |
8a2f75e38eb2
util.fsm: New utility lib for finite state machines
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
28 fire_event("transitioned/"..transition_event.name, transition_event); |
8a2f75e38eb2
util.fsm: New utility lib for finite state machines
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
29 end |
8a2f75e38eb2
util.fsm: New utility lib for finite state machines
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
30 fire_event("transitioned", transition_event); |
8a2f75e38eb2
util.fsm: New utility lib for finite state machines
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
31 end |
8a2f75e38eb2
util.fsm: New utility lib for finite state machines
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
32 |
8a2f75e38eb2
util.fsm: New utility lib for finite state machines
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
33 local function do_transition(name) |
8a2f75e38eb2
util.fsm: New utility lib for finite state machines
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
34 return function (self, attr) |
8a2f75e38eb2
util.fsm: New utility lib for finite state machines
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
35 local new_state = self.fsm.states[self.state][name] or self.fsm.states["*"][name]; |
8a2f75e38eb2
util.fsm: New utility lib for finite state machines
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
36 if not new_state then |
8a2f75e38eb2
util.fsm: New utility lib for finite state machines
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
37 return error(("Invalid state transition: %s cannot %s"):format(self.state, name)); |
8a2f75e38eb2
util.fsm: New utility lib for finite state machines
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
38 end |
8a2f75e38eb2
util.fsm: New utility lib for finite state machines
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
39 |
8a2f75e38eb2
util.fsm: New utility lib for finite state machines
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
40 local transition_event = { |
8a2f75e38eb2
util.fsm: New utility lib for finite state machines
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
41 instance = self; |
8a2f75e38eb2
util.fsm: New utility lib for finite state machines
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
42 |
8a2f75e38eb2
util.fsm: New utility lib for finite state machines
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
43 name = name; |
8a2f75e38eb2
util.fsm: New utility lib for finite state machines
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
44 to = new_state; |
8a2f75e38eb2
util.fsm: New utility lib for finite state machines
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
45 to_attr = attr; |
8a2f75e38eb2
util.fsm: New utility lib for finite state machines
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
46 |
8a2f75e38eb2
util.fsm: New utility lib for finite state machines
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
47 from = self.state; |
8a2f75e38eb2
util.fsm: New utility lib for finite state machines
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
48 from_attr = self.state_attr; |
8a2f75e38eb2
util.fsm: New utility lib for finite state machines
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
49 }; |
8a2f75e38eb2
util.fsm: New utility lib for finite state machines
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
50 |
8a2f75e38eb2
util.fsm: New utility lib for finite state machines
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
51 local fire_event = self.fsm.events.fire_event; |
8a2f75e38eb2
util.fsm: New utility lib for finite state machines
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
52 local ret = notify_transition(fire_event, transition_event); |
8a2f75e38eb2
util.fsm: New utility lib for finite state machines
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
53 if ret ~= nil then return nil, ret; end |
8a2f75e38eb2
util.fsm: New utility lib for finite state machines
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
54 |
8a2f75e38eb2
util.fsm: New utility lib for finite state machines
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
55 self.state = new_state; |
8a2f75e38eb2
util.fsm: New utility lib for finite state machines
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
56 self.state_attr = attr; |
8a2f75e38eb2
util.fsm: New utility lib for finite state machines
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
57 |
8a2f75e38eb2
util.fsm: New utility lib for finite state machines
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
58 notify_transitioned(fire_event, transition_event); |
8a2f75e38eb2
util.fsm: New utility lib for finite state machines
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
59 return true; |
8a2f75e38eb2
util.fsm: New utility lib for finite state machines
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
60 end; |
8a2f75e38eb2
util.fsm: New utility lib for finite state machines
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
61 end |
8a2f75e38eb2
util.fsm: New utility lib for finite state machines
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
62 |
8a2f75e38eb2
util.fsm: New utility lib for finite state machines
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
63 local function new(desc) |
8a2f75e38eb2
util.fsm: New utility lib for finite state machines
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
64 local self = setmetatable({ |
8a2f75e38eb2
util.fsm: New utility lib for finite state machines
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
65 default_state = desc.default_state; |
8a2f75e38eb2
util.fsm: New utility lib for finite state machines
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
66 events = events.new(); |
8a2f75e38eb2
util.fsm: New utility lib for finite state machines
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
67 }, fsm_mt); |
8a2f75e38eb2
util.fsm: New utility lib for finite state machines
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
68 |
8a2f75e38eb2
util.fsm: New utility lib for finite state machines
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
69 -- states[state_name][transition_name] = new_state_name |
8a2f75e38eb2
util.fsm: New utility lib for finite state machines
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
70 local states = { ["*"] = {} }; |
8a2f75e38eb2
util.fsm: New utility lib for finite state machines
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
71 if desc.default_state then |
8a2f75e38eb2
util.fsm: New utility lib for finite state machines
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
72 states[desc.default_state] = {}; |
8a2f75e38eb2
util.fsm: New utility lib for finite state machines
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
73 end |
8a2f75e38eb2
util.fsm: New utility lib for finite state machines
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
74 self.states = states; |
8a2f75e38eb2
util.fsm: New utility lib for finite state machines
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
75 |
8a2f75e38eb2
util.fsm: New utility lib for finite state machines
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
76 local instance_methods = {}; |
8a2f75e38eb2
util.fsm: New utility lib for finite state machines
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
77 self._instance_mt = { __index = instance_methods }; |
8a2f75e38eb2
util.fsm: New utility lib for finite state machines
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
78 |
8a2f75e38eb2
util.fsm: New utility lib for finite state machines
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
79 for _, transition in ipairs(desc.transitions or {}) do |
8a2f75e38eb2
util.fsm: New utility lib for finite state machines
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
80 local from_states = transition.from; |
8a2f75e38eb2
util.fsm: New utility lib for finite state machines
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
81 if type(from_states) ~= "table" then |
8a2f75e38eb2
util.fsm: New utility lib for finite state machines
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
82 from_states = { from_states }; |
8a2f75e38eb2
util.fsm: New utility lib for finite state machines
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
83 end |
8a2f75e38eb2
util.fsm: New utility lib for finite state machines
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
84 for _, from in ipairs(from_states) do |
8a2f75e38eb2
util.fsm: New utility lib for finite state machines
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
85 if not states[from] then |
8a2f75e38eb2
util.fsm: New utility lib for finite state machines
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
86 states[from] = {}; |
8a2f75e38eb2
util.fsm: New utility lib for finite state machines
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
87 end |
8a2f75e38eb2
util.fsm: New utility lib for finite state machines
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
88 if not states[transition.to] then |
8a2f75e38eb2
util.fsm: New utility lib for finite state machines
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
89 states[transition.to] = {}; |
8a2f75e38eb2
util.fsm: New utility lib for finite state machines
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
90 end |
8a2f75e38eb2
util.fsm: New utility lib for finite state machines
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
91 if states[from][transition.name] then |
8a2f75e38eb2
util.fsm: New utility lib for finite state machines
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
92 return error(("Duplicate transition in FSM specification: %s from %s"):format(transition.name, from)); |
8a2f75e38eb2
util.fsm: New utility lib for finite state machines
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
93 end |
8a2f75e38eb2
util.fsm: New utility lib for finite state machines
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
94 states[from][transition.name] = transition.to; |
8a2f75e38eb2
util.fsm: New utility lib for finite state machines
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
95 end |
8a2f75e38eb2
util.fsm: New utility lib for finite state machines
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
96 |
8a2f75e38eb2
util.fsm: New utility lib for finite state machines
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
97 -- Add public method to trigger this transition |
8a2f75e38eb2
util.fsm: New utility lib for finite state machines
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
98 instance_methods[transition.name] = do_transition(transition.name); |
8a2f75e38eb2
util.fsm: New utility lib for finite state machines
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
99 end |
8a2f75e38eb2
util.fsm: New utility lib for finite state machines
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
100 |
8a2f75e38eb2
util.fsm: New utility lib for finite state machines
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
101 if desc.state_handlers then |
8a2f75e38eb2
util.fsm: New utility lib for finite state machines
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
102 for state_name, handler in pairs(desc.state_handlers) do |
8a2f75e38eb2
util.fsm: New utility lib for finite state machines
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
103 self.events.add_handler("enter/"..state_name, handler); |
8a2f75e38eb2
util.fsm: New utility lib for finite state machines
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
104 end |
8a2f75e38eb2
util.fsm: New utility lib for finite state machines
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
105 end |
8a2f75e38eb2
util.fsm: New utility lib for finite state machines
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
106 |
8a2f75e38eb2
util.fsm: New utility lib for finite state machines
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
107 if desc.transition_handlers then |
8a2f75e38eb2
util.fsm: New utility lib for finite state machines
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
108 for transition_name, handler in pairs(desc.transition_handlers) do |
8a2f75e38eb2
util.fsm: New utility lib for finite state machines
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
109 self.events.add_handler("transition/"..transition_name, handler); |
8a2f75e38eb2
util.fsm: New utility lib for finite state machines
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
110 end |
8a2f75e38eb2
util.fsm: New utility lib for finite state machines
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
111 end |
8a2f75e38eb2
util.fsm: New utility lib for finite state machines
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
112 |
8a2f75e38eb2
util.fsm: New utility lib for finite state machines
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
113 if desc.handlers then |
8a2f75e38eb2
util.fsm: New utility lib for finite state machines
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
114 self.events.add_handlers(desc.handlers); |
8a2f75e38eb2
util.fsm: New utility lib for finite state machines
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
115 end |
8a2f75e38eb2
util.fsm: New utility lib for finite state machines
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
116 |
8a2f75e38eb2
util.fsm: New utility lib for finite state machines
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
117 return self; |
8a2f75e38eb2
util.fsm: New utility lib for finite state machines
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
118 end |
8a2f75e38eb2
util.fsm: New utility lib for finite state machines
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
119 |
8a2f75e38eb2
util.fsm: New utility lib for finite state machines
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
120 function fsm_methods:init(state_name, state_attr) |
8a2f75e38eb2
util.fsm: New utility lib for finite state machines
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
121 local initial_state = assert(state_name or self.default_state, "no initial state specified"); |
8a2f75e38eb2
util.fsm: New utility lib for finite state machines
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
122 if not self.states[initial_state] then |
8a2f75e38eb2
util.fsm: New utility lib for finite state machines
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
123 return error("Invalid initial state: "..initial_state); |
8a2f75e38eb2
util.fsm: New utility lib for finite state machines
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
124 end |
8a2f75e38eb2
util.fsm: New utility lib for finite state machines
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
125 local instance = setmetatable({ |
8a2f75e38eb2
util.fsm: New utility lib for finite state machines
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
126 fsm = self; |
8a2f75e38eb2
util.fsm: New utility lib for finite state machines
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
127 state = initial_state; |
8a2f75e38eb2
util.fsm: New utility lib for finite state machines
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
128 state_attr = state_attr; |
8a2f75e38eb2
util.fsm: New utility lib for finite state machines
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
129 }, self._instance_mt); |
8a2f75e38eb2
util.fsm: New utility lib for finite state machines
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
130 |
8a2f75e38eb2
util.fsm: New utility lib for finite state machines
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
131 if initial_state ~= self.default_state then |
8a2f75e38eb2
util.fsm: New utility lib for finite state machines
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
132 local fire_event = self.events.fire_event; |
8a2f75e38eb2
util.fsm: New utility lib for finite state machines
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
133 notify_transitioned(fire_event, { |
8a2f75e38eb2
util.fsm: New utility lib for finite state machines
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
134 instance = instance; |
8a2f75e38eb2
util.fsm: New utility lib for finite state machines
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
135 |
8a2f75e38eb2
util.fsm: New utility lib for finite state machines
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
136 to = initial_state; |
8a2f75e38eb2
util.fsm: New utility lib for finite state machines
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
137 to_attr = state_attr; |
8a2f75e38eb2
util.fsm: New utility lib for finite state machines
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
138 |
8a2f75e38eb2
util.fsm: New utility lib for finite state machines
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
139 from = self.default_state; |
8a2f75e38eb2
util.fsm: New utility lib for finite state machines
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
140 }); |
8a2f75e38eb2
util.fsm: New utility lib for finite state machines
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
141 end |
8a2f75e38eb2
util.fsm: New utility lib for finite state machines
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
142 |
8a2f75e38eb2
util.fsm: New utility lib for finite state machines
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
143 return instance; |
8a2f75e38eb2
util.fsm: New utility lib for finite state machines
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
144 end |
8a2f75e38eb2
util.fsm: New utility lib for finite state machines
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
145 |
8a2f75e38eb2
util.fsm: New utility lib for finite state machines
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
146 function fsm_methods:is_instance(o) |
8a2f75e38eb2
util.fsm: New utility lib for finite state machines
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
147 local mt = getmetatable(o); |
8a2f75e38eb2
util.fsm: New utility lib for finite state machines
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
148 return mt == self._instance_mt; |
8a2f75e38eb2
util.fsm: New utility lib for finite state machines
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
149 end |
8a2f75e38eb2
util.fsm: New utility lib for finite state machines
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
150 |
8a2f75e38eb2
util.fsm: New utility lib for finite state machines
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
151 return { |
8a2f75e38eb2
util.fsm: New utility lib for finite state machines
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
152 new = new; |
8a2f75e38eb2
util.fsm: New utility lib for finite state machines
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
153 is_fsm = is_fsm; |
8a2f75e38eb2
util.fsm: New utility lib for finite state machines
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
154 }; |