# 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  

interpret_methodes.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/script/interpret_object.h"
00025 #include "nel/ai/script/interpret_methodes.h"
00026 #include "nel/ai/script/type_def.h"
00027 #include <math.h>
00028 #include <stdarg.h>
00029 
00030 namespace NLAISCRIPT
00031 {
00032         //const NLAIAGENT::TProcessStatement CParam::_RunState = NLAIAGENT::processIdle;
00033 
00034         CParam::CParam():_Param(0),_ParamInfo(NULL){}
00035         CParam::CParam(const CParam &p):NLAIAGENT::IObjectIA(p),_Param(p._Param.size()),_ParamInfo(NULL)
00036         {
00037                 for(sint32 i = 0; i<p.size(); i++)
00038                 {
00039                         _Param[i] = (IOpType *)p._Param[i]->clone();//new NLAIC::CIdentType(*p._Param[i]);
00040                 }
00041         }
00042 
00043         CParam::CParam(int count, ...):_ParamInfo(NULL)
00044         {
00045                 va_list marker;
00046                 
00047                 va_start( marker, count );
00048                 while(count --)
00049                 {
00050                         IOpType *o = va_arg( marker, IOpType *);
00051                         _Param.push_back(o);
00052                 }
00053         }
00054 
00055         bool CParam::operator == (const CParam &p) const
00056         {
00057                 sint32 l;
00058                 if(( l = _Param.size() ) != (sint32)p._Param.size()) return false;
00059                 for(sint32 i = 0; i<l; i++)
00060                 {
00061                         IOpType &p1 = *_Param[i], &p2 = *p._Param[i];
00062                         if(p1.getConstraintTypeOf() == NULL || p2.getConstraintTypeOf() == NULL) return false;
00063                         if(!(*p1.getConstraintTypeOf() == *p2.getConstraintTypeOf())) return false;                     
00064                 }
00065                 return true;
00066         }
00067 
00068         double CParam::eval(const CParam &p) const
00069         {
00070                 sint32 l;
00071                 double D = 0.0;         
00072                 if(( l = _Param.size() ) != (sint32)p._Param.size()) return -1.0;
00073                 double k = 1.0;
00074                 for(sint32 i = 0; i<l; i++)
00075                 {
00076                         IOpType &p1 = *_Param[i], &p2 = *p._Param[i];
00077                         double r = p1.eval(&p2);
00078                         if( r < 0) return -1.0;
00079                         D += (r*r)*k;
00080                         k += 1.0;
00081                         /*if(p1.getConstraintTypeOf() != NULL && p2.getConstraintTypeOf() != NULL)
00082                         {                               
00083                                 
00084                                 if(!(p1 == p2))
00085                                 {
00086                                         const NLAIC::CIdentType &idG = *p1.getConstraintTypeOf();
00087                                         const NLAIC::CIdentType &idD = *p2.getConstraintTypeOf();
00088                                         if(!(idG == idD))
00089                                         {
00090                                                 if(((const NLAIC::CTypeOfObject &)idD) & NLAIC::CTypeOfObject::tAgentInterpret)
00091                                                 {
00092                                                         const IClassInterpret *o = (const IClassInterpret *)((CClassInterpretFactory *)idD.getFactory())->getClass();                                           
00093                                                         bool type = false;                                              
00094                                                         sint32 i = 0;                                           
00095                                                         while(o != NULL)
00096                                                         {
00097                                                                 i ++;                                                   
00098                                                                 if( o->getType() == idG)
00099                                                                 {
00100                                                                         d = ((double)(o->sizeVTable() - i - 1))*k;
00101                                                                         D += d*d;
00102                                                                         type = true;
00103                                                                 }
00104                                                                 o = o->getBaseClass();
00105                                                         }
00106                                                         if(!type) return -1.0;
00107                                                 }
00108                                                 else
00109                                                 {
00110                                                         //NLAIC::CTypeOfObject o_t(tNombre | tString | tList | tLogic);
00111                                                         if(((const NLAIC::CTypeOfObject &)idD) & ((const NLAIC::CTypeOfObject &)idG))
00112                                                         {
00113                                                                 return 0.0;
00114                                                         }
00115                                                         else return -1.0;
00116                                                 }
00117                                         }
00118                                 }
00119                                 else return 0.0;
00120                                 
00121                                 
00122                         }*/             
00123                 }
00124                 if(D != 0.0) return sqrt(D);
00125                 else return 0.0;
00126         }
00127 
00128         void CParam::operator = (const CParam &p)
00129         {
00130                 for(sint32 i = 0; i < p.size()  ; i ++)
00131                 {
00132                         _Param.push_back((IOpType *)p[i]->clone());
00133                 }
00134         }
00135 
00136         const NLAIC::IBasicType *CParam::clone() const
00137         {
00138                 CParam *cl = new CParam( *this );
00139                 return cl;
00140         }
00141 
00142         const NLAIC::IBasicType *CParam::newInstance() const
00143         {
00144                 CParam *instance = new CParam();
00145                 return instance;
00146         }
00147 
00148 
00149         const IOpType *CParam::operator[] (sint32 i) const
00150         {
00151                 return _Param[i];
00152         }
00153 
00154         void CParam::push(const NLAIAGENT::IBaseGroupType &g)
00155         {
00156                 NLAIAGENT::CConstIteratorContener  i = g.getConstIterator();
00157 
00158                 while( !i.isInEnd() )
00159                 {
00160                         const NLAIAGENT::IObjectIA *o = i++;
00161                         push(COperandSimple(new NLAIC::CIdentType(o->getType())));
00162                 }
00163         }
00164 
00165         void CParam::push(const IOpType &i)
00166         {
00167                 _Param.push_back((IOpType*)i.clone());
00168         }
00169 
00170         void CParam::push(IOpType *i)
00171         {
00172                 _Param.push_back(i);
00173         }
00174 
00175         const IOpType &CParam::get() const
00176         {
00177                 return *_Param.back();
00178         }
00179 
00180         void CParam::pop()
00181         {
00182                 _Param.pop_back();
00183         }
00184 
00185         void CParam::clear()
00186         {
00187                 while(_Param.size())
00188                 {
00189                         IOpType *i = _Param.back();
00190                         i->release();
00191                         pop();
00192                 }
00193         }
00194 
00195         sint32 CParam::size() const
00196         {
00197                 return _Param.size();
00198         }
00199 
00200         const NLAIAGENT::IObjectIA::CProcessResult &CParam::run()
00201         {
00202                 return NLAIAGENT::IObjectIA::ProcessRun;;
00203         }
00204 
00205         CParam::~CParam()
00206         {                       
00207                 for(int i = 0; i < (int)_Param.size(); i++)
00208                 {
00209                         _Param[i]->release();
00210                 }
00211         }
00212 
00213         void CParam::getDebugString(std::string &t) const
00214         {               
00215                 getString(t);
00216         }
00217 
00218         void CParam::load(NLMISC::IStream &is)
00219         {
00220                 sint32 n;
00221                 int i;
00222                 is.serial(n);
00223                 for(i = 0; i < (int)_Param.size(); i++)
00224                 {
00225                         _Param[i]->release();
00226                 }
00227                 _Param.clear();
00228                 _Param.resize(n);
00229                 for(i = 0; i < (int)_Param.size(); i++)
00230                 {                       
00231                         _Param[i] = (IOpType *)IOpType::loadIOpType(is);
00232                 }
00233         }
00234 
00235         void CParam::save(NLMISC::IStream &os)
00236         {
00237                 sint32 n = _Param.size();
00238                 os.serial(n);
00239                 for(int i = 0; i < (int)_Param.size(); i++)
00240                 {
00241                         _Param[i]->serial(os);
00242                 }
00243         }
00244 
00245         bool CParam::isEqual(const NLAIAGENT::IBasicObjectIA &a) const
00246         {
00247                 // TODO
00248                 const CParam &p = (const CParam &)a;
00249                 return *this == p;
00250         }
00251 
00252         const NLAIC::CIdentType &CParam::getType() const
00253         {
00254                 return IdParam;
00255         }
00256 
00257         void CParam::getString(std::string &txt) const
00258         {
00259                 txt += "(";
00260                 if(size())
00261                 {               
00262                         int i;
00263                         for( i = 0; i < size() - 1; i++)
00264                         {
00265                                 const NLAIC::CIdentType *id = _Param[i]->getConstraintTypeOf();
00266                                 if(id) txt += (const char *)*id;
00267                                 else txt += "UNDEF";
00268                                 txt += ",";
00269                         }
00270                         const NLAIC::CIdentType *id = _Param[i]->getConstraintTypeOf();
00271                         if(id) txt += (const char *)*id;
00272                         else txt += "UNDEF";
00273                 }
00274                 txt += ")";
00275         }
00276 
00277 
00278         void CParam::setInfo(NLAIAGENT::IBaseGroupType *pInfo)
00279         {
00280                 if(_ParamInfo != NULL) _ParamInfo->release();
00281                 _ParamInfo = pInfo;
00282         }
00283 
00284 
00285 
00286 
00289 
00290 
00291         CMethodeName::CMethodeName(const NLAIAGENT::IVarName &name):_MethodeName((NLAIAGENT::IVarName *)name.clone()),_Code(NULL)
00292         {
00293                 _TypeOfMethode = NULL;
00294         }
00295         CMethodeName::CMethodeName():_MethodeName(NULL),_Code(NULL)
00296         {
00297                 _TypeOfMethode = NULL;
00298         }
00299         CMethodeName::~CMethodeName()
00300         {
00301                 if ( _MethodeName != NULL ) 
00302                                 _MethodeName->release();
00303 
00304                 if ( _Code != NULL ) 
00305                                 _Code->release();
00306 
00307                 if ( _TypeOfMethode != NULL )
00308                                 _TypeOfMethode->release();
00309 
00310 
00311         }
00312 
00313         CMethodeName::CMethodeName(const CMethodeName &c)
00314         {
00315                 _MethodeName = (NLAIAGENT::IVarName *) c._MethodeName->clone();
00316 
00317                 if ( c._TypeOfMethode )
00318                         _TypeOfMethode = (NLAISCRIPT::IOpType *) c._TypeOfMethode->clone();
00319 
00320                 _MethodeName = (NLAIAGENT::IVarName *)c._MethodeName->clone();
00321 
00322                 _Code = c._Code;
00323                 _Code->incRef();
00324         }
00325 
00326         const NLAIC::IBasicType *CMethodeName::clone() const
00327         {
00328                 CMethodeName *cl = new CMethodeName( *this );
00329                 return cl;
00330         }
00331 
00332         const NLAIC::IBasicType *CMethodeName::newInstance() const
00333         {
00334                 CMethodeName *instance = new CMethodeName;
00335                 return instance;
00336         }
00337 
00338 
00339         void CMethodeName::setName(const NLAIAGENT::IVarName &name)
00340         {
00341                 if(_MethodeName != NULL)
00342                 {
00343                         _MethodeName->release();                                
00344                 }
00345                 _MethodeName = (NLAIAGENT::IVarName *)name.clone();
00346         }
00347 
00348         const NLAIAGENT::IVarName &CMethodeName::getName() const
00349         {
00350                 if(_MethodeName == NULL)
00351                         throw NLAIE::CExceptionUnReference("invalide reference for methodeName in class CMethodeName");
00352 
00353                 return *_MethodeName;
00354         }
00355         
00356         void CMethodeName::setParam(const CParam &p)
00357         {
00358                 _Param = p;
00359         }
00360 
00361         CParam &CMethodeName::getParam()
00362         {
00363                 return _Param;
00364         }
00365 
00366         void CMethodeName::setCode(IOpCode *c)
00367         {                       
00368                 _Code = c;
00369         }
00370 
00371         IOpCode *CMethodeName::getCode()
00372         {
00373                 return _Code;
00374         }
00375 
00376         void CMethodeName::setTypeOfMethode(IOpType *id)
00377         {
00378                 if ( _TypeOfMethode != NULL )
00379                                 _TypeOfMethode->release();
00380                 
00381                 _TypeOfMethode = id;            
00382         }
00383         const IOpType *CMethodeName::getTypeOfMethode() const
00384         {
00385                 return _TypeOfMethode;
00386         }
00387 
00388         void CMethodeName::getDebugString(std::string &t) const
00389         {
00390                 std::string txt;
00391                 _Param.getDebugString(txt);
00392                 t += std::string(_MethodeName->getString()) + txt;
00393         }
00394 
00395         void CMethodeName::save(NLMISC::IStream &os)
00396         {       
00397                 os.serial( (NLAIC::CIdentType &) _MethodeName->getType() );
00398                 _MethodeName->save( os );
00399 
00400                 os.serial( (NLAIC::CIdentType &) _Code->getType() );
00401                 _Code->save( os );
00402         }
00403 
00404         void CMethodeName::load(NLMISC::IStream &is)
00405         {
00406                 NLAIC::CIdentTypeAlloc id;
00407                 is.serial( id );
00408                 _MethodeName = (NLAIAGENT::IVarName *) id.allocClass();
00409                 _MethodeName->load( is );
00410                 _MethodeName->incRef();
00411                 
00412                 is.serial( id );
00413                 _Code = (IOpCode *) id.allocClass();
00414                 _Code->load( is );
00415                 _Code->incRef();
00416         }       
00417 
00418         bool CMethodeName::isEqual(const NLAIAGENT::IBasicObjectIA &) const
00419         {
00420                 // TODO
00421                 return false;
00422         }
00423 
00424         const NLAIAGENT::IObjectIA::CProcessResult &CMethodeName::run()
00425         {
00426                 return NLAIAGENT::IObjectIA::ProcessRun;
00427         }
00428 
00429         const NLAIC::CIdentType &CMethodeName::getType() const
00430         {
00431                 return IdMethodeName;
00432         }
00433 }