[med-svn] [aghermann] 22/85: new class CSstorablePPack (part 2/3)

andrei zavada hmmr-guest at alioth.debian.org
Thu Sep 26 23:46:25 UTC 2013


This is an automated email from the git hooks/post-receive script.

hmmr-guest pushed a commit to branch master
in repository aghermann.

commit 2360ab787f91a89516a0068aed2966379d4a9de1
Author: Andrei Zavada <johnhommer at gmail.com>
Date:   Fri Sep 13 02:06:40 2013 +0300

    new class CSstorablePPack (part 2/3)
---
 upstream/src/aghermann/expdesign/dirlevel.cc     |   98 +++++++++++++++++++++-
 upstream/src/aghermann/expdesign/dirlevel.hh     |   35 +++++++-
 upstream/src/aghermann/expdesign/expdesign.cc    |   44 +++++++++-
 upstream/src/aghermann/expdesign/expdesign.hh    |   29 ++++---
 upstream/src/aghermann/expdesign/loadsave.cc     |    2 -
 upstream/src/aghermann/expdesign/tree-scanner.cc |   22 +----
 upstream/src/aghermann/patterns/patterns.hh      |   28 +------
 upstream/src/aghermann/rk1968/rk1968-profiles.cc |   66 +++------------
 upstream/src/aghermann/rk1968/rk1968.cc          |    9 +-
 upstream/src/aghermann/rk1968/rk1968.hh          |    9 +-
 upstream/src/common/subject_id.hh                |   10 +--
 upstream/src/libsigfile/typed-source.cc          |   22 +++++
 upstream/src/libsigfile/typed-source.hh          |    3 +
 13 files changed, 239 insertions(+), 138 deletions(-)

diff --git a/upstream/src/aghermann/expdesign/dirlevel.cc b/upstream/src/aghermann/expdesign/dirlevel.cc
index 0e49339..ffee6fd 100644
--- a/upstream/src/aghermann/expdesign/dirlevel.cc
+++ b/upstream/src/aghermann/expdesign/dirlevel.cc
@@ -10,9 +10,12 @@
  */
 
 
-#include <stdarg.h>
+#include <errno.h>
 #include <string>
 
+#include "common/string.hh"
+#include "common/fs.hh"
+#include "expdesign.hh"
 #include "dirlevel.hh"
 
 using namespace std;
@@ -24,7 +27,9 @@ exp_dir_level_s( TExpDirLevel x)
 {
         switch (x) {
         case TExpDirLevel::transient:   return "~";
-        case TExpDirLevel::subject:     return "[S]";
+        case TExpDirLevel::session:     return "[D]";
+        case TExpDirLevel::subject:     return "[J]";
+        case TExpDirLevel::group:       return "[G]";
         case TExpDirLevel::experiment:  return "[E]";
         case TExpDirLevel::user:        return "[U]";
         case TExpDirLevel::system:      return "<S>";
@@ -33,6 +38,95 @@ exp_dir_level_s( TExpDirLevel x)
 }
 
 
+
+
+string
+CStorablePPack::
+path() const
+{
+        string append = string("/") + subdir + "/" + name;
+        switch (level) {
+        case TExpDirLevel::transient:
+                return move(string("/tmp") + append);
+
+        case TExpDirLevel::session:
+        case TExpDirLevel::subject:
+        case TExpDirLevel::group:
+        case TExpDirLevel::experiment:
+                return move(ED.make_dirname( level, level_id) + append);
+
+        case TExpDirLevel::user:
+                return move(str::sasprintf( "%s/.local/share/aghermann%s", getenv("HOME"), append.c_str()));
+
+        case TExpDirLevel::system:
+                return move(str::sasprintf( "%s/%s", PACKAGE_DATADIR, append.c_str()));
+        }
+
+        throw invalid_argument ("bad TExpDirLevel");
+}
+
+int
+CStorablePPack::
+load()
+{
+        libconfig::Config conf;
+
+        try {
+                conf.readFile( path().c_str());
+
+                using namespace confval;
+                get( config_keys_d, conf);
+                get( config_keys_z, conf);
+                get( config_keys_g, conf);
+                get( config_keys_b, conf);
+                get( config_keys_s, conf);
+
+                saved = true;
+
+                return 0;
+
+        } catch (...) {
+                return -1;
+        }
+}
+
+
+int
+CStorablePPack::
+save()
+{
+        if ( saved || level == TExpDirLevel::transient || level == TExpDirLevel::system )
+                return 0;
+
+        string p = path();
+        if ( fs::mkdir_with_parents( fs::dirname(p)) ) {
+                fprintf( stderr, "CStorablePPack::save(\"%s\"): mkdir failed: %s\n", p.c_str(), strerror(errno));
+                return -1;
+        }
+
+        try {
+                libconfig::Config conf;
+
+                using namespace confval;
+                put( config_keys_d, conf);
+                put( config_keys_g, conf);
+                put( config_keys_b, conf);
+                put( config_keys_s, conf);
+                put( config_keys_z, conf);
+
+                conf.writeFile( p.c_str());
+
+                saved = true;
+
+                return 0;
+
+        } catch (...) {
+                return -1;
+        }
+}
+
+
+
 // Local Variables:
 // Mode: c++
 // indent-tabs-mode: nil
diff --git a/upstream/src/aghermann/expdesign/dirlevel.hh b/upstream/src/aghermann/expdesign/dirlevel.hh
index 33ceb19..7458255 100644
--- a/upstream/src/aghermann/expdesign/dirlevel.hh
+++ b/upstream/src/aghermann/expdesign/dirlevel.hh
@@ -19,6 +19,7 @@
 #include <forward_list>
 
 #include "common/config-validate.hh"
+#include "forward-decls.hh"
 
 #if HAVE_CONFIG_H && !defined(VERSION)
 #  include "config.h"
@@ -31,25 +32,51 @@ using namespace std;
 
 enum class TExpDirLevel {
         transient,
+        session,
         subject,
+        group,
         experiment,
         user,
         system,
 };
 
+struct SExpDirLevelId {
+        string  g, j, d;
+};
+
 const char* exp_dir_level_s( agh::TExpDirLevel);
 
 
 class CStorablePPack {
     public:
-        TExpDirLevel level;
+        CStorablePPack (const string& subdir_, const string& name_, TExpDirLevel level_,
+                        CExpDesign& ED_, const SExpDirLevelId& level_id_)
+              : subdir (subdir_),
+                name (name_),
+                level (level_),
+                level_id (level_id_),
+                ED (ED_),
+                saved (false)
+                {}
+
         string  subdir,
-                path;
+                name;
+        TExpDirLevel
+                level;
+
+        SExpDirLevelId
+                level_id;
+        CExpDesign&
+                ED;
 
         bool    saved;
 
-        virtual int load() = 0;
-        virtual int save() = 0;
+        string path() const;
+
+        virtual int load();
+        virtual int save();
+        void touch()
+                { saved = false; }
 
         forward_list<confval::SValidator<double>>        config_keys_g;
         forward_list<confval::SValidator<int>>           config_keys_d;
diff --git a/upstream/src/aghermann/expdesign/expdesign.cc b/upstream/src/aghermann/expdesign/expdesign.cc
index fd57bc1..0ef1d7f 100644
--- a/upstream/src/aghermann/expdesign/expdesign.cc
+++ b/upstream/src/aghermann/expdesign/expdesign.cc
@@ -23,8 +23,7 @@
 
 
 using namespace std;
-
-using agh::confval::SValidator;
+using namespace agh;
 
 
 const char
@@ -42,6 +41,7 @@ double
 };
 
 
+using agh::confval::SValidator;
 
 agh::CExpDesign::
 CExpDesign (const string& session_dir_,
@@ -199,6 +199,46 @@ for_all_subjects( const TSubjectOpFun& F, const TSubjectReportFun& report, const
 }
 
 
+
+string
+CExpDesign::
+make_dirname( TExpDirLevel level, const SExpDirLevelId& level_id) const
+{
+        switch (level) {
+        case TExpDirLevel::session:
+        {
+                TJGroups::const_iterator Gi;
+                const CSubject& J = subject_by_x(level_id.j, &Gi);
+                if ( level_id.g != Gi->first || !J.have_session(level_id.d) )
+                        throw invalid_argument ("bad group/subject/session");
+                return str::sasprintf( "%s/%s/%s/%s", _session_dir.c_str(), Gi->first.c_str(), J.id.c_str(), level_id.d.c_str());
+        }
+
+        case TExpDirLevel::subject:
+        {
+                TJGroups::const_iterator Gi;
+                const CSubject& J = subject_by_x(level_id.j, &Gi);
+                if ( level_id.g != Gi->first )
+                        throw invalid_argument ("bad group/subject");
+                return str::sasprintf( "%s/%s/%s", _session_dir.c_str(), Gi->first.c_str(), J.id.c_str());
+        }
+
+        case TExpDirLevel::group:
+                if ( not have_group( level_id.g) )
+                        throw invalid_argument ("bad group");
+                return str::sasprintf( "%s/%s", _session_dir.c_str(), level_id.g.c_str());
+
+        case TExpDirLevel::experiment:
+                return make_dirname();
+
+        default:
+                throw invalid_argument ("level not appropriate");
+        }
+}
+
+
+
+
 void
 agh::CExpDesign::
 for_all_episodes( const TEpisodeOpFun& F, const TEpisodeReportFun& report, const TEpisodeFilterFun& filter)
diff --git a/upstream/src/aghermann/expdesign/expdesign.hh b/upstream/src/aghermann/expdesign/expdesign.hh
index b32054c..0e2d50d 100644
--- a/upstream/src/aghermann/expdesign/expdesign.hh
+++ b/upstream/src/aghermann/expdesign/expdesign.hh
@@ -28,6 +28,7 @@
 
 #include "forward-decls.hh"
 #include "subject.hh"
+#include "dirlevel.hh"
 
 #if HAVE_CONFIG_H && !defined(VERSION)
 #  include "config.h"
@@ -77,7 +78,7 @@ class CExpDesign {
 
         string
         name() const // dirname
-                { return _session_dir.substr( _session_dir.rfind( '/')); }
+                { return move(_session_dir.substr( _session_dir.rfind( '/'))); }
 
       // error log
         enum class TLogEntryStyle { plain, bold, italic };
@@ -111,17 +112,12 @@ class CExpDesign {
 
       // add subject to group; if he exists in another group, remove him therefrom first;
       // if he is already there, update his record
-        int add_subject( const char *name, CSubject::TGender gender, int age,
+        int add_subject( const char *name, CSubject::TGender, int age,
                          const char *group,
                          const char *comment = "");
 
         template <class T>
-        string subject_dir( const T& j) const
-                {
-                        map<string, CJGroup>::const_iterator G;
-                        const CSubject& J = subject_by_x(j, &G);
-                        return _session_dir + '/' + G->first + '/' + J.SSubjectId::id;
-                }
+        string subject_dir( const T& j) const;
 
       // scan tree: build all structures
         static TMsmtCollectProgressIndicatorFun progress_fun_stdout;
@@ -130,10 +126,9 @@ class CExpDesign {
         void compute_profiles();
 
     public:
-      // edf sources
+      // sources
         int register_intree_source( sigfile::CTypedSource&&,
                                     const char **reason_if_failed_p = nullptr);
-        static bool is_supported_source( sigfile::CTypedSource&);
 
       // model runs
         int setup_modrun( const string& j, const string& d, const string& h,
@@ -208,6 +203,11 @@ class CExpDesign {
         void
         for_all_modruns( const TModelRunOpFun&, const TModelRunReportFun&, const TModelRunFilterFun&);
 
+      // dir-level access
+        string make_dirname() const
+                { return _session_dir; }
+        string make_dirname( TExpDirLevel, const SExpDirLevelId&) const;
+
       // inventory
         size_t  num_threads;
         metrics::psd::SPPack
@@ -256,6 +256,7 @@ class CExpDesign {
                 _error_log;
 
         sid_t   _id_pool;
+
       // load/save
         forward_list<confval::SValidator<double>>        config_keys_g;
         forward_list<confval::SValidator<int>>           config_keys_d;
@@ -273,6 +274,14 @@ bool CExpDesign::have_group( const T& g) const
 }
 
 template <class T>
+string CExpDesign::subject_dir( const T& j) const
+{
+        map<string, CJGroup>::const_iterator G;
+        const CSubject& J = subject_by_x(j, &G);
+        return _session_dir + '/' + G->first + '/' + J.SSubjectId::id;
+}
+
+template <class T>
 CSubject& CExpDesign::subject_by_x( const T& jid)
 {
         for ( auto &G : groups ) {
diff --git a/upstream/src/aghermann/expdesign/loadsave.cc b/upstream/src/aghermann/expdesign/loadsave.cc
index 3c942ab..df184d5 100644
--- a/upstream/src/aghermann/expdesign/loadsave.cc
+++ b/upstream/src/aghermann/expdesign/loadsave.cc
@@ -26,8 +26,6 @@ load_settings()
 {
         libconfig::Config conf;
 
-        // Load the XML file into the property tree. If reading fails
-        // (cannot open file, parse error), an exception is thrown.
         try {
                 conf.readFile( EXPD_FILENAME);
 
diff --git a/upstream/src/aghermann/expdesign/tree-scanner.cc b/upstream/src/aghermann/expdesign/tree-scanner.cc
index c4b2a38..d0f3511 100644
--- a/upstream/src/aghermann/expdesign/tree-scanner.cc
+++ b/upstream/src/aghermann/expdesign/tree-scanner.cc
@@ -251,26 +251,6 @@ register_intree_source( sigfile::CTypedSource&& F,
 
 
 
-bool
-CExpDesign::
-is_supported_source( sigfile::CTypedSource& F)
-{
-        using namespace sigfile;
-        union {
-                CEDFFile::TSubtype e;
-                CTSVFile::TSubtype t;
-        } u;
-        return (F.type() == CTypedSource::TType::edf &&
-                (u.e = F.obj<CEDFFile>().subtype(),
-                 (u.e == CEDFFile::TSubtype::edf ||
-                  u.e == CEDFFile::TSubtype::edfplus_c)))
-                ||
-                (F.type() == CTypedSource::TType::ascii &&
-                 (u.t = F.obj<CTSVFile>().subtype(),
-                  (u.t == CTSVFile::TSubtype::csv ||
-                   u.t == CTSVFile::TSubtype::tsv)));
-}
-
 namespace {
 
 size_t  current_sigfile_source;
@@ -300,7 +280,7 @@ supported_sigfile_processor( const char *fname, const struct stat*, int flag, st
                                         only_expdesign->log_message( "%s", st.c_str());
                                 }
 
-                                if ( CExpDesign::is_supported_source(F) )
+                                if ( sigfile::is_supported_source(F) )
                                         only_expdesign -> register_intree_source( move(F));
                                 else
                                         only_expdesign -> log_message( "File %s: unsupported format", fname);
diff --git a/upstream/src/aghermann/patterns/patterns.hh b/upstream/src/aghermann/patterns/patterns.hh
index 6615e48..9085c0d 100644
--- a/upstream/src/aghermann/patterns/patterns.hh
+++ b/upstream/src/aghermann/patterns/patterns.hh
@@ -298,33 +298,7 @@ struct SPattern {
 
 template <typename T>
 list<SPattern<T>>
-load_patterns_from_location( const string&, TExpDirLevel);
-
-
-inline string
-make_system_location()
-{
-        return PACKAGE_DATADIR "/patterns";
-}
-
-inline string
-make_user_location()
-{
-        return move(agh::str::sasprintf( "%s/.local/share/aghermann/patterns", getenv("HOME")));
-}
-
-inline string
-make_experiment_location( const agh::CExpDesign& ED)
-{
-        return move(agh::str::sasprintf( "%s/.patterns", ED.session_dir()));
-}
-
-inline string
-make_subject_location( const agh::CExpDesign& ED, const agh::CSubject& J)
-{
-        return move(agh::str::sasprintf( "%s/.patterns", ED.subject_dir( J).c_str()));
-}
-
+load_patterns_from_location( agh::TExpDirLevel level, agh::CExpDesign& ED, const agh::SExpDirLevelId& level_id);
 
 
 #include "patterns.ii"
diff --git a/upstream/src/aghermann/rk1968/rk1968-profiles.cc b/upstream/src/aghermann/rk1968/rk1968-profiles.cc
index ead52b9..58d3b5d 100644
--- a/upstream/src/aghermann/rk1968/rk1968-profiles.cc
+++ b/upstream/src/aghermann/rk1968/rk1968-profiles.cc
@@ -14,6 +14,7 @@
 #include <sys/stat.h>
 
 #include "common/fs.hh"
+#include "common/config-validate.hh"
 #include "aghermann/expdesign/subject.hh"
 #include "aghermann/expdesign/expdesign.hh"
 
@@ -24,63 +25,13 @@ using namespace std;
 using namespace agh::rk1968;
 
 
-string
-make_system_profiles_location()
-{
-        return  PACKAGE_DATADIR "/rk1968_profiles";
-}
-
-string
-make_user_profiles_location()
-{
-        return agh::str::sasprintf( "%s/.local/share/aghermann/rk1968_profiles", getenv("HOME"));
-}
-
-string
-make_experiment_profiles_location( const agh::CExpDesign& ED)
-{
-        return agh::str::sasprintf( "%s/.rk1968_profiles", ED.session_dir());
-}
-
-string
-make_subject_profiles_location( const agh::CExpDesign& ED, const agh::CSubject& J)
-{
-        return agh::str::sasprintf( "%s/.rk1968_profiles", ED.subject_dir( J).c_str());
-}
-
 
 
 int
 CScoreAssistant::
-load()
-{
-	
-	return 0;
-}
-
-
-int
-CScoreAssistant::
-save()
-{
-        if ( saved || origin == agh::TExpDirLevel::transient || origin == agh::TExpDirLevel::system )
-                return 0;
-
-        if ( agh::fs::mkdir_with_parents( agh::fs::dirname(path)) ) {
-                fprintf( stderr, "save_profile(\"%s\"): mkdir failed\n", path.c_str());
-                return -1;
-        }
-
-	
-	saved = true;
-	return 0;
-}
-
-int
-CScoreAssistant::
 delete_file()
 {
-        return unlink( path.c_str());
+        return unlink( path().c_str());
 }
 
 
@@ -94,19 +45,24 @@ scandir_filter( const struct dirent *e)
 }
 
 list<CScoreAssistant>
-load_profiles_from_location( const string& loc, agh::TExpDirLevel origin)
+load_profiles_from_location( agh::TExpDirLevel level, agh::CExpDesign& ED, const agh::SExpDirLevelId& level_id)
 {
         list<CScoreAssistant>
                 ret;
 
+        string location = ED.make_dirname( level, level_id);
+
         struct dirent **eps;
-        int     total = scandir( loc.c_str(), &eps, scandir_filter, alphasort);
+        int     total = scandir( location.c_str(), &eps, scandir_filter, alphasort);
 
         if ( total != -1 ) {
                 for ( int i = 0; i < total; ++i ) {
                         try {
-                                ret.emplace_back( loc + '/' + eps[i]->d_name);
-                                ret.back().origin = origin;
+                                ret.emplace_back(
+                                        SScoreAssistantPPack(),
+                                        eps[i]->d_name,
+                                        level, ED, level_id);
+                                ret.back().load();
                         } catch (invalid_argument& ex) {
                                 ;
                         }
diff --git a/upstream/src/aghermann/rk1968/rk1968.cc b/upstream/src/aghermann/rk1968/rk1968.cc
index 0604cf3..ddaa69a 100644
--- a/upstream/src/aghermann/rk1968/rk1968.cc
+++ b/upstream/src/aghermann/rk1968/rk1968.cc
@@ -27,14 +27,15 @@ using namespace agh::rk1968;
 
 
 CScoreAssistant::
-CScoreAssistant (const string& fname)
-        : CStorablePPack ()
+CScoreAssistant (const SScoreAssistantPPack& Pp_,
+                 const string& name_,
+                 agh::TExpDirLevel level_, agh::CExpDesign& ED_, const agh::SExpDirLevelId& level_id_)
+      : CStorablePPack (".rk1968", name_, level_, ED_, level_id_),
+        Pp (Pp_)
 {
-	
 }
 
 
-
 int
 CScoreAssistant::
 score( agh::SEpisode& E)
diff --git a/upstream/src/aghermann/rk1968/rk1968.hh b/upstream/src/aghermann/rk1968/rk1968.hh
index a0a0daa..7fa52e6 100644
--- a/upstream/src/aghermann/rk1968/rk1968.hh
+++ b/upstream/src/aghermann/rk1968/rk1968.hh
@@ -13,7 +13,6 @@
 #define AGH_AGHERMANN_RK1968_H_
 
 #include <string>
-#include <list>
 #include "aghermann/expdesign/dirlevel.hh"
 
 using namespace std;
@@ -34,16 +33,14 @@ class CScoreAssistant
   : public CStorablePPack {
 
     public:
-        CScoreAssistant (const string& fname);
+        CScoreAssistant (const SScoreAssistantPPack&,
+                         const string& name_,
+                         TExpDirLevel, CExpDesign&, const SExpDirLevelId&);
 
         int score( agh::SEpisode&);
 
-        int save();
-        int load();
         int delete_file();
 
-        string  name;
-
         SScoreAssistantPPack
                 Pp;
 };
diff --git a/upstream/src/common/subject_id.hh b/upstream/src/common/subject_id.hh
index 2f77b4e..1bd1dd4 100644
--- a/upstream/src/common/subject_id.hh
+++ b/upstream/src/common/subject_id.hh
@@ -9,8 +9,8 @@
  *         License:  GPL
  */
 
-#ifndef _AGH_SUBJECT_ID
-#define _AGH_SUBJECT_ID
+#ifndef AGH_COMMON_SUBJECT_ID_H_
+#define AGH_COMMON_SUBJECT_ID_H_
 
 #include <ctime>
 
@@ -28,13 +28,13 @@ namespace agh {
 
 // follow http://www.edfplus.info/specs/edfplus.html#datarecords, section 2.1.3.3
 struct SSubjectId {
-        string        id,
+        string  id,
                 name;
-        time_t        dob;
+        time_t  dob;
         enum class TGender : char {
                 unknown = 'X', male = 'M', female = 'F'
         };
-        TGender        gender;
+        TGender gender;
 
         SSubjectId (const string& id_ = "", const string& name_ = "",
                     time_t dob_ = (time_t)0,
diff --git a/upstream/src/libsigfile/typed-source.cc b/upstream/src/libsigfile/typed-source.cc
index a178b1e..964919f 100644
--- a/upstream/src/libsigfile/typed-source.cc
+++ b/upstream/src/libsigfile/typed-source.cc
@@ -23,6 +23,28 @@ using sigfile::CEDFFile;
 
 
 
+bool
+sigfile::
+is_supported_source( CTypedSource& F)
+{
+        using namespace sigfile;
+        union {
+                CEDFFile::TSubtype e;
+                CTSVFile::TSubtype t;
+        } u;
+        return (F.type() == CTypedSource::TType::edf &&
+                (u.e = F.obj<CEDFFile>().subtype(),
+                 (u.e == CEDFFile::TSubtype::edf ||
+                  u.e == CEDFFile::TSubtype::edfplus_c)))
+                ||
+                (F.type() == CTypedSource::TType::ascii &&
+                 (u.t = F.obj<CTSVFile>().subtype(),
+                  (u.t == CTSVFile::TSubtype::csv ||
+                   u.t == CTSVFile::TSubtype::tsv)));
+}
+
+
+
 CTypedSource::
 CTypedSource (const string& fname,
               const size_t pagesize,
diff --git a/upstream/src/libsigfile/typed-source.hh b/upstream/src/libsigfile/typed-source.hh
index a62e6e3..3e81f6e 100644
--- a/upstream/src/libsigfile/typed-source.hh
+++ b/upstream/src/libsigfile/typed-source.hh
@@ -96,6 +96,9 @@ struct SNamedChannel {
 
 
 
+bool is_supported_source( sigfile::CTypedSource&);
+
+
 } // namespace sigfile
 
 #endif

-- 
Alioth's /git/debian-med/git-commit-notice on /srv/git.debian.org/git/debian-med/aghermann.git



More information about the debian-med-commit mailing list