# 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  

operation.cpp

Go to the documentation of this file.
00001 
00007 /* Copyright, 2001 Nevrax Ltd.
00008  *
00009  * This file is part of NEVRAX D.T.C. SYSTEM.
00010  * NEVRAX D.T.C. SYSTEM is free software; you can redistribute it and/or modify
00011  * it under the terms of the GNU General Public License as published by
00012  * the Free Software Foundation; either version 2, or (at your option)
00013  * any later version.
00014 
00015  * NEVRAX D.T.C. SYSTEM is distributed in the hope that it will be useful, but
00016  * WITHOUT ANY WARRANTY; without even the implied warranty of
00017  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
00018  * General Public License for more details.
00019 
00020  * You should have received a copy of the GNU General Public License
00021  * along with NEVRAX D.T.C. SYSTEM; see the file COPYING. If not, write to the
00022  * Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
00023  * MA 02111-1307, USA.
00024  */
00025 #include "nel/ai/agent/operation.h"
00026 #include "nel/ai/agent/agent_local_mailer.h"
00027 #include "nel/ai/script/object_unknown.h"
00028 #include "nel/ai/script/interpret_object_message.h"
00029 
00030 
00031 namespace NLAIAGENT
00032 {       
00033         const NLAIC::CIdentType *CAgentOperation::IdAgentOperation = NULL;
00034         const NLAIC::CIdentType *CAgentOperation::idMsgOnChangeMsg = NULL;
00035         const NLAIAGENT::IMessageBase *CAgentOperation::MsgOnChangeMsg = NULL;
00036 
00037         CAgentOperation::CAgentOperation():CAgentScript(NULL), _Op(NULL), _Name(NULL), _Change(false)
00038         {
00039         }
00040 
00041         CAgentOperation::CAgentOperation(IObjetOp *o):CAgentScript(NULL), _Op(o), _Name(NULL), _Change(true)
00042         {
00043 
00044         }
00045 
00046         CAgentOperation::CAgentOperation(const CAgentOperation &a):CAgentScript(a), _Op(a._Op), _Name(NULL), _Change(false)
00047         {
00048                 if(_Op != NULL)
00049                                 _Op->incRef();
00050 
00051                 if(a._Name != NULL) 
00052                                 setName(*a._Name);
00053 
00054                 /*std::list <IConnectIA *>::iterator it = _Connection.begin();
00055                 while(it != _Connection.end())
00056                 {                       
00057                         (*it)->connect(this);
00058                         it ++;
00059                 }*/
00060         }
00061 
00062         CAgentOperation::~CAgentOperation()
00063         {
00064                 if(_Op != NULL) 
00065                                 _Op->release();
00066                 if(_Name != NULL) 
00067                                 _Name->release();
00068         }
00069 
00070         void CAgentOperation::onKill(IConnectIA *A)
00071         {
00072                 std::list <IConnectIA *>::iterator it = _Connection.begin();
00073                 while(it != _Connection.end())
00074                 {                       
00075                         if((*it) == A)
00076                         {                               
00077                                 std::list < IConnectIA * >::iterator itTmp = it ++;
00078                                 _Connection.erase(itTmp);
00079                                 continue;
00080                         }
00081                         it ++;
00082                 }
00083         }
00084 
00085         void CAgentOperation::update(IObjectIA *obj)
00086         {
00087                 NLAIAGENT::IMessageBase *msg = (NLAIAGENT::IMessageBase *)CAgentOperation::MsgOnChangeMsg->clone();
00088                 msg->push(_Name);
00089                 msg->push(_Op);
00090                 msg->setPerformatif(IMessageBase::PTell);
00091                 msg->setSender((NLAIAGENT::IObjectIA *)((CAgentScript *)this));
00092                 _Op->incRef();
00093                 _Name->incRef();
00094                 (obj)->sendMessage(((NLAIAGENT::IObjectIA *)msg));
00095         }
00096 
00097         void CAgentOperation::connectOnChange(IConnectIA *ref)
00098         {
00099                 _Connection.push_back(ref);
00100                 IConnectIA *obj = ref;
00101                 if(CLocalAgentMail::LocalAgentMail == ref->getType())
00102                                                                                                 obj = (IConnectIA *)((CLocalAgentMail *)ref)->getHost();
00103                 
00104                 obj->connect(this);
00105 
00106                 if(_Op != NULL)
00107                                         update(ref);
00108         }
00109 
00110         IObjectIA::CProcessResult CAgentOperation::runActivity()
00111         {
00112                 if(changed())
00113                 {
00114                         std::list < IConnectIA * >::iterator it = _Connection.begin();
00115                         while(it != _Connection.end())
00116                         {
00117                                 /*NLAIAGENT::IMessageBase *msg = (NLAIAGENT::IMessageBase *)CAgentOperation::MsgOnChangeMsg->clone();                           
00118                                 msg->push(_Op);
00119                                 msg->setPerformatif(IMessageBase::PTell);
00120                                 msg->setSender((NLAIAGENT::IObjectIA *)((CAgentScript *)this));
00121                                 _Op->incRef();
00122                                 (*it)->sendMessage(((NLAIAGENT::IObjectIA *)msg));*/
00123                                 update(*it);
00124                                 it ++;
00125                         }
00126                         changeIsDone();
00127                 }
00128                 if(CAgentScript::haveActivity()) return CAgentScript::runActivity();
00129                 else return CProcessResult();
00130         }
00131 
00132         const IObjectIA::CProcessResult &CAgentOperation::run()
00133         {
00134                 return CAgentScript::run();
00135         }
00136 
00137         IMessageBase *CAgentOperation::runTell(const IMessageBase &msg)
00138         {
00139                 if(msg.getType() == *CAgentOperation::idMsgOnChangeMsg)
00140                 {
00141                         CConstIteratorContener iter = msg.getConstIterator();                           
00142                         ((IObjectIA &)*_Op) = (const IObjectIA &)*(iter ++);
00143                         _Change = true;
00144                         return NULL;
00145                 }
00146                 else
00147                 {
00148                         static NLAIC::CIdentType idMsgSetValue("SetValueMsg");
00149                         if(msg.getType() == idMsgSetValue)
00150                         {                       
00151                                 CConstIteratorContener iter = msg.getConstIterator();
00152                                 iter ++;
00153                                 setValue((IObjetOp *)iter ++);
00154                                 return NULL;
00155                         }
00156                 }
00157                 return CAgentScript::runTell(msg);
00158         }
00159 
00160         bool CAgentOperation::isEqual(const IBasicObjectIA &a) const
00161         {
00162                 if(CAgentScript::isEqual((const CAgentScript &)*this))
00163                 {
00164                         if(_Op) 
00165                                 return _Op->isEqual((const IObjetOp &)*this);           
00166                 }
00167                 return false;
00168         }
00169 
00170         void CAgentOperation::getDebugString(std::string &t) const
00171         {
00172                 t += "CAgentOperation";
00173                 if(_Op)
00174                 {
00175                         std::string s;
00176                         _Op->getDebugString(s);
00177                         t += "<" + s + ">";
00178                 }
00179         }
00180 
00181         const NLAIC::IBasicType *CAgentOperation::clone() const
00182         {
00183                 return (const CAgentScript *)new CAgentOperation(*this);
00184 
00185         }
00186 
00187         const NLAIC::IBasicType *CAgentOperation::newInstance() const
00188         {
00189                 return (const CAgentScript *)new CAgentOperation();
00190         }
00191 
00192         void CAgentOperation::load(NLMISC::IStream &is)
00193         {
00194                 
00195         }
00196 
00197         void CAgentOperation::save(NLMISC::IStream &os)
00198         {
00199 
00200         }
00201 
00202         IObjetOp &CAgentOperation::neg()
00203         {       
00204                 return _Op->neg();
00205         }
00206 
00207         IObjetOp &CAgentOperation::operator += (const IObjetOp &a)
00208         {
00209                 update();
00210                 return (*_Op) += a;
00211         }
00212 
00213         IObjetOp &CAgentOperation::operator -= (const IObjetOp &a)
00214         {
00215                 update();
00216                 return (*_Op) -= a;
00217         }
00218 
00219         IObjetOp &CAgentOperation::operator *= (const IObjetOp &a)
00220         {
00221                 update();
00222                 return (*_Op) *= a;
00223         }
00224 
00225         IObjetOp &CAgentOperation::operator /= (const IObjetOp &a)
00226         {
00227                 update();
00228                 return (*_Op) /= a;
00229         }
00230 
00231         bool CAgentOperation::isTrue() const
00232         {
00233                 if(_Op)
00234                         return _Op->isTrue();
00235                 return false;
00236         }
00237 
00238 
00239         int CAgentOperation::getBaseMethodCount() const
00240         {
00241                 return CAgentScript::getBaseMethodCount() + CAgentOperation::TLastM;
00242         }
00243 
00244         sint32 CAgentOperation::getMethodIndexSize() const
00245         {
00246                 return CAgentScript::getMethodIndexSize() + CAgentOperation::TLastM;
00247         }
00248 
00249         NLAIAGENT::tQueue CAgentOperation::isMember(const IVarName *className,const IVarName *methodName,const IObjectIA &param) const
00250         {
00251 #ifdef NL_DEBUG
00252                 const char *dbg_method_name = methodName->getString();
00253                 std::string buffer;
00254                 param.getDebugString(buffer);
00255 #endif
00256 
00257                 NLAIAGENT::tQueue r = NLAIAGENT::isTemplateMember(CAgentOperation::StaticMethod,CAgentOperation::TLastM, CAgentScript::getMethodIndexSize(),
00258                                                                                                                         className, methodName, param);
00259                 if(r.size()) return r;
00260                 else return CAgentScript::isMember(className,methodName,param);
00261         }               
00262 
00263         NLAIAGENT::IObjectIA::CProcessResult CAgentOperation::runMethodBase(int heritance, int index,NLAIAGENT::IObjectIA *o)
00264         {               
00265                 return runMethodBase(index,o);
00266         }
00267 
00268         NLAIAGENT::IObjectIA::CProcessResult CAgentOperation::runMethodBase(int index, IObjectIA *o)
00269         {
00270                 NLAIAGENT::IObjectIA::CProcessResult r;
00271                 NLAIAGENT::IBaseGroupType *param = (NLAIAGENT::IBaseGroupType *)o;
00272                 switch(index - CAgentScript::getMethodIndexSize())
00273                 {               
00274                 case CAgentOperation::TSetValue:
00275                         {
00276                                 IObjetOp *obj = (NLAIAGENT::IObjetOp *)param->get();
00277                                 setValue(obj);
00278                                 obj->incRef();
00279                         }
00280                         return r;
00281 
00282                 case CAgentOperation::TGetValue:
00283                         r.Result = _Op;
00284                         _Op->incRef();
00285                         return r;
00286 
00287                 case CAgentOperation::TSetName:
00288                         {
00289                                 NLAIAGENT::CStringType s(*(NLAIAGENT::CStringType *)param->get());
00290                                 setName(s);                     
00291                         }
00292                         return r;
00293 
00294                 case CAgentOperation::TGetName:
00295                         r.Result = _Name;
00296                         _Name->release();
00297                         return r;
00298 
00299                 case CAgentOperation::TUpdate:
00300                         update();
00301                         return r;
00302 
00303                 case CAgentOperation::TIsChange:                
00304                         r.Result = new NLAIAGENT::DDigitalType(changed() ? 1.0 : 0.0);
00305                         return r;
00306 
00307                 case CAgentOperation::TConnect:
00308                         {
00309                                 IConnectIA *c = (IConnectIA *)param->get();
00310                                 connectOnChange(c);
00311                                 c->incRef();
00312                         }
00313                         return r;
00314                 
00315 
00316                 default:
00317                         return CAgentScript::runMethodBase(index,o);
00318                 }
00319         }
00320 
00321         NLAIAGENT::CAgentScript::CMethodCall **CAgentOperation::StaticMethod = NULL;
00322 
00323         void CAgentOperation::initClass()
00324         {
00325                 CAgentOperation a;
00326                 CAgentOperation::IdAgentOperation = new NLAIC::CIdentType("AgentOperation", NLAIC::CSelfClassFactory( (const NLAIC::IBasicInterface &)((const CAgentScript &)a) ),
00327                                                                                                                                 NLAIC::CTypeOfObject::tAgent | NLAIC::CTypeOfObject::tInterpret, NLAIC::CTypeOfOperator::opEq );
00328 
00329                 CAgentOperation::idMsgOnChangeMsg = new NLAIC::CIdentType("OnChangeMsg");
00330                 CAgentOperation::MsgOnChangeMsg = (NLAIAGENT::IMessageBase *)CAgentOperation::idMsgOnChangeMsg->allocClass();
00331 
00332 
00333                 CAgentOperation::StaticMethod = new NLAIAGENT::CAgentScript::CMethodCall *[CAgentOperation::TLastM];
00334 
00335                 CAgentOperation::StaticMethod[CAgentOperation::TSetValue] = 
00336                                                 new CAgentScript::CMethodCall(  "SetValue",
00337                                                                                                                 CAgentOperation::TSetValue,
00338                                                                                                                 NULL,CAgentScript::CheckCount, 1, new NLAISCRIPT::CObjectUnknown(new NLAISCRIPT::COperandVoid));
00339 
00340                 CAgentOperation::StaticMethod[CAgentOperation::TGetValue] = 
00341                                                 new CAgentScript::CMethodCall(  "GetValue",
00342                                                                                                                 CAgentOperation::TGetValue,
00343                                                                                                                 NULL,CAgentScript::CheckCount, 0, new NLAISCRIPT::CObjectUnknown(new NLAISCRIPT::COperandAnyObject));
00344 
00345                 CAgentOperation::StaticMethod[CAgentOperation::TSetName] = 
00346                                                 new CAgentScript::CMethodCall(  "SetName",
00347                                                                                                                 CAgentOperation::TSetName,
00348                                                                                                                 NULL,CAgentScript::CheckCount, 1, new NLAISCRIPT::CObjectUnknown(new NLAISCRIPT::COperandVoid));
00349 
00350                 CAgentOperation::StaticMethod[CAgentOperation::TGetName] = 
00351                                                 new CAgentScript::CMethodCall(  "GetName",
00352                                                                                                                 CAgentOperation::TGetName,
00353                                                                                                                 NULL,CAgentScript::CheckCount, 0, new NLAISCRIPT::CObjectUnknown(new NLAISCRIPT::COperandAnyObject));
00354 
00355 
00356                 CAgentOperation::StaticMethod[CAgentOperation::TUpdate] = 
00357                                                 new CAgentScript::CMethodCall(  "Update",
00358                                                                                                                 CAgentOperation::TUpdate,
00359                                                                                                                 NULL,CAgentScript::CheckCount, 0, new NLAISCRIPT::CObjectUnknown(new NLAISCRIPT::COperandVoid));
00360 
00361                 CAgentOperation::StaticMethod[CAgentOperation::TIsChange] = 
00362                                                 new CAgentScript::CMethodCall(  "IsChange",
00363                                                                                                                 CAgentOperation::TIsChange,
00364                                                                                                                 NULL,CAgentScript::CheckCount, 0, new NLAISCRIPT::CObjectUnknown(       
00365                                                                                                                                                                                                 new NLAISCRIPT::COperandSimple(
00366                                                                                                                                                                                                 new NLAIC::CIdentType(NLAIAGENT::DDigitalType::IdDDigitalType))));
00367                 CAgentOperation::StaticMethod[CAgentOperation::TConnect] = 
00368                                                 new CAgentScript::CMethodCall(  "Connect",
00369                                                                                                                 CAgentOperation::TConnect,
00370                                                                                                                 NULL,CAgentScript::CheckCount, 1, new NLAISCRIPT::CObjectUnknown(new NLAISCRIPT::COperandVoid));
00371 
00372         }
00373         void CAgentOperation::releaseClass()
00374         {
00375                 //delete CAgentOperation::IdAgentOperation;
00376                 delete CAgentOperation::idMsgOnChangeMsg;
00377                 delete CAgentOperation::MsgOnChangeMsg;
00378 
00379                 sint i;
00380                 for(i = 0; i < CAgentOperation::TLastM; i++ )
00381                 {
00382                         delete CAgentOperation::StaticMethod[i];
00383                 }
00384                 delete CAgentOperation::StaticMethod;
00385         }
00386 }