# 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  

agent_timer.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/misc/time_nl.h"
00025 #include "nel/ai/nl_ai.h"
00026 #include "nel/ai/agent/agent_timer.h"
00027 #include "nel/ai/agent/agent_method_def.h"
00028 #include "nel/ai/agent/agent_digital.h"
00029 #include "nel/ai/script/object_unknown.h"
00030 
00031 namespace NLAIAGENT
00032 {
00033 //##################################
00034 //CAgentManagerTimer
00035 //##################################
00036         CAgentManagerTimer::CAgentManagerTimer(const CAgentManagerTimer &t):CAgentScript(t), _Time(t._Time)
00037         {
00038 
00039         }
00040 
00041         CAgentManagerTimer::CAgentManagerTimer(IAgentManager *m):CAgentScript(m), _Time(0)
00042         {
00043         }
00044 
00045         CAgentManagerTimer::CAgentManagerTimer(IAgentManager *m, IBasicAgent *a, std::list<IObjectIA *> &v, NLAISCRIPT::CAgentClass *c):CAgentScript(m,a,v,c), _Time(0)
00046         {
00047         }
00048 
00049         CAgentManagerTimer::~CAgentManagerTimer()
00050         {
00051         }
00052 
00053         void CAgentManagerTimer::load(NLMISC::IStream &is)
00054         {
00055         }
00056         void CAgentManagerTimer::save(NLMISC::IStream &os)
00057         {
00058         }
00059         const NLAIC::IBasicType *CAgentManagerTimer::clone() const
00060         {
00061                 return new CAgentManagerTimer(*this);
00062         }
00063         const NLAIC::IBasicType *CAgentManagerTimer::newInstance() const
00064         {
00065                 return new CAgentManagerTimer(NULL);
00066         }
00067 
00068         void CAgentManagerTimer::getDebugString(std::string &t) const
00069         {
00070                 t += NLAIC::stringGetBuild("CAgentManagerTimer <%d>",_Time);
00071         }
00072 
00073         const NLAIC::CIdentType &CAgentManagerTimer::getType() const
00074         {
00075                 return *IdAgentTimer;
00076         }
00077 
00078         IObjectIA::CProcessResult CAgentManagerTimer::runActivity()
00079         {
00080                 _Time ++;
00081                 return IObjectIA::CProcessResult();
00082         }
00083         
00084 
00085         const NLAIC::CIdentType *CAgentManagerTimer::IdAgentTimer =  NULL;
00086         NLMISC::CSynchronized<CAgentScript *> *CAgentManagerTimer::TimerManager = NULL;
00087         NLMISC::IThread *CAgentManagerTimer::TimerManagerRun = NULL;    
00088         CAgentManagerTimer::CRunTimer *CAgentManagerTimer::RunTimer = NULL;
00089         bool CAgentManagerTimer::IsRunning = false;
00090 
00091         const int CAgentManagerTimer::ClockTick = 40;
00092 
00093         void CAgentManagerTimer::initClass()
00094         {
00095                 if(!CAgentManagerTimer::IsRunning)
00096                 {
00097                         CAgentManagerTimer *h = new CAgentManagerTimer(NULL);
00098                         CAgentManagerTimer::IdAgentTimer = new NLAIC::CIdentType ("AgentStaticManagerTimer", NLAIC::CSelfClassFactory((const NLAIC::IBasicInterface &)*h), 
00099                                                                                                                                 NLAIC::CTypeOfObject(NLAIC::CTypeOfObject::tAgent),
00100                                                                                                                                 NLAIC::CTypeOfOperator(NLAIC::CTypeOfOperator::opNone));
00101 
00102                         CAgentManagerTimer::TimerManager = new NLMISC::CSynchronized<CAgentScript *>( "CAgentManagerTimer::TimerManager" );
00103                         {
00104                                 NLMISC::CSynchronized<CAgentScript *>::CAccessor accessor(CAgentManagerTimer::TimerManager);
00105                                 accessor.value() = h;
00106                         }
00107                         /*CAgentManagerTimer::RunTimer = new CAgentManagerTimer::CRunTimer();
00108                         CAgentManagerTimer::TimerManagerRun =  NLMISC::IThread::create(CAgentManagerTimer::RunTimer);
00109                         CAgentManagerTimer::TimerManagerRun->start();
00110                         CAgentManagerTimer::IsRunning = true;*/
00111                         CAgentManagerTimer::IsRunning = false;
00112                 }
00113         }
00114 
00115         void CAgentManagerTimer::releaseClass()
00116         {               
00117                 {
00118                         NLMISC::CSynchronized<CAgentScript *>::CAccessor accessor(CAgentManagerTimer::TimerManager);
00119                         accessor.value()->release();
00120                 }
00121                 delete CAgentManagerTimer::TimerManager;
00122                 delete CAgentManagerTimer::IdAgentTimer;
00123                 CAgentManagerTimer::IdAgentTimer = NULL;                
00124                 CAgentManagerTimer::TimerManager = NULL;
00125                 
00126                 if(CAgentManagerTimer::TimerManagerRun)
00127                 {
00128                         CAgentManagerTimer::TimerManagerRun->terminate();
00129                         delete CAgentManagerTimer::TimerManagerRun;
00130                         delete CAgentManagerTimer::RunTimer;            
00131                         CAgentManagerTimer::IsRunning = false;
00132                 }                                                                               
00133                 
00134         }
00135 
00136 //##################################
00137 //CAgentManagerTimer::CRunTimer
00138 //##################################
00139 
00140         void CAgentManagerTimer::CRunTimer::run()
00141         {
00142 
00143                 NLMISC::TTime start = NLMISC::CTime::getLocalTime();
00144                 NLMISC::TTime time;
00145                 while(1)
00146                 {
00147                         NLMISC::TTime t= NLMISC::CTime::getLocalTime();
00148                         time = (t - start);
00149                         if(time > (uint)CAgentManagerTimer::ClockTick)
00150                         {
00151                                 start = NLMISC::CTime::getLocalTime();
00152                                 NLMISC::CSynchronized<CAgentScript *>::CAccessor accessor(CAgentManagerTimer::TimerManager);
00153                                 accessor.value()->run();
00154                         }
00155                         NLMISC::nlSleep((uint)(CAgentManagerTimer::ClockTick/2));
00156                         //NLMISC::nlSleep(CAgentManagerTimer::ClockTick);
00157                 }
00158         }
00159 
00160 //##################################
00161 //CLibTimerManager
00162 //##################################
00163         const NLAIC::CIdentType *CLibTimerManager::IdAgentManagerTimer = NULL;
00164         NLAIAGENT::CAgentScript::CMethodCall **CLibTimerManager::StaticMethod = NULL;
00165 
00166         NLAIAGENT::tQueue CLibTimerManager::isMember(const NLAIAGENT::IVarName *className,const NLAIAGENT::IVarName *methodName,const NLAIAGENT::IObjectIA &param) const
00167         {
00168                 NLAIAGENT::tQueue r = NLAIAGENT::isTemplateMember(CLibTimerManager::StaticMethod,CLibTimerManager::TLastM,IObjectIA::getMethodIndexSize(),className,methodName,param);
00169                 if(r.size()) return r;
00170                 else return IObjectIA::isMember(className,methodName,param);
00171         }
00172         
00173 
00174         NLAIAGENT::IObjectIA::CProcessResult CLibTimerManager::runMethodeMember(sint32 index,NLAIAGENT::IObjectIA *o)
00175         {
00176                 NLAIAGENT::IBaseGroupType *param = (NLAIAGENT::IBaseGroupType *)o;
00177 
00178                 switch(index - IObjectIA::getMethodIndexSize())
00179                 {
00180                 case CLibTimerManager::TGetTimer:
00181                         {                                                                                               
00182                                 NLAIAGENT::IObjectIA::CProcessResult a;
00183                                 {                                       
00184                                         NLMISC::CSynchronized<CAgentScript *>::CAccessor accessor(CAgentManagerTimer::TimerManager);
00185                                         a = accessor.value()->getDynamicAgent(param);
00186                                 }                               
00187                                 return a;
00188                         }               
00189                 default:
00190                         return IObjectIA::runMethodeMember(index,o);
00191                 }
00192         }
00193 
00194         void CLibTimerManager::initClass()
00195         {
00196                 CLibTimerManager h;
00197                 CLibTimerManager::IdAgentManagerTimer = new NLAIC::CIdentType ("AgentManagerTimer", NLAIC::CSelfClassFactory((const NLAIC::IBasicInterface &)h), 
00198                                                                                                                                           NLAIC::CTypeOfObject(NLAIC::CTypeOfObject::tAgent),
00199                                                                                                                                           NLAIC::CTypeOfOperator(NLAIC::CTypeOfOperator::opNone));
00200 
00201                 NLAISCRIPT::CParam *ParamString = new NLAISCRIPT::CParam(1,new NLAISCRIPT::COperandSimple(new NLAIC::CIdentType(NLAIAGENT::CStringType::IdStringType)));
00202                 CLibTimerManager::StaticMethod = new NLAIAGENT::CAgentScript::CMethodCall *[CLibTimerManager::TLastM];
00203                 
00204                 CLibTimerManager::StaticMethod[CLibTimerManager::TGetTimer] = 
00205                                                         new NLAIAGENT::CAgentScript::CMethodCall(_GETTIMER_,
00206                                                                                                                                         CLibTimerManager::TGetTimer,
00207                                                                                                                                         ParamString,NLAIAGENT::CAgentScript::CheckAll,
00208                                                                                                                                         1,      new NLAISCRIPT::CObjectUnknown(
00209                                                                                                                                                 new NLAISCRIPT::COperandSimple(
00210                                                                                                                                                 new NLAIC::CIdentType(CVectorGroupManager::IdVectorGroupManager))));
00211         }
00212 
00213         void CLibTimerManager::releaseClass()
00214         {
00215                 delete CLibTimerManager::IdAgentManagerTimer;
00216                 int i;
00217                 for(i = 0; i < CLibTimerManager::TLastM; i++)
00218                 {
00219                         delete CLibTimerManager::StaticMethod[i];
00220                 }
00221                 delete CLibTimerManager::StaticMethod;
00222         }
00223 
00224 //##################################
00225 //CAgentWatchTimer
00226 //##################################
00227         const NLAIC::CIdentType *CAgentWatchTimer::IdAgentWatchTimer = NULL;
00228         
00229 
00230         CAgentWatchTimer::CAgentWatchTimer(): CAgentScript(NULL),_Clock(0)
00231         {               
00232                 _CallIter = _Call.end();
00233         }
00234         CAgentWatchTimer::CAgentWatchTimer(const CAgentWatchTimer &t):CAgentScript(t), _Clock(t._Clock),_Call(t._Call)
00235         {               
00236                 std::list<std::pair< IConnectIA *, std::pair<IMessageBase *, sint32> > >::iterator i = _Call.begin();
00237                 while(i != _Call.end())
00238                 {
00239                         connect((*i).first);
00240                         std::pair<IMessageBase *, sint32> p = ((*i).second);
00241                         p.first->incRef();
00242                         i ++;
00243                 }
00244                 _CallIter = _Call.end();
00245 
00246         }
00247         CAgentWatchTimer::CAgentWatchTimer(IAgentManager *m):CAgentScript(m), _Clock(0)/*,_Call(NULL),_MSG(NULL)*/
00248         {
00249         }
00250         CAgentWatchTimer::CAgentWatchTimer(IAgentManager *m, IBasicAgent *a, std::list<IObjectIA *> &v, NLAISCRIPT::CAgentClass *c):CAgentScript(m,a,v,c), _Clock(0)/*,_Call(NULL),_MSG(NULL)*/
00251         {
00252         }
00253         CAgentWatchTimer::~CAgentWatchTimer()
00254         {               
00255                 std::list<std::pair< IConnectIA *, std::pair<IMessageBase *, sint32> > >::iterator i = _Call.begin();
00256                 while(i != _Call.end())
00257                 {                       
00258                         std::pair<IMessageBase *, sint32> p = ((*i++).second);
00259                         IMessageBase * m = p.first;
00260                         m->release();
00261                 }               
00262 
00263         }
00264         
00265         const NLAIC::IBasicType *CAgentWatchTimer::clone() const
00266         {
00267                 return new CAgentWatchTimer(*this);
00268         }
00269 
00270         const NLAIC::IBasicType *CAgentWatchTimer::newInstance() const
00271         {
00272                 return new CAgentWatchTimer();
00273         }
00274 
00275         const NLAIC::CIdentType &CAgentWatchTimer::getType() const
00276         {
00277                 return *IdAgentWatchTimer;
00278         }
00279 
00280         IObjectIA::CProcessResult CAgentWatchTimer::runActivity()
00281         {                               
00282                 NLMISC::TTicks time = NLMISC::CTime::getLocalTime () - _DTime;
00283                 if((sint) (time) >= _Clock)
00284                 {                                       
00285                         tellBroker();
00286                         setState(processToKill,NULL);                   
00287                 }
00288                 return getState();
00289         }
00290 
00291         bool CAgentWatchTimer::tellBroker()
00292         {
00293                 if(_CallIter == _Call.end()) 
00294                                 _CallIter = _Call.begin();
00295                 while(_CallIter != _Call.end())
00296                 {
00297                         std::pair<IMessageBase *, sint32> p = ((*_CallIter).second);
00298                         IMessageBase *msg = (IMessageBase *)p.first;
00299                         msg->incRef();
00300                         if((*_CallIter).first->getState().ResultState == NLAIAGENT::processIdle) (*_CallIter).first->sendMessage((IObjectIA *)msg);
00301                         _CallIter ++;
00302                         /*n ++;
00303                         if(n >= 10) return false;*/
00304                 }
00305                 return true;
00306         }
00307 
00308         void CAgentWatchTimer::addAttrib(IConnectIA *c,IMessageBase *msg)
00309         {               
00310 #ifdef NL_DEBUG
00311                 if(c == NULL || msg == NULL)
00312                 {
00313                         throw;
00314                 }
00315 #endif
00316                 connect(c);
00317                 static CStringVarName sRunTell("RunTell");
00318                                 
00319 
00320                 NLAISCRIPT::COperandSimple *t = new NLAISCRIPT::COperandSimple(new NLAIC::CIdentType(msg->getType()));
00321                 NLAISCRIPT::CParam param(1,t);
00322                         
00323 
00324                 sint32 index = -1;
00325                 
00326                 tQueue r = c->isMember(NULL,&sRunTell,param);
00327                 if(r.size()) index = r.top().Index;
00328 
00329                 msg->setSender(this);
00330                 msg->setPerformatif(IMessageBase::PTell);
00331                 msg->setMethodIndex(0,index);
00332 
00333                 std::pair<IMessageBase *, sint32> p (msg,index);                
00334                 _Call.push_front(std::pair< IConnectIA *, std::pair<IMessageBase *, sint32> > (c,p));
00335                 
00336         }
00337 
00338         void CAgentWatchTimer::onKill(IConnectIA *a)
00339         {
00340                 if(!detach(a,false)) CAgentScript::onKill(a);
00341         }
00342 
00343         bool CAgentWatchTimer::detach(IConnectIA *a,bool deleteFromConnection)
00344         {
00345                 std::list<std::pair< IConnectIA *, std::pair<IMessageBase *, sint32> > >::iterator i = _Call.begin();
00346                 while(i != _Call.end())
00347                 {
00348                         if((*i).first == a)
00349                         {                       
00350                                 (*i).second.first->release();
00351                                 break;
00352                         }
00353                         i++;
00354                 }
00355 
00356                 if(i != _Call.end())
00357                 {
00358                         _Call.erase(i);
00359                         if(!_Call.size()) detach();
00360 
00361                         if(deleteFromConnection) 
00362                         {
00363                                 removeConnection(a);
00364                         }
00365                         return true;
00366                 }               
00367                 else return false;
00368         }
00369 
00370         void CAgentWatchTimer::getDebugString(std::string &t) const
00371         {
00372                 std::string s;
00373                 ((const IWordNumRef &)*this).getNumIdent().getDebugString(s);
00374                 t = NLAIC::stringGetBuild("timer: %s",s.c_str());
00375         }
00376 
00377         void CAgentWatchTimer::detach()
00378         {
00379                 /*_Call = NULL;
00380                 if(_MSG != NULL) _MSG->release();
00381                 _MSG = NULL;*/
00382                 CVectorGroupType g(1);
00383                 std::string t;
00384                 ((const IWordNumRef &)*this).getNumIdent().getDebugString(t);           
00385                 g.set(0,new CStringType(CStringVarName(t.c_str())));
00386                 IObjectIA::CProcessResult r;
00387                 if(CAgentManagerTimer::TimerManager != NULL)
00388                 {
00389                         NLMISC::CSynchronized<CAgentScript *>::CAccessor accessor(CAgentManagerTimer::TimerManager);
00390                         r = accessor.value()->removeDynamic(&g);
00391                         if(r.Result != NULL) r.Result->release();
00392                         //release();
00393                 }               
00394         }
00395 
00396         void CAgentWatchTimer::attach()
00397         {               
00398                 CVectorGroupType g(2);
00399                 std::string t;
00400                 ((const IWordNumRef &)*this).getNumIdent().getDebugString(t);           
00401                 g.set(0,new CStringType(CStringVarName(t.c_str())));
00402                 this->incRef();
00403                 g.set(1,this);
00404                 {
00405                         NLMISC::CSynchronized<CAgentScript *>::CAccessor accessor(CAgentManagerTimer::TimerManager);
00406                         accessor.value()->addDynamicAgent(&g);
00407                 }
00408                 _DTime = NLMISC::CTime::getLocalTime ();
00409         }
00410 
00411         int CAgentWatchTimer::getBaseMethodCount() const
00412         {
00413                 return CAgentScript::getBaseMethodCount() + CAgentWatchTimer::TLastM;
00414         }
00415 
00416 
00417         sint32 CAgentWatchTimer::getMethodIndexSize() const
00418         {
00419                 return CAgentScript::getMethodIndexSize() + CAgentWatchTimer::TLastM;
00420         }
00421 
00422         
00423 
00424         NLAIAGENT::tQueue CAgentWatchTimer::isMember(const NLAIAGENT::IVarName *className,const NLAIAGENT::IVarName *methodName,const NLAIAGENT::IObjectIA &param) const
00425         {
00426                 NLAIAGENT::tQueue r = NLAIAGENT::isTemplateMember(CAgentWatchTimer::StaticMethod,CAgentWatchTimer::TLastM,CAgentScript::getMethodIndexSize(),className,methodName,param);
00427                 if(r.size()) return r;
00428                 else return CAgentScript::isMember(className,methodName,param);
00429         }               
00430 
00431         NLAIAGENT::IObjectIA::CProcessResult CAgentWatchTimer::runMethodBase(int heritance, int index,NLAIAGENT::IObjectIA *o)
00432         {               
00433                 return runMethodBase(index,o);
00434         }
00435 
00436         IObjectIA::CProcessResult CAgentWatchTimer::runMethodeMember(sint32 index,NLAIAGENT::IObjectIA *o)
00437         {
00438                 return runMethodBase(index, o);
00439         }
00440 
00441         NLAIAGENT::IObjectIA::CProcessResult CAgentWatchTimer::runMethodBase(int index,NLAIAGENT::IObjectIA *o)
00442         {
00443                 NLAIAGENT::IBaseGroupType *param = (NLAIAGENT::IBaseGroupType *)o;
00444 
00445                 switch(index - CAgentScript::getMethodIndexSize())
00446                 {
00447                 case CAgentWatchTimer::TAttach:
00448                         {                               
00449                                 CIteratorContener i = param->getIterator();
00450 
00451                                 if(param->size())
00452                                 {
00453                                         IConnectIA *call = (IConnectIA *)i++;
00454                                         call->incRef();
00455                                         IMessageBase *msg = (IMessageBase *)i++;
00456                                         msg->incRef();
00457                                         addAttrib(call,msg);
00458                                 }
00459                                 attach();
00460                                 IObjectIA::CProcessResult a;                            
00461                                 
00462                                 a.Result = new CAgentTimerHandle(this);
00463                                 return a;
00464                         }
00465 
00466                 case CAgentWatchTimer::TAddAttrib:
00467                         {                               
00468                                 CIteratorContener i = param->getIterator();
00469                                 IConnectIA *call = (IConnectIA *)i++;
00470                                 call->incRef();
00471                                 IMessageBase *msg = (IMessageBase *)i++;
00472                                 msg->incRef();
00473                                 addAttrib(call,msg);                            
00474                                 IObjectIA::CProcessResult a;                            
00475                                 
00476                                 a.Result = new CAgentTimerHandle(this);
00477                                 return a;
00478                         }
00479                         
00480                 case CAgentWatchTimer::TSetClock:
00481                         {
00482                                 setClock( (uint)((NLAIAGENT::INombreDefine *)param->get())->getNumber() );
00483                                 return NLAIAGENT::IObjectIA::CProcessResult();
00484                         }
00485                 case CAgentWatchTimer::TGetClock:
00486                         {
00487                                 NLAIAGENT::IObjectIA::CProcessResult r;
00488                                 r.Result = new NLAIAGENT::DDigitalType((double)getClock());
00489                                 return r;
00490                         }
00491                 default:
00492                         return CAgentScript::runMethodBase(index,o);
00493                 }
00494         }
00495 
00496         IMessageBase *CAgentWatchTimer::runTell(const IMessageBase &m)
00497         {
00498                 static NLAIC::CIdentType idMsgKillTimer ("MsgStopTimer");
00499                 static NLAIC::CIdentType idMsgStartTimer ("MsgStartTimer");
00500                 if(m.getType() == idMsgKillTimer)
00501                 {
00502                         setState(processEnd,NULL);
00503                         return NULL;
00504                 }
00505                 else
00506                 if(m.getType() == idMsgStartTimer)
00507                 {
00508                         setState(processIdle,NULL);
00509                         return NULL;
00510                 }
00511                 else return CAgentScript::runTell(m);
00512         }
00513 
00514         NLAIAGENT::CAgentScript::CMethodCall **CAgentWatchTimer::StaticMethod = NULL;
00515 
00516         void CAgentWatchTimer::initMsgClass()
00517         {
00518                 std::string msgStr;
00519                 std::string scriptName("MsgTimer");
00520                 msgStr = std::string("From Message : Define MsgStopTimer\n{");
00521                 msgStr += std::string("Component:\n");          
00522                 msgStr += std::string("End\n}\n");              
00523                 NLAILINK::buildScript(msgStr,scriptName);
00524                 msgStr = std::string("From Message : Define MsgStartTimer\n{");
00525                 msgStr += std::string("Component:\n");
00526                 msgStr += std::string("End\n}\n");              
00527                 NLAILINK::buildScript(msgStr,scriptName);
00528         }
00529 
00530         void CAgentWatchTimer::initClass()
00531         {
00532                 CAgentTimerHandle::initClass();
00533                 initMsgClass();
00534                 CAgentWatchTimer h;
00535                 CAgentWatchTimer::IdAgentWatchTimer = new NLAIC::CIdentType ("AgentWatchTimer", NLAIC::CSelfClassFactory((const NLAIC::IBasicInterface &)h), 
00536                                                                                                                                           NLAIC::CTypeOfObject(NLAIC::CTypeOfObject::tAgent),
00537                                                                                                                                           NLAIC::CTypeOfOperator(NLAIC::CTypeOfOperator::opNone));
00538 
00539                 NLAISCRIPT::CParam *Param = new NLAISCRIPT::CParam(1, new NLAISCRIPT::COperandSimpleListOr(2,
00540                                                                                                                                 new NLAIC::CIdentType(NLAIAGENT::DDigitalType::IdDDigitalType),
00541                                                                                                                                 new NLAIC::CIdentType(NLAIAGENT::DigitalType::IdDigitalType)));
00542 
00543 
00544                 CAgentWatchTimer::StaticMethod = new NLAIAGENT::CAgentScript::CMethodCall *[CAgentWatchTimer::TLastM];
00545                 
00546                 CAgentWatchTimer::StaticMethod[CAgentWatchTimer::TAttach] = 
00547                                                         new NLAIAGENT::CAgentScript::CMethodCall(_ATTACH_,
00548                                                                                                                                         CAgentWatchTimer::TAttach,
00549                                                                                                                                         NULL,NLAIAGENT::CAgentScript::DoNotCheck,
00550                                                                                                                                         2,      new NLAISCRIPT::CObjectUnknown( new NLAISCRIPT::COperandSimple(
00551                                                                                                                                                 new NLAIC::CIdentType(*CAgentTimerHandle::IdAgentTimerHandle))));
00552 
00553                 CAgentWatchTimer::StaticMethod[CAgentWatchTimer::TAddAttrib] = 
00554                                                         new NLAIAGENT::CAgentScript::CMethodCall(_ADDATTRIB_,
00555                                                                                                                                         CAgentWatchTimer::TAddAttrib,
00556                                                                                                                                         NULL,NLAIAGENT::CAgentScript::CheckCount,
00557                                                                                                                                         2,      new NLAISCRIPT::CObjectUnknown( new NLAISCRIPT::COperandSimple(
00558                                                                                                                                                 new NLAIC::CIdentType(*CAgentTimerHandle::IdAgentTimerHandle))));
00559 
00560                 CAgentWatchTimer::StaticMethod[CAgentWatchTimer::TGetClock] = 
00561                                                         new NLAIAGENT::CAgentScript::CMethodCall(_GETCLOCK_,
00562                                                                                                                                         CAgentWatchTimer::TGetClock,
00563                                                                                                                                         NULL,NLAIAGENT::CAgentScript::CheckCount,
00564                                                                                                                                         0, new NLAISCRIPT::CObjectUnknown(
00565                                                                                                                                                                         new NLAISCRIPT::COperandSimple(
00566                                                                                                                                                                         new NLAIC::CIdentType(NLAIAGENT::DigitalType::IdDigitalType))));
00567 
00568                 CAgentWatchTimer::StaticMethod[CAgentWatchTimer::TSetClock] = 
00569                                                         new NLAIAGENT::CAgentScript::CMethodCall(_SETCLOCK_,
00570                                                                                                                                         CAgentWatchTimer::TSetClock,
00571                                                                                                                                         Param,NLAIAGENT::CAgentScript::CheckAll,
00572                                                                                                                                         1, new NLAISCRIPT::CObjectUnknown(
00573                                                                                                                                                                         new NLAISCRIPT::COperandSimple(
00574                                                                                                                                                                         new NLAIC::CIdentType(NLAIAGENT::DigitalType::IdDigitalType))));
00575         }
00576 
00577         void CAgentWatchTimer::releaseClass()
00578         {
00579                 CAgentTimerHandle::releaseClass();
00580                 delete CAgentWatchTimer::IdAgentWatchTimer;
00581                 int i;
00582                 for(i = 0; i < CAgentWatchTimer::TLastM; i++)
00583                 {
00584                         delete CAgentWatchTimer::StaticMethod[i];
00585                 }
00586                 delete CAgentWatchTimer::StaticMethod;
00587         }
00588 
00589 //##################################
00590 //CAgentClockTimer
00591 //##################################
00592 
00593         const NLAIC::CIdentType *CAgentClockTimer::IdAgentClockTimer = NULL;
00594 
00595         CAgentClockTimer::CAgentClockTimer():CAgentWatchTimer(), _TimeCount(0)
00596         {
00597         }
00598 
00599         CAgentClockTimer::CAgentClockTimer(IAgentManager *m):CAgentWatchTimer(m), _TimeCount(0)
00600         {
00601         }
00602 
00603         CAgentClockTimer::CAgentClockTimer(IAgentManager *m, IBasicAgent *a, std::list<IObjectIA *> &v, NLAISCRIPT::CAgentClass *c):
00604                                         CAgentWatchTimer(m,a,v,c), _TimeCount(0)
00605         {
00606         }
00607 
00608         CAgentClockTimer::CAgentClockTimer(const CAgentClockTimer &t):CAgentWatchTimer(t), _TimeCount(t._TimeCount)
00609         {
00610         }
00611 
00612         CAgentClockTimer::~CAgentClockTimer()
00613         {
00614         }
00615 
00616         const NLAIC::IBasicType *CAgentClockTimer::clone() const
00617         {
00618                 return new CAgentClockTimer(*this);
00619         }
00620 
00621         const NLAIC::IBasicType *CAgentClockTimer::newInstance() const
00622         {
00623                 return new CAgentClockTimer();
00624         }
00625 
00626         const NLAIC::CIdentType &CAgentClockTimer::getType() const
00627         {
00628                 return *IdAgentClockTimer;
00629         }
00630 
00631         IObjectIA::CProcessResult CAgentClockTimer::runActivity()
00632         {
00633                 //_Clock -= CAgentManagerTimer::ClockTick;
00634                 NLMISC::TTicks time = NLMISC::CTime::getLocalTime () - _DTime;
00635                 if((sint) (time) >= _Clock)
00636                 {                       
00637                         tellBroker();
00638                         {
00639                                 _Clock = _TimeCount;
00640                                 _DTime = NLMISC::CTime::getLocalTime ();
00641                         }
00642 
00643                 }
00644                 return getState();
00645         }
00646 
00647         void CAgentClockTimer::setClock(uint c)
00648         {       
00649                 _TimeCount = c;
00650                 CAgentWatchTimer::setClock(c);          
00651 
00652         }
00653 
00654         void CAgentClockTimer::initClass()
00655         {
00656                 CAgentClockTimer h;
00657                 CAgentClockTimer::IdAgentClockTimer = new NLAIC::CIdentType ("AgentClockTimer", NLAIC::CSelfClassFactory((const NLAIC::IBasicInterface &)h), 
00658                                                                                                                                           NLAIC::CTypeOfObject(NLAIC::CTypeOfObject::tAgent),
00659                                                                                                                                           NLAIC::CTypeOfOperator(NLAIC::CTypeOfOperator::opNone));
00660         }
00661 
00662         void CAgentClockTimer::releaseClass()
00663         {
00664                 delete CAgentClockTimer::IdAgentClockTimer;
00665         }
00666 
00667 //##################################
00668 //CAgentTimerHandle
00669 //##################################
00670         const NLAIC::CIdentType *CAgentTimerHandle::IdAgentTimerHandle = NULL;
00671 
00672         CAgentTimerHandle::CAgentTimerHandle():_Timer (NULL)
00673         {
00674         }
00675 
00676         CAgentTimerHandle::CAgentTimerHandle(CAgentWatchTimer *t)
00677         {
00678                 _Timer = t;
00679                 if(_Timer) _Timer->incRef();
00680         }
00681 
00682         CAgentTimerHandle::CAgentTimerHandle(const CAgentTimerHandle &t)
00683         {
00684                 _Timer = t._Timer;
00685                 if(_Timer) _Timer->incRef();
00686         }
00687 
00688         CAgentTimerHandle::~CAgentTimerHandle()
00689         {
00690 
00691                 if(_Timer != NULL)
00692                 {
00693                         if(_Timer->getRef() == 1)
00694                         {
00695                                 CVectorGroupType g(1);                  
00696                                 std::string t;
00697                                 ((const IWordNumRef &)*_Timer).getNumIdent().getDebugString(t);
00698                                 g.set(0,new CStringType(CStringVarName(t.c_str())));
00699                                 IObjectIA::CProcessResult r;
00700                                 if(CAgentManagerTimer::TimerManager != NULL)
00701                                 {
00702                                         NLMISC::CSynchronized<CAgentScript *>::CAccessor accessor(CAgentManagerTimer::TimerManager);
00703                                         r = accessor.value()->removeDynamic(&g);                                        
00704                                 }
00705                                 if( ((INombreDefine *)r.Result)->getNumber() != 1.0) 
00706                                                                                                                         _Timer->release();
00707                                 r.Result->release();
00708                                 
00709                         }
00710                         else _Timer->decRef();
00711                 }
00712         }
00713 
00714 
00715         void CAgentTimerHandle::getDebugString(std::string &t) const
00716         {
00717                 if(_Timer != NULL) _Timer->getDebugString(t);           
00718         }
00719 
00720         const NLAIC::IBasicType *CAgentTimerHandle::clone() const
00721         {
00722                 return new CAgentTimerHandle(*this);
00723         }
00724 
00725         const NLAIC::IBasicType *CAgentTimerHandle::newInstance() const
00726         {
00727                 return new CAgentTimerHandle();
00728         }
00729 
00730         bool CAgentTimerHandle::isEqual(const NLAIAGENT::IBasicObjectIA &a) const
00731         {
00732                 const CAgentTimerHandle &h = (const CAgentTimerHandle &)a;
00733                 return _Timer == h._Timer; 
00734         }
00735 
00736         const NLAIC::CIdentType &CAgentTimerHandle::getType() const
00737         {
00738                 return *IdAgentTimerHandle;
00739         }
00740 
00741         IObjectIA::CProcessResult CAgentTimerHandle::sendMessage(IObjectIA *m)
00742         {
00743                 if(_Timer != NULL) 
00744                 {
00745                         return _Timer->sendMessage(m);
00746                 }
00747                 else
00748                 {
00749                         m->release();
00750                         return IObjectIA::CProcessResult();
00751                 }
00752         }
00753 
00754         sint32 CAgentTimerHandle::getMethodIndexSize() const
00755         {
00756                 if(_Timer != NULL)
00757                 {
00758                         return _Timer->getMethodIndexSize();
00759                 }
00760                 else
00761                 {
00762                         CAgentWatchTimer a;
00763                         return a.getMethodIndexSize();
00764                 }
00765         }
00766 
00767         tQueue CAgentTimerHandle::isMember(const IVarName *h,const IVarName *m,const IObjectIA &p) const
00768         {
00769                 if(_Timer != NULL)
00770                 {
00771                         return _Timer->isMember(h,m,p);
00772                 }
00773                 else
00774                 {
00775                         CAgentWatchTimer a;
00776                         return a.isMember(h,m,p);
00777                 }
00778         }
00779 
00780         IObjectIA::CProcessResult CAgentTimerHandle::runMethodeMember(sint32 h, sint32 m, IObjectIA *p)
00781         {
00782                 return IObjectIA::runMethodeMember(h,m,p);
00783         }
00784         IObjectIA::CProcessResult CAgentTimerHandle::runMethodeMember(sint32 m,IObjectIA *p)
00785         {
00786                 return _Timer->runMethodeMember(m,p);
00787         }
00788 
00789         void CAgentTimerHandle::save(NLMISC::IStream &os) {}
00790         void CAgentTimerHandle::load(NLMISC::IStream &is) {}
00791         
00792         void CAgentTimerHandle::initClass()
00793         {
00794                 CAgentWatchTimer *w = new CAgentWatchTimer();
00795                 CAgentTimerHandle h(w);
00796                 CAgentTimerHandle::IdAgentTimerHandle = new NLAIC::CIdentType ("AgentTimerHandle", NLAIC::CSelfClassFactory((const NLAIC::IBasicInterface &)h), 
00797                                                                                                                                           NLAIC::CTypeOfObject(NLAIC::CTypeOfObject::tObject),
00798                                                                                                                                           NLAIC::CTypeOfOperator(NLAIC::CTypeOfOperator::opNone));
00799                 w->release();
00800 
00801         }
00802 
00803         void CAgentTimerHandle::releaseClass()
00804         {
00805                 delete CAgentTimerHandle::IdAgentTimerHandle;
00806         }       
00807 }