# 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  

fact.cpp

Go to the documentation of this file.
00001 /* Copyright, 2000 Nevrax Ltd.
00002  *
00003  * This file is part of NEVRAX <MODULE_NAME>.
00004  * NEVRAX <MODULE_NAME> is free software; you can redistribute it and/or modify
00005  * it under the terms of the GNU General Public License as published by
00006  * the Free Software Foundation; either version 2, or (at your option)
00007  * any later version.
00008 
00009  * NEVRAX <MODULE_NAME> is distributed in the hope that it will be useful, but
00010  * WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
00012  * General Public License for more details.
00013 
00014  * You should have received a copy of the GNU General Public License
00015  * along with NEVRAX <MODULE_NAME>; see the file COPYING. If not, write to the
00016  * Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
00017  * MA 02111-1307, USA.
00018  */
00019 
00020 #include "nel/ai/logic/fact.h"
00021 #include "nel/ai/logic/ai_assert.h"
00022 #include "nel/ai/logic/varset.h"
00023 #include "nel/ai/agent/object_type.h"
00024 
00025 namespace NLAILOGIC
00026 {
00027         using namespace NLAIAGENT;
00028 
00029         CFact::CFact() : CValueSet()
00030         {
00031                 _Assert = NULL;
00032                 _AssertName = NULL;
00033         }       
00034 
00035 
00036         CFact::CFact(IBaseAssert *a) : CValueSet(a->nbVars() )
00037         {
00038                 _Assert = a;
00039                 _AssertName = NULL;
00040         }
00041 
00042         CFact::CFact(IBaseAssert *a, CValueSet *vals) : CValueSet( *vals )
00043         {
00044                 _Assert = a;
00045                 _AssertName = NULL;
00046         }
00047 
00048         CFact::CFact(IBaseAssert *a, bool v) : CValueSet(1)
00049         {
00050                 _Assert = a;
00051                 _Values[0] = new CBoolType(v);
00052                 _AssertName = NULL;
00053         }
00054 
00055         CFact::CFact(IBaseAssert *a, CVarSet *vars) : CValueSet( vars->size() )
00056         {
00057                 std::list<IObjetOp *> *vals = vars->getValues();
00058                 std::list<IObjetOp *>::iterator it_v = vals->begin();
00059                 while ( it_v != vals->end() )
00060                 {
00061                         vals->push_back( *it_v );
00062                         it_v++;
00063                 }
00064                 delete vals;
00065                 _Assert = a;
00066                 _AssertName = NULL;
00067         }
00068 
00069 
00070         CFact::CFact(NLAIAGENT::IVarName &name) : CValueSet()
00071         {
00072                 _AssertName = (NLAIAGENT::IVarName *) name.clone();
00073                 _Assert = NULL;
00074         }
00075 
00076         CFact::CFact(sint32 i,NLAIAGENT::IVarName *a) : CValueSet(i )
00077         {
00078                 //_Assert = a;
00079                 _AssertName = (NLAIAGENT::IVarName *) a;
00080                 _Assert = NULL;
00081         }
00082 
00083         CFact::CFact(NLAIAGENT::IVarName &a, CValueSet *vals) : CValueSet( *vals )
00084         {
00085                 _AssertName = (NLAIAGENT::IVarName *) a.clone();
00086                 _Assert = NULL;
00087         }
00088 
00089         CFact::CFact(NLAIAGENT::IVarName &a, bool v) : CValueSet(1)
00090         {
00091                 _Values[0] = new CBoolType(v);
00092                 _AssertName = (NLAIAGENT::IVarName *) a.clone();
00093                 _Assert = NULL;
00094         }
00095 
00096         CFact::CFact(NLAIAGENT::IVarName &a, CVarSet *vars) : CValueSet( vars->size() )
00097         {
00098                 _Assert = NULL;
00099                 std::list<IObjetOp *> *vals = vars->getValues();
00100                 std::list<IObjetOp *>::iterator it_v = vals->begin();
00101                 while ( it_v != vals->end() )
00102                 {
00103                         vals->push_back( *it_v );
00104                         it_v++;
00105                 }
00106                 delete vals;
00107                 _AssertName = (NLAIAGENT::IVarName *) a.clone();
00108         }
00109 
00110         CFact::~CFact()
00111         {
00112                 if(_AssertName != NULL) _AssertName->release();
00113 /*              if ( _Assert != NULL )
00114                         _Assert->release();
00115                         */
00116         }
00117 
00118 
00119         IBaseAssert *CFact::getAssert() 
00120         {
00121                 return _Assert;
00122         }
00123 
00124         NLAIAGENT::IVarName &CFact::getAssertName()
00125         {
00126                 return *_AssertName;
00127         }
00128 
00129         void CFact::getDebugString(std::string &txt) const
00130         {
00131                 std::string buf;
00132                 if(_Assert != NULL) _Assert->getDebugString(buf);
00133                 else buf = "NULL";
00134                 txt += NLAIC::stringGetBuild("<CFact> %s\n", buf.c_str());
00135                 for (sint32 i = 0; i < _NbValues; i++ )
00136                 {
00137                         txt += " , ";
00138                         if ( _Values[i] )
00139                         {
00140                                 _Values[i]->getDebugString( buf );
00141 
00142                                  txt += buf;
00143                         }
00144                         else txt += "NULL";
00145                 }
00146         }
00147 
00148         void CFact::propagate()
00149         {
00150                 if ( _Assert )
00151                         _Assert->addFact( this );
00152         }
00153 
00154         bool CFact::operator== (const IBasicObjectIA &a) const
00155         {
00156                 if ( ! (_Assert->getName() == ( (CFact &)a )._Assert->getName() ) )
00157                         return false;
00158 
00159                 if( ((CFact &) a)._NbValues != _NbValues )
00160                         return false;
00161 
00162                 for (sint32 i = 0; i < _NbValues; i++ )
00163                 {
00164 /*                      IObjetOp *test = (*_Values[i]) == *((CValueSet &)a).getValue(i);
00165                         bool test_result = test->isTrue();
00166                         test->release();
00167                         if ( test_result )
00168                                 return false;
00169                                 */
00170 
00171                         if ( ! ( (*_Values[i]) == *((CValueSet &)a).getValue(i) ) )
00172                                 return false;
00173                 }
00174                 return true;
00175         }
00176 
00177         CValueSet *CFact::asValueSet()
00178         {
00179                 CValueSet *result = new CValueSet( _NbValues );
00180                 for (sint32 i = 0; i < _NbValues; i++ )
00181                 {
00182                         result->setValue(i, _Values[i] );
00183                         _Values[i]->incRef();
00184                 }
00185                 return result;
00186         }
00187 
00188         NLAIAGENT::tQueue CFact::isMember(const NLAIAGENT::IVarName *className,const NLAIAGENT::IVarName *funcName,const NLAIAGENT::IObjectIA &params) const
00189         {
00190 
00191 #ifdef NL_DEBUG 
00192                 std::string nameP;
00193                 std::string nameM;
00194         funcName->getDebugString(nameM);
00195         params.getDebugString(nameP);
00196 
00197         const char *dbg_class_name = (const char *) getType();
00198 #endif
00199                 NLAIAGENT::tQueue r;
00200                 if(className == NULL)
00201                 {
00202                         if( (*funcName) == NLAIAGENT::CStringVarName( "Constructor" ) )
00203                         {                                       
00204                                 NLAIAGENT::CObjectType *c = new NLAIAGENT::CObjectType( new NLAIC::CIdentType( CFact::IdFact ) );                                       
00205                                 r.push( NLAIAGENT::CIdMethod( 0 + CValueSet::getMethodIndexSize(), 0.0, NULL, c) );                                     
00206                         }
00207                 }
00208 
00209                 if ( r.empty() )
00210                         return CValueSet::isMember(className, funcName, params);
00211                 else
00212                         return r;
00213         }
00214 
00216 
00217         NLAIAGENT::IObjectIA::CProcessResult CFact::runMethodeMember(sint32, sint32, NLAIAGENT::IObjectIA *)
00218         {
00219                 return IObjectIA::CProcessResult();
00220         }
00221 
00222         NLAIAGENT::IObjectIA::CProcessResult CFact::runMethodeMember(sint32 index, NLAIAGENT::IObjectIA *p)
00223         {
00224                 NLAIAGENT::IBaseGroupType *param = (NLAIAGENT::IBaseGroupType *)p;
00225 
00226                 switch(index - CValueSet::getMethodIndexSize())
00227                 {
00228                 case 0:
00229                         {                                       
00230 
00231                                 NLAIAGENT::CStringType *name = (NLAIAGENT::CStringType *) param->getFront()->clone();
00232                                 param->popFront();
00233 #ifdef NL_DEBUG
00234                                 const char *dbg_name = name->getStr().getString();
00235 #endif
00236                                 // If the constructor() function is explicitely called and the object has already been initialised
00237                                 if ( _AssertName )
00238                                         _AssertName->release();
00239 //                              _Args.clear();
00240 
00241                                 _AssertName = (NLAIAGENT::IVarName *) name->getStr().clone();
00242 /*                              std::list<const NLAIAGENT::IObjectIA *> args;
00243                                 while ( param->size() )
00244                                 {
00245                                         _Args.push_back( (NLAIAGENT::IObjectIA *) param->getFront() );
00246                                         param->popFront();
00247                                 }
00248                                 */
00249                                 return IObjectIA::CProcessResult();             
00250                         }
00251                         break;
00252                 }
00253 
00254                 return IObjectIA::CProcessResult();
00255         }
00256 
00257         sint32 CFact::getMethodIndexSize() const
00258         {
00259                 return CValueSet::getMethodIndexSize() + 1;
00260         }
00261 
00262         const NLAIC::CIdentType &CFact::getType() const
00263         {
00264                 return IdFact;
00265         }
00266 
00268 
00269         CFact::CFact(const CFact &fact) : CValueSet(fact)
00270         {
00271                 if ( fact._AssertName != NULL )
00272                         _AssertName = (NLAIAGENT::IVarName *) fact._AssertName->clone();
00273                 else
00274                         _AssertName = NULL;
00275                 _Assert = fact._Assert;
00276         }
00277 
00278         const NLAIC::IBasicType *CFact::clone() const
00279         {
00280                 return new CFact( *this );
00281         }
00282 
00283         const NLAIC::IBasicType *CFact::newInstance() const
00284         {
00285                 return new CFact();
00286         }
00287 }