# 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  

main_agent_script.cpp

Go to the documentation of this file.
00001 
00006 /* Copyright, 2000 Nevrax Ltd.
00007  *
00008  * This file is part of NEVRAX NEL.
00009  * NEVRAX NEL is free software; you can redistribute it and/or modify
00010  * it under the terms of the GNU General Public License as published by
00011  * the Free Software Foundation; either version 2, or (at your option)
00012  * any later version.
00013 
00014  * NEVRAX NEL is distributed in the hope that it will be useful, but
00015  * WITHOUT ANY WARRANTY; without even the implied warranty of
00016  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
00017  * General Public License for more details.
00018 
00019  * You should have received a copy of the GNU General Public License
00020  * along with NEVRAX NEL; see the file COPYING. If not, write to the
00021  * Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
00022  * MA 02111-1307, USA.
00023  */
00024 #include "nel/ai/agent/agent.h"
00025 #include "nel/ai/agent/main_agent_script.h"
00026 #include "nel/ai/e/ai_exception.h"
00027 
00028 namespace NLAIAGENT
00029 {
00030 
00031         const sint StackMemSize = 256;//1024*32;
00032         CMainAgentScript::CMainAgentScript(const CMainAgentScript &a): IMainAgent(a)
00033         {
00034                 _Stack = new NLAISCRIPT::CStackPointer(StackMemSize);
00035                 _Heap = new NLAISCRIPT::CStackPointer(StackMemSize);
00036 
00037                 _CodeContext = new NLAISCRIPT::CCodeContext(*_Stack,*_Heap,NULL,this,a._CodeContext->InputOutput);
00038         }
00039 
00040         CMainAgentScript::CMainAgentScript(IAgentManager *main,NLAIC::IIO *io):IMainAgent (main)
00041         {
00042                 _Stack = new NLAISCRIPT::CStackPointer(StackMemSize);
00043                 _Heap = new NLAISCRIPT::CStackPointer(StackMemSize);
00044                 _CodeContext = new NLAISCRIPT::CCodeContext(*_Stack,*_Heap,NULL,this,io);
00045         }
00046 
00047         CMainAgentScript::CMainAgentScript(IAgentManager *a,NLAIC::IIO *io, IBasicAgent *b, std::list<IObjectIA *> &v, NLAISCRIPT::CAgentClass *c):IMainAgent(a,b,v,c)
00048         {
00049                 _Stack = new NLAISCRIPT::CStackPointer(StackMemSize);
00050                 _Heap = new NLAISCRIPT::CStackPointer(StackMemSize);
00051                 _CodeContext = new NLAISCRIPT::CCodeContext(*_Stack,*_Heap,NULL,this,io);
00052         }
00053         
00054         CMainAgentScript::CMainAgentScript(NLAIC::IIO *io):IMainAgent (NULL)
00055         {               
00056                 _Stack = new NLAISCRIPT::CStackPointer(StackMemSize);
00057                 _Heap = new NLAISCRIPT::CStackPointer(StackMemSize);
00058                 _CodeContext = new NLAISCRIPT::CCodeContext(*_Stack,*_Heap,NULL,this,io);
00059         }
00060 
00061         CMainAgentScript::~CMainAgentScript()
00062         {
00063                 Kill();
00064                 _CodeContext->release();
00065                 delete _Stack;
00066                 delete _Heap;
00067         }
00068 
00069         const IObjectIA *CMainAgentScript::getAgentContext() const
00070         {
00071                 return _CodeContext;
00072         }
00073 
00074         int CMainAgentScript::getBaseMethodCount() const
00075         {
00076                 return CAgentScript::getBaseMethodCount();
00077         }
00078 
00079         IObjectIA::CProcessResult CMainAgentScript::sendMessage(IObjectIA *m)
00080         {
00081                 return CAgentScript::sendMessage(m);
00082         }
00083 
00084         const NLAIC::IBasicType *CMainAgentScript::clone() const
00085         {
00086                 NLAIC::IBasicType *x = new CMainAgentScript(*this);
00087                 return x;
00088         }
00089         
00090         const NLAIC::IBasicType *CMainAgentScript::newInstance() const
00091         {
00092                 NLAIC::IBasicType *x = new CMainAgentScript(_CodeContext->InputOutput);
00093                 return x;
00094         }
00095 
00096         IMessageBase *CMainAgentScript::runExec(const IMessageBase &m)
00097         {
00098                 if(m.getGroup().getId() == 1)
00099                 {                       
00100                         _CodeContext->Self = this;
00101 
00102                         CConstIteratorContener i = m.getConstIterator();
00103                         NLAISCRIPT::CCodeBrancheRun *o = (NLAISCRIPT::CCodeBrancheRun *)i++;                                            
00104                         NLAISCRIPT::CStackPointer stack;
00105                         NLAISCRIPT::CStackPointer heap;
00106                         _CodeContext->InputOutput->incRef();
00107                         NLAISCRIPT::CCodeContext codeContext(stack,heap,NULL,this,_CodeContext->InputOutput);
00108                         codeContext.Code = o;
00109                         (void)o->run(codeContext);
00110                         heap -= (int)heap;
00111                         stack -= (int)stack;
00112                 }
00113                 return NULL;
00114         }       
00115 
00116         void CMainAgentScript::processMessages()
00117         {
00118                 /*if(getFactoryClass() != NULL)
00119                 {               
00120                         NLAISCRIPT::CCodeContext &context = (NLAISCRIPT::CCodeContext &)*getAgentManager()->getAgentContext();
00121                         while(getMail()->getMessageCount())
00122                         {
00123                                 const IMessageBase &msg = getMail()->getMessage();
00124                                 IBaseGroupType *param = new CGroupType();
00125                                 param->push(&msg);
00126                                 context.Stack ++;
00127                                 context.Stack[(int)context.Stack] = param;
00128                                 if(msg.getHeritanceIndex())
00129                                 {
00130                                         runMethodeMember(msg.getHeritanceIndex(),msg.getMethodIndex(),&context);
00131                                 }                       
00132                         }
00133                 }*/
00134                 try
00135                 {
00136                         CAgentScript::processMessages();
00137                 }
00138                 catch(NLAIE::IException &e)
00139                 {                       
00140                         const char *w = e.what();
00141                         _CodeContext->InputOutput->Echo("\n\n%s\n\n",(char *)w);
00142                 }
00143         }
00144 
00145         const IObjectIA::CProcessResult &CMainAgentScript::run()
00146         {               
00147                 runChildren();
00148 
00149                 //((IMailBox *)getLocalMailBox())->run();
00150                 getMail()->run();
00151                 
00152                 if(getFactoryClass() != NULL && getFactoryClass()->getRunMethod() >= 0) 
00153                 {
00154                         runMethodeMember(getFactoryClass()->getRunMethod(),_CodeContext);
00155                 }                               
00156 
00157                 processMessages();
00158 
00159                 if(haveActivity() && getState().ResultState == processIdle) runActivity();
00160                 
00161                 return getState();
00162         }
00163 
00164         IObjectIA::CProcessResult CMainAgentScript::addDynamicAgent(IBaseGroupType *g)
00165         {
00166                 CIteratorContener i = g->getIterator();
00167                 i++;
00168                 IBasicAgent *o = (IBasicAgent *)i++;
00169 
00170                 uint b = NLAIC::CTypeOfObject::tInterpret | NLAIC::CTypeOfObject::tAgent;
00171                 const NLAIC::CTypeOfObject &t = o->getType();
00172                 if((t.getValue() & b) == b)
00173                 {
00174                         ((CAgentScript *)o)->setAgentManager(this);
00175                 }
00176                 
00177                 return CAgentScript::addDynamicAgent(g);                
00178         }
00179 }