Menge
Modular Pedestrian Simulation Framework for Research and Development
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
FSM.h
Go to the documentation of this file.
1 /*
2 
3 License
4 
5 Menge
6 Copyright and trademark 2012-14 University of North Carolina at Chapel Hill.
7 All rights reserved.
8 
9 Permission to use, copy, modify, and distribute this software and its documentation
10 for educational, research, and non-profit purposes, without fee, and without a
11 written agreement is hereby granted, provided that the above copyright notice,
12 this paragraph, and the following four paragraphs appear in all copies.
13 
14 This software program and documentation are copyrighted by the University of North
15 Carolina at Chapel Hill. The software program and documentation are supplied "as is,"
16 without any accompanying services from the University of North Carolina at Chapel
17 Hill or the authors. The University of North Carolina at Chapel Hill and the
18 authors do not warrant that the operation of the program will be uninterrupted
19 or error-free. The end-user understands that the program was developed for research
20 purposes and is advised not to rely exclusively on the program for any reason.
21 
22 IN NO EVENT SHALL THE UNIVERSITY OF NORTH CAROLINA AT CHAPEL HILL OR THE AUTHORS
23 BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL
24 DAMAGES, INCLUDING LOST PROFITS, ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS
25 DOCUMENTATION, EVEN IF THE UNIVERSITY OF NORTH CAROLINA AT CHAPEL HILL OR THE
26 AUTHORS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 
28 THE UNIVERSITY OF NORTH CAROLINA AT CHAPEL HILL AND THE AUTHORS SPECIFICALLY
29 DISCLAIM ANY WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
30 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE AND ANY STATUTORY WARRANTY
31 OF NON-INFRINGEMENT. THE SOFTWARE PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND
32 THE UNIVERSITY OF NORTH CAROLINA AT CHAPEL HILL AND THE AUTHORS HAVE NO OBLIGATIONS
33 TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
34 
35 Any questions or comments should be sent to the authors {menge,geom}@cs.unc.edu
36 
37 */
38 
44 #ifndef __FSM_H__
45 #define __FSM_H__
46 
47 // Finite-state machine used to compute preferred velocity
48 // according to varying conditions
49 
50 #include "fsmCommon.h"
51 #include "FSMDescrip.h"
52 #include "MengeException.h"
53 
54 #include <vector>
55 #include <cassert>
56 #include <map>
57 #ifndef _MSC_VER
58 #include <string.h>
59 #endif
60 
61 
62 namespace Menge {
63  // forward declaration
64  namespace Agents {
65  class SimulatorInterface;
66  class BaseAgent;
67  }
68 
69  namespace BFSM {
70 
71  // Forward declaration
72  class FsmContext;
73  class State;
74  class Transition;
75  class Goal;
76  class GoalSet;
77  class Task;
78  class FSMDescrip;
79 
81 
85  class FSMException : public virtual MengeException {
86  public:
91 
97  FSMException( const std::string & s ): MengeException(s) {}
98  };
99 
101 
107  public:
112 
118  FSMFatalException( const std::string & s ): MengeException(s), FSMException(), MengeFatalException() {}
119  };
120 
122 
126  class FSM {
127  public:
134 
138  ~FSM();
139 
143  void collectTasks();
144 
156  void addTask( Task * task );
157 
164  void setAgentCount( size_t count );
165 
172  void advance( Agents::BaseAgent * agent );
173 
178  void computePrefVelocity( Agents::BaseAgent * agent );
179 
186  State * getNode( size_t id ) { return _nodes[id]; }
187 
195  State * getNode( const std::string & name );
196 
202  size_t getNodeCount() const { return _nodes.size(); }
203 
210  size_t addNode( State * node );
211 
222  bool addTransition( size_t fromNode, Transition * t );
223 
232  bool addGoal( size_t goalSet, size_t goalID, Goal * goal );
233 
242  const Goal * getGoal( size_t goalSet, size_t goalID );
243 
251  const GoalSet * getGoalSet( size_t goalSetID );
252 
259  bool doStep();
260 
268  void setCurrentState( Agents::BaseAgent * agt, size_t currNode );
269 
276  State * getCurrentState( const Agents::BaseAgent * agt ) const;
277 
284  size_t getAgentStateID( const Agents::BaseAgent * agent ) const;
285 
292  size_t getAgentStateID( size_t agentID ) const;
293 
299  bool allFinal() const;
300 
304  inline const Agents::SimulatorInterface * getSimulator() const { return _sim; }
305 
309  void doTasks();
310 
314  size_t getTaskCount() const { return _tasks.size(); }
315 
319  void finalize();
320 
331 
337  std::map< size_t, GoalSet * > & getGoalSets() { return _goalSets; }
338 
339  friend FSM * buildFSM( FSMDescrip & fsmDescrip, Agents::SimulatorInterface * sim, bool VERBOSE );
340 
346  void addVelModifier( VelModifier * v ) { _velModifiers.push_back( v ); }
347 
348  protected:
353 
359  size_t _agtCount;
360 
365 
369  std::vector< State * > _nodes;
370 
374  std::vector< Task * > _tasks;
375 
379  std::map< size_t, GoalSet * > _goalSets;
380 
384  std::vector< VelModifier * > _velModifiers;
385 
386  };
387 
389 
401  FSM * buildFSM( FSMDescrip & fsmDescrip, Agents::SimulatorInterface * sim, bool VERBOSE=false );
402 
403  } // namespace BFSM
404 } // namespace Menge
405 #endif // __FSM_H__
The transition between BFSM states.
Definition: Transition.h:67
The basic state of the behavior finite state machine.
Definition: State.h:123
The full description of the behavioral finite state machine.
Definition: FSMDescrip.h:84
State * getNode(size_t id)
Get the pointer for the node with the given identifier.
Definition: FSM.h:186
The core namespace. All elements of Menge are contained in this namespace.
Definition: AgentGenerator.cpp:43
void finalize()
Finalize the FSM.
Definition: FSM.cpp:299
void collectTasks()
Collects all tasks from the FSM elements.
Definition: FSM.cpp:85
const Agents::SimulatorInterface * getSimulator() const
Retrieve the simulator.
Definition: FSM.h:304
std::vector< VelModifier * > _velModifiers
A list of velocity modifiers to be applied to all states in the simulator.
Definition: FSM.h:384
void computePrefVelocity(Agents::BaseAgent *agent)
Computes the preferred velocity for the given agent based on the FSM's record of which state the agen...
Definition: FSM.cpp:140
FSMException(const std::string &s)
Constructor with message.
Definition: FSM.h:97
A set of goals. Maps integer identifiers to instances of goals.
Definition: GoalSet.h:63
The base, abstract class defining goals.
Definition: Goal.h:110
std::map< size_t, GoalSet * > _goalSets
Mapping from goal set identifier to GoalSet.
Definition: FSM.h:379
size_t getNodeCount() const
Reports the number of states in the FSM.
Definition: FSM.h:202
bool doStep()
Update the fsm state by one time step.
Definition: FSM.cpp:258
const Goal * getGoal(size_t goalSet, size_t goalID)
Retrieves the given goal from the given goal set.
Definition: FSM.cpp:205
FSM(Agents::SimulatorInterface *sim)
Constructor.
Definition: FSM.cpp:62
Exception thrown when the FSM has an error which cannot be recovered from.
Definition: FSM.h:106
~FSM()
Destructor.
Definition: FSM.cpp:68
The base definition for exceptions in Menge.
size_t getTaskCount() const
Returns the number of registered tasks.
Definition: FSM.h:314
The basic simulator interface required by the fsm.
Definition: SimulatorInterface.h:66
bool addGoal(size_t goalSet, size_t goalID, Goal *goal)
Adds a goal to the indicated goal set.
Definition: FSM.cpp:196
void setAgentCount(size_t count)
Initializes the memory required for the number of agents included in the FSm.
Definition: FSM.cpp:117
void addTask(Task *task)
Adds a task to the set.
Definition: FSM.cpp:103
Generic base class for FSM exceptions.
Definition: FSM.h:85
std::vector< State * > _nodes
The states in the BFSM.
Definition: FSM.h:369
Base exception class for menge operations.
Definition: MengeException.h:58
FSMFatalException(const std::string &s)
Constructor with message.
Definition: FSM.h:118
Base context for finite state machine elements.
Definition: FsmContext.h:71
bool allFinal() const
Reports if all agents are in final states (i.e. the simulation is done.)
Definition: FSM.cpp:248
void addVelModifier(VelModifier *v)
Add an velocity modifier to the FSM.
Definition: FSM.h:346
Agents::SimulatorInterface * _sim
The simulator on which the FSM acts.
Definition: FSM.h:352
Templated class for the behavior finite state machine.
Definition: FSM.h:126
size_t getAgentStateID(const Agents::BaseAgent *agent) const
Reports the state the given agent is currently in.
Definition: FSM.cpp:236
void advance(Agents::BaseAgent *agent)
Advances the FSM based on the current state for the given agent.
Definition: FSM.cpp:129
Base class for fatal exceptions.
Definition: MengeException.h:99
The namespace contains the Behavior Finite State Machine (BFSM) definition.
Collection of convenient pre-compiler information for fsm definitions.
friend FSM * buildFSM(FSMDescrip &fsmDescrip, Agents::SimulatorInterface *sim, bool VERBOSE)
Templated function which builds the behavior fsm from the behavior configuration given.
Definition: buildFSM.cpp:72
FsmContext * getContext()
Returns the BFSM Context for this FSM.
Definition: FSM.cpp:306
Interface for basic FSM task.
Definition: Task.h:110
std::map< size_t, GoalSet * > & getGoalSets()
Returns the goal sets defined in the simulator.
Definition: FSM.h:337
Defines the basic agent properties and functionality that all simulation agents share.
Definition: BaseAgent.h:123
void doTasks()
Performs the work in the FSM's tasks.
Definition: FSM.cpp:284
const GoalSet * getGoalSet(size_t goalSetID)
Retrives the given goal set.
Definition: FSM.cpp:214
The base class for modifying preferred velocities.
Definition: VelModifier.h:110
State * getCurrentState(const Agents::BaseAgent *agt) const
Gets a pointer to the state the agent is currently in.
Definition: FSM.cpp:230
State ** _currNode
The active state for each agent in the system.
Definition: FSM.h:364
The definition of behavior finite state machines.
size_t _agtCount
Number of agents attached to the state machine.
Definition: FSM.h:359
FSMException()
Default constructor.
Definition: FSM.h:90
FSMFatalException()
Default constructor.
Definition: FSM.h:111
The namespace that contains the basic simulation mechanisms.
std::vector< Task * > _tasks
The set of tasks to perform at each time step.
Definition: FSM.h:374
size_t addNode(State *node)
Adds a state to the BFSM.
Definition: FSM.cpp:175
bool addTransition(size_t fromNode, Transition *t)
Adds the given transition to the FSM.
Definition: FSM.cpp:187
void setCurrentState(Agents::BaseAgent *agt, size_t currNode)
Sets the current state for the given agent.
Definition: FSM.cpp:223