00001 #ifndef DB_PLANNER_CACHING_FEATURES_EXTRACTOR_H
00002 #define DB_PLANNER_CACHING_FEATURES_EXTRACTOR_H
00003
00004 #include <string>
00005 #include "db_manager.h"
00006 #include "features_extractor.h"
00007 #include "model.h"
00008 using std::string;
00009
00010 namespace db_planner {
00011
00012
00013
00014
00015
00016 template <class Features>
00017 class CachingFeaturesExtractor : public FeaturesExtractor<Features>{
00018 private:
00019 const DatabaseFeaturesManager& db_manager_;
00020 const bool cache_results_;
00021 const string features_type_name_;
00022 const FeaturesExtractor<Features>& features_extractor_;
00023 public:
00024 CachingFeaturesExtractor(
00025 const DatabaseFeaturesManager& db_manager,
00026 const bool cache_results,
00027 const string& features_type_name,
00028 const FeaturesExtractor<Features>& features_extractor
00029 = FeaturesExtractor<Features>())
00030 : db_manager_(db_manager),
00031 cache_results_(cache_results),
00032 features_type_name_(features_type_name),
00033 features_extractor_(features_extractor) { }
00034
00035 virtual bool Extract(const Model& model, Features* features) const {
00036
00037 if (db_manager.GetFeatures(model.ModelName(),
00038 features_type_name_,
00039 features)) {
00040 return true;
00041 }
00042
00043 if (features_extractor_.Extract(model, features)) {
00044
00045 if (cache_results_) {
00046 db_manager.SaveFeatures(model.ModelName(),
00047 features_type_name_,
00048 *features);
00049 }
00050 return true;
00051 }
00052 return false;
00053 }
00054 };
00055
00056
00057 }
00058
00059 #endif // DB_PLANNER_CACHING_FEATURES_EXTRACTOR_H