Menge
Modular Pedestrian Simulation Framework for Research and Development
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
State.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 __FSMNODE_H__
45 #define __FSMNODE_H__
46 
47 #include <vector>
48 #include <cassert>
49 #include "Transitions/Transition.h"
52 #include "Actions/Action.h"
53 #include "FSMEnumeration.h"
54 #include "PrefVelocity.h"
55 #include "ReadersWriterLock.h"
56 #include "MengeException.h"
57 #include <set>
58 
59 namespace Menge {
60 
61  namespace BFSM {
62 
63  // forward declaration
64  class StateContext;
65  class GoalSelector;
66  class Goal;
67  class FSM;
68 
72  class MENGE_API StateException : public virtual MengeException {
73  public:
78 
84  StateException( const std::string & s ): MengeException(s) {}
85 
86  };
87 
93  public:
98 
105  };
106 
108 
123  class State {
128  static size_t COUNT;
129 
133  static Vector2 NULL_POINT;
134 
135  public:
141  State( const std::string & name );
142 
146  ~State();
147 
153  void getTasks( FSM * fsm );
154 
161  void getPrefVelocity( Agents::BaseAgent * agent, Agents::PrefVelocity &velocity );
162 
168  inline void setFinal( bool isFinal ) { _final = isFinal; }
169 
175  inline bool getFinal() const { return _final; }
176 
188 
194  virtual void enter( Agents::BaseAgent * agent );
195 
201  virtual void leave( Agents::BaseAgent * agent );
202 
212  void addTransition( Transition * t ) { transitions_.push_back( t ); }
213 
223 
229  void addAction( Action * a ) { actions_.push_back( a ); }
230 
236  void addVelModifier( VelModifier * v ) { velModifiers_.push_back( v ); }
237 
246  size_t getID() const { return _id; }
247 
253  std::string getName() const { return _name; }
254 
260  size_t getPopulation() const;
261 
270  void setGoalSelector( GoalSelector * selector );
271 
278 
282  void clearGoalSelector();
283 
284  friend class StateContext;
285 
286 
287  protected:
300  State * testTransitions( Agents::BaseAgent * agent, std::set< State * > &visited );
301 
306 
311  std::vector< Transition * > transitions_;
312 
317  std::vector< VelModifier * > velModifiers_;
318 
322  std::vector< Action * > actions_;
323 
327  bool _final;
328 
333 
337  HASH_MAP< size_t, Goal * > _goals;
338 
342  std::string _name;
343 
347  size_t _id;
348 
353  };
354  } // namespace BFSM
355 } // namespace Menge
356 
357 #endif //__FSMNODE_H__
The transition between BFSM states.
Definition: Transition.h:67
The basic state of the behavior finite state machine.
Definition: State.h:123
GoalSelector * getGoalSelector()
Returns a pointer to the goal selector.
Definition: State.h:277
HASH_MAP< size_t, Goal * > _goals
A mapping from agent id to its per-agent goal.
Definition: State.h:337
The core namespace. All elements of Menge are contained in this namespace.
Definition: AgentGenerator.cpp:43
virtual void leave(Agents::BaseAgent *agent)
Automatically called when an agent leaves the state.
Definition: State.cpp:188
std::vector< VelModifier * > velModifiers_
A priority-ordered list of velocity modifiers to determine if the state changes. The order of the mod...
Definition: State.h:317
void getPrefVelocity(Agents::BaseAgent *agent, Agents::PrefVelocity &velocity)
Modifies the input preferred velocity to reflect a velocity for the agent specified.
Definition: State.cpp:99
void getTasks(FSM *fsm)
Gets the tasks for all of the state's FSM elements.
Definition: State.cpp:77
The definition of a preferred velocity.
The definition of actions that are taken as agents enter states.
The definition of a readers-writer lock.
Definition: ReadersWriterLock.h:62
The base class for computing an agent's preferred velocity.
Definition: VelComponent.h:112
The definition of how preferred velocity is modified by a filter.
Exception class for BFSM states.
Definition: State.h:72
The base definition for exceptions in Menge.
VelComponent * _velComponent
The single velocity component associated with this state.
Definition: State.h:305
Base context for finite state machine states.
Definition: StateContext.h:80
~State()
Destructor.
Definition: State.cpp:64
State(const std::string &name)
Constructor.
Definition: State.cpp:58
State * testTransitions(Agents::BaseAgent *agent)
Test the transitions out of this state for the given agent.
Definition: State.cpp:118
void setVelComponent(VelComponent *vc)
Sets the velocity component to the state.
Definition: State.h:222
The definition of state transitions in the BFSM.
Base exception class for menge operations.
Definition: MengeException.h:58
void addAction(Action *a)
Add an action to the state.
Definition: State.h:229
std::string getName() const
Returns the name of the state.
Definition: State.h:253
size_t getID() const
Returns the globally unique state identifier.
Definition: State.h:246
void setFinal(bool isFinal)
Set whether the state is final or not.
Definition: State.h:168
Templated class for the behavior finite state machine.
Definition: FSM.h:126
void setGoalSelector(GoalSelector *selector)
Sets the goal selector for the state.
Definition: State.cpp:223
The definition of a readers-writer lock.
std::string _name
The name of the state.
Definition: State.h:342
Base class for fatal exceptions.
Definition: MengeException.h:99
Exception thrown when the state has an error which cannot be recovered from.
Definition: State.h:92
The namespace contains the Behavior Finite State Machine (BFSM) definition.
void clearGoalSelector()
Clears the state's current goal selector.
Definition: State.cpp:233
StateException(const std::string &s)
Constructor with message.
Definition: State.h:84
The abstract definition of an action.
Definition: Action.h:78
The definition of how preferred velocity is computed in a state.
The definition of a preferred velocity.
Definition: PrefVelocity.h:68
Defines the basic agent properties and functionality that all simulation agents share.
Definition: BaseAgent.h:123
Enumerated types for the State class.
The base, abstract class for selecting per-agent goals.
Definition: GoalSelector.h:113
The base class for modifying preferred velocities.
Definition: VelModifier.h:110
size_t getPopulation() const
Returns the number of agents in this state.
Definition: State.cpp:210
std::vector< Action * > actions_
Actions to take upon entering and leaving the state.
Definition: State.h:322
StateFatalException(const std::string &s)
Constructor with message.
Definition: State.h:104
GoalSelector * _goalSelector
The goal selector for this state.
Definition: State.h:332
void addTransition(Transition *t)
Add a transition to the state.
Definition: State.h:212
StateFatalException()
Default constructor.
Definition: State.h:97
void addVelModifier(VelModifier *v)
Add an velocity modifier to the state.
Definition: State.h:236
ReadersWriterLock _goalLock
The lock for accessing the goals.
Definition: State.h:352
std::vector< Transition * > transitions_
A priority-ordered list of transitions to determine if the state changes. The order of the transition...
Definition: State.h:311
virtual void enter(Agents::BaseAgent *agent)
Automatically called when an agent enters the state.
Definition: State.cpp:158
bool getFinal() const
Reports if the state is final or not.
Definition: State.h:175
bool _final
Determines if the state is a final state (true), or not (false).
Definition: State.h:327
StateException()
Default constructor.
Definition: State.h:77
size_t _id
The globally unique id of state.
Definition: State.h:347