00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00028 #ifndef _INFENG_HOOK_H
00029 #define _INFENG_HOOK_H
00030
00031 #include <comp_hook.h>
00032 #include <inference_engine.h>
00033 #include <triple>
00034 #include <print_seq.h>
00035 #include <print_matrix.h>
00036 #include <counted_ptr.h>
00037
00038 namespace coco {
00039
00041
00048 class inference_engine_comp_hook : public wnc_hook_base
00049 {
00050 private:
00052 counted_ptr<inference_engine> ie;
00053
00055 control_data ie_ctrl;
00056
00058 std::map<std::triple<std::string,bool,int>,
00059 std::pair<std::string,basic_alltype> > info_to_col;
00060
00062 double thresh;
00063
00064 public:
00066 inference_engine_comp_hook(inference_engine *_ie, const control_data& _cd,
00067 double _thr = -COCO_INF)
00068 : wnc_hook_base("inference engine"),
00069 ie(_ie), ie_ctrl(_cd), info_to_col(), thresh(_thr)
00070 {}
00071
00073 inference_engine_comp_hook(inference_engine *_ie, const control_data& _cd,
00074 const std::map<std::triple<std::string,bool,int>,
00075 std::pair<std::string,basic_alltype> >& _itc,
00076 double _thr = -COCO_INF)
00077 : wnc_hook_base("inference engine"),
00078 ie(_ie), ie_ctrl(_cd), info_to_col(_itc), thresh(_thr)
00079 {}
00080
00082 inference_engine_comp_hook(const inference_engine_comp_hook& iec)
00083 : wnc_hook_base(iec), ie(iec.ie), ie_ctrl(iec.ie_ctrl),
00084 info_to_col(iec.info_to_col), thresh(iec.thresh)
00085 {}
00086
00088 virtual ~inference_engine_comp_hook() { }
00089
00091 inference_engine_comp_hook* new_copy() const
00092 { return new inference_engine_comp_hook(*this); }
00093
00097 void operator()(const work_node& wn, dbt_row& dbr,
00098 std::list<delta>* add_ds = NULL,
00099 std::list<certificate>* add_cs = NULL) const
00100 {
00101
00102 ptr<work_node> pwn(& const_cast<work_node&>(wn));
00103 if(!ie->update_engine(pwn))
00104 throw api_exception(apiee_search_graph, std::string("Inferene Engine `")+
00105 ie->get_name()+"': update_engine returned false.");
00106 ie_return_type ier(ie->infer(ie_ctrl));
00107
00108 std::map<std::triple<std::string,bool,int>,
00109 std::pair<std::string,basic_alltype> >::const_iterator cms;
00110 basic_alltype bat;
00111
00112
00113 for(cms = info_to_col.begin(); cms != info_to_col.end(); ++cms)
00114 {
00115 if((*cms).first.second)
00116 bat = ier.information((*cms).first.first);
00117 else
00118 bat = ier.information((*cms).first.first, (*cms).first.third);
00119 switch(bat.contents_type())
00120 {
00121 case ALLTYPE_INTERVAL:
00122 dbr.add((*cms).second.first, bat.ni());
00123 break;
00124 case ALLTYPE_DOUBLE:
00125 dbr.add((*cms).second.first, bat.nd());
00126 break;
00127 case ALLTYPE_UINT:
00128 dbr.add((*cms).second.first, bat.nu());
00129 break;
00130 case ALLTYPE_INT:
00131 dbr.add((*cms).second.first, bat.nn());
00132 break;
00133 case ALLTYPE_BOOL:
00134 dbr.add((*cms).second.first, bat.nb());
00135 break;
00136 case ALLTYPE_EMPTY:
00137 break;
00138 case ALLTYPE_ALLOCED_B:
00139 dbr.add((*cms).second.first, bat.b());
00140 break;
00141 case ALLTYPE_ALLOCED_N:
00142 dbr.add((*cms).second.first, bat.n());
00143 break;
00144 case ALLTYPE_ALLOCED_U:
00145 dbr.add((*cms).second.first, bat.u());
00146 break;
00147 case ALLTYPE_ALLOCED_D:
00148 dbr.add((*cms).second.first, bat.d());
00149 break;
00150 case ALLTYPE_ALLOCED_I:
00151 dbr.add((*cms).second.first, bat.i());
00152 break;
00153 case ALLTYPE_ALLOCED_C:
00154 dbr.add((*cms).second.first, bat.sv());
00155 break;
00156 case ALLTYPE_ALLOCED_S:
00157 dbr.add((*cms).second.first, bat.s());
00158 break;
00159 case ALLTYPE_ALLOCED_M:
00160 dbr.add((*cms).second.first, bat.m());
00161 break;
00162 case ALLTYPE_ALLOCED_NM:
00163 dbr.add((*cms).second.first, bat.nm());
00164 break;
00165 case ALLTYPE_ALLOCED_IM:
00166 dbr.add((*cms).second.first, bat.im());
00167 break;
00168 case ALLTYPE_ALLOCED_SM:
00169 dbr.add((*cms).second.first, bat.sm());
00170 break;
00171 #if 0
00172 case ALLTYPE_ALLOCED_X:
00173 dbr.add((*cms).second.first, bat.x());
00174 break;
00175 case ALLTYPE_ALLOCED_Y:
00176 dbr.add((*cms).second.first, bat.nx());
00177 break;
00178 case ALLTYPE_ALLOCED_XM:
00179 dbr.add((*cms).second.first, bat.xm());
00180 break;
00181 #endif
00182 }
00183 }
00184
00185 if(add_ds)
00186
00187 ier.getd(*add_ds, *add_cs, thresh);
00188 }
00189
00191 bool init_columns(vdbl::standard_table& stable)
00192 {
00193 bool ret(false);
00194 std::map<std::triple<std::string,bool,int>,
00195 std::pair<std::string,basic_alltype> >::const_iterator cms;
00196
00197 for(cms = info_to_col.begin(); cms != info_to_col.end(); ++cms)
00198 {
00199 switch((*cms).second.second.contents_type())
00200 {
00201 case ALLTYPE_INTERVAL:
00202 ret = _init_column(stable, (*cms).second.first,
00203 (*cms).second.second.ni()) || ret;
00204 break;
00205 case ALLTYPE_DOUBLE:
00206 ret = _init_column(stable, (*cms).second.first,
00207 (*cms).second.second.nd()) || ret;
00208 break;
00209 case ALLTYPE_UINT:
00210 ret = _init_column(stable, (*cms).second.first,
00211 (*cms).second.second.nu()) || ret;
00212 break;
00213 case ALLTYPE_INT:
00214 ret = _init_column(stable, (*cms).second.first,
00215 (*cms).second.second.nn()) || ret;
00216 break;
00217 case ALLTYPE_BOOL:
00218 ret = _init_column(stable, (*cms).second.first,
00219 (*cms).second.second.nb()) || ret;
00220 break;
00221 case ALLTYPE_EMPTY:
00222 throw api_exception(apiee_search_graph,
00223 std::string("inference_engine_hook: in column ")+
00224 (*cms).second.first+", info name: "+(*cms).first.first+
00225 "default value is empty.");
00226 break;
00227 case ALLTYPE_ALLOCED_B:
00228 ret = _init_column(stable, (*cms).second.first,
00229 (*cms).second.second.b()) || ret;
00230 break;
00231 case ALLTYPE_ALLOCED_N:
00232 ret = _init_column(stable, (*cms).second.first,
00233 (*cms).second.second.n()) || ret;
00234 break;
00235 case ALLTYPE_ALLOCED_U:
00236 ret = _init_column(stable, (*cms).second.first,
00237 (*cms).second.second.u()) || ret;
00238 break;
00239 case ALLTYPE_ALLOCED_D:
00240 ret = _init_column(stable, (*cms).second.first,
00241 (*cms).second.second.d()) || ret;
00242 break;
00243 case ALLTYPE_ALLOCED_I:
00244 ret = _init_column(stable, (*cms).second.first,
00245 (*cms).second.second.i()) || ret;
00246 break;
00247 case ALLTYPE_ALLOCED_C:
00248 ret = _init_column(stable, (*cms).second.first,
00249 (*cms).second.second.sv()) || ret;
00250 break;
00251 case ALLTYPE_ALLOCED_S:
00252 ret = _init_column(stable, (*cms).second.first,
00253 (*cms).second.second.s()) || ret;
00254 break;
00255 case ALLTYPE_ALLOCED_M:
00256 ret = _init_column(stable, (*cms).second.first,
00257 (*cms).second.second.m()) || ret;
00258 break;
00259 case ALLTYPE_ALLOCED_NM:
00260 ret = _init_column(stable, (*cms).second.first,
00261 (*cms).second.second.nm()) || ret;
00262 break;
00263 case ALLTYPE_ALLOCED_IM:
00264 ret = _init_column(stable, (*cms).second.first,
00265 (*cms).second.second.im()) || ret;
00266 break;
00267 case ALLTYPE_ALLOCED_SM:
00268 ret = _init_column(stable, (*cms).second.first,
00269 (*cms).second.second.sm()) || ret;
00270 break;
00271 #if 0
00272 case ALLTYPE_ALLOCED_X:
00273 ret = _init_column(stable, (*cms).second.first,
00274 (*cms).second.second.x()) || ret;
00275 break;
00276 case ALLTYPE_ALLOCED_Y:
00277 ret = _init_column(stable, (*cms).second.first,
00278 (*cms).second.second.nx()) || ret;
00279 break;
00280 case ALLTYPE_ALLOCED_XM:
00281 ret = _init_column(stable, (*cms).second.first,
00282 (*cms).second.second.xm()) || ret;
00283 break;
00284 #endif
00285 }
00286 }
00287 return ret;
00288 }
00289
00291 bool drop_columns(vdbl::standard_table& stable)
00292 { return _drop_columns(stable); }
00293 };
00294
00295 }
00296
00297 #endif // _INFENG_HOOK_H