# 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  

valueset.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/valueset.h"
00021 #include "nel/ai/logic/var.h"
00022 #include "nel/ai/logic/boolval.h"
00023 #include "nel/ai/logic/clause.h"
00024 #include "nel/ai/logic/ai_assert.h"
00025 
00026 namespace NLAILOGIC
00027 {
00028         using namespace NLAIAGENT;
00029 
00030         const TProcessStatement CValueSet::state = processIdle;
00031 
00032         CValueSet::CValueSet()
00033         {
00034                 _NbValues = 0;
00035                 _Values = NULL;
00036         }
00037 
00039         CValueSet::CValueSet(sint32 size)
00040         {
00041                 _NbValues = size;
00042                 _Values = new IObjectIA *[ _NbValues ];
00043 
00044                 for ( sint32 i = 0; i < _NbValues; i++ )
00045                 {
00046                         _Values[ i ] = NULL;
00047                 }
00048         }
00049 
00051         CValueSet::CValueSet(const CValueSet &cp)
00052         {
00053                 _NbValues = cp._NbValues;
00054 
00055                 _Values = new IObjectIA *[ _NbValues ];
00056 
00057                 for ( sint32 i = 0; i < _NbValues; i++ )
00058                 {
00059                         _Values[i] = (IObjectIA *) cp._Values[i];
00060 
00061                         if ( _Values[i] )
00062                                 _Values[i]->incRef();
00063                 }
00064         }
00065 
00067         CValueSet::CValueSet(std::vector<IBaseVar *> &vars)
00068         {
00069                 _NbValues = vars.size();
00070 
00071                 _Values = new IObjectIA *[ _NbValues ];
00072 
00073                 for (sint32 i = 0; i < _NbValues; i++ )
00074                 {
00075                         _Values[ i ] =  vars[i]->getValue();
00076 
00077                         if ( _Values[i] )
00078                                 _Values[i]->incRef();
00079                 }
00080         }
00081 
00082         // Construit un valueSet à partir d'un autre en changeant la position des valeurs:
00083         // pos = positions dans le nouveau CValueSet des valeurs du premier.
00084 
00087         CValueSet::CValueSet(CValueSet *vals, std::vector<sint32> &pos)
00088         {
00089                 _NbValues = pos.size();
00090 
00091                 _Values = new IObjectIA *[ _NbValues ];
00092 
00093                 for (sint32 i = 0; i < _NbValues; i++ )
00094                 {
00095                         _Values[ i ] =  NULL;
00096                 }
00097 
00098                 std::vector<sint32>::iterator it_pos = pos.begin();
00099                 sint32 index = 0;
00100                 while ( it_pos != pos.end() )
00101                 {
00102                         _Values[ index ] = vals->_Values[ *it_pos ];
00103                         it_pos++;
00104                         index++;
00105                 }
00106         }
00107 
00109         CValueSet::CValueSet(sint32 size, std::list<IObjectIA *> *vals, std::vector<sint32> &pos)
00110         {
00111                 _NbValues = size;
00112 
00113                 _Values = new IObjectIA *[ _NbValues ];
00114 
00115                 for (sint32 i = 0; i < _NbValues; i++ )
00116                 {
00117                         _Values[ i ] =  NULL;
00118                 }
00119 
00120                 std::list<IObjectIA *>::iterator it_val = vals->begin();
00121                 std::vector<sint32>::iterator it_pos = pos.begin();
00122 
00123                 while ( it_val != vals->end() && it_pos != pos.end() )
00124                 {
00125                         _Values[ *it_pos ] = *it_val;
00126                         (*it_val)->incRef();
00127                         it_val++;
00128                         it_pos++;
00129                 }
00130         }
00131 
00132         CValueSet::~CValueSet()
00133         {
00134                 for ( sint32 i = 0; i < _NbValues; i++ )
00135                         if ( _Values[i] )
00136                                 _Values[i]->release();
00137                 delete[] _Values;
00138         }
00139 
00140         void CValueSet::setValue(sint32 pos, IObjectIA *obj)
00141         {
00142                 _Values[pos] = obj;
00143                 if ( obj )
00144                         obj->incRef();
00145         }
00146 
00147         // Unification d'un CValueSet ave un autre.
00148         // Renvoie NULL si l'unification échoue, 
00149         // Un nouveau CValueSet corrrespondant à la l'unification si réussit
00150 
00151 
00155         CValueSet *CValueSet::unify(const CValueSet *un) const
00156         {
00157                 CValueSet *result = new CValueSet( _NbValues );
00158                 int i;
00159                 for( i = 0; i < _NbValues; i++ )
00160                 {
00161                         result->_Values[i] = _Values[i];
00162                         if ( _Values[i] )
00163                                 _Values[i]->incRef();
00164 
00165                 }
00166                 
00167                 for (i = 0; i < _NbValues ; i++ )
00168                 {
00169                         IObjectIA *x_val = result->_Values[ i ];
00170                         IObjectIA *y_val = un->_Values[ i ];
00171         
00172 /*                      if ( !x_val && !y_val )
00173                                 result->_Values[i] = NULL ;
00174 */
00175                         if ( x_val && y_val )
00176                         {
00177 //                              IObjectIA *test;
00178                                 if ( (*x_val) == ( *y_val )  )
00179                                 {
00180                                         // Nothing to do 
00181                                 }                                               
00182                                 else
00183                                 {
00184                                         result->release();
00185                                         return NULL;
00186                                 }
00187 //                              test->release();
00188                         }
00189                         else
00190                         {
00191                                 if ( y_val )
00192                                 {
00193                                         result->_Values[i] = y_val;
00194                                         result->_Values[i]->incRef();
00195                                 }
00196                         }
00197                 }
00198                 return result;
00199         }
00200 
00202         CValueSet *CValueSet::unify(std::list<IObjectIA *> *vals, std::vector<sint32> &pos_vals) const
00203         {
00204                 CValueSet *result = new CValueSet( _NbValues );
00205                 for (sint32 i = 0; i < _NbValues; i++ )
00206                 {
00207                         result->_Values[i] = _Values[i];
00208                         if ( _Values[i] )
00209                                 _Values[i]->incRef();
00210 
00211                 }
00212 
00213                 std::list<IObjectIA *>::iterator it_val = vals->begin();
00214                 std::vector<sint32>::iterator it_pos = pos_vals.begin();
00215 
00216                 // -----------------
00217 
00218 #ifdef NL_DEBUG
00219                 std::string buf;
00220                 this->getDebugString(buf);
00221                 std::string buf2;
00222                 CValueSet tmp(_NbValues, vals, pos_vals);
00223                 tmp.getDebugString(buf2);
00224 #endif
00225                 //TRACE(" \nUNIFICATION:\n VS = %s\n VP = %s \n", buf, buf2);
00226                 
00227                 // -----------------
00228 
00229                 while ( it_val != vals->end()  && it_pos != pos_vals.end() )
00230                 {
00231                         sint32 pos = *it_pos;
00232                         IObjectIA *l_val = _Values[ pos ];
00233 //                      IObjectIA *test = NULL;
00234                         if (  !l_val || (  (*l_val) == ( **it_val ) ) )
00235                         {
00236 //                              if ( test )
00237 //                                      test->release();
00238                                 result->_Values[ pos ] = *it_val;
00239                                 result->_Values[ pos ]->incRef();
00240                         }
00241                         else
00242                         {
00243 //                              if ( test )
00244 //                                      test->release();
00245                                 delete result;
00246                                 return NULL;
00247                         }
00248                         it_val++;
00249                         it_pos++;
00250 
00251                 }
00252 
00253                 return result;
00254         }
00255 
00256         CValueSet *CValueSet::unify(CValueSet *vals, std::vector<sint32> &pos_vals) const
00257         {
00258                 CValueSet *result = new CValueSet( _NbValues );
00259                 int i;
00260                 for ( i = 0; i < _NbValues; i++ )
00261                 {
00262                         result->_Values[i] = _Values[i];
00263                         if ( _Values[i] )
00264                                 _Values[i]->incRef();
00265 
00266                 }
00267 
00268                 for (i = 0; i < (sint32) pos_vals.size(); i++ )
00269                 {
00270                         sint32 pos = pos_vals[i];
00271                         IObjectIA *l_val = _Values[ pos ];
00272                         IObjectIA *r_val = (*vals)[i];
00273 //                      IObjectIA *test = NULL;
00274 //                      if (  !l_val || !( test = (*l_val) != ( *r_val ) )->isTrue() )
00275                         if (  !l_val || ( (*l_val) == ( *r_val ) ) )
00276                         {
00277 //                              if ( test )
00278 //                                      test->release();
00279                                 if ( !l_val )
00280                                 {
00281                                         result->_Values[ pos ] = r_val;
00282                                         if ( (*vals)[i] )
00283                                                 result->_Values[ pos ]->incRef();
00284                                 }
00285                         }
00286                         else
00287                         {
00288 //                              if ( test )
00289 //                                      test->release();
00290                                 result->release();
00291                                 return NULL;
00292                         }
00293                 }
00294                 return result;
00295         }
00296 
00297 
00298         bool CValueSet::operator==(CValueSet *cp)
00299         {
00300                 if( cp->_NbValues != _NbValues )
00301                         return false;
00302 
00303                 for ( sint32 i = 0; i < _NbValues; i++ )
00304                         if ( cp->_Values[i] != _Values[i] )
00305                                 return false;
00306                 return true;
00307         }
00308 
00309         IObjectIA *CValueSet::operator[](sint32 pos)
00310         {
00311                 if ( pos <= _NbValues )
00312                         return _Values[pos];    
00313                 else
00314                         return NULL;    // TODO: exception!!!!!!!!!!!!
00315         }
00316 
00317         IObjectIA *CValueSet::getValue(sint32 pos)
00318         {
00319                 if ( pos <= _NbValues )
00320                         return _Values[pos];    
00321                 else
00322                         return NULL;    // TODO: exception!!!!!!!!!!!!
00323         }
00324 
00326         sint32 CValueSet::undefined() const
00327         {
00328                 sint32 nb_undef = _NbValues;
00329                 for ( sint32 i = 0; i < _NbValues; i++ )
00330                         if ( _Values[i] != NULL )
00331                                 nb_undef--;
00332                 return nb_undef;
00333         }
00334 
00335         sint32 CValueSet::size()
00336         {
00337                 return _NbValues;
00338         }
00339 
00341         std::list<IObjectIA *> *CValueSet::getValues()
00342         {
00343                 // Warning: this list must be deleted after use!!!
00344                 std::list<IObjectIA *> *result = new std::list<IObjectIA *>;
00345                 for (sint32 i = 0; i < _NbValues; i++ )
00346                 {
00347                         if ( _Values[i] )
00348                                 result->push_back( _Values[i] );
00349                 }
00350                 return result;
00351         }
00352 
00353         const NLAIC::IBasicType *CValueSet::clone() const
00354         {
00355                 NLAIC::IBasicType *tmp = new CValueSet( *this );
00356                 return tmp;
00357         }
00358 
00359         const NLAIC::IBasicType *CValueSet::newInstance() const
00360         {
00361                 return clone();
00362         }
00363 
00364         const NLAIC::CIdentType &CValueSet::getType() const
00365         {
00366                 return IdValueSet;
00367         }
00368 
00369         void CValueSet::save(NLMISC::IStream &os)
00370         {
00371                 sint32 nb_Values = (sint32) _NbValues;
00372                 os.serial( nb_Values );
00373                 std::list<IObjectIA *> values;
00374                 std::vector<sint32> pos;
00375 
00376                 for ( sint32 i = 0; i < _NbValues; i++ )
00377                 {
00378                         if ( _Values[i] )
00379                         {
00380                                 values.push_back( _Values[i] );
00381                                 pos.push_back( i );
00382                         }
00383                 }
00384                 sint32 size = (sint32) values.size();
00385                 os.serial( size );
00386 
00387                 std::list<IObjectIA *>::iterator it_val = values.begin();
00388                 std::vector<sint32>::iterator it_pos = pos.begin();
00389 
00390                 while ( it_val != values.end() )
00391                 {
00392                         os.serial( (NLAIC::CIdentType &) ( *it_val )->getType() );
00393                         ( *it_val )->save( os );
00394                         sint32 ip = (sint32) *it_pos;
00395                         os.serial( ip );
00396                 }
00397         }
00398 
00399         void CValueSet::load(NLMISC::IStream &is)
00400         {
00401                 // Deletes the old table
00402                 int i;
00403                 for ( i = 0; i < _NbValues; i++ )
00404                         if ( _Values[i] )
00405                                 _Values[i]->release();
00406                 delete[] _Values;
00407                 
00408                 // Creates the new one
00409                 sint32 nb_Values;
00410                 is.serial( nb_Values );
00411                 _NbValues = (sint32) nb_Values;
00412                 sint32 nbvals;
00413                 is.serial( nbvals );
00414                 
00415                 _Values = new IObjectIA *[ _NbValues ];
00416 
00417                 for ( i = 0; i < _NbValues; i++ )
00418                 {
00419                         _Values[ i ] = NULL;
00420                 }
00421 
00422                 std::list<IObjectIA *> vals;
00423                 std::vector<sint32> pos;
00424 
00425                 for ( i = 0; i < nbvals; i++ )
00426                 {
00427                         NLAIC::CIdentTypeAlloc id;
00428                         is.serial( id );
00429                         IObjectIA *tmp_val = (IObjectIA *) id.allocClass();
00430                         tmp_val->load( is );
00431                         tmp_val->incRef();
00432                         vals.push_back(  tmp_val );
00433                 }
00434         }
00435 
00436         void CValueSet::getDebugString(std::string &txt) const
00437         {
00438                 std::string buf;
00439                 txt += " <CValueSet> ";
00440                 for (sint32 i = 0; i < _NbValues; i++ )
00441                 {
00442                         txt += " , ";
00443                         if ( _Values[i] )
00444                         {
00445                                 _Values[i]->getDebugString( buf );
00446 
00447                                 txt += buf ;
00448                         }
00449                         else txt += "NULL";
00450                 }
00451         }
00452 
00453         IObjetOp *CValueSet::operator ! () const
00454         {
00455                 CBoolType *result = new CBoolType ( !isTrue() );
00456                 return result;
00457         }
00458 
00459         IObjectIA *CValueSet::operator != (IObjectIA &a) const
00460         {
00461                 if ( _NbValues != ((CValueSet &)a)._NbValues )
00462                 {
00463                         CBoolType *result = new CBoolType ( false );
00464                         return result;
00465                 }
00466 
00467 
00468                 for (sint32 i = _NbValues; i < _NbValues; i++ )
00469                 {
00470                         if ( _Values[i] != ((CValueSet &)a)._Values[i] )
00471                         {
00472                                 CBoolType *result = new CBoolType ( false );
00473                                 return result;
00474                         }
00475 
00476                 }
00477                 CBoolType *result = new CBoolType ( true );
00478                 return result;
00479         }
00480 
00481         IObjectIA *CValueSet::operator == (IObjectIA &a) const
00482         {
00483                 if ( _NbValues != ((CValueSet &)a)._NbValues )
00484                 {
00485                         CBoolType *result = new CBoolType ( false );
00486                         return result;
00487                 }
00488 
00489                 for (sint32 i = 0; i < _NbValues; i++ )
00490                 {
00491                         bool test_result = (*_Values[i]) == *((CValueSet &)a)._Values[i];
00492 /*                      IObjectIA *test = (*_Values[i]) != *((CValueSet &)a)._Values[i];
00493                         bool test_result = test->isTrue();
00494                         test->release(); */
00495                         if ( !test_result )
00496                         {
00497                                 CBoolType *result = new CBoolType ( false );
00498                                 return result;
00499                         }
00500                 }
00501                 CBoolType *result = new CBoolType ( true );
00502                 return result;
00503         }
00504 
00505         bool CValueSet::isEqual(const IBasicObjectIA &cmp) const
00506         {
00507                 if ( _NbValues != ((CValueSet &)cmp)._NbValues )
00508                 {
00509                         return false;
00510                 }
00511 
00512                 for (sint32 i = _NbValues; i < _NbValues; i++ )
00513                 {
00514                         if ( _Values[i] != ((CValueSet &)cmp)._Values[i] )
00515                         {
00516                                 return false;
00517                         }
00518                 }
00519                 return true;
00520         }
00521 
00522         bool CValueSet::isTrue() const
00523         {
00524                 return ( undefined() == (sint32) 0 );
00525         }
00526 
00527         const IObjectIA::CProcessResult &CValueSet::run()
00528         {
00529                 return IObjectIA::ProcessRun;
00530         }
00531 
00532         CValueSet *CValueSet::forward(CClause *clause,std::vector<sint32> &pos)
00533         {
00534                 CValueSet *result = new CValueSet( clause->nbVars() );
00535 
00536                 std::vector<sint32>::iterator it_pos = pos.begin();
00537 
00538                 for (sint32 i = 0; i < _NbValues; i++ )
00539                 {
00540                         if ( _Values[i] )
00541                         {
00542                                 result->_Values[ *it_pos ] = _Values[i];
00543                                 _Values[i]->incRef();
00544                                 it_pos++;
00545                         }
00546                 }
00547                 return result;
00548         }
00549 
00550         void CValueSet::setSize(sint32 size)
00551         {
00552                 _NbValues = size;
00553                 sint32 i;
00554 
00555                 if ( _Values != NULL )
00556                 {
00557                         for ( i = 0; i < _NbValues; i++ );
00558                         _Values[i]->release();
00559                         delete _Values;
00560                 }
00561 
00562                 _Values = new IObjectIA *[ _NbValues ];
00563 
00564                 for ( i = 0; i < _NbValues; i++ )
00565                 {
00566                         _Values[ i ] = NULL;
00567                 }
00568         }
00569 }