00001 #ifndef Impala_Persistency_KernelMatrixRepositoryInFile_h
00002 #define Impala_Persistency_KernelMatrixRepositoryInFile_h
00003
00004 #include "Persistency/RepositoryInFileSystem.h"
00005 #include "Persistency/KernelMatrixLocator.h"
00006 #include "Core/Training/KernelMatrix.h"
00007 #include "Persistency/DistributedAccessRepository.h"
00008
00009 namespace Impala
00010 {
00011 namespace Persistency
00012 {
00013
00014
00015 class KernelMatrixRepositoryInFile
00016 {
00017 public:
00018
00019 typedef Core::Training::KernelMatrix KernelMatrix;
00020 typedef Core::Feature::FeatureTable FeatureTable;
00021 typedef Core::Matrix::DistributedAccess DistributedAccess;
00022
00023 KernelMatrixRepositoryInFile()
00024 {
00025 }
00026
00027 bool
00028 Exists(const KernelMatrixLocator& loc)
00029 {
00030 if (loc.GetIsIndex())
00031 return DistributedAccessRepository().Exists(loc);
00032
00033 String dir = GetDir(loc, false);
00034 String fName = loc.GetFeature() + ".tab";
00035 File file = RepFS().GetFile(loc, dir, fName, false, true);
00036 return file.Valid();
00037 }
00038
00039 KernelMatrix*
00040 Get(const KernelMatrixLocator& loc)
00041 {
00042 if (loc.GetIsIndex())
00043 {
00044 DistributedAccess* da = DistributedAccessRepository().Get(loc);
00045 KernelMatrix* res = new KernelMatrix(da);
00046 return res;
00047 }
00048
00049 String dir = GetDir(loc, false);
00050 String fName = loc.GetFeature() + ".tab";
00051 File file = RepFS().GetFile(loc, dir, fName, false, false);
00052 if (!file.Valid())
00053 return 0;
00054
00055 Core::Feature::FeatureDefinition def("cache");
00056 FeatureTable* tab = new FeatureTable(def);
00057 Read(tab, file);
00058 KernelMatrix* res = new KernelMatrix(tab);
00059 return res;
00060 }
00061
00062 void
00063 Add(const KernelMatrixLocator& loc, KernelMatrix* mat)
00064 {
00065 if (loc.GetIsIndex())
00066 return DistributedAccessRepository().Add(loc, mat->GetDA());
00067
00068 String dir = GetDir(loc, false);
00069 String fName = loc.GetFeature() + ".tab";
00070 File file = RepFS().GetFile(loc, dir, fName, true, false);
00071 Write(mat->GetFeatureTab(), file);
00072 }
00073
00074 void
00075 Delete(const KernelMatrixLocator& loc)
00076 {
00077 if ((loc.GetFeature() == "ALL") && (loc.GetContainer() == "ALL"))
00078 {
00079 String dir = GetDir(loc, true);
00080 RepFS().DeleteDir(loc, dir);
00081 }
00082 else
00083 {
00084 String dir = GetDir(loc, false);
00085 ILOG_ERROR("[Delete] not fully implemented yet");
00086
00087
00088
00089 }
00090 }
00091
00092 File
00093 ExposeFile(const KernelMatrixLocator& loc, bool toWrite)
00094 {
00095 if (loc.GetIsIndex())
00096 return DistributedAccessRepository().ExposeFile(loc, toWrite);
00097
00098 String dir = GetDir(loc, false);
00099 String fName = loc.GetFeature() + ".tab";
00100 bool silent = !toWrite;
00101 return RepFS().GetFile(loc, dir, fName, toWrite, silent);
00102 }
00103
00104 private:
00105
00106 RepositoryInFileSystem&
00107 RepFS()
00108 {
00109 return RepositoryInFileSystem::GetInstance();
00110 }
00111
00112 String
00113 GetDir(const KernelMatrixLocator& loc, bool forAll)
00114 {
00115 String dir = loc.GetTopString();
00116 if (!loc.GetIsIndex())
00117 dir = FileNameConcat(dir, loc.GetWalkType());
00118
00119 if (forAll)
00120 return dir;
00121
00122 dir = FileNameConcat(dir, loc.GetAnnoSet());
00123 dir = FileNameConcat(dir, loc.GetModel());
00124 dir = FileNameConcat(dir, loc.GetFeature());
00125 if (!loc.GetIsIndex())
00126 dir = FileNameConcat(dir, loc.GetContainer());
00127 return dir;
00128 }
00129
00130 ILOG_VAR_DEC;
00131 };
00132
00133 ILOG_VAR_INIT(KernelMatrixRepositoryInFile, Impala.Persistency);
00134
00135 }
00136 }
00137
00138 #endif