# 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  

baseai.cpp

Go to the documentation of this file.
00001 
00006 /* Copyright, 2000 Nevrax Ltd.
00007  *
00008  * This file is part of NEVRAX NEL.
00009  * NEVRAX NEL is free software; you can redistribute it and/or modify
00010  * it under the terms of the GNU General Public License as published by
00011  * the Free Software Foundation; either version 2, or (at your option)
00012  * any later version.
00013 
00014  * NEVRAX NEL is distributed in the hope that it will be useful, but
00015  * WITHOUT ANY WARRANTY; without even the implied warranty of
00016  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
00017  * General Public License for more details.
00018 
00019  * You should have received a copy of the GNU General Public License
00020  * along with NEVRAX NEL; see the file COPYING. If not, write to the
00021  * Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
00022  * MA 02111-1307, USA.
00023  */ 
00024 #include "nel/ai/agent/agentexport.h"
00025 #include "nel/ai/agent/agent_object.h"
00026 #include "nel/ai/agent/baseai.h"
00027 #include "nel/ai/agent/agent_method_def.h"
00028 #include "nel/ai/agent/object_type.h"
00029 #include "nel/ai/agent/msg.h"
00030 #include "nel/ai/agent/agent_local_mailer.h"
00031 #include "nel/ai/agent/performative.h"
00032 #include "nel/ai/script/interpret_methodes.h"
00033 
00034 namespace NLAIAGENT
00035 {
00036         void IConnectIA::save(NLMISC::IStream &os)
00037         {
00038                 IRefrence::save(os);
00039                 tListiBasicCstItr i = _Connection.begin();
00040                 sint32 size = _Connection.size();
00041                 os.serial( size );
00042                 while(i != _Connection.end())
00043                 {       
00044                         IWordNumRef &r = (IWordNumRef &)((const IWordNumRef&)*((const IConnectIA *)*i++));
00045                         os.serial( (NLAIC::CIdentType &) (r.getType()) );
00046                         r.save(os);
00047                 }
00048 
00049                 i = _Connected.begin();
00050                 size = _Connected.size(); 
00051                 os.serial( size );
00052                 while(i != _Connected.end())
00053                 {                                               
00054                         IWordNumRef &r = (IWordNumRef &)((const IWordNumRef&)*((const IConnectIA *)*i++));
00055                         os.serial( (NLAIC::CIdentType &) (r.getType()) );
00056                         r.save(os);
00057                 }
00058         }       
00059 
00062 
00063         bool CIdMethod::operator < (const CIdMethod &a) const
00064         {
00065                 return Weight < a.Weight;
00066         }
00067 
00068         const CIdMethod &CIdMethod::operator = (const CIdMethod &a)
00069         {
00070                 Index = a.Index;
00071                 Weight = a.Weight;
00072                 Method = a.Method;
00073                 if(ReturnType != NULL)
00074                 {
00075                         ReturnType->release();
00076                 }
00077                 ReturnType = a.ReturnType;
00078                 if(ReturnType != NULL) ReturnType->incRef();
00079 
00080                 return *this;
00081         }
00082 
00083         CIdMethod::CIdMethod(const CIdMethod &a)
00084         {
00085                 Index = a.Index;
00086                 Weight = a.Weight;
00087                 Method = a.Method;              
00088                 ReturnType = a.ReturnType;
00089                 if(ReturnType != NULL) 
00090                         ReturnType->incRef();
00091         }
00092         
00093         CIdMethod::CIdMethod(sint32 i, double w,IObjectIA *m,IObjectIA *r)
00094         {
00095                 Index = i;
00096                 Weight = w;
00097                 Method = m;
00098                 ReturnType = r;
00099         }
00100 
00101         CIdMethod::CIdMethod()
00102         {
00103                 Method = NULL;
00104                 ReturnType = NULL;
00105         }
00106 
00107         CIdMethod::~CIdMethod()
00108         {
00109                 if(ReturnType != NULL) 
00110                         ReturnType->release();
00111         }
00112 
00115 
00116         void IObjectIA::init(IObjectIA *)
00117         {
00118                 
00119         }
00120 
00121         IBasicObjectIA::~IBasicObjectIA()
00122         {
00123                 //delete _Name;
00124         }
00125         
00126         IBasicObjectIA::IBasicObjectIA()
00127         {
00128         }
00129 
00130         IBasicObjectIA::IBasicObjectIA(const IBasicObjectIA &a): NLAIC::IBasicInterface(a)
00131         {
00132         }
00133                 
00134         bool IBasicObjectIA::operator == (const IBasicObjectIA &a) const
00135         {                                               
00136                 if(a.getType() == getType() ) return isEqual(a);                        
00137                 return false;
00138         }               
00139 
00142 
00143         IObjectIA::CProcessResult IObjectIA::ProcessRun = IObjectIA::CProcessResult();
00144         IObjectIA::CProcessResult IObjectIA::ProcessNotComplit = IObjectIA::CProcessResult(processNotComplete); 
00145         TProcessStatement IObjectIA::ProcessIdle = processIdle;
00146         TProcessStatement IObjectIA::ProcessLocked = processLocked;
00147         TProcessStatement IObjectIA::ProcessBuzzy = processBuzzy;
00148         TProcessStatement IObjectIA::ProcessEnd = processEnd;
00149         TProcessStatement IObjectIA::ProcessError = processError;       
00150 
00151         IObjectIA::IObjectIA(const IObjectIA &a):IBasicObjectIA(a)
00152         {                       
00153         }
00154 
00155         IObjectIA::IObjectIA()
00156         {                       
00157         }               
00158 
00159         IObjectIA::~IObjectIA()
00160         {
00161         }
00162 
00163         const IObjectIA::CProcessResult &IObjectIA::runStep()
00164         {
00165                 std::string text;
00166                 text = NLAIC::stringGetBuild("const CProcessResult &IObjectIA::runStep() note implementaited for the '%s' interface",(const char *)getType());
00167                 throw NLAIE::CExceptionNotImplemented(text.c_str());
00168                 return IObjectIA::ProcessNotComplit;
00169         }
00170 
00171         IObjectIA &IObjectIA::operator = (const IObjectIA &a)
00172         {
00173                 std::string text;
00174                 text = NLAIC::stringGetBuild("operator <IObjectIA &operator = (const IObjectIA &a)> note implementaited for the '%s' interface",(const char *)getType());
00175                 throw NLAIE::CExceptionNotImplemented(text.c_str());
00176                 return *this;
00177         }
00178 
00179 
00180         sint32 IObjectIA::getStaticMemberSize() const
00181         {
00182                 std::string text;
00183                 text = NLAIC::stringGetBuild("sint32 IObjectIA::getStaticMemberSize() note implementaited for the '%s' interface",(const char *)getType());
00184                 throw NLAIE::CExceptionNotImplemented(text.c_str());
00185                 return 0;       
00186         }
00187 
00188         sint32 IObjectIA::getStaticMemberIndex(const IVarName &) const
00189         {
00190                 return -1;
00191         }
00192 
00193         const IObjectIA *IObjectIA::getStaticMember(sint32) const
00194         {
00195                 return NULL;
00196         }
00197 
00198         bool IObjectIA::setStaticMember(sint32,IObjectIA *)
00199         {
00200                 std::string text;
00201                 text = NLAIC::stringGetBuild("Function void IObjectIA::setStaticMember(sint32,IObjectIA *) note implementaited for the '%s' interface",(const char *)getType());
00202                 throw NLAIE::CExceptionNotImplemented(text.c_str());
00203                 return false;
00204         }
00205 
00206         tQueue IObjectIA::isMember(const IVarName *className,const IVarName *methodName,const IObjectIA &param) const
00207         {               
00208                 static CStringVarName send(_SEND_);
00209                 static CStringVarName constructor(_CONSTRUCTOR_);
00210                 static CStringVarName run(_RUN_);
00211                 static CStringVarName statM("GetStaticMember");
00212 
00213                 if(*methodName == send)
00214                 {
00215                         tQueue r;                       
00216                         CObjectType *c = new CObjectType(new NLAIC::CIdentType(NLAIC::CIdentType::VoidType));
00217                         r.push(CIdMethod(0,0.0,NULL,c));
00218                         return r;
00219                 }
00220                 else
00221                 if(*methodName == constructor && !((const NLAISCRIPT::CParam &)param).size())
00222                 {
00223                         tQueue r;
00224                         CObjectType *c = new CObjectType(new NLAIC::CIdentType(NLAIC::CIdentType::VoidType));
00225                         r.push(CIdMethod(1,0.0,NULL,c));
00226                         return r;
00227                 }
00228                 else
00229                 if(*methodName == run && !((const NLAISCRIPT::CParam &)param).size())
00230                 {
00231                         tQueue r;
00232                         CObjectType *c = new CObjectType(new NLAIC::CIdentType(NLAIC::CIdentType::VoidType));
00233                         r.push(CIdMethod(2,0.0,NULL,c));
00234                         return r;                       
00235                 }
00236                 if(*methodName == statM && ((const NLAISCRIPT::CParam &)param).size())
00237                 {
00238                         tQueue r;
00239                         CObjectType *c = new CObjectType(new NLAIC::CIdentType(*IAgent::IdAgent));
00240                         r.push(CIdMethod(3,0.0,NULL,c));
00241                         return r;                       
00242                 }
00243                 return tQueue();
00244         }
00245 
00246         sint32 IObjectIA::isClassInheritedFrom(const IVarName &) const
00247         {
00248                 return -1;
00249         }       
00250 
00251         sint32 IObjectIA::getMethodIndexSize() const
00252         {
00253                 return 4;
00254         }
00255 
00256         // Executes a method from its index i and with its parameters
00257         IObjectIA::CProcessResult IObjectIA::runMethodeMember(sint32 h, sint32 id,IObjectIA *a)
00258         {
00259                 /*char text[2048*8];
00260                 sprintf(text,"method  runMethodeMember(sint32 h, sint32 id,const IObjectIA &) not implemented for the '%s' interface",(const char *)getType());
00261                 throw NLAIE::CExceptionNotImplemented(text);
00262                 return CProcessResult();*/
00263                 return IObjectIA::runMethodeMember(id,a);
00264         }
00265 
00266         IObjectIA::CProcessResult IObjectIA::runMethodeMember(sint32 id,IObjectIA *a)
00267         {
00268                 
00269                 switch(id)
00270                 {
00271                 case 0:
00272                         {
00273                                 IMessageBase *msg;
00274 
00275                                 IPerformative *p;
00276                                 msg = (IMessageBase *)((IBaseGroupType *)a)->pop();
00277                                 //this->incRef();
00278                                 msg->setReceiver(this);
00279                                 p = (IPerformative *)((IBaseGroupType *)a)->pop();
00280                                 msg->setPerformatif((IMessageBase::TPerformatif)(sint)p->getNumber());
00281                                 p->release();
00282                                 if(((IBaseGroupType *)a)->size())
00283                                 {                                       
00284                                         CStringType *name = (CStringType *)((IBaseGroupType *)a)->pop();
00285                                         IObjectIA::CProcessResult r = sendMessage(name->getStr(),msg);
00286                                         name->release();
00287                                         return r;
00288                                         
00289                                 }                               
00290                                 return sendMessage(msg);
00291                         }                       
00292                         break;
00293                 case 1:                 
00294                         break;
00295                 case 2:
00296                         return run();
00297                         break;
00298 
00299                 case 3:
00300                         {
00301                                 CProcessResult r;
00302                                 NLAIAGENT::IBaseGroupType *param = (NLAIAGENT::IBaseGroupType *)a;
00303                                 r.Result =      (IObjectIA *)getStaticMember((sint)((NLAIAGENT::INombreDefine *)param->get())->getNumber());
00304                                 r.Result->incRef();
00305                                 return r;
00306                         }
00307                 }
00308                 return CProcessResult();
00309         }
00310 
00311         std::string IObjectIA::getMethodeMemberDebugString(sint32 h, sint32 id) const
00312         {
00313                 switch(id)
00314                 {
00315                 case 0:
00316                         {
00317                                 return std::string("IObjectIA::sendMessage(IMessage)");
00318                         }                       
00319                         break;
00320                 case 1:                 
00321                         break;
00322                 case 2:                 
00323                         return std::string("IObjectIA::run()");
00324                         break;
00325 
00326                 case 3:
00327                         {                               
00328                                 return std::string("IObjectIA::getStaticMember(Integer)");                              
00329                         }
00330                 }
00331                 return std::string("Mathod_?????(Param_?????)");
00332         }
00333 
00334         IObjectIA::CProcessResult IObjectIA::sendMessage(const IVarName &name,IObjectIA *)
00335         {
00336                 std::string text;
00337                 text = NLAIC::stringGetBuild("method 'sendMessage(%s,const IObjectIA &)' '%s' interface",name.getString(), (const char *)getType());
00338                 throw NLAIE::CExceptionNotImplemented(text.c_str());
00339                 return CProcessResult();
00340         }
00341 
00342         IObjectIA::CProcessResult IObjectIA::sendMessage(IObjectIA *)
00343         {
00344                 std::string text;
00345                 text = NLAIC::stringGetBuild("method 'sendMessage(const IObjectIA &)' '%s' interface", (const char *)getType());
00346                 throw NLAIE::CExceptionNotImplemented(text.c_str());
00347                 return CProcessResult();
00348         }
00349 
00350         tQueue IObjectIA::canProcessMessage(const IVarName &)
00351         {
00352                 return tQueue();
00353         }
00354 
00355 
00356         bool IObjectIA::isLocal()
00357         {
00358                 return true;
00359         }
00360 
00363 
00364         IRefrence::IRefrence():_Parent(NULL)
00365         {
00366                 _NumRef = new CLocWordNumRef(this);
00367         }
00368 
00369         IRefrence::IRefrence(const IWordNumRef *parent):_Parent((IWordNumRef *)parent)
00370         {
00371                 //if(_Parent != NULL) _Parent->incRef();
00372                 _NumRef = new CLocWordNumRef(this);
00373         }
00374 
00375         IRefrence::IRefrence(NLMISC::IStream &is)
00376         {
00377                 refLoadStream(is);
00378         }
00379 
00380         IRefrence::operator const IWordNumRef &() const
00381         {
00382                 return *_NumRef;
00383         }
00384 
00385         IRefrence::operator const IWordNumRef *() const 
00386         {
00387                 return _NumRef;
00388         }
00389 
00390         IRefrence *IRefrence::getParent() const
00391         {
00392                 return _Parent != NULL ? (IRefrence *)((const IRefrence *)*_Parent) : NULL;
00393         }
00394 
00395         void IRefrence::setParent(const IWordNumRef *parent)
00396         {
00397                 //if(_Parent != NULL) _Parent->release();
00398                 _Parent = (IWordNumRef *)parent;
00399                 //if(_Parent != NULL) _Parent->incRef();                
00400         }
00401 
00402         const IRefrence *IRefrence::getOwner() const
00403         {
00404                 const IRefrence *p = getParent();
00405 
00406                 if(p == NULL) return this;
00407 
00408                 while( true )
00409                 {
00410                         if(p->getParent() == NULL) return p;
00411                 }
00412         }
00413 
00414         void IRefrence::save(NLMISC::IStream &os)
00415         {
00416                 IObjectIA::save(os);
00417                 os.serial( (NLAIC::CIdentType &) (_NumRef->getType()) );
00418                 _NumRef->save(os);
00419                 if(_Parent != NULL)
00420                 {
00421                         bool t = true;
00422                         os.serial( t );
00423                         os.serial( (NLAIC::CIdentType &) (_Parent->getType()) );
00424                         _Parent->save(os);
00425                 }
00426                 else
00427                 {
00428                         bool t =  false;
00429                         os.serial( t ); 
00430                 }
00431         }
00432 
00433         void IRefrence::refLoadStream(NLMISC::IStream &is)
00434         {
00435                 IObjectIA::load(is);
00436                 _NumRef->release();
00437                 NLAIC::CIdentTypeAlloc id;
00438                 is >> id;
00439                 _NumRef = (IWordNumRef *)id.allocClass();
00440                 _NumRef->load(is);
00441                 bool k;
00442                 is.serial( k );
00443                 if ( k ) 
00444                 {
00445                         if(_Parent) _Parent->release();
00446                         is >> id;
00447                         _Parent = (IWordNumRef *)id.allocClass();
00448                         _Parent->load(is);
00449                 }
00450                 else 
00451                 {
00452                         _Parent = NULL;
00453                 }
00454         }
00455 
00456         void IRefrence::load(NLMISC::IStream &is)
00457         {
00458                 refLoadStream(is);
00459         }
00460 
00461         void IRefrence::setNumRef(const IWordNumRef &ref)
00462         {
00463                 if(_NumRef != NULL) _NumRef->release();
00464                 _NumRef = new CLocWordNumRef(ref.getNumIdent(),this);
00465         }
00466 
00467         IRefrence::~IRefrence()
00468         {
00469                 //if(_Parent != NULL) _Parent->release();
00470                 _NumRef->release();
00471         }
00472 
00473         IRefrence::IRefrence(const IRefrence &A):IObjectIA(A),_Parent(A._Parent)//,_NumRef(A._NumRef)
00474         {                       
00475                 //if(_Parent != NULL) _Parent->incRef();
00476                 _NumRef = new CLocWordNumRef(this);
00477                 setTypeAt(A._NumRef->getNumIdent().getId().getType());
00478         }               
00479 
00480         const CIdent IRefrence::getIdentType() const
00481         {
00482                 return CIdent(0,_NumRef->getNumIdent().getId());
00483         }
00484 
00485         void IRefrence::setTypeAt(uint64 t)
00486         {
00487                 ((CAgentNumber &)(_NumRef->getNumIdent().getId())).setTypeAt(t);
00488         }
00489 
00490 
00493 
00494         IConnectIA::IConnectIA(const IConnectIA &A):IRefrence(A)
00495         {
00496         }
00497 
00498         IConnectIA::IConnectIA():IRefrence()
00499         {
00500         }
00501 
00502         IConnectIA::IConnectIA(const IWordNumRef *parent):IRefrence(parent)
00503         {
00504         }               
00505 
00506         IConnectIA::IConnectIA(NLMISC::IStream &is):IRefrence(is)
00507         {
00508                 connectLoadStream(is);
00509         }
00510 
00511         IConnectIA *IConnectIA::getParent() const
00512         {
00513                 return (IConnectIA *)IRefrence::getParent();
00514         }       
00515 
00516         void IConnectIA::load(NLMISC::IStream &is)
00517         {
00518                 connectLoadStream(is);
00519         }
00520 
00521         void IConnectIA::connectLoadStream(NLMISC::IStream &is)
00522         {
00523                 IRefrence::refLoadStream(is);
00524                 NLAIC::CIdentTypeAlloc id;
00525                 sint32 i;
00526                 is.serial( i );
00527 
00528                 if(i)
00529                 {
00530                         while(i --)
00531                         {                                       
00532                                 is >> id;
00533                                 IWordNumRef *num = (IWordNumRef *)id.allocClass();
00534                                 num->load(is);
00535                                 addInConnectionList((IConnectIA *)((const IRefrence *)*num));
00536                                 delete num; 
00537                         }
00538                 }
00539 
00540                 
00541                 is.serial(i);
00542                 if(i)
00543                 {
00544                         while(i--)
00545                         {
00546                                 is >> id;                                       
00547                                 IWordNumRef *num = (IWordNumRef *)id.allocClass();
00548                                 num->load(is);
00549                                 addInConnectedList((IConnectIA *)((const IRefrence *)*num));
00550                                 delete num;
00551                         }
00552                 }
00553         }
00554 
00555         void IConnectIA::connect(IConnectIA *b)
00556         {
00557                 b->addInConnectedList(this);
00558                 addInConnectionList(b);
00559         }
00560 
00561         void IConnectIA::removeConnection(IConnectIA *Agent)
00562         {               
00563                 Agent->removeInConnectedList(this);
00564                 removeInConnectionList(Agent);                  
00565         }
00566 
00567         IConnectIA::~IConnectIA()
00568         {
00569                 Kill();
00570         }
00571 
00572         void IConnectIA::addInConnectionList(const IConnectIA *a)
00573         {
00574                 _Connection.insert(a);
00575         }
00576 
00577         void IConnectIA::removeInConnectionList(IConnectIA *a)
00578         {
00579 
00580                 tListiBasicItr i = _Connection.find(a);
00581                 if(i != _Connection.end())
00582                                                         _Connection.erase(i);
00583                 /*tListiBasicItr i = _Connection.begin();
00584                 while(i != _Connection.end())
00585                 {                               
00586                         if(*i == a)
00587                         {
00588                                 _Connection.erase(i);
00589                                 return;
00590                         }
00591                         i++;
00592                 }*/
00593         }
00594 
00595         void IConnectIA::addInConnectedList(const IConnectIA *a)
00596         {
00597                 _Connected.insert(a);
00598         }
00599 
00600         void IConnectIA::removeInConnectedList(const IConnectIA *a)
00601         {
00602                 tListiBasicItr i = _Connected.find(a);
00603                 if(i != _Connected.end())
00604                                                 _Connected.erase(i);
00605                 /*tListiBasicItr i = _Connected.begin();
00606                 while(i != _Connected.end())
00607                 {                       
00608                         const IConnectIA *o = *i;
00609                         if(o == a)
00610                         {                                       
00611                                 _Connected.erase(i);
00612                                 return;
00613                         }
00614                         i++;
00615                 }*/
00616         }
00617 
00618         void IConnectIA::Kill()
00619         {
00620                 IConnectIA *parent = getParent();
00621                 if ( parent != NULL )
00622                 {
00623                         parent->onKill( this );
00624                 }
00625 
00626                 tListiBasicCstItr it = _Connection.begin();
00627 
00628                 while(it != _Connection.end())
00629                 {                               
00630                         IConnectIA *a = (IConnectIA *)*it ++;                   
00631                         if(a != NULL)
00632                         {
00633                                 a->removeInConnectedList(this);
00634                         }
00635                 }
00636                 _Connection.clear();
00637 
00638                 it = _Connected.begin();
00639                 while(it != _Connected.end())
00640                 {                               
00641                         IConnectIA *a = (IConnectIA *)*it ++;                   
00642                         if(a != NULL)
00643                         {
00644                                 a->removeInConnectionList(this);
00645                                 a->onKill(this);                                
00646                         }
00647                 }
00648                 _Connected.clear();
00649         }
00650 
00651         void IConnectIA::onKill(IConnectIA *a)
00652         {
00653                 IConnectIA *parent = getParent();
00654                 if ( parent == a )
00655                 {
00656                         //parent->release();
00657                         setParent(NULL);
00658                 }
00659         }
00660 
00661         void IConnectIA::setParent(const IWordNumRef *parent)
00662         {               
00663                 IRefrence::setParent(parent);
00664                 IConnectIA *p = (IConnectIA *)getParent();
00665 
00666                 if(p != NULL) connect(p);
00667         }
00668 }