# Home    # nevrax.com   
Nevrax
Nevrax.org
#News
#Mailing-list
#Documentation
#CVS
#Bugs
#License
Docs
 
Documentation  
Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members   Related Pages   Search  

goal_stack.cpp

Go to the documentation of this file.
00001 #include "nel/ai/agent/goal_stack.h"
00002 #include "nel/ai/agent/object_type.h"
00003 #include "nel/ai/agent/agent_digital.h"
00004 
00005 namespace NLAILOGIC
00006 {
00007         CGoalStack::CGoalStack()
00008         {
00009                 _MaxGoals = 1;
00010         }
00011 
00012         CGoalStack::CGoalStack(const CGoalStack &g)
00013         {
00014                 int i;
00015                 for( i = 0; i < (int) g._Goals.size(); i++ )
00016                 {
00017                         _Goals.push_back( (CGoal *) g._Goals[i]->clone() );
00018                 }
00019                 _MaxGoals = g._MaxGoals;
00020         }       
00021 
00022         CGoalStack::~CGoalStack()
00023         {
00024                 removeGoal();
00025         }
00026 
00027         NLAIAGENT::IObjectIA::CProcessResult CGoalStack::runActivity()
00028         {
00029                 sint32 i;
00030                 if ( _Goals.size() > 1)
00031                 {
00032                         CGoal *old_top = _Goals.front();
00033                         std::sort(_Goals.begin(), _Goals.end(), greater());
00034                 }
00035 
00036 #ifdef NL_DEBUG
00037                 /*std::string dbg_stack;
00038                 for ( i = 0; i < (sint32) _Goals.size(); i++ )
00039                 {
00040                         std::string tmp;
00041                          _Goals[i]->getDebugString( tmp );
00042                         dbg_stack += tmp;
00043                 }
00044                 const char *dbg_str = dbg_stack.c_str();*/
00045 #endif
00046 
00047                 i = 0;
00048                 std::vector<CGoal *>::iterator it_g = _Goals.begin();
00049                 while ( ( it_g != _Goals.end() ) )
00050                 {
00051                         if (  (*it_g)->priority() > 0 )
00052                         {               
00053                                 if ( (*it_g)->isExclusive() )
00054                                 {
00055                                         if ( i < _MaxGoals )            // Exclusive operators
00056                                         {
00057                                                 (*it_g)->select();
00058                                                 i++;
00059                                         }
00060                                         else
00061                                                 (*it_g)->unSelect();
00062                                 }
00063                                 else
00064                                         (*it_g)->select();                      // Background operators
00065                         }
00066                         else
00067                                 (*it_g)->unSelect();
00068                         it_g++;
00069                 }
00070                 return NLAIAGENT::IObjectIA::CProcessResult();
00071         }
00072 
00073 
00074         void CGoalStack::addGoal(CGoal *g) 
00075         {
00076                 _Goals.push_back( g );
00077         }
00078 
00079         void CGoalStack::removeGoal()
00080         {
00081                 std::vector<CGoal *>::iterator it_g = _Goals.begin();
00082                 while ( it_g != _Goals.end() )
00083                 {
00084                         (*it_g)->cancel();
00085                         (*it_g)->release();
00086                         it_g++;
00087                 }
00088                 _Goals.clear();
00089         }
00090 
00091         void CGoalStack::removeGoal(CGoal *g)
00092         {
00093                 std::vector<CGoal *>::iterator it_g = _Goals.begin();
00094                 while ( it_g != _Goals.end() )
00095                 {
00096                         if ( (**it_g) == *g )
00097                         {
00098                                 (*it_g)->cancel();
00099                                 //Samos
00100                                 (*it_g)->release();
00101                                 _Goals.erase( it_g );
00102                                 return;
00103                         }
00104                         it_g++;
00105                 }
00106         }
00107 
00108         CGoal *CGoalStack::getTopGoal()
00109         {
00110                 if ( _Goals.size() )
00111                         return _Goals.front();  
00112                 else
00113                         return NULL;
00114         }
00115 
00116         CGoal *CGoalStack::operator[](sint32 index)
00117         {
00118                 return _Goals[index];
00119         }
00120 
00121         void CGoalStack::getDebugString(std::string &t) const
00122         {
00123                 for (int i = 0; i < (int) _Goals.size(); i++ )
00124                 {
00125                         t += _Goals[i]->getName().getString();
00126                         std::string text;
00127                         text = NLAIC::stringGetBuild(" %f ", _Goals[i]->priority() );
00128                         t += text;
00129                         if ( _Goals[i]->isSelected() )
00130                                 t += "Selected";
00131                         else
00132                                 t += "Not selected";
00133                         t += '\n';
00134                 }
00135         }
00136 
00137         const NLAIC::CIdentType &CGoalStack::getType() const
00138         {
00139                 return IdGoalStack;
00140         }
00141 
00142         const NLAIC::IBasicType *CGoalStack::clone() const
00143         {
00144                 return new CGoalStack( *this );
00145         }
00146 
00147         const NLAIC::IBasicType *CGoalStack::newInstance() const
00148         {
00149                 return new CGoalStack();
00150         }
00151 
00152         void CGoalStack::save(NLMISC::IStream &os)
00153         {
00154         }
00155 
00156         void CGoalStack::load(NLMISC::IStream &is)
00157         {
00158         }
00159 
00160         bool CGoalStack::isTrue() const
00161         {
00162                 // TODO
00163                 return false;
00164         }
00165 
00166         float CGoalStack::truthValue() const
00167         {
00168                 // TODO
00169                 return 0.0;
00170         }
00171 
00172         const NLAIAGENT::IObjectIA::CProcessResult &CGoalStack::run()
00173         {
00174                 return NLAIAGENT::IObjectIA::ProcessRun;
00175         }
00176 
00177         bool CGoalStack::isEqual(const CGoal &a) const
00178         {
00179                 // TODO:
00180                 return false;
00181         }
00182 
00183         bool CGoalStack::isEqual(const NLAIAGENT::IBasicObjectIA &a) const
00184         {
00185                 // TODO:
00186                 return false;
00187         }
00188 
00189         const std::vector<CGoal *> &CGoalStack::getStack()
00190         {
00191                 return _Goals;
00192         }
00193 
00194         void CGoalStack::setMaxGoals(sint32 max)
00195         {
00196                 _MaxGoals = max;
00197         }
00198 
00199         NLAIAGENT::tQueue CGoalStack::isMember(const NLAIAGENT::IVarName *className,const NLAIAGENT::IVarName *funcName,const NLAIAGENT::IObjectIA &params) const
00200         {
00201 
00202 #ifdef NL_DEBUG 
00203                 std::string nameP;
00204                 std::string nameM;
00205                 funcName->getDebugString(nameM);
00206                 params.getDebugString(nameP);
00207 
00208                 const char *dbg_class_name = (const char *) getType();
00209 #endif
00210                 static NLAIAGENT::CStringVarName maxgoals_name("setMaxGoals");
00211                 NLAIAGENT::tQueue r;
00212                 if(className == NULL)
00213                 {
00214                         if( (*funcName) == maxgoals_name )
00215                         {                                       
00216                                 NLAIAGENT::CObjectType *c = new NLAIAGENT::CObjectType( new NLAIC::CIdentType( CGoal::IdGoal ) );                                       
00217                                 r.push( NLAIAGENT::CIdMethod( 0 + IObjectIA::getMethodIndexSize(), 0.0, NULL, c) );                                     
00218                         }
00219                 }
00220 
00221                 if ( r.empty() )
00222                         return IObjectIA::isMember(className, funcName, params);
00223                 else
00224                         return r;
00225         }
00226 
00228 
00229         NLAIAGENT::IObjectIA::CProcessResult CGoalStack::runMethodeMember(sint32, sint32, NLAIAGENT::IObjectIA *)
00230         {
00231                 return IObjectIA::CProcessResult();
00232         }
00233 
00234         NLAIAGENT::IObjectIA::CProcessResult CGoalStack::runMethodeMember(sint32 index, NLAIAGENT::IObjectIA *p)
00235         {
00236                 NLAIAGENT::IBaseGroupType *param = (NLAIAGENT::IBaseGroupType *)p;
00237 
00238                 switch ( index - IObjectIA::getMethodIndexSize() )
00239                 {
00240                         case 0:
00241                                 {                                       
00242 
00243                                         NLAIAGENT::DigitalType *maxgoals = (NLAIAGENT::DigitalType *) param->getFront();
00244                                         param->popFront();
00245 #ifdef NL_DEBUG
00246                                         std::string buf;
00247                                         maxgoals->getDebugString(buf);
00248 #endif
00249                                         _MaxGoals = (sint32) maxgoals->getNumber();
00250                                         return IObjectIA::CProcessResult();             
00251                                 }
00252                                 break;
00253                 }
00254                 return IObjectIA::CProcessResult();
00255         }
00256 
00257         sint32 CGoalStack::getMethodIndexSize() const
00258         {
00259                 return IObjectIA::getMethodIndexSize() + 1;
00260         }
00261 } // NLAILOGIC