00001
00006 00007 00008 00009 00010 00011 00012 00013 00014 00015 00016 00017 00018 00019 00020 00021 00022 00023
00024 #include "nel/ai/agent/agent_script.h"
00025 #include "nel/ai/agent/agent_manager.h"
00026 #include "nel/ai/agent/agent_local_mailer.h"
00027 #include "nel/ai/script/interpret_object_agent.h"
00028 #include "nel/ai/script/codage.h"
00029 #include "nel/ai/script/type_def.h"
00030 #include "nel/ai/script/object_unknown.h"
00031 #include "nel/ai/agent/agent_method_def.h"
00032 #include "nel/ai/agent/message_script.h"
00033 #include "nel/ai/script/interpret_object_message.h"
00034 #include "nel/ai/script/interpret_object_agent.h"
00035 #include "nel/ai/agent/agent_nombre.h"
00036 #include "nel/ai/agent/performative.h"
00037 #include "nel/ai/agent/msg_notify.h"
00038 #include "nel/ai/agent/msg_goal.h"
00039 #include "nel/ai/logic/factbase.h"
00040 #include "nel/ai/logic/goal.h"
00041 #include "nel/ai/agent/key_agent.h"
00042 #include "nel/ai/agent/list_manager.h"
00043
00044
00045
00046 namespace NLAIAGENT
00047 {
00048 static CGroupType listBidon;
00049
00050 static NLAISCRIPT::COperandSimpleListOr *msgType;
00051 static NLAISCRIPT::COperandSimpleListOr *msgPerf;
00052 static NLAISCRIPT::CParam *SendParamMessageScript;
00053 static NLAISCRIPT::CParam *SendCompParamMessageScript;
00054 static NLAISCRIPT::COperandSimple *IdMsgNotifyParentClass;
00055 static NLAISCRIPT::COperandSimpleListOr *IdMsgNotifyParent;
00056 static NLAISCRIPT::CParam *ParamRunParentNotify;
00057 CAgentScript::CMethodCall **CAgentScript::StaticMethod = NULL;
00058
00061 static NLAISCRIPT::COperandSimple *IdGoalMsgClass;
00062 static NLAISCRIPT::COperandSimpleListOr *IdGoalMsg;
00063 static NLAISCRIPT::CParam *ParamGoalMsg;
00065
00066 void CAgentScript::initAgentScript()
00067 {
00068
00069 msgType = new NLAISCRIPT::COperandSimpleListOr(3,
00070 new NLAIC::CIdentType(CMessageList::IdMessage),
00071 new NLAIC::CIdentType(CMessageVector::IdMessageVector),
00072 new NLAIC::CIdentType(NLAISCRIPT::CMessageClass::IdMessageClass));
00073
00074 msgPerf = new NLAISCRIPT::COperandSimpleListOr(7,
00075 new NLAIC::CIdentType(CPExec::IdPExec),
00076 new NLAIC::CIdentType(CPAchieve::IdPAchieve),
00077 new NLAIC::CIdentType(CPAsk::IdPAsk),
00078 new NLAIC::CIdentType(CPBreak::IdPBreak),
00079 new NLAIC::CIdentType(CPTell::IdPTell),
00080 new NLAIC::CIdentType(CPKill::IdPKill),
00081 new NLAIC::CIdentType(CPError::IdPError));
00082
00083
00084 SendParamMessageScript = new NLAISCRIPT::CParam(2,msgPerf, msgType);
00085
00086 msgPerf->incRef();
00087 msgType->incRef();
00088
00089 SendCompParamMessageScript = new NLAISCRIPT::CParam(3,new NLAISCRIPT::COperandSimple(new NLAIC::CIdentType(CStringType::IdStringType)),
00090 msgPerf, msgType);
00091
00092 IdMsgNotifyParentClass = new NLAISCRIPT::COperandSimple(new NLAIC::CIdentType(NLAISCRIPT::CMsgNotifyParentClass::IdMsgNotifyParentClass));
00093
00094 IdMsgNotifyParent = new NLAISCRIPT::COperandSimpleListOr(2,
00095 new NLAIC::CIdentType(NLAISCRIPT::CMsgNotifyParentClass::IdMsgNotifyParentClass),
00096 new NLAIC::CIdentType(CNotifyParentScript::IdNotifyParentScript));
00097
00098 ParamRunParentNotify = new NLAISCRIPT::CParam(1,IdMsgNotifyParent);
00099
00101
00102
00103 IdGoalMsgClass = new NLAISCRIPT::COperandSimple(new NLAIC::CIdentType(NLAISCRIPT::CGoalMsgClass::IdGoalMsgClass));
00104
00105 IdGoalMsg = new NLAISCRIPT::COperandSimpleListOr(2,
00106 new NLAIC::CIdentType(NLAISCRIPT::CGoalMsgClass::IdGoalMsgClass),
00107 new NLAIC::CIdentType(CGoalMsg::IdGoalMsg));
00108
00109 ParamGoalMsg = new NLAISCRIPT::CParam(1,IdGoalMsg);
00111
00112
00113
00114 StaticMethod = new CAgentScript::CMethodCall *[CAgentScript::TLastM];
00115
00116 StaticMethod[CAgentScript::TRunAskParentNotify] = new CAgentScript::CMethodCall( _RUNASK_,
00117 CAgentScript::TRunAskParentNotify, ParamRunParentNotify,
00118 CAgentScript::CheckAll,
00119 1,
00120 new NLAISCRIPT::CObjectUnknown(IdMsgNotifyParentClass));
00121
00122 IdMsgNotifyParentClass->incRef();
00123 StaticMethod[CAgentScript::TRunTellParentNotify] = new CAgentScript::CMethodCall( _RUNTEL_,
00124 CAgentScript::TRunTellParentNotify, ParamRunParentNotify,
00125 CAgentScript::CheckAll,
00126 1,
00127 new NLAISCRIPT::CObjectUnknown(IdMsgNotifyParentClass));
00128
00129 StaticMethod[CAgentScript::TSend] = new CAgentScript::CMethodCall( _SEND_,
00130 CAgentScript::TSend, SendParamMessageScript,
00131 CAgentScript::CheckAll,
00132 2,
00133 new NLAISCRIPT::CObjectUnknown(new NLAISCRIPT::COperandVoid));
00134
00135 StaticMethod[CAgentScript::TSendComponent] = new CAgentScript::CMethodCall( _SEND_,
00136 CAgentScript::TSendComponent,
00137 SendCompParamMessageScript,CAgentScript::CheckAll,
00138 3,
00139 new NLAISCRIPT::CObjectUnknown(new NLAISCRIPT::COperandVoid));
00140
00141 StaticMethod[CAgentScript::TGetChildTag] = new CAgentScript::CMethodCall( _GETCHILD_,
00142 CAgentScript::TGetChildTag,
00143 NULL,
00144 CAgentScript::CheckCount,
00145 1,
00146 new NLAISCRIPT::CObjectUnknown(new
00147 NLAISCRIPT::COperandSimple(
00148 new NLAIC::CIdentType(CVectorGroupManager::IdVectorGroupManager))));
00149 StaticMethod[CAgentScript::TAddChildTag] = new CAgentScript::CMethodCall( _ADDCHILD_,
00150 CAgentScript::TAddChildTag,
00151 NULL,CAgentScript::CheckCount,
00152 2,
00153 new NLAISCRIPT::CObjectUnknown(
00154 new NLAISCRIPT::COperandSimple(
00155 new NLAIC::CIdentType(DigitalType::IdDigitalType))));
00156
00157 StaticMethod[CAgentScript::TFather] = new CAgentScript::CMethodCall( _FATHER_,
00158 CAgentScript::TFather,
00159 NULL,CAgentScript::CheckCount,
00160 0,
00161 new NLAISCRIPT::CObjectUnknown(
00162 new NLAISCRIPT::COperandSimple(
00163 new NLAIC::CIdentType(CAgentScript::IdAgentScript))));
00164
00166
00167
00168 StaticMethod[CAgentScript::TGoal] = new CAgentScript::CMethodCall( _RUNACHIEVE_,
00169 CAgentScript::TGoal, ParamGoalMsg,
00170 CAgentScript::CheckAll,
00171 1,
00172 new NLAISCRIPT::CObjectUnknown(IdGoalMsgClass) );
00174
00175
00176
00177 StaticMethod[CAgentScript::TSelf] = new CAgentScript::CMethodCall( _SELF_,
00178 CAgentScript::TSelf,
00179 NULL,CAgentScript::CheckCount,
00180 0,
00181 new NLAISCRIPT::CObjectUnknown( new NLAISCRIPT::COperandSimple(
00182 new NLAIC::CIdentType(CAgentScript::IdAgentScript))));
00183
00184 StaticMethod[CAgentScript::TGetName] = new CAgentScript::CMethodCall( _GETNAME_,
00185 CAgentScript::TGetName,
00186 NULL,CAgentScript::CheckCount,
00187 1,
00188 new NLAISCRIPT::CObjectUnknown(
00189 new NLAISCRIPT::COperandSimple(
00190 new NLAIC::CIdentType(CStringType::IdStringType))));
00191
00192 StaticMethod[CAgentScript::TRemoveChild] = new CAgentScript::CMethodCall( _REMOVECHILD_,
00193 CAgentScript::TRemoveChild,
00194 NULL,CAgentScript::CheckCount,
00195 0,
00196 new NLAISCRIPT::CObjectUnknown(
00197 new NLAISCRIPT::COperandSimple(
00198 new NLAIC::CIdentType(DigitalType::IdDigitalType))));
00199
00200
00201 }
00202
00203 void CAgentScript::releaseAgentScript()
00204 {
00205 SendParamMessageScript->release();
00206 00208
00209
00210
00211 ParamGoalMsg->release();
00213
00214 ParamRunParentNotify->release();
00215 SendCompParamMessageScript->release();
00216 sint i;
00217 for(i = 0; i < CAgentScript::TLastM; i++)
00218 delete StaticMethod[i];
00219 delete StaticMethod;
00220 }
00221
00222 00223 00224 00225 00226 00227 00228 00229 00230 00231 00232 00233 00234 00235 00236 00237 00238 00239 00240 00241 00242 00243 00244 00245 00246 00247 00248 00249 00250 00251 00252 00253 00254 00255 00256
00257
00258 CAgentScript::CAgentScript(const CAgentScript &a): IAgentManager(a)
00259 {
00260 if ( a._AgentClass )
00261 {
00262 _AgentClass = a._AgentClass;
00263 a._AgentClass->incRef();
00264 }
00265
00266 _NbComponents = a._NbComponents;
00267 _Components = new IObjectIA *[ _NbComponents ];
00268
00269 for ( int i = 0; i < _NbComponents; i++ )
00270 {
00271 _Components[i] = (IObjectIA *)a._Components[i]->clone();
00272
00273 if(((const NLAIC::CTypeOfObject &)_Components[i]->getType()) & NLAIC::CTypeOfObject::tAgentInterpret)
00274 {
00276 ((CAgentScript *)_Components[i])->setParent( (const IWordNumRef *) *this);
00277
00278
00279 }
00280 }
00281
00282 00283 00284 00285 00286 00287 00288 00289 00290
00291
00292 _AgentManager = a._AgentManager;
00293
00294
00295 std::vector<NLAILOGIC::IBaseOperator *>::const_iterator it_o = a._Operators.begin();
00296 while ( it_o != a._Operators.end() )
00297 {
00298 addOperator( *it_o );
00299 it_o++;
00300 }
00301 }
00302
00303 CAgentScript::CAgentScript(IAgentManager *manager) : IAgentManager( NULL ), _AgentClass( NULL )
00304 {
00305 _Components = NULL;
00306 _NbComponents = 0;
00307 _AgentManager = manager;
00308
00309
00310 }
00311
00312 CAgentScript::CAgentScript(IAgentManager *manager, IBasicAgent *father,
00313 std::list<IObjectIA *> &components,
00314 NLAISCRIPT::CAgentClass *agent_class )
00315
00316 : IAgentManager(father), _AgentClass( agent_class )
00317 {
00318 if ( _AgentClass )
00319 _AgentClass->incRef();
00320
00321
00322 _NbComponents = components.size();
00323 _Components = new IObjectIA *[ _NbComponents ];
00324 std::list<IObjectIA *>::iterator it_c = components.begin();
00325 int id_c = 0;
00326
00327 _AgentManager = manager;
00328 while ( it_c != components.end() )
00329 {
00330 IObjectIA *o = (IObjectIA *)*it_c;
00331 _Components[id_c] = o;
00332
00333 if(((const NLAIC::CTypeOfObject &)o->getType()) & NLAIC::CTypeOfObject::tAgentInterpret)
00334 {
00335 ((CAgentScript *)o)->setParent( (const IWordNumRef *) *this);
00336
00337
00338 }
00339
00340 it_c++;
00341 id_c++;
00342 }
00343
00344
00345 }
00346
00347 CAgentScript::~CAgentScript()
00348 {
00349 if ( _AgentClass )
00350 _AgentClass->release();
00351
00352
00353 for ( int i = 0; i < _NbComponents; i++ )
00354 _Components[i]->release();
00355 delete[] _Components;
00356
00357
00358
00359
00360 }
00361
00362 void CAgentScript::setAgentManager(IAgentManager *manager)
00363 {
00364
00365 _AgentManager = manager;
00366 for ( int i = 0; i < _NbComponents; i++ )
00367 {
00368 if(((const NLAIC::CTypeOfObject &)_Components[i]->getType()) & NLAIC::CTypeOfObject::tAgentInterpret)
00369 {
00370
00371 ((CAgentScript *)_Components[i])->setAgentManager(_AgentManager);
00372 }
00373 }
00374
00375 }
00376
00377 sint32 CAgentScript::getChildMessageIndex(const IMessageBase *msg, sint32 child_index )
00378 {
00379 return _AgentClass->getChildMessageIndex( msg, child_index );
00380 }
00381
00382 void CAgentScript::setStaticMember(sint32 index,IObjectIA *op)
00383 {
00384 #ifdef NL_DEBUG
00385 if ( index >= _NbComponents )
00386 {
00387 throw NLAIE::CExceptionIndexError();
00388 }
00389 #endif
00390 _Components[ index ]->release();
00391 _Components[ index ] = op;
00392
00393
00394 if(((const NLAIC::CTypeOfObject &) op->getType()) & NLAIC::CTypeOfObject::tAgentInterpret)
00395 {
00396 ((CAgentScript *)op)->setParent( (const IWordNumRef *) *this);
00397
00398 }
00399 }
00400
00401 sint32 CAgentScript::getStaticMemberSize() const
00402 {
00403 return _AgentClass->getStaticMemberSize();
00404 }
00405
00406
00407 const IObjectIA *CAgentScript::getStaticMember(sint32 index) const
00408 {
00409 #ifdef NL_DEBUG
00410 if ( index >= _NbComponents )
00411 {
00412 throw NLAIE::CExceptionIndexError();
00413 }
00414 #endif
00415 return _Components[ index ];
00416 }
00417
00418 NLAISCRIPT::IOpCode *CAgentScript::getMethode(sint32 inheritance,sint32 index)
00419 {
00420 #ifdef NL_DEBUG
00421 if ( index >= _AgentClass->getMethodIndexSize())
00422 {
00423 throw NLAIE::CExceptionIndexError();
00424 }
00425
00426 if ( inheritance >= _AgentClass->sizeVTable())
00427 {
00428 throw NLAIE::CExceptionIndexError();
00429 }
00430 #endif
00431 return (NLAISCRIPT::IOpCode *)_AgentClass->getBrancheCode(inheritance,index).getCode();
00432 }
00433
00434 NLAISCRIPT::IOpCode *CAgentScript::getMethode(sint32 index)
00435 {
00436 #ifdef NL_DEBUG
00437 const char *dbg_class_name = (const char *) getType();
00438 const char *dbg_base_class_name = (const char *) _AgentClass->getType();
00439
00440 if ( index >= _AgentClass->getMethodIndexSize())
00441 {
00442 throw NLAIE::CExceptionIndexError();
00443 }
00444 #endif
00445 return (NLAISCRIPT::IOpCode *)_AgentClass->getBrancheCode(index).getCode();
00446 }
00447
00448 void CAgentScript::save(NLMISC::IStream &os)
00449 {
00450 IBasicAgent::save(os);
00451 sint32 size = _NbComponents;
00452 os.serial( size );
00453 for ( int i = 0; i < _NbComponents; i++ )
00454 {
00455 os.serial( (NLAIC::CIdentType &) _Components[i]->getType() );
00456 _Components[i]->save(os);
00457 }
00458 }
00459
00460 void CAgentScript::load(NLMISC::IStream &is)
00461 {
00462 IBasicAgent::load(is);
00463
00464
00465 sint32 size;
00466 is.serial( size );
00467 _NbComponents = size;
00468
00469 if(_Components)
00470 {
00471 for ( int i = 0; i < _NbComponents; i++ )
00472 _Components[i]->release();
00473 delete[] _Components;
00474 }
00475 _Components = new IObjectIA *[ _NbComponents ];
00476
00477 for ( int i = 0; i < _NbComponents; i++ )
00478 {
00479 NLAIC::CIdentTypeAlloc id;
00480 is.serial( id );
00481 IObjectIA *tmp_c = (IObjectIA *)id.allocClass();
00482 tmp_c->load(is);
00483 _Components[i] = tmp_c;
00484 }
00485 }
00486
00487 sint32 CAgentScript::getMethodIndexSize() const
00488 {
00489 return _AgentClass->getMethodIndexSize();
00490 }
00491
00492 const NLAIC::IBasicType *CAgentScript::clone() const
00493 {
00494 CAgentScript *result = new CAgentScript(*this);
00495 return result;
00496 }
00497
00498 const NLAIC::IBasicType *CAgentScript::newInstance() const
00499 {
00500 CAgentScript *instance;
00501 if ( _AgentClass )
00502 {
00503 instance = (CAgentScript *) _AgentClass->buildNewInstance();
00504 }
00505 else
00506 {
00507 instance = new CAgentScript(NULL);
00508 }
00509 return instance;
00510 }
00511
00512 void CAgentScript::getDebugString(char *t) const
00513 {
00514 sprintf(t,"class type <%s> ",(const char *)getType());
00515 if ( _AgentClass )
00516 sprintf(&t[strlen(t)],"<%s> (scripted) -StaticComponents:\n",(const char *)_AgentClass->getType());
00517 else
00518 sprintf(&t[strlen(t)],"<undefined_class> (scripted) -StaticComponents:\n");
00519
00520 char buf[1024*8];
00521 for (int i = 0; i < _NbComponents; i++ )
00522 {
00523 strcat(t, "\t\t");
00524 if ( _AgentClass->getComponentName(i) )
00525 {
00526 strcat(t, _AgentClass->getComponentName(i) );
00527 }
00528 else
00529 strcat(t, "<unnamed>");
00530
00531 strcat(t, "\t\t");
00532 _Components[i]->getDebugString(buf);
00533 strcat(t, "[ ");
00534 strcat(t, buf);
00535 strcat(t, " ]");
00536 if(i != (_NbComponents-1)) strcat(t, "\n");
00537 }
00538 }
00539
00540 bool CAgentScript::isEqual(const IBasicObjectIA &a) const
00541 {
00542 return false;
00543 }
00544
00545 void CAgentScript::onKill(IConnectIA *a)
00546 {
00547
00548 IAgent::onKill(a);
00549 }
00550
00551 IObjectIA::CProcessResult CAgentScript::getDynamicAgent(IBaseGroupType *g)
00552 {
00553 CStringType *s = (CStringType *)g->get();
00554
00555 IObjectIA::CProcessResult r;
00556 r.ResultState = IObjectIA::ProcessIdle;
00557
00558 std::pair<tsetDefNameAgent::iterator,tsetDefNameAgent::iterator> p = _DynamicAgentName.equal_range(CKeyAgent(*s));
00559 if(p.first != p.second)
00560 {
00561 sint size = _DynamicAgentName.count(CKeyAgent(*s));
00562 sint n = 0;
00563 CVectorGroupManager *x;
00564 while(p.first != p.second)
00565 {
00566 x = new CVectorGroupManager(size);
00567 x->set(n++, *p.first->Itr);
00568 p.first++;
00569 }
00570
00571 r.Result = x;
00572 r.Result->incRef();
00573 return r;
00574
00575 }
00576 r.Result = &DigitalType::NullOperator;
00577 r.Result->incRef();
00578 return r;
00579 }
00580
00581 IObjectIA::CProcessResult CAgentScript::runAskParentNotify(IBaseGroupType *g)
00582 {
00583 CNotifyParentScript *m = new CNotifyParentScript((IBasicAgent *)getParent());
00584 m->setPerformatif(IMessageBase::PTell);
00585 this->incRef();
00586 m->setSender(this);
00587 IObjectIA::CProcessResult r;
00588 r.Result = m;
00589 return r;
00590 }
00591
00592 IObjectIA::CProcessResult CAgentScript::runTellParentNotify(IBaseGroupType *g)
00593 {
00594 sint i;
00595
00596 CNotifyParentScript *m = new CNotifyParentScript(this);
00597 m->setPerformatif(IMessageBase::PTell);
00598 this->incRef();
00599 m->setSender(this);
00600
00601 for(i = 0; i < _NbComponents; i++)
00602 {
00603 if(_Components[i] != NULL)
00604 {
00605 CNotifyParentScript *msg = (CNotifyParentScript *)m->clone();
00606 try
00607 {
00608 _Components[i]->sendMessage(msg);
00609 }
00610 catch(NLAIE::IException &)
00611 {
00612 msg->release();
00613 }
00614 }
00615 }
00616 IObjectIA::CProcessResult r;
00617 r.Result = m;
00618 return r;
00619 }
00620
00621 IObjectIA::CProcessResult CAgentScript::addDynamicAgent(IBaseGroupType *g)
00622 {
00623 CIteratorContener i = g->getIterator();
00624 CStringType &s = (CStringType &)*i++;
00625 IBasicAgent *o = (IBasicAgent *)i++;
00626
00627 IObjectIA::CProcessResult r;
00628 r.ResultState = IObjectIA::ProcessIdle;
00629
00630 o->setParent( (const IWordNumRef *) *this );
00631 CNotifyParentScript *m = new CNotifyParentScript(this);
00632 this->incRef();
00633 m->setSender(this);
00634 m->setPerformatif(IMessageBase::PTell);
00635 ((IObjectIA *)o)->sendMessage(m);
00636
00637 _DynamicAgentName.insert(CKeyAgent(s,addChild(o)));
00638
00639 r.Result = NULL;
00640
00641 return r;
00642 }
00643
00644 IObjectIA::CProcessResult CAgentScript::getDynamicName(NLAIAGENT::IBaseGroupType *g)
00645 {
00646 #ifdef NL_DEBUG
00647 char txt[1024*8];
00648 g->getDebugString(txt);
00649 #endif
00650
00651 IObjectIA::CProcessResult r;
00652 const IObjectIA *o = ((CLocalAgentMail *)g->get())->getHost();
00653 tsetDefNameAgent::iterator i = _DynamicAgentName.begin();
00654
00655 while(i != _DynamicAgentName.end())
00656 {
00657 CKeyAgent key = *i;
00658 if( o == *key.Itr )
00659 {
00660 CStringType *s = new CStringType(key.Name);
00661 r.Result = s;
00662 return r;
00663 }
00664 i ++;
00665 }
00666 r.Result = new CStringType(CStringVarName("Unknown"));
00667 return r;
00668 }
00669 IObjectIA::CProcessResult CAgentScript::removeDynamic(NLAIAGENT::IBaseGroupType *g)
00670 {
00671 CStringType *s = (CStringType *)g->get();
00672 tsetDefNameAgent::iterator i = _DynamicAgentName.find(CKeyAgent(*s));
00673 IObjectIA::CProcessResult r;
00674 r.ResultState = IObjectIA::ProcessIdle;
00675
00676 if(i != _DynamicAgentName.end())
00677 {
00678
00679
00680
00681 throw;
00682 }
00683 r.Result = &DigitalType::NullOperator;
00684 r.Result->incRef();
00685 return r;
00686 }
00687
00688 IObjectIA::CProcessResult CAgentScript::sendMethod(IObjectIA *param)
00689 {
00690 INombreDefine *p = (INombreDefine *)((IBaseGroupType *)param)->popFront();
00691 IMessageBase *msg = (IMessageBase *)((IBaseGroupType *)param)->popFront();
00692 msg->setPerformatif((IMessageBase::TPerformatif)(sint)p->getNumber());
00693 p->release();
00694 return sendMessage(msg);
00695 }
00696
00697 IObjectIA::CProcessResult CAgentScript::sendMethodCompoment(IObjectIA *param)
00698 {
00699 IMessageBase *msg = (IMessageBase *)((IBaseGroupType *)param)->pop();
00700 return sendMessage(msg);
00701 }
00702
00703 IObjectIA::CProcessResult CAgentScript::sendMessage(const IVarName &compName,IObjectIA *msg)
00704 {
00705 IObjectIA *comp = (IObjectIA *)getStaticMember(getStaticMemberIndex(compName));
00706 if(comp != NULL)
00707 {
00708 comp->sendMessage(msg);
00709 }
00710 return IObjectIA::CProcessResult();
00711 }
00712
00713 IObjectIA::CProcessResult CAgentScript::sendMessage(IObjectIA *m)
00714 {
00715 #ifdef NL_DEBUG
00716 const char *txt = (const char *)m->getType();
00717 #endif
00718 IMessageBase *msg = (IMessageBase *)m;
00719 this->incRef();
00720 msg->setReceiver(this);
00721
00722 {
00723 00724 00725 00726
00727 if( ((const NLAIC::CTypeOfObject &)m->getType()) & NLAIC::CTypeOfObject::tAgentInterpret)
00728 {
00729 char runMsg[1024];
00730 strcpy(runMsg,_RUN_);
00731 switch(msg->getPerformatif())
00732 {
00733 case IMessageBase::PExec:
00734 strcat(runMsg,"Exec");
00735 break;
00736 case IMessageBase::PAchieve:
00737 strcat(runMsg,"Achieve");
00738 break;
00739 case IMessageBase::PAsk:
00740 strcat(runMsg,"Ask");
00741 break;
00742 case IMessageBase::PTell:
00743 strcat(runMsg,"Tell");
00744 break;
00745 case IMessageBase::PBreak:
00746 strcat(runMsg,"Break");
00747 break;
00748 case IMessageBase::PKill:
00749 strcat(runMsg,"Kill");
00750 break;
00751 case IMessageBase::PError:
00752 strcat(runMsg,"Error");
00753 break;
00754 }
00755
00756 NLAISCRIPT::COperandSimple *t = new NLAISCRIPT::COperandSimple(new NLAIC::CIdentType(m->getType()));
00757 NLAISCRIPT::CParam p(1,t);
00758
00759 CStringVarName tmp_name = CStringVarName(runMsg);
00760 tQueue r = isMember(NULL,&tmp_name,p);
00761 if(r.size())
00762 {
00763 NLAIAGENT::CIdMethod m = r.top();
00764 msg->setMethodIndex(0,m.Index);
00765
00766 }
00767
00768 }
00769
00770 }
00771 return IAgent::sendMessage(msg);
00772
00773 }
00774
00775 void CAgentScript::runChildren()
00776 {
00777 #ifdef NL_DEBUG
00778 const char *classBase = (const char *)getType();
00779 #endif
00780
00781 for ( int i = 0; i < _NbComponents; i++ )
00782 {
00783 IObjectIA *o = _Components[i];
00784 o->run();
00785 }
00786
00787
00788 IAgent::runChildren();
00789 }
00790
00791 void CAgentScript::processMessages(IMessageBase *msg,IObjectIA *c)
00792 {
00793 #ifdef NL_DEBUG
00794 const char *txt = (const char *)msg->getType();
00795 #endif
00796 NLAISCRIPT::CCodeContext &context = (NLAISCRIPT::CCodeContext &)*c;
00797 IBaseGroupType *param = new CGroupType();
00798 msg->incRef();
00799 param->push(msg);
00800 context.Stack ++;
00801 context.Stack[(int)context.Stack] = param;
00802
00803 int indexM = msg->getMethodIndex() - getBaseMethodCount();
00804 if(indexM >= 0)
00805 {
00806 IObjectIA *code = getMethode(indexM);
00807 if(code == NULL)
00808 {
00809 getMail()->popMessage();
00810 return;
00811 }
00812 }
00813
00814 NLAISCRIPT::IMethodContext *methodContex;
00815
00816 if (context.ContextDebug.Active)
00817 {
00818 context.ContextDebug.Param.push_back(&listBidon);
00819 listBidon.incRef();
00820 methodContex = new NLAISCRIPT::CMethodContextDebug();
00821 }
00822 else
00823 {
00824 methodContex = new NLAISCRIPT::CMethodContext();
00825 }
00826 const IObjectIA *self = context.Self;
00827 context.Self = this;
00828 NLAISCRIPT::CCallMethod opCall(methodContex,msg->getHeritanceIndex(),msg->getMethodIndex());
00829 opCall.runOpCode(context);
00830 context.Self = self;
00831 IMessageBase *returnMsg = (IMessageBase *)context.Stack[(int)context.Stack];
00832 returnMsg->incRef();
00833 context.Stack--;
00834 switch(msg->getPerformatif())
00835 {
00836 case IMessageBase::PExec:
00837 if(msg->getContinuation() != NULL)
00838 {
00839 IMessageBase *o = (IMessageBase *)returnMsg->clone();
00840 o->setMethodIndex(-1,-1);
00841 this->incRef();
00842 o->setSender(this);
00843 ((IObjectIA *)msg->getContinuation())->incRef();
00844 o->setReceiver((IObjectIA *)msg->getContinuation());
00845 ((IObjectIA *)msg->getContinuation())->sendMessage(o);
00846 }
00847 break;
00848 case IMessageBase::PAchieve:
00849 if(msg->getContinuation() != NULL)
00850 {
00851 IMessageBase *o = (IMessageBase *)returnMsg->clone();
00852 o->setMethodIndex(-1,-1);
00853 this->incRef();
00854 o->setSender(this);
00855 ((IObjectIA *)msg->getContinuation())->incRef();
00856 o->setReceiver((IObjectIA *)msg->getContinuation());
00857 ((IObjectIA *)msg->getContinuation())->sendMessage(o);
00858 }
00859 break;
00860 case IMessageBase::PAsk:
00861 {
00862 IMessageBase *o = (IMessageBase *)returnMsg->clone();
00863 o->setMethodIndex(-1,-1);
00864 this->incRef();
00865 o->setSender(this);
00866 o->setPerformatif(IMessageBase::PTell);
00867 if(returnMsg->getSender() != NULL) ((IObjectIA *)returnMsg->getSender())->incRef();
00868 o->setReceiver((IObjectIA *)returnMsg->getSender());
00869 ((IObjectIA *)msg->getSender())->sendMessage(o);
00870
00871
00872 if(msg->getContinuation() != NULL)
00873 {
00874 IMessageBase *o = (IMessageBase *)returnMsg->clone();
00875 o->setMethodIndex(-1,-1);
00876 this->incRef();
00877 o->setSender(this);
00878 if(msg->getContinuation() != NULL) ((IObjectIA *)msg->getContinuation())->incRef();
00879 o->setReceiver((IObjectIA *)msg->getContinuation());
00880 o->setPerformatif(IMessageBase::PTell);
00881 ((IObjectIA *)msg->getContinuation())->sendMessage(o);
00882 }
00883 }
00884 break;
00885 case IMessageBase::PTell:
00886 if(msg->getContinuation() != NULL)
00887 {
00888 IMessageBase *o = (IMessageBase *)returnMsg->clone();
00889 o->setMethodIndex(-1,-1);
00890 this->incRef();
00891 o->setSender(this);
00892 if(msg->getContinuation() != NULL) ((IObjectIA *)msg->getContinuation())->incRef();
00893 o->setReceiver((IObjectIA *)msg->getContinuation());
00894 ((IObjectIA *)msg->getContinuation())->sendMessage(o);
00895 }
00896 break;
00897 case IMessageBase::PBreak:
00898 if(msg->getContinuation() != NULL)
00899 {
00900 IMessageBase *o = (IMessageBase *)returnMsg->clone();
00901 o->setMethodIndex(-1,-1);
00902 this->incRef();
00903 o->setSender(this);
00904 if(msg->getContinuation() != NULL) ((IObjectIA *)msg->getContinuation())->incRef();
00905 o->setReceiver((IObjectIA *)msg->getContinuation());
00906 ((IObjectIA *)msg->getContinuation())->sendMessage(o);
00907 }
00908 break;
00909 case IMessageBase::PKill:
00910 if(msg->getContinuation() != NULL)
00911 {
00912 IMessageBase *o = (IMessageBase *)returnMsg->clone();
00913 o->setMethodIndex(-1,-1);
00914 this->incRef();
00915 o->setSender(this);
00916 if(msg->getContinuation() != NULL) ((IObjectIA *)msg->getContinuation())->incRef();
00917 o->setReceiver((IObjectIA *)msg->getContinuation());
00918 ((IObjectIA *)msg->getContinuation())->sendMessage(o);
00919 }
00920 break;
00921
00922 }
00923 returnMsg->release();
00924 }
00925
00926 void CAgentScript::processMessages()
00927 {
00928 IObjectIA *c;
00929 if( _AgentManager != NULL) c = (IObjectIA *)_AgentManager->getAgentContext();
00930 else c = NULL;
00931 while(getMail()->getMessageCount())
00932 {
00933 IMessageBase &msg = (IMessageBase &)getMail()->getMessage();
00934 if(msg.getMethodIndex() >= 0)
00935 {
00936 processMessages(&msg,c);
00937 getMail()->popMessage();
00938 }
00939 else
00940 {
00941 IAgent::processMessages();
00942 }
00943 }
00944 }
00945
00946 IObjectIA::CProcessResult CAgentScript::runActivity()
00947 {
00948
00949 NLAISCRIPT::CCodeContext *context = (NLAISCRIPT::CCodeContext *)_AgentManager->getAgentContext();
00950 context->Self = this;
00951 runMethodeMember(_AgentClass->getRunMethod(), context);
00952
00953 return ProcessRun;
00954 }
00955
00956 bool CAgentScript::haveActivity() const
00957 {
00958 return (_AgentClass->getRunMethod() >= 0);
00959 }
00960
00961 const IObjectIA::CProcessResult &CAgentScript::run()
00962 {
00963 setState(processBuzzy,NULL);
00964
00965 #ifdef NL_DEBUG
00966 const char *dbg_class_name = (const char *) getType();
00967
00968 #endif
00969
00970
00971 getMail()->run();
00972 runChildren();
00973
00974 processMessages();
00975
00976 if(haveActivity()) runActivity();
00977
00978 setState(processIdle,NULL);
00979 return getState();
00980 }
00981
00982 void CAgentScript::addOperator(NLAILOGIC::IBaseOperator *op)
00983 {
00984 _Operators.push_back(op);
00985 }
00986
00987 void CAgentScript::remOperator(NLAILOGIC::IBaseOperator *op)
00988 {
00989 std::vector<NLAILOGIC::IBaseOperator *>::iterator it_o = _Operators.begin();
00990 while ( it_o != _Operators.end() )
00991 {
00992 if ( op == *it_o )
00993 {
00994 _Operators.erase( it_o );
00995 return;
00996 }
00997 it_o++;
00998 }
00999 char buf[2048];
01000 op->getDebugString(buf);
01001 throw NLAIE::CExceptionObjectNotFoundError(buf);
01002 }
01003
01004 const NLAIC::CIdentType &CAgentScript::getType() const
01005 {
01006 if ( _AgentClass )
01007 return _AgentClass->getType();
01008 else
01009 return IdAgentScript;
01010 }
01011
01012 IObjectIA::CProcessResult CAgentScript::runMethodBase(int heritance, int index,IObjectIA *o)
01013 {
01014 switch(index - IAgent::getMethodIndexSize())
01015 {
01016 case TSend:
01017 {
01018 return sendMethod(o);
01019 }
01020
01021 case TSendComponent:
01022 {
01023 return sendMethodCompoment(o);
01024 }
01025
01026 case TGetChildTag:
01027 {
01028 return getDynamicAgent((IBaseGroupType *)o);
01029 }
01030
01031 case TAddChildTag:
01032 {
01033 return addDynamicAgent((IBaseGroupType *)o);
01034 }
01035 case TFather:
01036 {
01037 IObjectIA::CProcessResult a;
01038 a.Result = (IObjectIA *)getParent();
01039 a.Result->incRef();
01040 return a;
01041 }
01042
01043 case TSelf:
01044 {
01045 IObjectIA::CProcessResult a;
01046 a.Result = new CLocalAgentMail(this);
01047 return a;
01048 }
01049 case TGetName:
01050 {
01051 return getDynamicName((IBaseGroupType *)o);
01052 }
01053
01054 case TRemoveChild:
01055 {
01056 return removeDynamic((IBaseGroupType *)o);
01057 }
01058
01059 case TRunAskParentNotify:
01060 {
01061 return runAskParentNotify((IBaseGroupType *)o);
01062 }
01063
01064 case TRunTellParentNotify:
01065 {
01066 return runTellParentNotify((IBaseGroupType *)o);
01067 }
01069
01070
01071 case TGoal:
01072 {
01073 return runGoalMsg((IBaseGroupType *)o);
01074 }
01076
01077
01078 default:
01079 return IAgent::runMethodeMember(index,o);
01080
01081 }
01082
01083 }
01084
01085 IObjectIA::CProcessResult CAgentScript::runMethodBase(int index,IObjectIA *o)
01086 {
01087 switch(index - IAgent::getMethodIndexSize())
01088 {
01089 case TSend:
01090 {
01091 return sendMethod(o);
01092 }
01093
01094 case TSendComponent:
01095 {
01096 return sendMethodCompoment(o);
01097 }
01098
01099 case TGetChildTag:
01100 {
01101 return getDynamicAgent((IBaseGroupType *)o);
01102 }
01103
01104 case TAddChildTag:
01105 {
01106 return addDynamicAgent((IBaseGroupType *)o);
01107 }
01108 case TFather:
01109 {
01110 IObjectIA::CProcessResult a;
01111 a.Result = (IObjectIA *)getParent();
01112 a.Result->incRef();
01113 return a;
01114 }
01115
01116 case TSelf:
01117 {
01118 IObjectIA::CProcessResult a;
01119 a.Result = new CLocalAgentMail(this);
01120 return a;
01121 }
01122 case TGetName:
01123 {
01124 return getDynamicName((IBaseGroupType *)o);
01125 }
01126
01127 case TRunAskParentNotify:
01128 {
01129 return runAskParentNotify((IBaseGroupType *)o);
01130 }
01131
01132 case TRunTellParentNotify:
01133 {
01134 return runTellParentNotify((IBaseGroupType *)o);
01135 }
01136
01138
01139
01140 case TGoal:
01141 {
01142 return runGoalMsg((IBaseGroupType *)o);
01143 }
01145
01146 default:
01147 return IAgent::runMethodeMember(index,o);
01148 }
01149 }
01150
01151 int CAgentScript::getBaseMethodCount() const
01152 {
01153 return IAgentManager::getBaseMethodCount() + CAgentScript::TLastM;
01154 }
01155
01156 IObjectIA::CProcessResult CAgentScript::runMethodeMember(sint32 inheritance, sint32 index, IObjectIA *c)
01157 {
01158
01159 if(c->getType() != NLAISCRIPT::CCodeContext::IdCodeContext)
01160 {
01161 return IAgent::runMethodeMember(inheritance,index, c);
01162 }
01163
01164 NLAISCRIPT::IOpCode *opPtr = NULL;
01165 NLAISCRIPT::CCodeContext &context = (NLAISCRIPT::CCodeContext &)*c;
01166
01167 int i = index - getBaseMethodCount();
01168 if(i < 0)
01169 {
01170 if (context.ContextDebug.Active)
01171 {
01172 context.ContextDebug.Param.push_back(&listBidon);
01173 listBidon.incRef();
01174 }
01175
01176 IObjectIA::CProcessResult r = runMethodBase(index,(IObjectIA *)context.Param.back());
01177 if(r.Result != NULL)
01178 {
01179 context.Stack++;
01180 context.Stack[(int)context.Stack] = r.Result;
01181 }
01182 r.Result = NULL;
01183 return r;
01184 }
01185 else
01186 {
01187 opPtr = getMethode(inheritance,i);
01188 }
01189
01190 IObjectIA::CProcessResult r;
01191 if(opPtr != NULL)
01192 {
01193 NLAISCRIPT::IOpCode &op = *opPtr;
01194 NLAISCRIPT::CCodeBrancheRun *opTmp = context.Code;
01195 int ip = (uint32)*context.Code;
01196 context.Code = (NLAISCRIPT::CCodeBrancheRun *)&op;
01197 *context.Code = 0;
01198
01199 01200 01201 01202 01203 01204
01205
01206 r = ((NLAISCRIPT::ICodeBranche *)opPtr)->run(context);
01207
01208 if (context.ContextDebug.Active)
01209 {
01210 context.ContextDebug.callStackPop();
01211 }
01212 *context.Code = ip;
01213 context.Code = opTmp;
01214 }
01215 return r;
01216 }
01217
01218 IObjectIA::CProcessResult CAgentScript::runMethodeMember(sint32 index, IObjectIA *c)
01219 {
01220 if(c->getType() != NLAISCRIPT::CCodeContext::IdCodeContext)
01221 {
01222 return IAgent::runMethodeMember(index, c);
01223 }
01224
01225 NLAISCRIPT::IOpCode *opPtr = NULL;
01226 NLAISCRIPT::CCodeContext &context = (NLAISCRIPT::CCodeContext &)*c;
01227
01228 int i = index - getBaseMethodCount();
01229 if(i < 0)
01230 {
01231 if (context.ContextDebug.Active)
01232 {
01233 context.ContextDebug.Param.push_back(&listBidon);
01234 listBidon.incRef();
01235 }
01236
01237 IObjectIA::CProcessResult r = runMethodBase(index,(IObjectIA *)context.Param.back());
01238 if(r.Result != NULL)
01239 {
01240 context.Stack++;
01241 context.Stack[(int)context.Stack] = r.Result;
01242 }
01243 r.Result = NULL;
01244 return r;
01245 }
01246 else
01247 {
01248 opPtr = getMethode(i);
01249 }
01250 IObjectIA::CProcessResult r;
01251 if(opPtr != NULL)
01252 {
01253 NLAISCRIPT::IOpCode &op = *opPtr;
01254 NLAISCRIPT::CCodeBrancheRun *opTmp = context.Code;
01255 int ip = (uint32)*context.Code;
01256 context.Code = (NLAISCRIPT::CCodeBrancheRun *)opPtr;
01257 *context.Code = 0;
01258
01259 01260 01261 01262 01263 01264
01265 r = ((NLAISCRIPT::ICodeBranche *)opPtr)->run(context);
01266
01267
01268 if (context.ContextDebug.Active)
01269 {
01270 context.ContextDebug.callStackPop();
01271 }
01272
01273 *context.Code = ip;
01274 context.Code = opTmp;
01275 }
01276 return r;
01277 }
01278
01279 tQueue CAgentScript::getPrivateMember(const IVarName *className,const IVarName *methodName,const IObjectIA ¶m) const
01280 {
01281 sint i;
01282 CAgentScript::TMethodNumDef index = CAgentScript::TLastM;
01283 for(i = 0; i < CAgentScript::TLastM; i ++)
01284 {
01285 if(CAgentScript::StaticMethod[i]->MethodName == *methodName)
01286 {
01287 index = (CAgentScript::TMethodNumDef)CAgentScript::StaticMethod[i]->Index;
01288 switch(CAgentScript::StaticMethod[i]->CheckArgType)
01289 {
01290 case CAgentScript::CheckAll:
01291 {
01292 double d = ((NLAISCRIPT::CParam &)*CAgentScript::StaticMethod[i]->ArgType).eval((NLAISCRIPT::CParam &)param);
01293 if(d >= 0.0)
01294 {
01295 tQueue r;
01296 CAgentScript::StaticMethod[i]->ReturnValue->incRef();
01297 r.push(CIdMethod( (IAgent::getMethodIndexSize() + CAgentScript::StaticMethod[i]->Index),
01298 0.0,
01299 NULL,
01300 CAgentScript::StaticMethod[i]->ReturnValue));
01301 return r;
01302 }
01303 }
01304 index = CAgentScript::TLastM;
01305 break;
01306
01307
01308 case CAgentScript::CheckCount:
01309 {
01310 if(((NLAISCRIPT::CParam &)param).size() == CAgentScript::StaticMethod[i]->ArgCount)
01311 {
01312 tQueue r;
01313 CAgentScript::StaticMethod[i]->ReturnValue->incRef();
01314 r.push(CIdMethod( (IAgent::getMethodIndexSize() + CAgentScript::StaticMethod[i]->Index),
01315 0.0,
01316 NULL,
01317 CAgentScript::StaticMethod[i]->ReturnValue ));
01318 return r;
01319 }
01320 }
01321 index = CAgentScript::TLastM;
01322 break;
01323
01324 case CAgentScript::DoNotCheck:
01325 {
01326 tQueue r;
01327 CAgentScript::StaticMethod[i]->ReturnValue->incRef();
01328 r.push(CIdMethod( (IAgent::getMethodIndexSize() + CAgentScript::StaticMethod[i]->Index),
01329 0.0,
01330 NULL,
01331 CAgentScript::StaticMethod[i]->ReturnValue));
01332 return r;
01333 }
01334 index = CAgentScript::TLastM;
01335 break;
01336 }
01337 }
01338 }
01339 return tQueue();
01340 }
01341
01342 tQueue CAgentScript::isMember(const IVarName *className,const IVarName *methodName,const IObjectIA ¶m) const
01343 {
01344 #ifdef NL_DEBUG
01345 char nameM[1024*4];
01346 char nameP[1024*4];
01347 char name[1024*8];
01348 methodName->getDebugString(nameM);
01349 param.getDebugString(nameP);
01350 sprintf(name,"%s%s",nameM,nameP);
01351
01352 const char *dbg_class_name = (const char *) getType();
01353 const char *dbg_base_class_name = NULL;
01354 if(_AgentClass) dbg_base_class_name = (const char *) _AgentClass->getType();
01355 #endif
01356
01357
01358 if(className == NULL)
01359 {
01360
01361 tQueue r;
01362 if(_AgentClass != NULL) r = _AgentClass->getPrivateMember(className,methodName,param);
01363
01364 if(r.size()) return r;
01365 else
01366 {
01367 r = getPrivateMember(className,methodName,param);
01368 if(r.size()) return r;
01369 else
01370 {
01371 return IAgent::isMember(className,methodName,param);
01372 }
01373 }
01374
01375 }
01376 else
01377 if(*className == CStringVarName("Agent"))
01378 {
01379 tQueue r;
01380 r = getPrivateMember(className,methodName,param);
01381 if(r.size()) return r;
01382 else
01383 {
01384 r = _AgentClass->getPrivateMember(className,methodName,param);
01385
01386 if(r.size()) return r;
01387 else
01388 {
01389 return IAgent::isMember(className,methodName,param);
01390 }
01391
01392 }
01393
01394 }
01395 return tQueue();
01396 }
01397
01398 sint32 CAgentScript::isClassInheritedFrom(const IVarName &name) const
01399 {
01400 return _AgentClass->isClassInheritedFrom( name );
01401 }
01402
01403 sint32 CAgentScript::getStaticMemberIndex(const IVarName &name) const
01404 {
01405 return _AgentClass->getInheritedStaticMemberIndex(name);
01406 }
01407
01409
01410
01411 IObjectIA::CProcessResult CAgentScript::runGoalMsg(IBaseGroupType *g)
01412 {
01413 NLAILOGIC::CGoal *goal = (NLAILOGIC::CGoal *) g->get();
01414 #ifdef NL_DEBUG
01415 char buffer[1024 * 2];
01416 goal->getDebugString( buffer );
01417 #endif
01418 _GoalStack.push_back( goal );
01419
01420 IObjectIA::CProcessResult r;
01421 r.Result = NULL;
01422 return r;
01423 }
01425 }