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
|
/**
* @file llstatemachine.cpp
* @brief LLStateMachine implementation file.
*
* $LicenseInfo:firstyear=2001&license=viewerlgpl$
* Second Life Viewer Source Code
* Copyright (C) 2010, Linden Research, Inc.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation;
* version 2.1 of the License only.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
* Linden Research, Inc., 945 Battery Street, San Francisco, CA 94111 USA
* $/LicenseInfo$
*/
#include "linden_common.h"
#include "llstatemachine.h"
#include "llapr.h"
#define FSM_PRINT_STATE_TRANSITIONS (0)
U32 LLUniqueID::sNextID = 0;
bool operator==(const LLUniqueID &a, const LLUniqueID &b)
{
return (a.mId == b.mId);
}
bool operator!=(const LLUniqueID &a, const LLUniqueID &b)
{
return (a.mId != b.mId);
}
//-----------------------------------------------------------------------------
// LLStateDiagram
//-----------------------------------------------------------------------------
LLStateDiagram::LLStateDiagram()
{
mDefaultState = NULL;
mUseDefaultState = false;
}
LLStateDiagram::~LLStateDiagram()
{
}
// add a state to the state graph
bool LLStateDiagram::addState(LLFSMState *state)
{
mStates[state] = Transitions();
return true;
}
// add a directed transition between 2 states
bool LLStateDiagram::addTransition(LLFSMState& start_state, LLFSMState& end_state, LLFSMTransition& transition)
{
StateMap::iterator state_it;
state_it = mStates.find(&start_state);
Transitions* state_transitions = NULL;
if (state_it == mStates.end() )
{
addState(&start_state);
state_transitions = &mStates[&start_state];
}
else
{
state_transitions = &state_it->second;
}
state_it = mStates.find(&end_state);
if (state_it == mStates.end() )
{
addState(&end_state);
}
Transitions::iterator transition_it = state_transitions->find(&transition);
if (transition_it != state_transitions->end())
{
LL_ERRS() << "LLStateTable::addDirectedTransition() : transition already exists" << LL_ENDL;
return false; // transition already exists
}
(*state_transitions)[&transition] = &end_state;
return true;
}
// add an undirected transition between 2 states
bool LLStateDiagram::addUndirectedTransition(LLFSMState& start_state, LLFSMState& end_state, LLFSMTransition& transition)
{
bool result;
result = addTransition(start_state, end_state, transition);
if (result)
{
result = addTransition(end_state, start_state, transition);
}
return result;
}
// add a transition that exists for every state
void LLStateDiagram::addDefaultTransition(LLFSMState& end_state, LLFSMTransition& transition)
{
mDefaultTransitions[&transition] = &end_state;
}
// process a possible transition, and get the resulting state
LLFSMState* LLStateDiagram::processTransition(LLFSMState& start_state, LLFSMTransition& transition)
{
// look up transition
//LLFSMState** dest_state = (mStates.getValue(&start_state))->getValue(&transition);
LLFSMState* dest_state = NULL;
StateMap::iterator state_it = mStates.find(&start_state);
if (state_it == mStates.end())
{
return NULL;
}
Transitions::iterator transition_it = state_it->second.find(&transition);
// try default transitions if state-specific transition not found
if (transition_it == state_it->second.end())
{
dest_state = mDefaultTransitions[&transition];
}
else
{
dest_state = transition_it->second;
}
// if we have a destination state...
if (NULL != dest_state)
{
// ...return it...
return dest_state;
}
// ... otherwise ...
else
{
// ...look for default state...
if (mUseDefaultState)
{
// ...return it if we have it...
return mDefaultState;
}
else
{
// ...or else we're still in the same state.
return &start_state;
}
}
}
void LLStateDiagram::setDefaultState(LLFSMState& default_state)
{
mUseDefaultState = true;
mDefaultState = &default_state;
}
S32 LLStateDiagram::numDeadendStates()
{
S32 numDeadends = 0;
for (StateMap::value_type& state_pair : mStates)
{
if (state_pair.second.size() == 0)
{
numDeadends++;
}
}
return numDeadends;
}
bool LLStateDiagram::stateIsValid(LLFSMState& state)
{
if (mStates.find(&state) != mStates.end())
{
return true;
}
return false;
}
LLFSMState* LLStateDiagram::getState(U32 state_id)
{
for (StateMap::value_type& state_pair : mStates)
{
if (state_pair.first->getID() == state_id)
{
return state_pair.first;
}
}
return NULL;
}
bool LLStateDiagram::saveDotFile(const std::string& filename)
{
LLAPRFile outfile ;
outfile.open(filename, LL_APR_W);
apr_file_t* dot_file = outfile.getFileHandle() ;
if (!dot_file)
{
LL_WARNS() << "LLStateDiagram::saveDotFile() : Couldn't open " << filename << " to save state diagram." << LL_ENDL;
return false;
}
apr_file_printf(dot_file, "digraph StateMachine {\n\tsize=\"100,100\";\n\tfontsize=40;\n\tlabel=\"Finite State Machine\";\n\torientation=landscape\n\tratio=.77\n");
for (StateMap::value_type& state_pair : mStates)
{
apr_file_printf(dot_file, "\t\"%s\" [fontsize=28,shape=box]\n", state_pair.first->getName().c_str());
}
apr_file_printf(dot_file, "\t\"All States\" [fontsize=30,style=bold,shape=box]\n");
for (Transitions::value_type& transition_pair : mDefaultTransitions)
{
apr_file_printf(dot_file, "\t\"All States\" -> \"%s\" [label = \"%s\",fontsize=24];\n", transition_pair.second->getName().c_str(),
transition_pair.second->getName().c_str());
}
if (mDefaultState)
{
apr_file_printf(dot_file, "\t\"All States\" -> \"%s\";\n", mDefaultState->getName().c_str());
}
for (StateMap::value_type& state_pair : mStates)
{
LLFSMState *state = state_pair.first;
for (Transitions::value_type& transition_pair : state_pair.second)
{
std::string state_name = state->getName();
std::string target_name = transition_pair.second->getName();
std::string transition_name = transition_pair.first->getName();
apr_file_printf(dot_file, "\t\"%s\" -> \"%s\" [label = \"%s\",fontsize=24];\n", state->getName().c_str(),
target_name.c_str(),
transition_name.c_str());
}
}
apr_file_printf(dot_file, "}\n");
return true;
}
std::ostream& operator<<(std::ostream &s, LLStateDiagram &FSM)
{
if (FSM.mDefaultState)
{
s << "Default State: " << FSM.mDefaultState->getName() << "\n";
}
for (LLStateDiagram::Transitions::value_type& transition_pair : FSM.mDefaultTransitions)
{
s << "Any State -- " << transition_pair.first->getName()
<< " --> " << transition_pair.second->getName() << "\n";
}
for (LLStateDiagram::StateMap::value_type& state_pair : FSM.mStates)
{
for (LLStateDiagram::Transitions::value_type& transition_pair : state_pair.second)
{
s << state_pair.first->getName() << " -- " << transition_pair.first->getName()
<< " --> " << transition_pair.second->getName() << "\n";
}
s << "\n";
}
return s;
}
//-----------------------------------------------------------------------------
// LLStateMachine
//-----------------------------------------------------------------------------
LLStateMachine::LLStateMachine()
{
// we haven't received a starting state yet
mCurrentState = NULL;
mLastState = NULL;
mLastTransition = NULL;
mStateDiagram = NULL;
}
LLStateMachine::~LLStateMachine()
{
}
// returns current state
LLFSMState* LLStateMachine::getCurrentState() const
{
return mCurrentState;
}
// executes current state
void LLStateMachine::runCurrentState(void *data)
{
mCurrentState->execute(data);
}
// set current state
bool LLStateMachine::setCurrentState(LLFSMState *initial_state, void* user_data, bool skip_entry)
{
llassert(mStateDiagram);
if (mStateDiagram->stateIsValid(*initial_state))
{
mLastState = mCurrentState = initial_state;
if (!skip_entry)
{
initial_state->onEntry(user_data);
}
return true;
}
return false;
}
bool LLStateMachine::setCurrentState(U32 state_id, void* user_data, bool skip_entry)
{
llassert(mStateDiagram);
LLFSMState* state = mStateDiagram->getState(state_id);
if (state)
{
mLastState = mCurrentState = state;
if (!skip_entry)
{
state->onEntry(user_data);
}
return true;
}
return false;
}
void LLStateMachine::processTransition(LLFSMTransition& transition, void* user_data)
{
llassert(mStateDiagram);
if (NULL == mCurrentState)
{
LL_WARNS() << "mCurrentState == NULL; aborting processTransition()" << LL_ENDL;
return;
}
LLFSMState* new_state = mStateDiagram->processTransition(*mCurrentState, transition);
if (NULL == new_state)
{
LL_WARNS() << "new_state == NULL; aborting processTransition()" << LL_ENDL;
return;
}
mLastTransition = &transition;
mLastState = mCurrentState;
if (*mCurrentState != *new_state)
{
mCurrentState->onExit(user_data);
mCurrentState = new_state;
mCurrentState->onEntry(user_data);
#if FSM_PRINT_STATE_TRANSITIONS
LL_INFOS() << "Entering state " << mCurrentState->getName() <<
" on transition " << transition.getName() << " from state " <<
mLastState->getName() << LL_ENDL;
#endif
}
}
void LLStateMachine::setStateDiagram(LLStateDiagram* diagram)
{
mStateDiagram = diagram;
}
|