# 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  

operator.cpp

Go to the documentation of this file.
00001 /* Copyright, 2000 Nevrax Ltd.
00002  *
00003  * This file is part of NEVRAX <MODULE_NAME>.
00004  * NEVRAX <MODULE_NAME> is free software; you can redistribute it and/or modify
00005  * it under the terms of the GNU General Public License as published by
00006  * the Free Software Foundation; either version 2, or (at your option)
00007  * any later version.
00008 
00009  * NEVRAX <MODULE_NAME> is distributed in the hope that it will be useful, but
00010  * WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
00012  * General Public License for more details.
00013 
00014  * You should have received a copy of the GNU General Public License
00015  * along with NEVRAX <MODULE_NAME>; see the file COPYING. If not, write to the
00016  * Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
00017  * MA 02111-1307, USA.
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 }