00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include "nel/ai/logic/factbase.h"
00021 #include "nel/ai/logic/varset.h"
00022 #include "nel/ai/logic/ai_assert.h"
00023 #include "nel/ai/logic/bool_assert.h"
00024 #include "nel/ai/logic/fo_assert.h"
00025 #include "nel/ai/logic/fact.h"
00026 #include "nel/ai/logic/valueset.h"
00027
00028 namespace NLAILOGIC
00029 {
00030 using namespace NLAIAGENT;
00031
00032 CFactBase::CFactBase()
00033 {
00034 }
00035
00036 CFactBase::~CFactBase() {
00037 while ( _Asserts.size() )
00038 {
00039 _Asserts.front()->release();
00040 _Asserts.pop_front();
00041 }
00042 }
00043
00044 IBaseAssert *CFactBase::addAssert(IVarName &n, sint32 nb_vars)
00045 {
00046 IBaseAssert *my_assert = findAssert( n , nb_vars );
00047 if ( ! my_assert )
00048 {
00049 if ( nb_vars > 0)
00050 _Asserts.push_back( my_assert = new CFirstOrderAssert(n, nb_vars) );
00051 else
00052 _Asserts.push_back( my_assert = new CBoolAssert(n) );
00053 }
00054 return my_assert;
00055 }
00056
00057 IBaseAssert *CFactBase::findAssert(IBaseAssert *a)
00058 {
00059 std::list<IBaseAssert *>::iterator it_a = _Asserts.begin();
00060 while ( it_a != _Asserts.end() )
00061 {
00062 if ( a->getName() == (*it_a)->getName()
00063 && a->nbVars() == (*it_a)->nbVars() )
00064 {
00065 return *it_a;
00066 }
00067 it_a++;
00068 }
00069 return NULL;
00070 }
00071
00072 IBaseAssert *CFactBase::findAssert( IVarName &a_name, sint32 nb_vars)
00073 {
00074 std::list<IBaseAssert *>::iterator it_a = _Asserts.begin();
00075 while ( it_a != _Asserts.end() )
00076 {
00077 if ( a_name == ( *it_a )->getName()
00078 && nb_vars == (*it_a)->nbVars() )
00079 {
00080 return *it_a;
00081 }
00082 it_a++;
00083 }
00084 return NULL;
00085 }
00086
00087 void CFactBase::addFact(CFact *f)
00088 {
00089 IBaseAssert *assert = findAssert( f->getAssertName(), f->size() );
00090
00091 if ( !assert )
00092 {
00093 if ( f->size() == 0 )
00094 _Asserts.push_back( assert = new CBoolAssert( f->getAssertName() ) );
00095 else
00096 {
00097 CStringVarName x((CStringVarName &)f->getAssertName());
00098 _Asserts.push_back( assert = new CFirstOrderAssert(x) );
00099 assert->addFact( f );
00100 }
00101 }
00102 }
00103
00104 void CFactBase::addFact(IVarName &a_name , CValueSet *fp)
00105 {
00106
00107 IBaseAssert *assert = findAssert( a_name, fp->size() );
00108
00109
00110 if ( !assert )
00111 {
00112 if ( !fp )
00113 _Asserts.push_back( assert = new CBoolAssert( a_name ) );
00114 else
00115 {
00116 CStringVarName x((CStringVarName &)a_name);
00117 _Asserts.push_back( assert = new CFirstOrderAssert(x) );
00118 assert->addFact( fp );
00119 }
00120 }
00121 }
00122
00123 void CFactBase::removeFact(CFact *fp)
00124 {
00125
00126 IBaseAssert *assert = findAssert( fp->getAssert());
00127
00128 if ( assert )
00129 assert->removeFact( fp );
00130 }
00131
00132 const NLAIC::IBasicType *CFactBase::clone() const
00133 {
00134 CFactBase *result = new CFactBase;
00135 std::list<IBaseAssert *>::const_iterator it_a = _Asserts.begin();
00136 while ( it_a != _Asserts.end() )
00137 {
00138 result->_Asserts.push_back( (IBaseAssert *) (*it_a)->clone() );
00139 it_a++;
00140 }
00141 return result;
00142 }
00143 const NLAIC::IBasicType *CFactBase::newInstance() const
00144 {
00145 CFactBase *instance = new CFactBase();
00146 return instance;
00147 }
00148
00149 const NLAIC::CIdentType &CFactBase::getType() const
00150 {
00151 return IdFactBase;
00152 }
00153
00154 void CFactBase::save(NLMISC::IStream &os)
00155 {
00156 IObjectIA::save( os );
00157 sint32 size = _Asserts.size();
00158 os.serial( size );
00159 std::list<IBaseAssert *>::const_iterator it_a = _Asserts.begin();
00160 while ( it_a != _Asserts.end() )
00161 {
00162 os.serial( (NLAIC::CIdentType &) (*it_a)->getType() );
00163 (*it_a)->save( os );
00164 it_a++;
00165 }
00166 }
00167
00168 void CFactBase::load(NLMISC::IStream &is)
00169 {
00170 IObjectIA::load( is );
00171
00172 sint32 nb_asserts;
00173 is.serial( nb_asserts );
00174 for (sint32 i = 0; i < nb_asserts; i++ )
00175 {
00176 NLAIC::CIdentTypeAlloc id;
00177 is.serial( id );
00178 IBaseAssert *tmp_val = (IBaseAssert *) id.allocClass();
00179 tmp_val->load( is );
00180 _Asserts.push_back( tmp_val );
00181 }
00182 }
00183
00184 void CFactBase::getDebugString(std::string &txt) const
00185 {
00186 txt += "CFactBase";
00187 }
00188
00189 bool CFactBase::isEqual(const IBasicObjectIA &a) const
00190 {
00191 return false;
00192 }
00193
00194 const IObjectIA::CProcessResult &CFactBase::run()
00195 {
00196 return IObjectIA::ProcessRun;
00197 }
00198
00199 bool CFactBase::isTrue() const
00200 {
00201 return false;
00202 }
00203
00204
00205 std::list<CFact *> *CFactBase::getAssertFacts(IBaseAssert *a)
00206 {
00207 IBaseAssert *assert = findAssert( a );
00208 if ( assert )
00209 {
00210 return assert->getFacts();
00211 }
00212 return NULL;
00213 }
00214
00215 void CFactBase::addAssert(IBaseAssert *a)
00216 {
00217 a->incRef();
00218 _Asserts.push_back(a);
00219 }
00220
00221 void CFactBase::addGoal(IVarName &a_name , CValueSet *fp)
00222 {
00223 addFact(a_name, fp);
00224 }
00225 }