# 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.cpp

Go to the documentation of this file.
00001 #include "nel/ai/logic/goal.h"
00002 #include "nel/ai/logic/var.h"
00003 #include "nel/ai/agent/object_type.h"
00004 #include "nel/ai/logic/operator_script.h"
00005 #include "nel/ai/agent/msg_action.h"
00006 
00007 namespace NLAILOGIC
00008 {
00009 
00010 
00011         IGoal::IGoal() : IBaseBoolType()
00012         {
00013                 _Name = NULL;
00014                 _Sender = NULL;
00015                 _Receiver = NULL;
00016                 _Selected = false;
00017                 _Mode = achieveOnce;
00018         }
00019                 
00020         IGoal::IGoal(const NLAIAGENT::IVarName &name, TTypeOfGoal mode) : IBaseBoolType()
00021         {
00022                 _Name = (NLAIAGENT::IVarName *) name.clone();
00023                 _Sender = NULL;
00024                 _Receiver = NULL;
00025                 _Mode = mode;
00026                 _Selected = false;
00027         }
00028 
00029         IGoal::IGoal(const NLAIAGENT::IVarName &name, std::list<const NLAIAGENT::IObjectIA *> &args, TTypeOfGoal mode)
00030         {
00031                 _Name = (NLAIAGENT::IVarName *) name.clone();
00032                 while ( !args.empty() )
00033                 {
00034                         _Args.push_back( (NLAIAGENT::IObjectIA *) args.front()->clone() );
00035                         args.pop_front();
00036                 }
00037                 _Sender = NULL;
00038                 _Receiver = NULL;
00039                 _Mode = mode;
00040                 _Selected = false;
00041         }
00042 
00043         IGoal::IGoal(const IGoal &c) : IBaseBoolType()
00044         {
00045                 if ( c._Name )
00046                         _Name = (NLAIAGENT::IVarName *) c._Name->clone();
00047                 else
00048                         _Name = NULL;
00049                 _Sender =c._Sender;
00050                 _Receiver = c._Receiver;
00051                 _Mode = c._Mode;
00052 
00053                 for ( int i = 0; i < (int) c._Args.size(); i++ )
00054                         _Args.push_back( (NLAIAGENT::IObjectIA *) c._Args[i]->clone() );
00055 
00056                 _Selected = c._Selected;
00057         }
00058 
00059         IGoal::~IGoal()
00060         {
00061                 if ( _Name )
00062                         _Name->release();
00063 
00064                 int i;          
00065                 for ( i = 0; i < (int) _Args.size(); i++ )
00066                         _Args[i]->release();
00067                 
00068                 std::vector<NLAIAGENT::IBasicAgent *>::iterator it_s = _Successors.begin();
00069                 while ( it_s != _Successors.end() )
00070                 {                       
00071                         (*it_s ++)->release();                  
00072                 }
00073         }
00074 
00075 
00076         void IGoal::setSender(NLAIAGENT::IBasicAgent *s)
00077         {
00078                 _Sender = s;
00079         }
00080 
00081         void IGoal::setReceiver(NLAIAGENT::IBasicAgent *r)
00082         {
00083                 _Receiver = r;
00084         }
00085 
00086         NLAIAGENT::IBasicAgent *IGoal::getSender()
00087         {
00088                 return _Sender;
00089         }
00090 
00091         NLAIAGENT::IBasicAgent *IGoal::getReceiver()
00092         {
00093                 return _Receiver;
00094         }
00095 
00096         void IGoal::failure()
00097         {
00098                 if ( _Sender != NULL )
00099                 {
00100                         NLAIAGENT::IMessageBase *msg = new NLAIAGENT::CSuccessMsg((NLAIAGENT::IBasicAgent *)NULL);
00101                         msg->setPerformatif(NLAIAGENT::IMessageBase::PTell);
00102                         msg->setSender( this );
00103                         msg->setReceiver( _Sender);
00104                         _Sender->sendMessage(msg);
00105                 }
00106         }
00107 
00108         void IGoal::success()
00109         {
00110 
00111                 if ( _Sender != NULL )
00112                 {
00113                         NLAIAGENT::IMessageBase *msg = new NLAIAGENT::CSuccessMsg((NLAIAGENT::IBasicAgent *)NULL);
00114                         msg->setPerformatif(NLAIAGENT::IMessageBase::PTell);
00115                         msg->setSender( this );
00116                         msg->setReceiver( _Sender );
00117                         _Sender->sendMessage(msg);
00118                 }
00119         }
00120 
00121         void IGoal::operatorSuccess(NLAIAGENT::IBasicAgent *op)
00122         {
00123                 success();
00124 //              nlinfo("operatorSuccess: 0x%0x, %d, (0x%0x, %s)", this, _Successors.size(),op,(const char *)op->getType());
00125                 std::vector<NLAIAGENT::IBasicAgent *>::iterator it_s = _Successors.begin();
00126                 while ( it_s != _Successors.end() )
00127                 {                       
00128 /*
00129                         nlinfo("\t0x%0x",*it_s);
00130                         nlinfo("\t\t%s", (const char *)(*it_s)->getType());
00131 */
00132                         if ( (**it_s) == *op )
00133                         {
00134                                 (*it_s)->release();
00135                                 _Successors.erase( it_s );
00136                                 break;
00137                         }
00138                         it_s++;
00139                 }
00140 
00141                 switch ( _Mode )
00142                 {
00143                         case achieveOnce:
00144                                 ( (NLAIAGENT::CAgentScript *) _Receiver)->removeGoal( (NLAILOGIC::CGoal *) this );
00145                                 break;
00146 
00147                         case achieveForever:
00148                                 break;
00149                 }
00150 
00151         }
00152 
00153         void IGoal::operatorFailure(NLAIAGENT::IBasicAgent *op)
00154         {
00155                 failure();
00156                 std::vector<NLAIAGENT::IBasicAgent *>::iterator it_s = _Successors.begin();
00157                 while ( it_s != _Successors.end() )
00158                 {
00159                         if ( (**it_s) == *op )
00160                         {
00161                                 (*it_s)->release();
00162                                 _Successors.erase( it_s );
00163                                 break;
00164                         }
00165                         it_s++;
00166                 }
00167 
00168                 switch ( _Mode )
00169                 {
00170                         case achieveOnce:
00171                                 ( (NLAIAGENT::CAgentScript *) _Receiver )->removeGoal( (NLAILOGIC::CGoal *) this );
00172                                 break;
00173 
00174                         case achieveForever:
00175                                 break;
00176                 }
00177         }
00178 
00179         bool IGoal::isExclusive()
00180         {
00181                 if ( _Successors.empty() )
00182                         return false;
00183                 else
00184                         return ( (NLAIAGENT::COperatorScript *)_Successors.front())->isExclusive();
00185         }
00186 
00187         void IGoal::setPriority(float p)
00188         {
00189                 _Priority = p;
00190         }
00191 
00192         void IGoal::calcPriority()
00193         {
00194         }
00195 
00196         float IGoal::getPriority()
00197         {
00198                 return _Priority;
00199         }
00200 
00201 
00204 
00205         CGoal::CGoal() : IGoal()
00206         {
00207         }
00208                 
00209         CGoal::CGoal(const NLAIAGENT::IVarName &name, TTypeOfGoal mode) : IGoal(name, mode)
00210         {
00211         }
00212 
00213         CGoal::CGoal(const NLAIAGENT::IVarName &name, std::list<const NLAIAGENT::IObjectIA *> &args, TTypeOfGoal mode) : IGoal(name, args, mode)
00214         {
00215         }
00216 
00217         CGoal::CGoal(const CGoal &c) : IGoal( c )
00218         {
00219         }
00220 
00221         CGoal::~CGoal()
00222         {
00223         }
00224 
00225         void CGoal::operatorSuccess(NLAIAGENT::IBasicAgent *op)
00226         {
00227                 success();
00228 //              nlinfo("operatorSuccess: 0x%0x, %d, (0x%0x, %s)", this, _Successors.size(),op,(const char *)op->getType());
00229                 std::vector<NLAIAGENT::IBasicAgent *>::iterator it_s = _Successors.begin();
00230                 while ( it_s != _Successors.end() )
00231                 {                       
00232 /*
00233                         nlinfo("\t0x%0x",*it_s);
00234                         nlinfo("\t\t%s", (const char *)(*it_s)->getType());
00235 */
00236                         if ( (**it_s) == *op )
00237                         {
00238                                 (*it_s)->release();
00239                                 _Successors.erase( it_s );
00240                                 break;
00241                         }
00242                         it_s++;
00243                 }
00244 
00245                 switch ( _Mode )
00246                 {
00247                         case achieveOnce:
00248                                 {
00249                                         NLAIAGENT::CAgentScript *dest = (NLAIAGENT::CAgentScript *) _Receiver;
00250                                         dest->removeGoal( this );
00251                                 }
00252                                 break;
00253 
00254                         case achieveForever:
00255                                 break;
00256                 }
00257 
00258         }
00259 
00260         void CGoal::operatorFailure(NLAIAGENT::IBasicAgent *op)
00261         {
00262                 failure();
00263                 std::vector<NLAIAGENT::IBasicAgent *>::iterator it_s = _Successors.begin();
00264                 while ( it_s != _Successors.end() )
00265                 {
00266                         if ( (**it_s) == *op )
00267                         {
00268                                 (*it_s)->release();
00269                                 _Successors.erase( it_s );
00270                                 break;
00271                         }
00272                         it_s++;
00273                 }
00274 
00275                 switch ( _Mode )
00276                 {
00277                         case achieveOnce:
00278                                 ( (NLAIAGENT::CAgentScript *) _Receiver )->removeGoal( this );
00279                                 break;
00280 
00281                         case achieveForever:
00282                                 break;
00283                 }
00284         }
00285 
00286         const NLAIC::IBasicType *CGoal::clone() const
00287         {
00288                 NLAIC::IBasicInterface *m = new CGoal( *this );
00289                 return m;
00290         }
00291 
00292         const NLAIC::IBasicType *CGoal::newInstance() const
00293         {
00294                 return clone();
00295         }
00296 
00297 
00298         void CGoal::save(NLMISC::IStream &os)
00299         {                       
00300         }
00301 
00302         void CGoal::load(NLMISC::IStream &is)
00303         {
00304                 IObjectIA::load( is );
00305         }
00306 
00307         void CGoal::getDebugString(std::string &text) const
00308         {
00309                 text += "<CGoal> (";
00310                 if ( _Name ) 
00311                         text += _Name->getString();
00312                 std::string buf;
00313                 int i;
00314                 for ( i = 0; i < (int) _Args.size(); i++ )
00315                 {
00316                         _Args[i]->getDebugString(buf);
00317                         text += " ";
00318                         text += buf;
00319                 }
00320                 text += ") - ";
00321 
00322                 text += NLAIC::stringGetBuild(" PRI = %f", priority() );
00323                 text += " PRE = ";
00324 
00325                 for ( i = 0; i < (int) _Predecessors.size(); i++ )
00326                 {
00327                         _Predecessors[i]->getDebugString( buf );
00328                         text += buf;
00329                 }
00330                 text += " POST = ";
00331                 for ( i = 0; i < (int) _Successors.size(); i++ )
00332                 {
00333                         _Successors[i]->getDebugString( buf );
00334                         text += buf;
00335                 }
00336         }
00337 
00338         bool CGoal::isTrue() const
00339         {
00340                 return false;
00341         }
00342 
00343         float CGoal::truthValue() const
00344         {
00345                 return 1.0;
00346         }
00347 
00348         const NLAIAGENT::IObjectIA::CProcessResult &CGoal::run()
00349         {
00350                 return IObjectIA::ProcessRun;
00351         }
00352 
00353         bool CGoal::isEqual(const CGoal &a) const
00354         {
00355                 return false;
00356         }
00357 
00358         bool CGoal::isEqual(const NLAIAGENT::IBasicObjectIA &a) const
00359         {
00360                 return false;
00361         }
00362 
00363         const std::vector<NLAIAGENT::IObjectIA *> &CGoal::getArgs()
00364         {
00365                 return _Args;
00366         }
00367 
00368         const NLAIC::CIdentType &CGoal::getType() const
00369         {
00370                 return IdGoal;
00371         }
00372 
00373         void CGoal::setArgs(std::list<NLAIAGENT::IObjectIA *> &args)
00374         {
00375                 std::list<NLAIAGENT::IObjectIA *>::iterator it_var = args.begin();
00376                 while ( it_var != args.end() )
00377                 {
00378                         _Args.push_back( (NLAIAGENT::IObjectIA *) (*it_var)->clone() );
00379                         it_var++;
00380                 }
00381         }
00382 
00383         NLAIAGENT::tQueue CGoal::isMember(const NLAIAGENT::IVarName *className,const NLAIAGENT::IVarName *funcName,const NLAIAGENT::IObjectIA &params) const
00384         {
00385 
00386 #ifdef NL_DEBUG 
00387         std::string nameP;
00388         std::string nameM;
00389         funcName->getDebugString(nameM);
00390         params.getDebugString(nameP);
00391         const char *dbg_class_name = (const char *) getType();
00392 #endif
00393                 static NLAIAGENT::CStringVarName constructor_name("Constructor");
00394                 static NLAIAGENT::CStringVarName mode_once_name("SetModeOnce");
00395                 static NLAIAGENT::CStringVarName mode_repeat_name("SetModeRepeat");
00396                 static NLAIAGENT::CStringVarName reply_to_name("ReplyTo");
00397 
00398                 NLAIAGENT::tQueue r;
00399                 if(className == NULL)
00400                 {
00401                         if( (*funcName) == constructor_name )
00402                         {                                       
00403                                 NLAIAGENT::CObjectType *c = new NLAIAGENT::CObjectType( new NLAIC::CIdentType( CGoal::IdGoal ) );                                       
00404                                 r.push( NLAIAGENT::CIdMethod( 0 + IObjetOp::getMethodIndexSize(), 0.0, NULL, c) );                                      
00405                         }
00406 
00407                         if( (*funcName) == mode_once_name )
00408                         {                                       
00409                                 NLAIAGENT::CObjectType *c = new NLAIAGENT::CObjectType( new NLAIC::CIdentType( CGoal::IdGoal ) );                                       
00410                                 r.push( NLAIAGENT::CIdMethod( 1 + IObjetOp::getMethodIndexSize(), 0.0, NULL, c) );                                      
00411                         }
00412 
00413                         if( (*funcName) == mode_repeat_name )
00414                         {                                       
00415                                 NLAIAGENT::CObjectType *c = new NLAIAGENT::CObjectType( new NLAIC::CIdentType( CGoal::IdGoal ) );                                       
00416                                 r.push( NLAIAGENT::CIdMethod( 2 + IObjetOp::getMethodIndexSize(), 0.0, NULL, c) );                                      
00417                         }
00418 
00419                         if( (*funcName) == reply_to_name )
00420                         {                                       
00421                                 NLAIAGENT::CObjectType *c = new NLAIAGENT::CObjectType( new NLAIC::CIdentType( CGoal::IdGoal ) );                                       
00422                                 r.push( NLAIAGENT::CIdMethod( 3 + IObjetOp::getMethodIndexSize(), 0.0, NULL, c) );                                      
00423                         }
00424                 }
00425 
00426                 if ( r.empty() )
00427                         return IBaseBoolType::isMember(className, funcName, params);
00428                 else
00429                         return r;
00430         }
00431 
00433 
00434         NLAIAGENT::IObjectIA::CProcessResult CGoal::runMethodeMember(sint32, sint32, NLAIAGENT::IObjectIA *)
00435         {
00436                 return IObjectIA::CProcessResult();
00437         }
00438 
00439         NLAIAGENT::IObjectIA::CProcessResult CGoal::runMethodeMember(sint32 index, NLAIAGENT::IObjectIA *p)
00440         {
00441                 NLAIAGENT::IBaseGroupType *param = (NLAIAGENT::IBaseGroupType *)p;
00442 
00443                 switch(index - IObjetOp::getMethodIndexSize())
00444                 {
00445                         case 0:
00446                                 {                                       
00447 
00448                                         NLAIAGENT::CStringType *name = (NLAIAGENT::CStringType *) param->getFront();
00449                                         param->popFront();
00450 #ifdef NL_DEBUG
00451                                         const char *dbg_name = name->getStr().getString();
00452 #endif
00453                                         // If the constructor() function is explicitely called and the object has already been initialised
00454                                         if ( _Name )
00455                                                 _Name->release();
00456                                         _Args.clear();
00457 
00458                                         _Name = (NLAIAGENT::IVarName *) name->getStr().clone();
00459                                         std::list<const NLAIAGENT::IObjectIA *> args;
00460                                         while ( param->size() )
00461                                         {
00462                                                 _Args.push_back( (NLAIAGENT::IObjectIA *) param->getFront()->clone() );
00463                                                 param->popFront();
00464                                         }
00465                                         name->release();
00466                                         return IObjectIA::CProcessResult();             
00467                                 } 
00468                                 break;
00469                         
00470                         case 1:
00471                                 _Mode = achieveOnce;
00472                                 break;
00473 
00474                         case 2:
00475                                 _Mode = achieveForever;
00476                                 break;
00477 
00478                         case 3:
00479                                 {
00480                                         _Sender = ( NLAIAGENT::IBasicAgent *) ( (NLAIAGENT::CLocalAgentMail *) ( (NLAIAGENT::IBasicAgent *) param->getFront()) )->getHost();
00481                                         param->popFront();
00482                                 }
00483                                 break;
00484                 }
00485 
00486                 return IObjectIA::CProcessResult();
00487         }
00488 
00489         sint32 CGoal::getMethodIndexSize() const
00490         {
00491                 return IBaseBoolType::getMethodIndexSize() + 4;
00492         }
00494 
00495         void CGoal::addSuccessor(NLAIAGENT::IBasicAgent *s)
00496         {               
00497                 //nlinfo("addSuccessor: 0x%0x, %d, (0x%0x, %s)", this, _Successors.size(),s,(const char *)s->getType());
00498                 _Successors.push_back(s);
00499                 s->incRef();
00500         }
00501 
00502         void CGoal::addPredecessor(NLAIAGENT::IBasicAgent *p)
00503         {
00504                 _Predecessors.push_back(p);
00505                 p->incRef();
00506         }
00507 
00508         bool CGoal::operator==(const CGoal &g)
00509         {
00510                 if ( (*g._Name) == (*_Name) && _Args.size() == g._Args.size() )
00511                         return true;
00512 
00513                 return false;
00514         }
00515 
00516         void CGoal::cancel()
00517         {
00518                 std::vector<NLAIAGENT::IBasicAgent *>::iterator i, end = _Successors.end();
00519                 for ( i = _Successors.begin(); i != end; i++ )          
00520                 {
00521                         ( (NLAIAGENT::COperatorScript *)*i )->cancel();
00522                 }
00523         }
00524 
00525         float CGoal::priority() const
00526         {
00527                 if ( _Successors.empty() )
00528                         return 0.0;
00529 
00530                 float pri = 256;                
00531                 std::vector<NLAIAGENT::IBasicAgent *>::const_iterator i, end = _Successors.end();
00532                 for ( i = _Successors.begin(); i != end; i++ )
00533                 {
00534                         float suc_pri = ( (NLAIAGENT::COperatorScript *)*i )->priority();
00535                         if ( suc_pri < pri )
00536                         {
00537                                 pri = suc_pri;
00538                         }
00539                 }
00540                 return pri;
00541         }
00542 
00545 
00546         CInternalGoal::CInternalGoal() : IGoal()
00547         {
00548                 _Property = NULL;
00549         }
00550                 
00551         CInternalGoal::CInternalGoal(const NLAIAGENT::IVarName &name, TTypeOfGoal mode) : IGoal(name, mode)
00552         {
00553                 _Property = NULL;
00554         }
00555 
00556         CInternalGoal::CInternalGoal(const NLAIAGENT::IVarName &name, std::list<const NLAIAGENT::IObjectIA *> &args, TTypeOfGoal mode) : IGoal(name, args, mode)
00557         {
00558                 _Property = NULL;
00559         }
00560 
00561         CInternalGoal::CInternalGoal(const CInternalGoal &c) : IGoal( c )
00562         {
00563                 if ( c._Property != NULL )
00564                         setProperty( c._Property );
00565                 else
00566                         _Property = NULL;
00567         }
00568 
00569         CInternalGoal::~CInternalGoal()
00570         {
00571                 if ( _Property != NULL )
00572                         _Property->release();
00573         }
00574 
00575         void CInternalGoal::operatorSuccess(NLAIAGENT::IBasicAgent *op)
00576         {
00577 //              nlinfo("operatorSuccess: 0x%0x, %d, (0x%0x, %s)", this, _Successors.size(),op,(const char *)op->getType());
00578                 std::vector<NLAIAGENT::IBasicAgent *>::iterator it_s = _Successors.begin();
00579                 while ( it_s != _Successors.end() )
00580                 {                       
00581                         if ( (**it_s) == *op )
00582                         {
00583                                 (*it_s)->release();
00584                                 _Successors.erase( it_s );
00585                                 break;
00586                         }
00587                         it_s++;
00588                 }
00589 
00590                 switch ( _Mode )
00591                 {
00592                         case achieveOnce:
00593                                 ( (NLAIAGENT::CAgentScript *) _Receiver)->removeGoal( (NLAILOGIC::CGoal *) this );
00594                                 break;
00595 
00596                         case achieveForever:
00597                                 break;
00598                 }
00599 
00600         }
00601 
00602         void CInternalGoal::operatorFailure(NLAIAGENT::IBasicAgent *op)
00603         {
00604                 std::vector<NLAIAGENT::IBasicAgent *>::iterator it_s = _Successors.begin();
00605                 while ( it_s != _Successors.end() )
00606                 {
00607                         if ( (**it_s) == *op )
00608                         {
00609                                 (*it_s)->release();
00610                                 _Successors.erase( it_s );
00611                                 break;
00612                         }
00613                         it_s++;
00614                 }
00615 
00616                 switch ( _Mode )
00617                 {
00618                         case achieveOnce:
00619                                 ( (NLAIAGENT::CAgentScript *) _Receiver )->removeGoal( (NLAILOGIC::CGoal *) this );
00620                                 break;
00621 
00622                         case achieveForever:
00623                                 break;
00624                 }
00625         }
00626 
00627         const NLAIC::IBasicType *CInternalGoal::clone() const
00628         {
00629                 NLAIC::IBasicInterface *m = new CInternalGoal( *this );
00630                 return m;
00631         }
00632 
00633         const NLAIC::IBasicType *CInternalGoal::newInstance() const
00634         {
00635                 return clone();
00636         }
00637 
00638 
00639         void CInternalGoal::save(NLMISC::IStream &os)
00640         {                       
00641         }
00642 
00643         void CInternalGoal::load(NLMISC::IStream &is)
00644         {
00645                 IObjectIA::load( is );
00646         }
00647 
00648         void CInternalGoal::getDebugString(std::string &text) const
00649         {
00650                 text += "<CInternalGoal> (";
00651                 if ( _Name ) 
00652                         text += _Name->getString();
00653                 std::string buf;
00654                 int i;
00655                 for ( i = 0; i < (int) _Args.size(); i++ )
00656                 {
00657                         _Args[i]->getDebugString(buf);
00658                         text += " ";
00659                         text += buf;
00660                 }
00661                 text += ") - ";
00662 
00663                 text += NLAIC::stringGetBuild(" PRI = %f", priority() );
00664                 text += " PRE = ";
00665 
00666                 for ( i = 0; i < (int) _Predecessors.size(); i++ )
00667                 {
00668                         _Predecessors[i]->getDebugString( buf );
00669                         text += buf;
00670                 }
00671                 text += " POST = ";
00672                 for ( i = 0; i < (int) _Successors.size(); i++ )
00673                 {
00674                         _Successors[i]->getDebugString( buf );
00675                         text += buf;
00676                 }
00677         }
00678 
00679         bool CInternalGoal::isTrue() const
00680         {
00681                 return false;
00682         }
00683 
00684         float CInternalGoal::truthValue() const
00685         {
00686                 return 1.0;
00687         }
00688 
00689         const NLAIAGENT::IObjectIA::CProcessResult &CInternalGoal::run()
00690         {
00691                 return IObjectIA::ProcessRun;
00692         }
00693 
00694         bool CInternalGoal::isEqual(const CInternalGoal &a) const
00695         {
00696                 return false;
00697         }
00698 
00699         bool CInternalGoal::isEqual(const NLAIAGENT::IBasicObjectIA &a) const
00700         {
00701                 return false;
00702         }
00703 
00704         const std::vector<NLAIAGENT::IObjectIA *> &CInternalGoal::getArgs()
00705         {
00706                 return _Args;
00707         }
00708 
00709         const NLAIC::CIdentType &CInternalGoal::getType() const
00710         {
00711                 return IdInternalGoal;
00712         }
00713 
00714         void CInternalGoal::setArgs(std::list<NLAIAGENT::IObjectIA *> &args)
00715         {
00716                 std::list<NLAIAGENT::IObjectIA *>::iterator it_var = args.begin();
00717                 while ( it_var != args.end() )
00718                 {
00719                         _Args.push_back( (NLAIAGENT::IObjectIA *) (*it_var)->clone() );
00720                         it_var++;
00721                 }
00722         }
00723 
00724         NLAIAGENT::tQueue CInternalGoal::isMember(const NLAIAGENT::IVarName *className,const NLAIAGENT::IVarName *funcName,const NLAIAGENT::IObjectIA &params) const
00725         {
00726 
00727 #ifdef NL_DEBUG 
00728         std::string nameP;
00729         std::string nameM;
00730         funcName->getDebugString(nameM);
00731         params.getDebugString(nameP);
00732         const char *dbg_class_name = (const char *) getType();
00733 #endif
00734                 static NLAIAGENT::CStringVarName constructor_name("Constructor");
00735                 static NLAIAGENT::CStringVarName mode_once_name("SetModeOnce");
00736                 static NLAIAGENT::CStringVarName mode_repeat_name("SetModeRepeat");
00737                 NLAIAGENT::tQueue r;
00738                 if(className == NULL)
00739                 {
00740                         if( (*funcName) == constructor_name )
00741                         {                                       
00742                                 NLAIAGENT::CObjectType *c = new NLAIAGENT::CObjectType( new NLAIC::CIdentType( CInternalGoal::IdInternalGoal ) );                                       
00743                                 r.push( NLAIAGENT::CIdMethod( 0 + IObjetOp::getMethodIndexSize(), 0.0, NULL, c) );                                      
00744                         }
00745 
00746                         if( (*funcName) == mode_once_name )
00747                         {                                       
00748                                 NLAIAGENT::CObjectType *c = new NLAIAGENT::CObjectType( new NLAIC::CIdentType( CInternalGoal::IdInternalGoal ) );                                       
00749                                 r.push( NLAIAGENT::CIdMethod( 1 + IObjetOp::getMethodIndexSize(), 0.0, NULL, c) );                                      
00750                         }
00751 
00752                         if( (*funcName) == mode_repeat_name )
00753                         {                                       
00754                                 NLAIAGENT::CObjectType *c = new NLAIAGENT::CObjectType( new NLAIC::CIdentType( CInternalGoal::IdInternalGoal ) );                                       
00755                                 r.push( NLAIAGENT::CIdMethod( 2 + IObjetOp::getMethodIndexSize(), 0.0, NULL, c) );                                      
00756                         }
00757 
00758                 }
00759 
00760                 if ( r.empty() )
00761                         return IBaseBoolType::isMember(className, funcName, params);
00762                 else
00763                         return r;
00764         }
00765 
00767 
00768         NLAIAGENT::IObjectIA::CProcessResult CInternalGoal::runMethodeMember(sint32, sint32, NLAIAGENT::IObjectIA *)
00769         {
00770                 return IObjectIA::CProcessResult();
00771         }
00772 
00773         NLAIAGENT::IObjectIA::CProcessResult CInternalGoal::runMethodeMember(sint32 index, NLAIAGENT::IObjectIA *p)
00774         {
00775                 NLAIAGENT::IBaseGroupType *param = (NLAIAGENT::IBaseGroupType *)p;
00776 
00777                 switch(index - IObjetOp::getMethodIndexSize())
00778                 {
00779                         case 0:
00780                                 {                                       
00781 
00782                                         NLAIAGENT::CStringType *name = (NLAIAGENT::CStringType *) param->getFront();
00783                                         param->popFront();
00784 #ifdef NL_DEBUG
00785                                         const char *dbg_name = name->getStr().getString();
00786 #endif
00787                                         // If the constructor() function is explicitely called and the object has already been initialised
00788                                         if ( _Name )
00789                                                 _Name->release();
00790                                         _Args.clear();
00791 
00792                                         _Name = (NLAIAGENT::IVarName *) name->getStr().clone();
00793                                         std::list<const NLAIAGENT::IObjectIA *> args;
00794                                         while ( param->size() )
00795                                         {
00796                                                 _Args.push_back( (NLAIAGENT::IObjectIA *) param->getFront()->clone() );
00797                                                 param->popFront();
00798                                         }
00799                                         name->release();
00800                                         return IObjectIA::CProcessResult();             
00801                                 } 
00802                                 break;
00803                         
00804                         case 1:
00805                                 _Mode = achieveOnce;
00806                                 break;
00807 
00808                         case 2:
00809                                 _Mode = achieveForever;
00810                                 break;
00811                 }
00812 
00813                 return IObjectIA::CProcessResult();
00814         }
00815 
00816         sint32 CInternalGoal::getMethodIndexSize() const
00817         {
00818                 return IBaseBoolType::getMethodIndexSize() + 3;
00819         }
00821 
00822         void CInternalGoal::addSuccessor(NLAIAGENT::IBasicAgent *s)
00823         {               
00824                 //nlinfo("addSuccessor: 0x%0x, %d, (0x%0x, %s)", this, _Successors.size(),s,(const char *)s->getType());
00825                 _Successors.push_back(s);
00826                 s->incRef();
00827         }
00828 
00829         void CInternalGoal::addPredecessor(NLAIAGENT::IBasicAgent *p)
00830         {
00831                 _Predecessors.push_back(p);
00832                 p->incRef();
00833         }
00834 
00835         bool CInternalGoal::operator==(const CInternalGoal &g)
00836         {
00837                 if ( (*g._Name) == (*_Name) && _Args.size() == g._Args.size() )
00838                         return true;
00839 
00840                 return false;
00841         }
00842 
00843         void CInternalGoal::setSender(NLAIAGENT::IBasicAgent *s)
00844         {
00845                 _Sender = s;
00846         }
00847 
00848         void CInternalGoal::setReceiver(NLAIAGENT::IBasicAgent *r)
00849         {
00850                 _Receiver = r;
00851         }
00852 
00853         NLAIAGENT::IBasicAgent *CInternalGoal::getSender()
00854         {
00855                 return _Sender;
00856         }
00857 
00858         NLAIAGENT::IBasicAgent *CInternalGoal::getReceiver()
00859         {
00860                 return _Receiver;
00861         }
00862 
00863         void CInternalGoal::cancel()
00864         {
00865                 std::vector<NLAIAGENT::IBasicAgent *>::iterator i, end = _Successors.end();
00866                 for ( i = _Successors.begin(); i != end; i++ )          
00867                 {
00868                         ( (NLAIAGENT::COperatorScript *)*i )->cancel();
00869                 }
00870         }
00871 
00872         float CInternalGoal::priority() const
00873         {
00874                 if ( _Successors.empty() )
00875                         return 0.0;
00876 
00877                 float pri = 256;                
00878                 std::vector<NLAIAGENT::IBasicAgent *>::const_iterator i, end = _Successors.end();
00879                 for ( i = _Successors.begin(); i != end; i++ )
00880                 {
00881                         float suc_pri = ( (NLAIAGENT::COperatorScript *)*i )->priority();
00882                         if ( suc_pri < pri )
00883                         {
00884                                 pri = suc_pri;
00885                         }
00886                 }
00887                 return pri;
00888         }
00889 
00890 
00891         void CInternalGoal::setProperty(NLAIAGENT::IObjectIA *property)
00892         {
00893                 if ( _Property != NULL )
00894                 {
00895                         _Property->release();   
00896                 }
00897                 _Property = property;
00898         }
00899 }
00900 
00901 
00902 
00903 
00904 
00905 
00906 
00907