00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #include <clipsmm/fact.h>
00020
00021 extern "C" {
00022 #include <clips/clips.h>
00023 };
00024
00025 #include <clipsmm/utility.h>
00026 #include <clipsmm/environment.h>
00027
00028 namespace DACLIPS {
00029
00030 Fact::Fact( Environment& environment, void* cobj ) :
00031 EnvironmentObject(environment, cobj)
00032 {
00033 if (m_cobj)
00034 EnvIncrementFactCount( m_environment.cobj(), m_cobj );
00035 }
00036
00037 Fact::pointer Fact::create( Environment& environment, void* cobj ) {
00038 return Fact::pointer( new Fact( environment, cobj ) );
00039 }
00040
00041 Fact::~Fact() {
00042 EnvDecrementFactCount( m_environment.cobj(), m_cobj );
00043 }
00044
00045 bool Fact::assign_slot_defaults() {
00046 if ( m_cobj )
00047 return EnvAssignFactSlotDefaults( m_environment.cobj(), m_cobj );
00048 return false;
00049 }
00050
00051 Template::pointer Fact::get_template() {
00052 if ( !m_cobj )
00053 return Template::pointer();
00054
00055 void* tem = EnvFactDeftemplate( m_environment.cobj(), m_cobj );
00056
00057 if ( tem )
00058 return Template::create( m_environment, tem );
00059 else
00060 return Template::pointer();
00061 }
00062
00063 bool Fact::exists() {
00064 if ( m_cobj )
00065 return EnvFactExistp( m_environment.cobj(), m_cobj );
00066 return false;
00067 }
00068
00069 long int Fact::index() {
00070 if ( m_cobj )
00071 return EnvFactIndex( m_environment.cobj(), m_cobj );
00072 return -1;
00073 }
00074
00075 std::vector<std::string> Fact::slot_names() {
00076 DATA_OBJECT clipsdo;
00077
00078 if ( !m_cobj )
00079 return std::vector<std::string>();
00080
00081 EnvFactSlotNames( m_environment.cobj(), m_cobj, &clipsdo );
00082
00083 return data_object_to_strings( clipsdo );
00084 }
00085
00086 Values Fact::slot_value( const std::string & name )
00087 {
00088 DATA_OBJECT data_object;
00089 int result;
00090
00091 if ( !m_cobj )
00092 return Values();
00093
00094 if ( name == "" ) {
00095 result = EnvGetFactSlot( m_environment.cobj(), m_cobj, NULL, &data_object );
00096
00097 }
00098 else
00099 result = EnvGetFactSlot( m_environment.cobj(), m_cobj, const_cast<char*>(name.c_str()), &data_object );
00100 if (result)
00101 return data_object_to_values( data_object );
00102 else
00103 return Values();
00104 }
00105
00106 Fact::pointer Fact::next( )
00107 {
00108 void* next_fact;
00109
00110 if ( !m_cobj )
00111 return Fact::pointer();
00112
00113
00114 if ( ! this->exists() )
00115 return Fact::pointer();
00116
00117 next_fact = EnvGetNextFact( m_environment.cobj(), m_cobj );
00118 if ( next_fact )
00119 return Fact::create( m_environment, next_fact );
00120 else
00121 return Fact::pointer();
00122 }
00123
00124 bool Fact::retract( )
00125 {
00126 if ( !m_cobj )
00127 return false;
00128 return EnvRetract( m_environment.cobj(), m_cobj );
00129 }
00130
00131 bool Fact::set_slot( const std::string & slot_name, const Value & value )
00132 {
00133 DATA_OBJECT* clipsdo = value_to_data_object( m_environment, value );
00134 if ( !clipsdo || !m_cobj )
00135 return false;
00136 return EnvPutFactSlot( m_environment.cobj(),
00137 m_cobj,
00138 const_cast<char*>(slot_name.c_str()),
00139 clipsdo
00140 );
00141 delete clipsdo;
00142 }
00143
00144 bool Fact::set_slot( const std::string & slot_name, const Values & values )
00145 {
00146 DATA_OBJECT* clipsdo = value_to_data_object( m_environment, values );
00147 if ( !clipsdo || !m_cobj )
00148 return false;
00149 return EnvPutFactSlot( m_environment.cobj(),
00150 m_cobj,
00151 const_cast<char*>(slot_name.c_str()),
00152 clipsdo
00153 );
00154 delete clipsdo;
00155 }
00156
00157 }