00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include "nel/ai/logic/operator.h"
00021
00022 #include <list>
00023 #include <vector>
00024 #include "nel/ai/logic/ai_assert.h"
00025 #include "nel/ai/logic/varset.h"
00026 #include "nel/ai/logic/valueset.h"
00027 #include "nel/ai/logic/fact.h"
00028
00029 namespace NLAILOGIC
00030 {
00031 using namespace NLAIAGENT;
00032
00033 IBaseOperator::IBaseOperator()
00034 {
00035 _Comment = NULL;
00036 _Goal = NULL;
00037 }
00038
00039 IBaseOperator::IBaseOperator(const char *c)
00040 {
00041 _Comment = new char[ strlen(c) ];
00042 strcpy( _Comment , c);
00043 _Goal = NULL;
00044 }
00045
00046 IBaseOperator::IBaseOperator(const IBaseOperator &c)
00047 {
00048 if ( c._Comment != NULL )
00049 {
00050 _Comment = new char[strlen(c._Comment)];
00051 strcpy(_Comment,c._Comment);
00052 }
00053
00054 if ( c._Goal != NULL )
00055 {
00056 _Goal = c._Goal;
00057 _Goal->release();
00058 }
00059
00060 sint32 i;
00061 for ( i = 0; i < (sint32) c._Conds.size(); i++ )
00062 _Conds.push_back( c._Conds[i] );
00063
00064 for ( i = 0; i < (sint32) c._Concs.size(); i++ )
00065 _Concs.push_back( c._Concs[i] );
00066 }
00067
00068 IBaseOperator::~IBaseOperator()
00069 {
00070 if ( _Comment != NULL )
00071 delete[] _Comment;
00072
00073 if ( _Goal != NULL )
00074 _Goal->release();
00075 }
00076
00078 void IBaseOperator::setComment(char *c)
00079 {
00080 if ( _Comment )
00081 delete[] _Comment;
00082
00083 if ( c )
00084 {
00085 _Comment = new char[ strlen( c ) ];
00086 strcpy( _Comment, c );
00087 }
00088 else
00089 _Comment = NULL;
00090 }
00091
00092 const std::vector<IBaseAssert *> &IBaseOperator::getPrecondAsserts() const
00093 {
00094 return _Conds;
00095 }
00096
00097 const std::vector<IBaseAssert *> &IBaseOperator::getPostCondAsserts() const
00098 {
00099 return _Concs;
00100 }
00101
00102 void IBaseOperator::save(NLMISC::IStream &os)
00103 {
00104 sint32 i;
00105 sint32 size = (sint32) _Conds.size();
00106 os.serial( size );
00107 for ( i = 0; i < (sint32) _Conds.size(); i++ )
00108 {
00109 os.serial( (NLAIC::CIdentType &) _Conds[i]->getType() );
00110 _Conds[i]->save(os);
00111 }
00112
00113 size = (sint32) _Concs.size();
00114 os.serial( size );
00115 for ( i = 0; i < (sint32) _Concs.size(); i++ )
00116 {
00117 os.serial( (NLAIC::CIdentType &) _Concs[i]->getType() );
00118 _Concs[i]->save(os);
00119 }
00120 }
00121
00122 void IBaseOperator::load(NLMISC::IStream &is)
00123 {
00124 sint32 i;
00125 sint32 nb_vals;
00126
00127 is.serial( nb_vals );
00128 for ( i = 0; i < nb_vals; i++ )
00129 {
00130 NLAIC::CIdentTypeAlloc id;
00131 is.serial( id );
00132 IBaseAssert *tmp_val = (IBaseAssert *) id.allocClass();
00133 tmp_val->load( is );
00134 tmp_val->incRef();
00135 _Conds.push_back( tmp_val );
00136 }
00137
00138 is.serial( nb_vals );
00139 for ( i = 0; i < nb_vals; i++ )
00140 {
00141 NLAIC::CIdentTypeAlloc id;
00142 is.serial( id );
00143 IBaseAssert *tmp_val = (IBaseAssert *) id.allocClass();
00144 tmp_val->load( is );
00145 tmp_val->incRef();
00146 _Concs.push_back( tmp_val );
00147 }
00148 }
00149
00150 void IBaseOperator::setGoal(IBaseAssert *goal)
00151 {
00152 if ( _Goal )
00153 _Goal->release();
00154
00155 _Goal = goal;
00156
00157 if ( goal != NULL )
00158 _Goal->incRef();
00159 }
00160 }