Menge
Modular Pedestrian Simulation Framework for Research and Development
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
PropertyAction.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 
45 #ifndef __PROPERTY_ACTION_H__
46 #define __PROPERTY_ACTION_H__
47 
48 #include "CoreConfig.h"
49 #include "Actions/Action.h"
50 #include "Actions/ActionFactory.h"
51 #include "fsmCommon.h"
53 
54 namespace Menge {
55 
56  // forward declaration
57 
58  namespace Agents {
59  class BaseAgent;
60  }
61 
62  namespace BFSM {
63 
65 
73  template <class Manipulator>
74  class MENGE_API PropertyAction : public Action {
75  public:
79  PropertyAction():Action(), _manip() {}
80 
81  protected:
85  virtual ~PropertyAction(){}
86 
87  public:
94  virtual void onEnter( Agents::BaseAgent * agent ) { _manip.manipulate( agent ); }
95 
99  Manipulator * getManipulator() { return &_manip; }
100 
101  protected:
102 
108  virtual void leaveAction( Agents::BaseAgent * agent ) { _manip.restore( agent ); }
109 
113  Manipulator _manip;
114 
115  };
116 
118 
125  template <class Manipulator>
126  class MENGE_API PropertyActFactory : public ActionFactory {
127  public:
132  _propertyID = _attrSet.addStringAttribute( "property", true /*required*/ );
133  _generatorID = _attrSet.addFloatDistAttribute( "", true /*required*/, 0.f, 1.f );
134  }
135 
136  protected:
155  virtual bool setFromXML( Action * action, TiXmlElement * node, const std::string & behaveFldr ) const {
156  PropertyAction< Manipulator > * pAction = dynamic_cast< PropertyAction< Manipulator > * >( action );
157  assert( pAction != 0x0 && "Trying to set property action properties on an incompatible object" );
158 
159  if ( ! ActionFactory::setFromXML( action, node, behaveFldr ) ) return false;
160  Menge::AgentPropertyManipulator * manip = pAction->getManipulator();
161 
162  PropertyOperand prop = Menge::parsePropertyName( _attrSet.getString( _propertyID ) );
163  manip->setProperty( prop );
164  if ( prop == NO_PROPERTY ) {
165  logger << Logger::ERR_MSG << "The property action defined on line " << node->Row() << " specified an invalid value for the \"property\" attribute";
166  return false;
167  }
168  manip->setGenerator( _attrSet.getFloatGenerator( _generatorID ) );
169 
170  return true;
171  }
172 
176  size_t _propertyID;
177 
181  size_t _generatorID;
182  };
183 
185 
189  class MENGE_API SetPropertyActFactory : public PropertyActFactory< Menge::SetPropertyManipulator > {
190  public:
191 
200  virtual const char * name() const { return "set_property"; }
201 
209  virtual const char * description() const {
210  return "Causes the specified property to be *replaced* by the user-defined value";
211  };
212 
213  protected:
225  };
226 
228 
232  class MENGE_API OffsetPropertyActFactory : public PropertyActFactory< Menge::OffsetPropertyManipulator > {
233  public:
234 
243  virtual const char * name() const { return "offset_property"; }
244 
252  virtual const char * description() const {
253  return "Adds the user-defined value into the agent's specified property";
254  };
255 
256  protected:
268  };
269 
271 
275  class MENGE_API ScalePropertyActFactory : public PropertyActFactory< Menge::ScalePropertyManipulator > {
276  public:
277 
286  virtual const char * name() const { return "scale_property"; }
287 
295  virtual const char * description() const {
296  return "Scales the user-defined value into the agent's specified property";
297  };
298 
299  protected:
311  };
312 
313  } // namespace BFSM
314 
315 } // namespace Menge
316 
317 #endif // __PROPERTY_ACTION_H__
Action * instance() const
Create an instance of this class's action.
Definition: PropertyAction.h:310
BFSM::PropertyOperand parsePropertyName(const std::string &opName)
Helper function for parsing property actions.
Definition: AgentPropertyManipulator.cpp:59
Factory for the SetPropertyAction.
Definition: PropertyAction.h:189
The core namespace. All elements of Menge are contained in this namespace.
Definition: AgentGenerator.cpp:43
Factory for the PropertyAction.
Definition: PropertyAction.h:126
Sets up the proper compiler directives for platform and dll export/import.
void setProperty(BFSM::PropertyOperand prop)
Sets the property operand.
Definition: AgentPropertyManipulator.h:130
Action * instance() const
Create an instance of this class's action.
Definition: PropertyAction.h:267
Factory for the ScalePropertyAction.
Definition: PropertyAction.h:275
The definition of actions that are taken as agents enter states.
virtual const char * description() const
A description of the action.
Definition: PropertyAction.h:295
virtual void onEnter(Agents::BaseAgent *agent)
Upon entering the state, this is called – it is the main work of the action.
Definition: PropertyAction.h:94
size_t _propertyID
The identifier for the "property" string attribute.
Definition: PropertyAction.h:176
Action * instance() const
Create an instance of this class's action.
Definition: PropertyAction.h:224
Factory for the OffsetPropertyAction.
Definition: PropertyAction.h:232
virtual bool setFromXML(Action *action, TiXmlElement *node, const std::string &behaveFldr) const
Given a pointer to an Action instance, sets the appropriate fields from the provided XML node...
Definition: PropertyAction.h:155
The factory for parsing xml data and instantiating actions.
void setGenerator(FloatGenerator *gen)
Sets the generator for the manipulator.
Definition: AgentPropertyManipulator.cpp:170
Manipulator * getManipulator()
Returns a pointer to the manipulator.
Definition: PropertyAction.h:99
virtual const char * description() const
A description of the action.
Definition: PropertyAction.h:252
virtual const char * description() const
A description of the action.
Definition: PropertyAction.h:209
virtual ~PropertyAction()
Virtual destructor.
Definition: PropertyAction.h:85
Classes for manipulating agent properties in an "undoable" manner (albeit, to a limited degree)...
The namespace contains the Behavior Finite State Machine (BFSM) definition.
Logger logger
Globally available Logger.
Definition: Logger.cpp:49
virtual const char * name() const
The name of the action.
Definition: PropertyAction.h:286
virtual const char * name() const
The name of the action.
Definition: PropertyAction.h:200
The abstract definition of an action.
Definition: Action.h:78
PropertyAction()
Constructor.
Definition: PropertyAction.h:79
Collection of convenient pre-compiler information for fsm definitions.
Manipulator _manip
The manipulator responsible for changing agent properties.
Definition: PropertyAction.h:113
Base class for manipulating agent properties in an undoable manner. The undo is limited, it only maintains knowledge of one manipulation. If multiple manipulations are applied, only the last can be undone.
Definition: AgentPropertyManipulator.h:83
Defines the basic agent properties and functionality that all simulation agents share.
Definition: BaseAgent.h:123
The base class for modifying agent properties.
Definition: PropertyAction.h:74
size_t _generatorID
The identifier for the float distribution attribute.
Definition: PropertyAction.h:181
virtual const char * name() const
The name of the action.
Definition: PropertyAction.h:243
A class for parsing the xml description of an action and instantiating particular instances...
Definition: ActionFactory.h:62
virtual void leaveAction(Agents::BaseAgent *agent)
The work to do upon state exit.
Definition: PropertyAction.h:108
The namespace that contains the basic simulation mechanisms.
virtual bool setFromXML(Action *action, TiXmlElement *node, const std::string &behaveFldr) const
Given a pointer to an Action instance, sets the appropriate fields from the provided XML node...
Definition: ActionFactory.cpp:56
PropertyActFactory()
Constructor.
Definition: PropertyAction.h:131
Error encountered but not handled.
Definition: Logger.h:69