#if defined (_MSC_VER) && (_MSC_VER >= 1000) #pragma once #endif #ifndef _INC_BEHAVIOR #define _INC_BEHAVIOR #include "Physics.h" #include "stdafx.h" #include "itkSmartPointer.h" #include namespace mial { //! Perform one or many sequences of actions /*! Behaviors are particular actions or sequences of actions that a deformable organism can perform. They may elicit sensory operations, deformations, and decisions. To ensure meaningful interaction with other organisms and users each behavior has a name. So for example, despite the action "running" being carried out differently by different animals each can always be told to run, or report that it is running. This class is currently classified as unstable. Sub-behaviors are smaller behaviors performed as part of a larger action. This enables significant levels of abstraction, allowing users to issue single commands and carry out vast and complex sequences of actions, or small exact ones. For example, one could tell the organism to simply inflate, or one could tell it to segment which includes inflation. Behaviors are responsible for un-marshaling their own run-time arguments. Upon completion a behavior will set its state to finished. Error reporting is handled by the Error structure, which reports an error message as well as the name of the behavior involved. Wherever possible Behaviors should not contain "if" statements, as any such decision should be left up to the control center. \param Type the internal storage type \param nDims the number of dimensions */ template class Behavior:public itk::LightObject { public: //Smart pointers typedef Behavior Self; typedef itk::SmartPointer Pointer; typedef itk::SmartPointer ConstPointer; typedef itk::WeakPointer ConstWeakPointer; //itkNewMacro(Self); //! The error structure thrown by behaviors run method when an error is encountered. /*! \param msg A message to be sent to the users, should also be placed on std::cerr \param name The name of the behavior. */ struct Error{ std::string msg; std::string name; }; //! A structure defining the inputs of a Behavior. /*! Since structures support public inheritance derived class must inherit from this class in their definitions of behaviorIn. */ struct behaviorIn:public itk::LightObject{ //Define all common behavior inputs here //std::istream * args; typedef behaviorIn Self; typedef itk::SmartPointer Pointer; typedef itk::SmartPointer ConstPointer; typedef itk::WeakPointer ConstWeakPointer; itkNewMacro(Self); //! Overide if you want to convert from stream input (be ran by name). /*! Return false if method not provided or unsuccessful. */ virtual bool fillFromStream(std::istream &args){return false;}; protected: behaviorIn(){}; }; //! Public pure virtual function. Implementation required to provide cognitive layer with a method to run the behavior. /*! Note: This method will only be ran once per behavior, subsequent calls will be made to the update method. Note:that this function must take as an argument the basemost behaviorIn class, implementations can then downcast to their derived behaviorIn classes. \param i the input behavior struct. Typically, setting to NULL indicates stream should be used. \param s the input stream, typically only used if input struct is NULL. */ virtual bool run(typename Behavior::behaviorIn * i, std::stringstream *s = NULL)=0; //! Public pure virtual function. This method allows behaviors to run in multiple stages. /*! This method will be ran after the run method, until the isFinished() returns true. */ virtual bool update()=0; //! Public pure virtual function. Method of cleaning up after the behavior. /*! Since behaviors may be ran multiple times before being destructed they must provide a method to clean up for the next run. This method will be ran after the behavior asserts itself as finished. */ virtual void cleanUp()=0; //! Public accessor to the name string. std::string getName(){return name;}; // inline //! Optional function that converts an input stream to a behaviorIn struct. Returns true iff conversion is succussfull. //virtual bool marshallStream(std::istream &args){return false;}; //! Public modifier to set the physics layer the behavior will be run on. bool setPhysLayer(Physics * phys){ physLayer = phys; return true;}; // inline virtual bool isFinished()=0; //bool isFinished(){return finished;}; protected: //! Default constructor, must provide a name. Behavior(std::string name); //! List of sub-behaviors available Behavior *subBehaviors; //! A pointer for the input behaviorIn * input; //! The name of the behavior used to identify it to other organisms, users, and behaviors. std::string name; //! The physics layer used to simulate the actions Physics *physLayer; typedef Physics PhysicsType; typedef typename PhysicsType::MatrixType MatrixType; typedef typename PhysicsType::VectorType VectorType; //! The state variable //bool finished; }; }// end mial #include "Behavior.cxx" #endif /* _INC_BEHAVIOR*/