00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
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
00037
00038
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
00068 for ( sint32 i = 0; i < (sint32)_Outputs.size(); i++ )
00069 {
00070
00071 std::list<IObjetOp *> *values = f->getValues();
00072
00073
00074
00075 delete values;
00076 }
00077
00078 _Facts.pushBack( 50, f->asCValueSet() );
00079 }
00080
00081 void CFirstOrderAssert::addFact(CValueSet *f)
00082 {
00083
00084 for ( sint32 i = 0; i < (sint32)_Outputs.size(); i++ )
00085 {
00086
00087
00088
00089
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
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
00217
00218
00219
00220
00221
00222
00223
00224
00225
00226
00227
00228
00229
00230
00231
00232
00233 }
00234
00235 std::list<CFact *> *CFirstOrderAssert::backward(CFact *fact)
00236 {
00237
00238 std::list<CFact *> *result = new std::list<CFact *>;
00239 CValueSet *liaison;
00240
00241
00242
00243
00244
00245
00246
00247
00248
00249
00250
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
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