# 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_object_operator.cpp

Go to the documentation of this file.
00001 #include "nel/ai/c/registry_class.h"
00002 #include "nel/ai/agent/actor_script.h"
00003 #include "nel/ai/script/lexsupport.h"
00004 #include "nel/ai/logic/interpret_object_operator.h"
00005 #include "nel/ai/logic/operator_script.h"
00006 #include "nel/ai/logic/fact.h"
00007 #include "nel/ai/logic/factbase.h"
00008 #include "nel/ai/logic/varset.h"
00009 #include "nel/ai/script/codage.h"
00010 
00011         
00012 #ifdef NL_DEBUG 
00013 #ifdef NL_OS_WINDOWS
00014 #include "windows.h"
00015 #endif
00016 #endif
00017 
00018 namespace NLAISCRIPT
00019 {
00020 #ifdef NL_DEBUG 
00021         extern bool NL_AI_DEBUG_SERVER;
00022 #endif
00023         COperatorClass::COperatorClass(const NLAIAGENT::IVarName &n) : CAgentClass(n)
00024         {
00025                 setBaseMethodCount(((NLAIAGENT::COperatorScript *)(NLAIAGENT::COperatorScript::IdOperatorScript.getFactory()->getClass()))->getBaseMethodCount());              
00026                 setBaseObjectInstance(((NLAIAGENT::COperatorScript *)(NLAIAGENT::COperatorScript::IdOperatorScript.getFactory()->getClass())));         
00027                 _Goal = NULL;
00028                 _Comment = NULL;
00029                 _FactBase = new NLAILOGIC::CFactBase();
00030                 _UpdateCycles = 0;
00031                 _Priority = 1;
00032         }
00033         
00034         COperatorClass::COperatorClass(const NLAIC::CIdentType &id): CAgentClass(id)
00035         {
00036                 setBaseMethodCount(((NLAIAGENT::COperatorScript *)(NLAIAGENT::COperatorScript::IdOperatorScript.getFactory()->getClass()))->getBaseMethodCount());
00037                 setBaseObjectInstance(((NLAIAGENT::COperatorScript *)(NLAIAGENT::COperatorScript::IdOperatorScript.getFactory()->getClass())));         
00038                 _Goal = NULL; 
00039                 _Comment = NULL;
00040                 _FactBase = new NLAILOGIC::CFactBase();
00041                 _UpdateCycles = 0;
00042                 _Priority = 1;
00043         }
00044 
00045         COperatorClass::COperatorClass(const NLAIAGENT::IVarName &n, const NLAIAGENT::IVarName &inheritance) : CAgentClass( inheritance )
00046         {
00047                 setBaseMethodCount(((NLAIAGENT::COperatorScript *)(NLAIAGENT::COperatorScript::IdOperatorScript.getFactory()->getClass()))->getBaseMethodCount());
00048                 setBaseObjectInstance(((NLAIAGENT::COperatorScript *)(NLAIAGENT::COperatorScript::IdOperatorScript.getFactory()->getClass())));         
00049                 _Goal = NULL;
00050                 _Comment = NULL;
00051                 _FactBase = new NLAILOGIC::CFactBase();
00052                 _UpdateCycles = 0;
00053                 _Priority = 1;
00054         }
00055 
00056         COperatorClass::COperatorClass(const COperatorClass &c) : CAgentClass( c )
00057         {
00058                 setBaseMethodCount(((NLAIAGENT::COperatorScript *)(NLAIAGENT::COperatorScript::IdOperatorScript.getFactory()->getClass()))->getBaseMethodCount());
00059                 setBaseObjectInstance(((NLAIAGENT::COperatorScript *)(NLAIAGENT::COperatorScript::IdOperatorScript.getFactory()->getClass())));         
00060                 if ( c._Goal != NULL)
00061                         _Goal = (NLAILOGIC::CGoal *) c._Goal->clone();
00062                 else
00063                         _Goal = NULL;
00064 
00065                 if ( c._Comment != NULL )
00066                 {
00067                         _Comment = new char[ strlen( c._Comment ) ];
00068                         strcpy( _Comment, c._Comment );
00069                 }
00070                 else
00071                         _Comment = NULL;
00072 
00073                 if ( c._FactBase != NULL)
00074                         _FactBase = (NLAILOGIC::CFactBase *) c._FactBase->clone();
00075                 else
00076                         _FactBase = new NLAILOGIC::CFactBase();
00077 
00078                 _UpdateCycles = c._UpdateCycles;
00079                 _Priority = c._Priority;
00080 
00081         }       
00082 
00083         COperatorClass::COperatorClass()
00084         {
00085                 setBaseMethodCount(((NLAIAGENT::COperatorScript *)(NLAIAGENT::COperatorScript::IdOperatorScript.getFactory()->getClass()))->getBaseMethodCount());
00086                 setBaseObjectInstance(((NLAIAGENT::COperatorScript *)(NLAIAGENT::COperatorScript::IdOperatorScript.getFactory()->getClass())));
00087                 _Goal = NULL;
00088                 _Comment = NULL;
00089                 _FactBase = new NLAILOGIC::CFactBase();
00090                 _UpdateCycles = 0;
00091                 _Priority = 1;
00092         }
00093 
00094         COperatorClass::~COperatorClass()
00095         {
00096                 if ( _Comment != NULL )
00097                         delete[] _Comment;
00098 
00099                 if ( _FactBase != NULL )
00100                         _FactBase->release();
00101 
00102                 if ( _Goal != NULL )
00103                         _Goal->release();
00104 
00105         
00106                 std::vector<NLAIAGENT::IVarName *>::iterator it_fvar = _FuzzyVars.begin();
00107                 while ( it_fvar != _FuzzyVars.end() )
00108                 {
00109                         (*it_fvar)->release();
00110                         it_fvar++;
00111                 }
00112 
00113                 it_fvar = _FuzzySets.begin();
00114                 while ( it_fvar != _FuzzySets.end() )
00115                 {
00116                         (*it_fvar)->release();
00117                         it_fvar++;
00118                 }
00119 
00120                 std::vector< IOpCode *>::iterator it_code =  _CondCode.begin();
00121                 while ( it_code != _CondCode.end() )
00122                 {
00123                         (*it_code)->release();
00124                         it_code++;
00125                 }
00126 
00127                 it_code = _ConcCode.begin();
00128                 while ( it_code != _ConcCode.end() )
00129                 {
00130                         (*it_code)->release();
00131                         it_code++;
00132                 }
00133 /*
00134                 int i;
00135                 for ( i = 0; i < (int) _CondCode.size(); i++ )
00136                 {
00137                         ( (NLAIAGENT::IVarName *)_CondCode[i] )->release();
00138                 }
00139 
00140                 for ( i = 0; i < (int) _CondCode.size(); i++ )
00141                 {
00142                         ( (NLAIAGENT::IVarName *)_ConcCode[i] )->release();
00143                 }
00144 
00145                 for ( i = 0; i < (int) _CondAsserts.size(); i++ )
00146                 {
00147                         ( (NLAIAGENT::IVarName *) _CondAsserts[i] )->release();
00148                 }
00149 
00150                 for ( i = 0; i < (int) _ConcAsserts.size(); i++ )
00151                 {
00152                         ( (NLAIAGENT::IVarName *) _ConcAsserts[i] )->release();
00153                 }
00154 
00155                 std::list<const NLAIAGENT::IVarName *>::iterator it_n = _BooleanConds.begin();
00156                 while ( _BooleanConds.size() )
00157                 {
00158                         ( (NLAIAGENT::IVarName *) _BooleanConds.front() )->release();
00159                         _BooleanConds.pop_front();
00160                 }
00161 
00162                 it_n = _BooleanConcs.begin();
00163                 while ( _BooleanConcs.size() )
00164                 {
00165                         ( (NLAIAGENT::IVarName *) _BooleanConcs.front() )->release();
00166                         _BooleanConcs.pop_front();
00167                 }
00168                 */
00169         }
00170 
00171 
00172         const NLAIC::IBasicType *COperatorClass::clone() const
00173         {
00174                 NLAIC::IBasicType *clone = new COperatorClass(*this);
00175                 return clone;
00176         }
00177 
00178         const NLAIC::IBasicType *COperatorClass::newInstance() const
00179         {
00180                 NLAIC::IBasicType *instance = new COperatorClass();
00181                 return instance;
00182         }
00183 
00184         void COperatorClass::getDebugString(std::string &t) const
00185         {
00186                 t += "<COperatorClass>";
00187                 int i;
00188                 for ( i = 0; i < (int) _Vars.size(); i++ )
00189                 {
00190                         std::string buf;
00191                         _Vars[i]->getDebugString(buf);
00192                         t += "   -";
00193                         t += buf;
00194                 }
00195         }
00196 
00197         NLAIAGENT::IObjectIA *COperatorClass::buildNewInstance() const
00198         {
00199                 // Création des composants statiques
00200                 std::list<NLAIAGENT::IObjectIA *> components;
00201                 createBaseClassComponents( components );
00202 
00203                 // Création du message
00204                 NLAIAGENT::COperatorScript *instance = new NLAIAGENT::COperatorScript( NULL, NULL ,components,  (COperatorClass *) this );
00205                 instance->setPriority(getPriority());
00206 
00207                 return instance;
00208         }
00209 
00211         bool COperatorClass::isValid(NLAILOGIC::CFactBase *fb)
00212         {               
00213                 std::list<NLAILOGIC::CFact *> *facts = new std::list<NLAILOGIC::CFact *>;
00214                 for (sint32 i = 0; i < (sint32) _Conds.size(); i++ )
00215                 {
00216                         std::list<NLAILOGIC::CFact *> *fa = fb->getAssertFacts( _Conds[i] );
00217                         while ( fa->size() )
00218                         {
00219                                 facts->push_back( fa->front() );
00220                                 fa->pop_front();
00221                         }
00222                         delete fa;
00223                 }
00224                 std::list<NLAILOGIC::CFact *> *res = propagate( *facts );
00225                 bool is_valid = !res->empty();
00226                 while ( res->size() )
00227                 {
00228 #ifdef NL_DEBUG
00229                                                 
00230 #endif
00231                         res->front()->release();
00232                         res->pop_front();
00233                 }
00234                 delete res;
00235 
00236                 while ( facts->size() )
00237                 {
00238                         facts->front()->release();
00239                         facts->pop_front();     
00240                 }
00241                 delete facts;
00242 
00243                 return is_valid;
00244         }
00245 
00246         NLAILOGIC::CValueSet *COperatorClass::unifyBackward(std::list<NLAILOGIC::CFact *> &facts)
00247         {
00248                 NLAILOGIC::CValueSet *unified = new NLAILOGIC::CValueSet( _Vars.size() );
00249                 std::list<NLAILOGIC::CFact *>::iterator it_f = facts.begin();
00250                 while ( it_f != facts.end() )
00251                 {
00252                         std::vector<sint32> pos_assert;
00253                         getAssertPos( (*it_f)->getAssert(), _Concs, pos_assert );
00254                         for (sint32 pos = 0; pos < (sint32) pos_assert.size(); pos++)
00255                         {
00256                                 for ( sint32 ivar = 0; ivar < (sint32) _PosVarsConc[ pos_assert[pos] ].size(); ivar++ )
00257                                 {
00258                                         sint32 l_pos = _PosVarsConc[ pos_assert[pos] ][ivar];
00259 
00260                                         NLAIAGENT::IObjectIA *l_val = (*unified)[ l_pos ]; 
00261                                         NLAIAGENT::IObjectIA *r_val = (**it_f)[ ivar ];
00262 
00263                                         if ( !l_val )
00264                                         {
00265                                                 if ( r_val )
00266                                                 {
00267                                                         unified->setValue( l_pos, r_val );
00268                                                 }
00269                                         }
00270                                         else
00271                                         {
00272                                                 if ( r_val && ( l_val != r_val ) )
00273                                                 {
00274                                                         unified->release();
00275                                                         return NULL;
00276                                                 }
00277                                         }
00278                                 }
00279                         }
00280                         it_f++;
00281                 }
00282                 return unified;
00283         }
00284 
00285         NLAILOGIC::CValueSet *COperatorClass::unifyForward(std::list<NLAILOGIC::CFact *> &facts)
00286         {
00287                 NLAILOGIC::CValueSet *unified = new NLAILOGIC::CValueSet( _Vars.size() );
00288                 std::list<NLAILOGIC::CFact *>::iterator it_f = facts.begin();
00289                 while ( it_f != facts.end() )
00290                 {
00291                         std::vector<sint32> pos_assert;
00292                         getAssertPos( (*it_f)->getAssert(), _Conds, pos_assert );
00293                         for (sint32 pos = 0; pos < (sint32) pos_assert.size(); pos++)
00294                         {
00295                                 for ( sint32 ivar = 0; ivar < (sint32) _PosVarsCond[ pos_assert[pos] ].size(); ivar++ )
00296                                 {
00297                                         sint32 l_pos = _PosVarsCond[ pos_assert[pos] ][ivar];
00298 
00299                                         NLAIAGENT::IObjectIA *l_val = (*unified)[ l_pos ]; 
00300                                         NLAIAGENT::IObjectIA *r_val = (**it_f)[ ivar ];
00301 
00302                                         if ( !l_val )
00303                                         {
00304                                                 if ( r_val )
00305                                                 {
00306                                                         unified->setValue( l_pos, r_val );
00307                                                 }
00308                                         }
00309                                         else
00310                                         {
00311                                                 if ( r_val && ( l_val != r_val ) )
00312                                                 {
00313                                                         unified->release();
00314                                                         return NULL;
00315                                                 }
00316                                         }
00317                                 }
00318                         }
00319                         it_f++;
00320                 }
00321                 return unified;
00322         }
00323 
00324         NLAILOGIC::CFact *COperatorClass::buildFromVars(NLAILOGIC::IBaseAssert *assert, std::vector<sint32> &pl, NLAILOGIC::CValueSet *vars)
00325         {
00326                 NLAILOGIC::CFact *result = new NLAILOGIC::CFact( assert);       // TODO:: pas besoin du nombre dans ce constructeur puisqu'on a l'assert
00327                 for (sint32 i = 0; i < (sint32) pl.size() ; i++ )
00328                 {
00329                         result->setValue( i, (*vars)[ pl[i] ] );
00330                 }
00331                 return result;
00332         }
00333 
00334         std::list<NLAILOGIC::CFact *> *COperatorClass::backward(std::list<NLAILOGIC::CFact *> &facts)
00335         {
00336                 NLAILOGIC::CValueSet *unified = unifyBackward( facts );
00337                 std::list<NLAILOGIC::CFact *> *result = new std::list<NLAILOGIC::CFact *>;
00338                 for (sint32 i = 0; i < (sint32) _Conds.size(); i++ )
00339                 {
00340                         NLAILOGIC::CFact *tmp = buildFromVars( _Conds[i], _PosVarsCond[i], unified );
00341                         result->push_back( tmp );
00342 #ifdef NL_DEBUG
00343                                                 
00344 #endif
00345                 }
00346                 unified->release();
00347                 return result;
00348         }
00349 
00350         std::list<NLAILOGIC::CFact *> *COperatorClass::forward(std::list<NLAILOGIC::CFact *> &facts)
00351         {
00352                 NLAILOGIC::CValueSet *unified = unifyForward( facts );
00353 #ifdef NL_DEBUG
00354                 
00355 #endif
00356                 std::list<NLAILOGIC::CFact *> *result = new std::list<NLAILOGIC::CFact *>;
00357                 for (sint32 i = 0; i < (sint32) _Concs.size(); i++ )
00358                 {
00359                         NLAILOGIC::CFact *tmp = buildFromVars( _Concs[i], _PosVarsConc[i], unified );
00360                         result->push_back( tmp );
00361 #ifdef NL_DEBUG
00362                         
00363 #endif
00364                 }
00365                 unified->release();
00366                 return result;
00367         }
00368 
00369 
00370         std::list<NLAILOGIC::CFact *> *COperatorClass::propagate(std::list<NLAILOGIC::CFact *> &facts)
00371         {
00372                 std::list<NLAILOGIC::CFact *> *conflicts = new std::list<NLAILOGIC::CFact *>;
00373                 std::list< NLAILOGIC::CValueSet *>      liaisons;
00374                 NLAILOGIC::CValueSet *empty = new NLAILOGIC::CValueSet( _Vars.size() );
00375                 liaisons.push_back( empty );
00376 
00377                 std::list<NLAILOGIC::CFact *>::iterator it_f = facts.begin();
00378                 while ( it_f != facts.end() )
00379                 {
00380                         std::vector<sint32> pos_asserts;
00381                         getAssertPos( (*it_f)->getAssert() , _Conds, pos_asserts);
00382                         for (sint32 i = 0; i < (sint32) pos_asserts.size(); i++ )
00383                         {
00384 /*                              std::list<NLAILOGIC::CValueSet *> *links = propagate( liaisons, *it_f, _PosVarsCond[ pos_asserts[i] ] );
00385                                 if ( links )
00386                                 {
00387                                         while ( links->size() )
00388                                         {
00389                                                 for (sint32 i = 0; i < (sint32) _Concs.size(); i++ )
00390                                                 {
00391                                                         NLAILOGIC::CFact *r = buildFromVars( _Concs[i], _PosVarsConc[i], links->front() );
00392                                                         char buf[1024];
00393                                                         r->getDebugString( buf );
00394                                                         // Tests if the fact is already in the conflicts list
00395                                                         bool found = false;
00396                                                         std::list<NLAILOGIC::CFact *>::iterator it_c = conflicts->begin();
00397                                                         while ( ! found && it_c != conflicts->end() )
00398                                                         {
00399                                                                 found = (**it_c) == *r;
00400                                                                 it_c++;
00401                                                         }
00402                                                         if ( !found )
00403                                                         {
00404                                                                 char buf[1024];
00405                                                                 r->getDebugString( buf );
00406                                                                 conflicts->push_back( r );
00407                                                         }
00408                                                 }
00409                                                 links->front()->release();
00410                                                 links->pop_front();
00411                                         }
00412                                         delete links;
00413                                 }
00414                                 */
00415                         }
00416                         it_f++;
00417                 }
00418 
00419                 while ( liaisons.size() )
00420                 {
00421                         liaisons.front()->release();
00422                         liaisons.pop_front();
00423                 }
00424 
00425                 return conflicts;
00426         }
00427 
00428         NLAILOGIC::CValueSet *COperatorClass::unifyLiaison( const NLAILOGIC::CValueSet *fp, NLAILOGIC::CValueSet *vals, std::vector<sint32> &pos_vals)
00429         {
00430                 NLAILOGIC::CValueSet *result;
00431 
00432                 if ( (result = fp->unify( vals, pos_vals )) )
00433                         return result;
00434                 else
00435                 {
00436                         delete result;
00437                         return NULL;
00438                 }
00439         }
00440 
00441         void COperatorClass::getAssertPos(NLAILOGIC::IBaseAssert *a, std::vector<NLAILOGIC::IBaseAssert *> &l, std::vector<sint32> &pos)
00442         {
00443                 for (sint32 i = 0; i < (sint32) l.size() ; i++ )
00444                 {
00445                         if ( (*(l[i])) == a )
00446                                 pos.push_back(i);
00447                 }
00448         }
00449 
00450         float COperatorClass::priority() const
00451         {
00452                 return 0.0;
00453         }
00454 
00455         void COperatorClass::success()
00456         {
00457         }
00458 
00459         void COperatorClass::failure()
00460         {
00461         }
00462 
00463         void COperatorClass::success(NLAILOGIC::IBaseOperator *)
00464         {
00465         }
00466 
00467         void COperatorClass::failure(NLAILOGIC::IBaseOperator *)
00468         {
00469         }
00470 
00471         const NLAILOGIC::CGoal *COperatorClass::getGoal()
00472         {
00473                 return _Goal;
00474         }
00475 
00476         void COperatorClass::addPrecondition(NLAILOGIC::CFactPattern *pattern)
00477         {
00478                 if ( pattern->getAssert() )
00479                 {
00480                         std::vector<sint32> pos_Vars;
00481                         compileFactPattern( pattern, _Conds, pos_Vars);
00482 
00483 //                      pattern->getAssert()->addClause( this, pos_Vars );
00484                         _Conds.push_back( pattern->getAssert() );
00485                         _PosVarsCond.push_back( pos_Vars );
00486 
00487                 }
00488         }
00489 
00490         void COperatorClass::addPostcondition(NLAILOGIC::CFactPattern *pattern)
00491         {
00492                 if ( pattern->getAssert() )
00493                 {
00494                         std::vector<sint32> pos_Vars;
00495                         compileFactPattern( pattern, _Conds, pos_Vars);
00496 
00497 //                      pattern->getAssert()->addInput( this );
00498                         _Concs.push_back( pattern->getAssert() );
00499                         _PosVarsConc.push_back( pos_Vars );
00500                 }
00501         }
00502 
00504 
00505 /*
00506         void COperatorClass::compileFactPattern(NLAILOGIC::CFactPattern *fp, std::vector<NLAILOGIC::IBaseAssert *>&patterns, std::vector<sint32> &pos_Vars)
00507         {
00508                 // Recherche si variables à ajouter
00509                 std::vector<NLAILOGIC::IBaseVar *> *vars_pattern = fp->getVars();
00510                 if ( vars_pattern )
00511                 {
00512                         std::vector<NLAILOGIC::IBaseVar *>::iterator it_cond = vars_pattern->begin();
00513                         while ( it_cond != vars_pattern->end() )
00514                         {
00515                                 sint32 id_var = getVarPos( *it_cond );
00516                                 if ( id_var != -1 )
00517                                 {
00518                                         pos_Vars.push_back( id_var );
00519                                 }
00520                                 else
00521                                 {
00522                                         _Vars.push_back( (NLAILOGIC::IBaseVar *)(*it_cond)->clone() );
00523                                         pos_Vars.push_back( _Vars.size() - 1);
00524                                 }
00525                                 it_cond++;
00526                         }
00527                 }
00528 
00529                 for ( sint32 i = 0; i < (sint32) vars_pattern->size(); i++ )
00530                 {
00531                         (*vars_pattern)[i]->release();
00532                 }
00533                 delete vars_pattern;
00534         }
00535         */
00536 
00537         void COperatorClass::compileFactPattern(NLAILOGIC::CFactPattern *fp, std::vector<NLAILOGIC::IBaseAssert *>&patterns, std::vector<sint32> &pos_Vars)
00538         {
00539                 // Recherche si variables à ajouter
00540                 std::vector<NLAILOGIC::IBaseVar *> *vars_pattern = fp->getVars();
00541                 if ( vars_pattern )
00542                 {
00543                         std::vector<NLAILOGIC::IBaseVar *>::iterator it_cond = vars_pattern->begin();
00544                         while ( it_cond != vars_pattern->end() )
00545                         {
00546                                 // Looks in the class components if the var already exists
00547                                 sint32 id_var = getComponentIndex( (*it_cond)->getName() );
00548 
00549                                 if ( id_var != -1 )
00550                                 {
00551                                         // If it exists, stores its index
00552                                         pos_Vars.push_back( id_var );
00553                                 }
00554                                 else
00555                                 {
00556                                         // If it doesn't exist, registers the var as a component of the class
00557                                         NLAIAGENT::CStringVarName var_name("Var");
00558                                         registerComponent( var_name , (const NLAIAGENT::CStringVarName &) (*it_cond)->getName() );
00559 
00560                                         // TODO: contrôle de type
00561 
00562                                         _Vars.push_back( (NLAILOGIC::IBaseVar *)(*it_cond)->clone() );
00563                                         pos_Vars.push_back( _Vars.size() - 1);
00564                                 }
00565                                 it_cond++;
00566                         }
00567                 }
00568 
00569                 for ( sint32 i = 0; i < (sint32) vars_pattern->size(); i++ )
00570                 {
00571                         (*vars_pattern)[i]->release();
00572                 }
00573                 delete vars_pattern;
00574         }
00576 
00578         sint32 COperatorClass::getVarPos(NLAILOGIC::IBaseVar *var)
00579         {
00580                 if ( _Vars.size() )
00581                 {
00582                         for (sint32 i = 0; i < (sint32)_Vars.size() ; i++ ) 
00583                         {
00584                                 if ( var->getName() == _Vars[ i ]->getName() )
00585                                 {
00586                                         return i;
00587                                 }
00588                         }
00589                 }
00590                 return -1;
00591         }
00592 
00594         void COperatorClass::addFirstOrderCond(const NLAIAGENT::IVarName *assert_name, std::list<const NLAIAGENT::IVarName *> &params_list)
00595         {
00596         /*      _CondAsserts.push_back( assert_name );
00597                 std::list<const NLAIAGENT::IVarName *> *tmp_list = new std::list<const NLAIAGENT::IVarName *>;
00598                 while ( !params_list.empty() )
00599                 {
00600                         const char *txt = params_list.front()->getString();
00601                         tmp_list->push_back( params_list.front() );
00602                         params_list.pop_front();
00603                 }               
00604                 _ClassCondVars.push_back( tmp_list );
00605                 */
00606 
00607                         NLAIAGENT::CStringVarName name = *(NLAIAGENT::CStringVarName *)assert_name->clone();
00608                         NLAILOGIC::IBaseAssert *assert = _FactBase->addAssert( name, params_list.size() ); 
00609                         NLAILOGIC::CFactPattern *pattern = new NLAILOGIC::CFactPattern( assert );
00610                         std::list<const NLAIAGENT::IVarName *>::iterator it_var = params_list.begin();
00611                         while ( it_var != params_list.end() )
00612                         {
00613                                 NLAIAGENT::CStringVarName var_name = *(NLAIAGENT::CStringVarName *)(*it_var);
00614                                 pattern->addVar(  new NLAILOGIC::CVar( var_name ) );
00615                                 it_var++;
00616                         }
00617                         addPrecondition( pattern );
00618         }
00619 
00620         void  COperatorClass::addFirstOrderConc(const NLAIAGENT::IVarName *assert_name, std::list<const NLAIAGENT::IVarName *> &params_list)
00621         {
00622                 _ConcAsserts.push_back( assert_name );
00623                 std::list<const NLAIAGENT::IVarName *> *tmp_list = new std::list<const NLAIAGENT::IVarName *>;
00624                 while ( !params_list.empty() )
00625                 {
00626                         tmp_list->push_back( params_list.front() );
00627                         params_list.pop_front();
00628                 }
00629                 _ClassConcVars.push_back( tmp_list );
00630         }
00631 
00633         void COperatorClass::addBoolCond(const NLAIAGENT::IVarName *cond_name)
00634         {
00635                 _BooleanConds.push_back( cond_name );
00636         }
00637 
00638         void COperatorClass::addBoolConc(const NLAIAGENT::IVarName *conc_name)
00639         {
00640                 _BooleanConcs.push_back( conc_name );
00641         }
00642         
00645         void COperatorClass::addCodeCond(IOpCode *code)
00646         {
00647                 _CondCode.push_back( code );
00648         }
00649 
00651         void COperatorClass::addCodeConc(IOpCode *code)
00652         {
00653                 _ConcCode.push_back( code );
00654         }
00655 
00657         void COperatorClass::buildLogicTables()
00658         {
00659 //              _FactBase = new NLAILOGIC::CFactBase();
00660                 int i;
00661                 for ( i = 0; i < (int) _CondAsserts.size() ; i++ )
00662                 {
00663                         NLAIAGENT::CStringVarName name = *(NLAIAGENT::CStringVarName *)_CondAsserts[i]->clone();
00664                         NLAILOGIC::IBaseAssert *assert = _FactBase->addAssert( name, _ClassCondVars[i]->size() );
00665                         NLAILOGIC::CFactPattern *pattern = new NLAILOGIC::CFactPattern( assert );
00666                         std::list<const NLAIAGENT::IVarName *>::iterator it_var = _ClassCondVars[i]->begin();
00667                         while ( it_var != _ClassCondVars[i]->end() )
00668                         {
00669                                 NLAIAGENT::CStringVarName var_name = *(NLAIAGENT::CStringVarName *)(*it_var);
00670                                 pattern->addVar(  new NLAILOGIC::CVar( var_name ) );
00671                                 it_var++;
00672                         }
00673                         addPrecondition( pattern );
00674                 }
00675 
00676                 for ( i = 0; i < (int) _ConcAsserts.size() ; i++ )
00677                 {
00678                         NLAIAGENT::CStringVarName name = *(NLAIAGENT::CStringVarName *)_ConcAsserts[i]->clone();
00679                         NLAILOGIC::IBaseAssert *assert = _FactBase->addAssert( name, _ClassConcVars[i]->size() );
00680                         NLAILOGIC::CFactPattern *pattern = new NLAILOGIC::CFactPattern( assert );
00681                         std::list<const NLAIAGENT::IVarName *>::iterator it_var = _ClassConcVars[i]->begin();
00682                         while ( it_var != _ClassConcVars[i]->end() )
00683                         {
00684                                 NLAIAGENT::CStringVarName var_name = *(NLAIAGENT::CStringVarName *)(*it_var);
00685                                 pattern->addVar(  new NLAILOGIC::CVar( var_name ) );
00686                                 it_var++;
00687                         }
00688                         addPostcondition( pattern );
00689                 }               
00690         }
00691 
00692         void COperatorClass::setGoal(const NLAIAGENT::CStringVarName &g)
00693         {
00694 
00695                 _Goal = new NLAILOGIC::CGoal( *(NLAIAGENT::CStringVarName *) g.clone());
00696         }
00697 
00698         void COperatorClass::setGoal(const NLAIAGENT::IVarName *assert, std::list<const NLAIAGENT::IVarName *> &args)
00699         {
00700                 _GoalAssert = (const NLAIAGENT::IVarName *) assert->clone();
00701                 _GoalVars = args;
00702 
00703                 if ( _Goal != NULL )
00704                 {
00705                         _Goal->release();
00706                 }
00707         
00708                 _Goal = new NLAILOGIC::CGoal( *(NLAIAGENT::CStringVarName *) assert->clone() );
00709 
00710                 std::list<const NLAIAGENT::IVarName *>::iterator it_var = args.begin();
00711                 while ( it_var != args.end() )
00712                 {
00713                         // Looks in the class components if the var already exists
00714                         sint32 id_var = getComponentIndex( **it_var );
00715 
00716                         if ( id_var != -1 )
00717                         {
00718                                 // If it exists, stores its index
00719                                 _GoalPosVar.push_back( id_var );
00720                         }
00721                         else
00722                         {
00723                                 // If it doesn't exist, registers the var as a component of the class
00724                                 NLAIAGENT::CStringVarName var_name("Var");
00725                                 registerComponent( var_name , (const NLAIAGENT::CStringVarName &) **it_var );
00726 //                              _Vars.push_back( (NLAILOGIC::IBaseVar *)(*it_cond)->clone() );
00727                                 _GoalPosVar.push_back( getComponentIndex(**it_var) );
00728                         }
00729                         it_var++;
00730                 }
00731 
00732                 std::list<NLAIAGENT::IObjectIA *> arg_list;
00733                 while ( !args.empty() )
00734                 {
00735                         arg_list.push_back( (NLAIAGENT::IObjectIA *) args.front()->clone() );
00736                         ( (NLAIAGENT::IObjectIA *)args.front() )->release();
00737                         args.pop_front();
00738                 }
00739 
00740                 _Goal->setArgs( arg_list );
00741         }
00742 
00743         bool COperatorClass::isValidFonc(NLAIAGENT::IObjectIA *c)
00744         {
00745                 NLAISCRIPT::CCodeContext &context = (NLAISCRIPT::CCodeContext &)*c;
00746                 
00747                 for ( int i = 0; i < (int) _CondCode.size(); i++ )
00748                 {
00749                         NLAISCRIPT::IOpCode *opPtr = _CondCode[ i ];
00750 
00751                         IObjectIA::CProcessResult r;
00752                         if(opPtr != NULL)
00753                         {
00754                                 NLAISCRIPT::IOpCode &op = *opPtr;
00755                                 NLAISCRIPT::CCodeBrancheRun *opTmp = context.Code;
00756                                 int ip;
00757                                 if(context.Code != NULL) ip = (uint32)*context.Code;
00758                                 else ip =0;
00759                                 context.Code = (NLAISCRIPT::CCodeBrancheRun *)&op;              
00760                                 *context.Code = 0;
00761 
00762 #ifdef NL_DEBUG
00763                                 sint sp = context.Stack.CIndexStackPointer::operator int ();
00764 #endif
00765                                 r = ((NLAISCRIPT::ICodeBranche *)opPtr)->run(context);
00766 
00767                                 *context.Code = ip;
00768                                 context.Code = opTmp;           
00769 
00770                                 NLAIAGENT::IObjetOp *result = (NLAIAGENT::IObjetOp *)context.Stack[(int)context.Stack];
00771                                 result->incRef();
00772                                 context.Stack--;
00773 
00774 #ifdef NL_DEBUG
00775                                 sint u = context.Stack.CIndexStackPointer::operator int ();
00776                                 if(sp != u)
00777                                 {
00778                                         
00779                                         throw;
00780                                 }
00781 #endif
00782 
00783                                 if ( result != NULL)
00784                                 {
00785 #ifdef NL_DEBUG
00786                                         const char *dbg_return_type = (const char *) result->getType();
00787                                         
00788 #endif
00789                                         bool br = !result->isTrue();
00790                                         result->release();
00791                 
00792                                         if ( br )
00793                                                 return false;
00794                                 }
00795 
00796                         }
00797                 }
00798                 return true;
00799         }
00800 
00801         void COperatorClass::activatePostConditions(NLAIAGENT::IObjectIA *c)
00802         {
00803                 NLAISCRIPT::CCodeContext &context = (NLAISCRIPT::CCodeContext &)*c;
00804                 
00805                 for ( int i = 0; i < (int) _ConcCode.size(); i++ )
00806                 {
00807                         NLAISCRIPT::IOpCode *opPtr = _ConcCode[ i ];
00808 
00809                         IObjectIA::CProcessResult r;
00810                         if(opPtr != NULL)
00811                         {
00812                                 NLAISCRIPT::IOpCode &op = *opPtr;
00813                                 NLAISCRIPT::CCodeBrancheRun *opTmp = context.Code;
00814                                 int ip = (uint32)*context.Code;
00815                                 context.Code = (NLAISCRIPT::CCodeBrancheRun *)&op;              
00816                                 *context.Code = 0;
00817 
00818                                 r = ((NLAISCRIPT::ICodeBranche *)opPtr)->run(context);
00819                                 // If we are in Debug Mode
00820                                 if (context.ContextDebug.Active)
00821                                 {
00822                                         context.ContextDebug.callStackPop();
00823                                 }
00824                                 *context.Code = ip;
00825                                 context.Code = opTmp;           
00826                         }
00827                 }
00828         }
00829         
00830         void COperatorClass::initialiseFactBase(NLAILOGIC::CFactBase *inst__FactBase)
00831         {
00832                 int i;
00833                 for ( i = 0; i < (int) _CondAsserts.size() ; i++ )
00834                 {
00835                         NLAIAGENT::CStringVarName name = *(NLAIAGENT::CStringVarName *)_CondAsserts[i]->clone();
00836                         NLAILOGIC::IBaseAssert *assert = inst__FactBase->addAssert( name, _ClassCondVars[i]->size() ); 
00837                         NLAILOGIC::CFactPattern *pattern = new NLAILOGIC::CFactPattern( assert );
00838                         std::list<const NLAIAGENT::IVarName *>::iterator it_var = _ClassCondVars[i]->begin();
00839                         while ( it_var != _ClassCondVars[i]->end() )
00840                         {
00841                                 NLAIAGENT::CStringVarName var_name = *(NLAIAGENT::CStringVarName *)(*it_var);
00842                                 pattern->addVar(  new NLAILOGIC::CVar( var_name ) );
00843                                 it_var++;
00844                         }
00845                         addPrecondition( pattern );
00846                 }
00847 
00848                 for ( i = 0; i < (int) _ConcAsserts.size() ; i++ )
00849                 {
00850                         NLAIAGENT::CStringVarName name = *(NLAIAGENT::CStringVarName *)_ConcAsserts[i]->clone();
00851                         NLAILOGIC::IBaseAssert *assert = inst__FactBase->addAssert( name, _ClassConcVars[i]->size() ); 
00852                         NLAILOGIC::CFactPattern *pattern = new NLAILOGIC::CFactPattern( assert );
00853                         std::list<const NLAIAGENT::IVarName *>::iterator it_var = _ClassConcVars[i]->begin();
00854                         while ( it_var != _ClassConcVars[i]->end() )
00855                         {
00856                                 NLAIAGENT::CStringVarName var_name = *(NLAIAGENT::CStringVarName *)(*it_var);
00857                                 pattern->addVar(  new NLAILOGIC::CVar( var_name ) );
00858                                 it_var++;
00859                         }
00860                         addPostcondition( pattern );
00861                 }
00862         }
00863 
00865         void COperatorClass::setComment(char *c)
00866         {
00867                 if ( _Comment != NULL )
00868                 {
00869                         delete[] _Comment;
00870                 }
00871                 _Comment = new char[ strlen(c) + 1];
00872                 strcpy(_Comment, c);
00873         }
00874 
00875         void COperatorClass::addFuzzyCond(NLAIAGENT::IVarName *var_name, NLAIAGENT::IVarName *fset)
00876         {
00877                 _FuzzyVars.push_back( var_name );
00878                 _FuzzySets.push_back( fset );
00879         }
00880 
00881         void COperatorClass::RegisterMessage(NLAIAGENT::IMessageBase::TPerformatif perf, const std::string &msg_class, const std::string &msg_varname)
00882         {
00883                 try
00884                 {
00885 
00886                         // Checks if a trigger with the same message or var doesn't already exist
00887                         std::vector<std::string>::iterator it_s = _TrigMsgVarname.begin();
00888                         while ( it_s != _TrigMsgVarname.end() )
00889                         {
00890                                 if ( msg_varname == *it_s )
00891                                 {
00892                                         std::string debugString;
00893                                         std::string text;
00894                                         getDebugString(debugString);
00895                                         text += NLAIC::stringGetBuild("MessageCond(%s) defined twice in operator class '%s'",msg_varname.c_str(), debugString.c_str());
00896                                         throw NLAIE::CExceptionNotImplemented(text.c_str()); 
00897                                 }
00898                                 it_s++;
00899                         }
00900 
00901                         NLAIC::CIdentType id_class( msg_class.c_str() );
00902                         _TrigMsgClass.push_back( id_class );
00903                         _TrigMsgPerf.push_back( perf );
00904                         _TrigMsgVarname.push_back( msg_varname );
00905 
00906                         // Looks in the class components if the var already exists
00907                         sint32 id_var = getInheritedStaticMemberIndex( NLAIAGENT::CStringVarName((const char *) msg_varname.c_str()) );
00908 
00909                         if ( id_var != -1 )
00910                         {
00911                                 // If it exists, stores its index
00912                                 _TrigMsgPos.push_back( id_var );
00913                         }
00914                         else
00915                         {
00916                                 // If it doesn't exist, registers the var as a component of the class
00917                                 NLAIAGENT::CStringVarName class_name( (const char *) msg_class.c_str() );
00918                                 NLAIAGENT::CStringVarName var_name( (const char *) msg_varname.c_str() );
00919                                 registerComponent( class_name, var_name );
00920                                 _TrigMsgPos.push_back( getInheritedStaticMemberIndex( NLAIAGENT::CStringVarName((const char *) msg_varname.c_str()) ) );
00921                         }
00922                 }
00923                 catch (NLAIE::IException &err)
00924                 {                               
00925                         throw CExceptionHaveNoType( err.what() );
00926                 }
00927         }
00928 
00929         // Checks if a message is in the PreCondition messages list
00930         sint32 COperatorClass::checkTriggerMsg(const NLAIAGENT::IMessageBase *msg)
00931         {
00932                 sint32 n = _TrigMsgPerf.size();
00933 
00934                 while ( n-- )
00935                 {
00936                         if ( msg->getPerformatif() == _TrigMsgPerf[n] )
00937                         {
00938 #ifdef NL_DEBUG
00939                                 const char *msg_dbg = (const char *) msg->getType();                            
00940 #endif
00941                                 if ( msg->getType() == _TrigMsgClass[n] )
00942                                         return _TrigMsgPos[n];
00943                         }
00944                 }
00945                 return -1;
00946         }
00947 }