# 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  

bool_operator.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/bool_operator.h"
00021 #include "nel/ai/logic/fact.h"
00022 
00023 namespace NLAILOGIC
00024 {
00025         using namespace NLAIAGENT;
00026 
00027         CBoolOperator::CBoolOperator()
00028         {
00029         }
00030 
00031         CBoolOperator::CBoolOperator(char *c) : IBaseOperator(c)
00032         {
00033         }
00034 
00035         CBoolOperator::CBoolOperator(CBoolOperator &c) : IBaseOperator(c)
00036         {
00037         }
00038 
00039         bool CBoolOperator::isTrue() const 
00040         {
00041                 for (sint32 i = 0; i < (sint32) _Conds.size(); i++ )
00042                 {
00043                         if ( _CondsVal[i] != _Conds[i]->isTrue() )
00044                                 return false;
00045                 }
00046                 return true;
00047         }
00048 
00049         std::list<CFact *> *CBoolOperator::backward(std::list<IBaseAssert *> &conds, std::list<bool> &vals)
00050         {
00051                 std::list<CFact *> *result = new std::list<CFact *>;
00052                 for ( sint32 i = 0; i < (sint32) _Conds.size(); i++ )
00053                 {
00054                         CFact *tmp = new CFact (_Conds[i], _CondsVal[i] );
00055                         result->push_back( tmp );
00056                 }
00057                 return result;
00058         }
00059 
00060         void CBoolOperator::save(NLMISC::IStream &os)
00061         {
00062                 IBaseOperator::save( os );
00063                 
00064                 sint32 i;
00065                 sint32 size = _Conds.size();
00066                 os.serial( size );
00067                 for ( i = 0; i < size; i++ )
00068                 {
00069                         bool ct = _CondsVal[i];
00070                         os.serial( (bool &) ct );
00071                 }
00072                 
00073                 size = _Concs.size();
00074                 os.serial( size );
00075                 for ( i = 0; i < size; i++ )
00076                 {
00077                         bool test = _ConcsVal[i];
00078                         os.serial( test  );
00079                 }
00080         }
00081 
00082         void CBoolOperator::load(NLMISC::IStream &is)
00083         {
00084                 sint32 i;
00085                 sint32 nb_vals;
00086 
00087                 is.serial( nb_vals );
00088                 for ( i = 0; i < nb_vals; i++ )
00089                 {
00090                         bool val;
00091                         is.serial( val );
00092                         _CondsVal.push_back( val );
00093                 }
00094 
00095                 is.serial( nb_vals );
00096                 for ( i = 0; i < nb_vals; i++ )
00097                 {
00098                         bool val;
00099                         is.serial( val );
00100                         _ConcsVal.push_back( val );
00101                 }
00102 
00103         }
00104 
00105         bool CBoolOperator::isValid(CFactBase *)
00106         {
00107                 return isTrue();
00108         }
00109 
00110         void CBoolOperator::addPrecondition(IBaseAssert *a, bool v)
00111         {
00112                 _Conds.push_back( a );
00113                 a->incRef();
00114                 a->addOutput( this );
00115                 _CondsVal.push_back( v );
00116         }
00117 
00118         void CBoolOperator::addPostcondition(IBaseAssert *a, bool v)
00119         {
00120                 _Concs.push_back( a );
00121                 a->incRef();
00122                 a->addInput( this );
00123                 _ConcsVal.push_back( v );
00124         }
00125 
00126         const NLAIC::IBasicType *CBoolOperator::clone() const
00127         {
00128                 CBoolOperator *clone = new CBoolOperator;
00129                 sint32 i;
00130                 for (i = 0; i < (sint32) _Conds.size(); i++ )
00131                 {
00132                         clone->addPrecondition( _Conds[i], _CondsVal[i] );
00133                 }
00134 
00135                 for (i = 0; i < (sint32) _Concs.size(); i++ )
00136                 {
00137                         clone->addPrecondition( _Concs[i], _ConcsVal[i] );
00138                 }
00139                 return clone;
00140         }
00141 
00142         const NLAIC::IBasicType *CBoolOperator::newInstance() const
00143         {
00144                 CBoolOperator *instance = new CBoolOperator();
00145                 return instance;
00146         }
00147 
00148         void CBoolOperator::getDebugString(std::string &text) const
00149         {
00150                 text += "<CBoolOperator>\n  -Preconditions:\n";
00151 
00152                 sint32 i;
00153                 for ( i = 0; i < (sint32) _Conds.size() ; i++ )
00154                 {
00155                         if ( _CondsVal[i] )
00156                                 text += "    ";
00157                         else
00158                                 text += "     !";
00159                         text += _Conds[i]->getName().getString();
00160 
00161                         if ( _CondsVal[i] == _Conds[i]->isTrue() )
00162                                 text += " (true)\n";
00163                         else
00164                                 text += " (false)\n";
00165                 }
00166 
00167                 text += "  -Postconditions:\n";
00168                 for ( i = 0; i < (sint32) _Concs.size() ; i++ )
00169                 {
00170                         if ( _ConcsVal[i] )
00171                                 text += "      ";
00172                         else
00173                                 text += "   !";
00174                         text += _Concs[i]->getName().getString();
00175                         text += "\n";
00176                 }
00177         }
00178 
00179         bool CBoolOperator::isEqual(const CBoolOperator &a) const
00180         {
00181                 return a.isTrue() == isTrue();
00182         }
00183 
00184         bool CBoolOperator::isEqual(const IBasicObjectIA &a) const
00185         {
00186                 return false;
00187         }
00188 
00189         const NLAIC::CIdentType &CBoolOperator::getType() const
00190         {
00191                 return IdBoolOperator;
00192         }
00193 
00194         float CBoolOperator::truthValue() const
00195         {
00196                 if ( isTrue() )
00197                         return 1.0;
00198                 else
00199                         return 0.0;
00200         }
00201 
00202         std::list<CFact *> *CBoolOperator::backward(std::list<CFact *> &facts)
00203         {
00204                 return NULL;
00205         }
00206 
00207         std::list<CFact *> *CBoolOperator::forward(std::list<CFact *> &facts) 
00208         {
00209                 return propagate( facts );
00210         }
00211 
00212         std::list<CFact *> *CBoolOperator::propagate(std::list<CFact *> &facts)
00213         {
00214                 std::list<CFact *> *result = new std::list<CFact *>;
00215 
00216                 bool *test= new bool[ _Conds.size() ];
00217                 sint32 i;
00218                 std::list<CFact *>::iterator it_f = facts.begin();
00219                 while ( it_f != facts.end() )
00220                 {
00221                         for ( i = 0; i < (sint32) _Conds.size(); i++ )
00222                         {
00223                                 if ( (*_Conds[i]) == (*it_f)->getAssert()  )
00224                                         if ( ((CBoolType *)(*it_f)->getValue(0))->isTrue() == _CondsVal[i] ) 
00225                                                 test[i] = true;
00226                                         else
00227                                                 test[i] = false;
00228                         }
00229                         it_f++;
00230                 }
00231 
00232                 for ( i = 0; i < (sint32) _Conds.size(); i++ )
00233                 {
00234                         if ( !test[i] )
00235                         {
00236                                 delete test;
00237                                 return result;
00238                         }
00239                 }
00240                 delete test;
00241 
00242                 for (i = 0; i < (sint32) _Concs.size(); i++ )
00243                 {
00244                         CFact *tmp = new CFact (_Concs[i], _ConcsVal[i] );
00245                         result->push_back( tmp );
00246                 }
00247                 return result;
00248         }
00249 
00250         const IObjectIA::CProcessResult &CBoolOperator::run()
00251         {               
00252                 return IObjectIA::ProcessRun;
00253         }
00254 
00255         float CBoolOperator::priority() const
00256         {
00257                 return 0.0;
00258         }
00259 
00260         void CBoolOperator::success()
00261         {
00262         }
00263 
00264         void CBoolOperator::failure()
00265         {
00266         }
00267 
00268         void CBoolOperator::success(IBaseOperator *)
00269         {
00270         }
00271 
00272         void CBoolOperator::failure(IBaseOperator *)
00273         {
00274         }
00275 }