00001 #ifndef Impala_Persistency_SvmRepositoryInFile_h
00002 #define Impala_Persistency_SvmRepositoryInFile_h
00003
00004 #include "Core/Training/Svm.h"
00005 #include "Persistency/RepositoryInFileSystem.h"
00006 #include "Persistency/ModelLocator.h"
00007
00008 namespace Impala
00009 {
00010 namespace Persistency
00011 {
00012
00013
00014 class SvmRepositoryInFile
00015 {
00016 public:
00017
00018 typedef Core::Training::Svm Svm;
00019
00020 SvmRepositoryInFile()
00021 {
00022 }
00023
00024 bool
00025 Exists(const ModelLocator& loc)
00026 {
00027 String dir = GetDir(loc, false);
00028 File file = RepFS().GetFile(loc, dir, loc.GetConcept() + ".model",
00029 false, true);
00030 return file.Valid();
00031 }
00032
00033 Svm*
00034 Get(const ModelLocator& loc)
00035 {
00036 String dir = GetDir(loc, false);
00037 File file = RepFS().GetFile(loc, dir, loc.GetConcept() + ".model",
00038 false, false);
00039 if (!file.Valid())
00040 return 0;
00041
00042 svm_model* model = 0;
00043 if (loc.GetProtocol() == "file")
00044 {
00045 String path = file.GetPath();
00046 model = svm_load_model(path.c_str());
00047 }
00048 else
00049 {
00050 FileLocator tmpLoc("file", "direct", FileNameTmp());
00051 File tmpFile = RepFS().GetFile(tmpLoc, true, false);
00052 tmpFile.CopyFrom(file);
00053 String path = tmpFile.GetPath();
00054 model = svm_load_model(path.c_str());
00055 tmpFile.Delete();
00056 }
00057 if (!model)
00058 {
00059 ILOG_ERROR("[Get] Unable to load " << loc);
00060 return 0;
00061 }
00062 Svm* res = new Svm();
00063 res->SetModel(model);
00064 return res;
00065 }
00066
00067 void
00068 Add(const ModelLocator& loc, Svm* svm)
00069 {
00070 String dir = GetDir(loc, false);
00071 File file = RepFS().GetFile(loc, dir, loc.GetConcept() + ".model",
00072 true, false);
00073 if (!file.Valid())
00074 return;
00075
00076 if (loc.GetProtocol() == "file")
00077 {
00078 String path = file.GetPath();
00079 if (svm_save_model(path.c_str(), svm->GetModel()) == -1)
00080 ILOG_ERROR("[Add] Unable to save " << loc);
00081 }
00082 else
00083 {
00084 FileLocator tmpLoc("file", "direct", FileNameTmp());
00085 File tmpFile = RepFS().GetFile(tmpLoc, true, false);
00086 String path = tmpFile.GetPath();
00087 if (svm_save_model(path.c_str(), svm->GetModel()) == -1)
00088 ILOG_ERROR("[Add] Unable to save " << loc);
00089 file.CopyFrom(tmpFile);
00090 tmpFile.Delete();
00091 }
00092 }
00093
00094 void
00095 Delete(const ModelLocator& loc)
00096 {
00097 if (loc.GetConceptSet() == "ALL")
00098 {
00099 String dir = GetDir(loc, true);
00100 RepFS().DeleteDir(loc, dir);
00101 }
00102 else
00103 {
00104 String dir = GetDir(loc, false);
00105 File file = RepFS().GetFile(loc, dir, loc.GetConcept() + ".model",
00106 true, true);
00107 file.Delete();
00108 }
00109 }
00110
00111 private:
00112
00113 RepositoryInFileSystem&
00114 RepFS()
00115 {
00116 return RepositoryInFileSystem::GetInstance();
00117 }
00118
00119 String
00120 GetDir(const ModelLocator& loc, bool forAll)
00121 {
00122 String dir = "ConceptModels";
00123
00124 if (forAll)
00125 return dir;
00126
00127 dir = FileNameConcat(dir, loc.GetConceptSet());
00128 dir = FileNameConcat(dir, loc.GetModel());
00129 dir = FileNameConcat(dir, loc.GetFeature());
00130 return dir;
00131 }
00132
00133 ILOG_VAR_DEC;
00134 };
00135
00136 ILOG_VAR_INIT(SvmRepositoryInFile, Impala.Persistency);
00137
00138 }
00139 }
00140
00141 #endif