# 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  

fo_assert.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 
00021 #include "nel/ai/logic/fo_assert.h"
00022 #include "nel/ai/logic/fo_operator.h"
00023 #include "nel/ai/logic/valueset.h"
00024 #include "nel/ai/logic/varset.h"
00025 #include "nel/ai/logic/fact.h"
00026 #include "nel/ai/logic/clause.h"
00027 #include "nel/ai/agent/agent_digital.h"
00028 
00029 namespace NLAILOGIC
00030 {
00031         using namespace NLAIAGENT;
00032 
00033         CFirstOrderAssert::CFirstOrderAssert(const IVarName &n, sint32 nb_vars) : IBaseAssert( n )
00034         {
00035                 _NbVars = nb_vars;
00036 /*              std::vector<IBaseVar *> *lv = new std::vector<IBaseVar *>;
00037                 for (sint32 i = 0; i < _NbVars; i++)
00038                         lv->pushBack(NULL);*/
00039         }
00040 
00041 
00042         CFirstOrderAssert::CFirstOrderAssert(const CFirstOrderAssert &cp) : IBaseAssert( *cp._Name )
00043         {
00044 
00045                 CConstIteratorContener r = _Facts.getConstIterator();
00046                 while( !r.isInEnd() )
00047                 {
00048                         _Facts.pushBack( 50, (CValueSet *) (*r).clone() );
00049                 }
00050         }
00051 
00052         CFirstOrderAssert::~CFirstOrderAssert()
00053         {
00054                 while ( _Facts.size() )
00055                 {
00056                         _Facts.Front()->release();
00057                         _Facts.popFront();
00058                 }
00059         }
00060 
00061         void CFirstOrderAssert::connectClause(CClause *, std::vector<sint32> &)
00062         {
00063         }
00064 
00065         void CFirstOrderAssert::addFact(CVarSet *f)
00066         {
00067                 // Lorsqu'on ajoute un fait, le transmet à toutes les conditions qui en dépendent avec la position des variables dans la condition
00068                 for ( sint32 i = 0; i < (sint32)_Outputs.size(); i++ )
00069                 {
00070                         // Créé la liste des valeurs
00071                         std::list<IObjetOp *> *values = f->getValues(); 
00072 
00073 //                      TODO: unifier pour bool et first order operator
00074 //                      (CFirstOrderOperator *) _Outputs[i]->propagate(values, _PosVars[i] );
00075                         delete values;
00076                 }
00077                 
00078                 _Facts.pushBack( 50, f->asCValueSet() );
00079         }
00080 
00081         void CFirstOrderAssert::addFact(CValueSet *f)
00082         {
00083                 // Lorsqu'on ajoute un fait, le transmet à toutes les conditions qui en dépendent avec la position des variables dans la condition
00084                 for ( sint32 i = 0; i < (sint32)_Outputs.size(); i++ )
00085                 {
00086                         // Créé la liste des valeurs
00087 //                      std::list<IObjectIA *> *values = f->getValues();
00088 //                      TODO: unifier pour bool et first order operator                 
00089 //                      _Outputs[i]->propagate(values, _PosVars[i] );
00090                 }
00091 
00092                 _Facts.pushBack( 50, (CValueSet *) f->clone() );
00093         }
00094 
00095         void CFirstOrderAssert::removeFact(CFact *f)
00096         {
00097                 std::vector<IBaseOperator *>::iterator it_cl = _Outputs.begin();
00098                 while ( it_cl != _Outputs.end() )
00099                 {
00100                         // doit passer par un envoi de message!!!
00101                         it_cl++;
00102                 }
00103 
00104                 CValueSet *tmp = f->asValueSet();
00105 
00106                 CConstIteratorContener it_f = _Facts.getConstIterator();
00107                 while( !it_f.isInEnd() )
00108                 {
00109                         if ( *f == *(CValueSet *)((const IObjetOp*)it_f) )
00110                         {
00111                                 _Facts.erase( it_f );
00112                                 tmp->release();
00113                                 return;
00114                         }
00115                         it_f++;
00116                 }
00117                 tmp->release();
00118         }
00119 
00120         void CFirstOrderAssert::addClause(CClause *clause, std::vector<sint32> &posvars) 
00121         {
00122                 _Clauses.push_back( clause );
00123                 _PosVars.push_back(std::vector<sint32>());
00124                 
00125                 std::vector<sint32>::iterator it_pos = posvars.begin();
00126                 while ( it_pos != posvars.end() )
00127                 {
00128                         _PosVars[ _Clauses.size() -1 ].push_back( *it_pos );
00129                         it_pos++;
00130                 }
00131         }
00132 
00133         const NLAIC::IBasicType *CFirstOrderAssert::clone() const
00134         {
00135                 CFirstOrderAssert *clone = new CFirstOrderAssert( *this );              
00136                 return (NLAIC::IBasicInterface *) clone;
00137         }
00138 
00139         const NLAIC::IBasicType *CFirstOrderAssert::newInstance() const
00140         {
00141                 return clone();
00142         }
00143                 
00144         void CFirstOrderAssert::save(NLMISC::IStream &os)
00145         {
00146         }
00147 
00148         void CFirstOrderAssert::load(NLMISC::IStream &is)
00149         {
00150         }
00151 
00152         void CFirstOrderAssert::getDebugString(std::string &text) const
00153         {
00154                 std::string buf;
00155                 getName().getDebugString(buf);
00156                 text += NLAIC::stringGetBuild("<CFirstOrderAssert> %s", buf.c_str());
00157         }
00158 
00159         bool CFirstOrderAssert::isEqual(const CFirstOrderAssert &a) const
00160         {
00161                 return ( getName() == a.getName() );
00162         }
00163 
00164         const IObjectIA::CProcessResult &CFirstOrderAssert::run()
00165         {
00166                 return IObjectIA::ProcessRun;
00167         }
00168 
00169         bool CFirstOrderAssert::isEqual(const IBasicObjectIA &a) const
00170         {
00171                 return false;
00172         }
00173 
00174         bool CFirstOrderAssert::isTrue() const
00175         {
00176                 return false;
00177         }
00178 
00179         void CFirstOrderAssert::addInput(CClause *clause, std::vector<sint32> &posvars)
00180         {
00181                 _Clauses.push_back( clause );
00182                 _PosVarsInputs.push_back( std::vector<sint32>() );
00183                 
00184                 std::vector<sint32>::iterator it_pos = posvars.begin();
00185                 while ( it_pos != posvars.end() )
00186                 {
00187                         _PosVarsInputs[_Inputs.size() - 1].push_back( *it_pos );
00188                         it_pos++;
00189                 }
00190         }
00191 
00192         const std::vector<CClause *> &CFirstOrderAssert::getClauses()
00193         {
00194                 return _Clauses;
00195         }
00196 
00197         const NLAIC::CIdentType &CFirstOrderAssert::getType() const
00198         {
00199                 return IdFirstOrderAssert;
00200         }
00201 
00202         void CFirstOrderAssert::init(IObjectIA *params)
00203         {
00204                 ((IBaseGroupType *)params)->popFront();
00205                 
00206                 _NbVars = (sint32) ( (DigitalType *) ((IBaseGroupType *)params)->popFront() )->getValue();
00207         }
00208 
00209         sint32 CFirstOrderAssert::nbVars() const
00210         {
00211                 return _NbVars;
00212         }
00213 
00214         void CFirstOrderAssert::backward(CValueSet *vs, std::list<CValueSet *> &bindings)
00215         {
00216                 /*              std::list<CValueSet *> *facts = new std::list<CValueSet *>;
00217         
00218                 CFactPattern *tmp_fp = new CFactPattern(*vs);
00219                 tmp_fp->setAssert( this );
00220 
00221                 std::list<CClause *>::iterator it_cl = _Inputs.begin();
00222                 while ( it_cl != _Inputs.end() )
00223                 {
00224                         CVarSet *l = ( (CRule *) *it_cl )->backward( tmp_fp );
00225                         if ( l )
00226                         {
00227                                 char buf[1024 * 2];
00228                                 l->getDebugString( buf );
00229                                 bindings.pushBack( l );
00230                         }
00231                         it_cl++;
00232                 }*/
00233         }
00234 
00235         std::list<CFact *> *CFirstOrderAssert::backward(CFact *fact)
00236         {
00237                 // Recherche dans l'assertion...
00238                 std::list<CFact *> *result = new std::list<CFact *>;
00239                 CValueSet *liaison;
00240 /*
00241                 CConstIteratorContener it_f = _Facts.getConstIterator();
00242                 while( !it_f.isInEnd() )
00243                 {
00244                         if ( *f == *(CFact *)((const IObjetOp*)it_f) )
00245                         {
00246                                 _Facts.erase( it_f );
00247                                 tmp->release();
00248                                 return;
00249                         }
00250                         it_f++;
00251                 }
00252 */
00253 
00254                 CConstIteratorContener it_l = _Facts.getConstIterator();
00255                 while ( !it_l.isInEnd() )
00256                 {
00257 #ifdef NL_DEBUG
00258                         std::string buffer;
00259 
00260                         ((CValueSet *)((const IObjetOp*)it_l))->getDebugString(buffer);
00261                         std::string buffer2;
00262                         fact->getDebugString(buffer2);
00263 #endif
00264 
00265                         liaison = ((CValueSet *)((const IObjetOp*)it_l))->unify( (CValueSet *) fact );
00266                         if ( liaison )
00267                         {
00268                                 if ( liaison->undefined() == 0 )
00269                                 {
00270                                         result->push_back( (CFact *) ((CValueSet *)((const IObjetOp*)it_l))->clone());
00271                                 }
00272                                 liaison->release();
00273                         }
00274                         it_l++;
00275                 }
00276 
00277 
00278                 // Et dans les règles dont la partie conclusion infèrent des faits de cette assertion
00279                 std::list<CFact *> param;
00280                 param.push_back( fact );
00281                 std::vector<IBaseOperator *>::iterator it_i = _Inputs.begin();
00282                 while ( it_i != _Inputs.end() )
00283                 {
00284                         ((CFirstOrderOperator *)(*it_i))->backward( param );
00285                         it_i++;
00286                 }
00287 
00288                 return result;
00289         }
00290 
00291         std::list<CFact *> *CFirstOrderAssert::getFacts() const
00292         {
00293                 std::list<CFact *> *result = new std::list<CFact *>;
00294                 CConstIteratorContener it_f = _Facts.getConstIterator();
00295                 while ( !it_f.isInEnd() )
00296                 {
00297                         CFact *tmp = new CFact( (IBaseAssert *) this, ((CValueSet *)((const IObjetOp*)it_f)) );
00298                         result->push_back( tmp );
00299                         it_f++;
00300                 }
00301                 return result;
00302         }
00303 }
00304