# 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  

fuzzyrule.cpp

Go to the documentation of this file.
00001 
00007 /* Copyright, 2000 Nevrax Ltd.
00008  *
00009  * This file is part of NEVRAX NEL.
00010  * NEVRAX NEL is free software; you can redistribute it and/or modify
00011  * it under the terms of the GNU General Public License as published by
00012  * the Free Software Foundation; either version 2, or (at your option)
00013  * any later version.
00014 
00015  * NEVRAX NEL is distributed in the hope that it will be useful, but
00016  * WITHOUT ANY WARRANTY; without even the implied warranty of
00017  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
00018  * General Public License for more details.
00019 
00020  * You should have received a copy of the GNU General Public License
00021  * along with NEVRAX NEL; see the file COPYING. If not, write to the
00022  * Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
00023  * MA 02111-1307, USA.
00024  */
00025 #include "nel/ai/agent/agent.h"
00026 #include "nel/ai/logic/boolval.h"
00027 #include "nel/ai/fuzzy/fuzzyrule.h"
00028 #include "nel/ai/fuzzy/fuzzyvar.h"
00029 
00030 
00031 namespace NLAIFUZZY
00032 {
00033 
00034         CFuzzyRule::CFuzzyRule(char *comment)
00035         {
00036                 if ( comment )
00037                 {
00038                         _Comment = new char[strlen(comment) + 1];
00039                         strcpy(_Comment, comment);
00040                 }
00041                 else 
00042                         _Comment = NULL;
00043         }
00044 
00045         CFuzzyRule::CFuzzyRule(std::list<NLAILOGIC::IBaseBoolType *> &conds, char *comment, float threshold) : NLAILOGIC::CondAnd( conds )
00046         {
00047                 _Threshold = threshold;
00048 
00049                 if ( comment )
00050                 {
00051                         _Comment = new char[strlen(comment) + 1];
00052                         strcpy(_Comment, comment);
00053                 }
00054                 else 
00055                         _Comment = NULL;
00056         }
00057 
00058         CFuzzyRule::CFuzzyRule(const CFuzzyRule &cp) : NLAILOGIC::CondAnd(cp)
00059         {
00060                 _Threshold = cp._Threshold;
00061 
00062                 if ( cp._Comment )
00063                 {
00064                         _Comment = new char[strlen(cp._Comment) + 1];
00065                         strcpy(_Comment, cp._Comment);
00066                 }
00067                 else
00068                         _Comment = NULL;
00069         }
00070 
00071         CFuzzyRule::CFuzzyRule(const NLAILOGIC::CondAnd &cp) : NLAILOGIC::CondAnd(cp)
00072         {
00073         }
00074 
00075         CFuzzyRule::~CFuzzyRule()
00076         {
00077                 if( _Comment )
00078                         delete[] _Comment;
00079                 // TODO: destruction des concs....
00080         }
00081 
00082         void CFuzzyRule::addCond(CFuzzyVar *var, char *set_name)
00083         {
00084                 IFuzzySet *set = var->getSet(set_name);
00085                 if ( set )
00086                 {
00087                         _Vars.push_back( var );
00088                         _Sets.push_back( set );
00089                 }
00090                 else
00091                 {
00092                         // TODO: exception
00093                 }
00094         }
00095         
00096         void CFuzzyRule::addConc(CFuzzyVar *var, IFuzzySet *set)
00097         {
00098                 _Vars.push_back(var);
00099                 _Sets.push_back(set);
00100         }
00101 
00102         void CFuzzyRule::addConc(CFuzzyVar *var, char *set_name)
00103         {
00104                 IFuzzySet *set = var->getSet(set_name);
00105                 if ( set )
00106                 {
00107                         _Vars.push_back(var);
00108                         _Sets.push_back(set);
00109                 }
00110                 // TODO: exception
00111         }
00112 
00113         void CFuzzyRule::getDebugString(std::string &txt) const
00114         {               
00115                 std::string buf_cond;
00116                 std::string buf_conc;           
00117 
00118                 //FuzzyCond::getDebugString(buf_cond);
00119                 for ( sint32 i = 0; i < (sint32) _Vars.size(); i++ )
00120                 {
00121                         buf_conc += "(";
00122                         buf_conc += _Vars[i]->getName().getString() ;
00123                         buf_conc += " is ";
00124                         buf_conc += _Sets[i]->getName() ;
00125                         buf_conc += ")";
00126                         
00127                         if ( i < (sint32) (_Vars.size() - 1) )
00128                                 buf_conc += " and ";
00129                 }
00130         
00131                 txt += NLAIC::stringGetBuild("if\n %s\n then\n %s", buf_cond.c_str(), buf_conc.c_str());
00132         }
00133 
00134         void CFuzzyRule::save(NLMISC::IStream &os)
00135         {
00136                 // TODO: FuzzyCond::save(os);
00137                 sint32 size = _Vars.size();
00138                 for ( sint32 i = 0; i < size; i++ )
00139                 {
00140                         _Vars[i]->save(os);
00141                         _Sets[i]->save(os);
00142                 }
00143         }
00144 
00145         void CFuzzyRule::load(NLMISC::IStream &is)
00146         {
00147                 // TODO:                FuzzyCond::load( is );
00148 
00149                 sint32 nb_sets;
00150                 is.serial( nb_sets );
00151                 for (sint32 i = 0; i < nb_sets; i++ )
00152                 {
00153                         NLAIC::CIdentTypeAlloc id_var;
00154                         is.serial( id_var );
00155                         CFuzzyVar *tmp_var = (CFuzzyVar *) id_var.allocClass();
00156                         tmp_var->load( is );
00157                         _Vars.push_back( tmp_var );
00158 
00159                         NLAIC::CIdentTypeAlloc id_set;
00160                         is.serial( id_set );
00161                         IFuzzySet *tmp_set = (IFuzzySet *) id_set.allocClass();
00162                         tmp_set->load( is );
00163                         _Sets.push_back( tmp_set );
00164                 }
00165         }
00166 
00167         const NLAIC::IBasicType *CFuzzyRule::clone() const
00168         {
00169                 CFuzzyRule *tmp = new CFuzzyRule( *this );
00170                 return (NLAIC::IBasicType *) tmp;
00171         }
00172 
00173         const NLAIC::IBasicType *CFuzzyRule::newInstance() const
00174         {
00175                 CFuzzyRule *tmp = new CFuzzyRule();
00176                 return (NLAIC::IBasicType *) tmp;
00177         }
00178 
00179         const NLAIAGENT::IObjectIA::CProcessResult &CFuzzyRule::run()
00180         {
00181                 double cond_value;
00182                 if ( ( cond_value = truthValue() ) > 0 )
00183                 {
00184                         for ( sint32 i = 0; i < (sint32) _Vars.size(); i++ )
00185                         {
00186                                 _Sets[i]->addFact( cond_value );
00187                         }
00188                 }
00189                 return IObjectIA::ProcessRun;
00190         }
00191 
00192         bool CFuzzyRule::isEqual(const NLAIAGENT::IBasicObjectIA &) const
00193         {
00194                 // TODO
00195                 return false;
00196         }
00197 
00198         bool CFuzzyRule::isTrue() const
00199         {
00200                 return ( truthValue() > _Threshold );
00201         }
00202 
00203         const NLAIC::CIdentType &CFuzzyRule::getType() const
00204         {
00205                 return IdFuzzyRule;
00206         }
00207 
00208         NLAIAGENT::IObjetOp *CFuzzyRule::operator== (NLAIAGENT::IObjetOp &) const
00209         {
00210                 NLAILOGIC::CBoolFalse *x = new NLAILOGIC::CBoolFalse;
00211                 return x;
00212         }
00213 
00214         void CFuzzyRule::init(NLAIAGENT::IObjectIA *p)
00215         {               
00216                 NLAILOGIC::CondAnd *cond = (NLAILOGIC::CondAnd *) ((NLAIAGENT::IBaseGroupType *)p)->popFront();
00217                 cond->release();
00218         }
00219 }