[706] | 1 | |
---|
| 2 | /*! |
---|
| 3 | \file |
---|
| 4 | \brief Bayesian Models (bm) that use Bayes rule to learn from observations |
---|
| 5 | \author Vaclav Smidl. |
---|
| 6 | |
---|
| 7 | ----------------------------------- |
---|
| 8 | BDM++ - C++ library for Bayesian Decision Making under Uncertainty |
---|
| 9 | |
---|
| 10 | Using IT++ for numerical operations |
---|
| 11 | ----------------------------------- |
---|
| 12 | */ |
---|
| 13 | |
---|
| 14 | #ifndef root_H |
---|
| 15 | #define root_H |
---|
| 16 | |
---|
| 17 | #include <string> |
---|
[850] | 18 | #include <bitset> |
---|
[706] | 19 | |
---|
| 20 | #include "itpp_ext.h" |
---|
[766] | 21 | #include "bdmerror.h" |
---|
[706] | 22 | |
---|
[789] | 23 | #ifdef MEX |
---|
| 24 | #include "base/libconfig/lib/libconfig.h++" |
---|
| 25 | // TODO DODELAT A NAHRADIT #include "base/libconfig_mex.h" |
---|
| 26 | #else |
---|
| 27 | #include "base/libconfig/lib/libconfig.h++" |
---|
| 28 | #endif |
---|
| 29 | |
---|
| 30 | |
---|
[706] | 31 | using namespace libconfig; |
---|
| 32 | using namespace itpp; |
---|
| 33 | using namespace std; |
---|
| 34 | |
---|
| 35 | namespace bdm { |
---|
[769] | 36 | |
---|
[789] | 37 | //! auxiliary function for debugging |
---|
| 38 | void UI_DBG (const Setting &S, const string &spc, ostream &out ); |
---|
[907] | 39 | |
---|
[910] | 40 | |
---|
| 41 | //! Forward class declaration |
---|
[907] | 42 | class logger; |
---|
| 43 | |
---|
| 44 | //! base class for all log_levels |
---|
| 45 | //! |
---|
| 46 | //! the existence of this class is forced by the necessity of passing log_levels to user_info methods, however, the main functionality |
---|
| 47 | //! is located in \c log_level_template class |
---|
[910] | 48 | class log_level_base { |
---|
[850] | 49 | private: |
---|
[870] | 50 | // UserInfo class have to be able to read all the internal |
---|
| 51 | // attributes to be able to write/read log_level to/from a Setting structure |
---|
[850] | 52 | friend class UI; |
---|
| 53 | |
---|
[910] | 54 | //! this is necessary to allow logger to set ids vector appropriately and also to set registered_logger |
---|
| 55 | friend class logger; |
---|
| 56 | |
---|
[907] | 57 | protected: |
---|
[850] | 58 | //! boolean flags related indicating which details will be logged to a logger |
---|
| 59 | bitset<32> values; |
---|
| 60 | |
---|
[910] | 61 | //! vector of log IDs - one element for each entry |
---|
| 62 | ivec ids; |
---|
| 63 | |
---|
| 64 | //! internal pointer to the logger to which this log_level is registered |
---|
| 65 | //! |
---|
| 66 | //! it is set to NULL at the beginning |
---|
| 67 | logger * registered_logger; |
---|
| 68 | |
---|
[907] | 69 | public: |
---|
[910] | 70 | //! default constructor |
---|
| 71 | log_level_base() { |
---|
| 72 | registered_logger = NULL; |
---|
| 73 | } |
---|
[907] | 74 | |
---|
[910] | 75 | |
---|
[870] | 76 | //! a general utility transforming a comma-separated sequence of strings into an instance of Array<strings> |
---|
[907] | 77 | static Array<string> string2Array( const string &input ); |
---|
[870] | 78 | |
---|
[907] | 79 | //! string equivalents of the used enumerations which are filled with a help of #LOG_LEVEL macro within class T |
---|
| 80 | virtual const Array<string> &names() const = 0; |
---|
[850] | 81 | }; |
---|
| 82 | |
---|
[706] | 83 | //! Root class of BDM objects |
---|
| 84 | class root { |
---|
[853] | 85 | private: |
---|
| 86 | //! It is necessary to allow calling of from_setting and to_setting within the UI class ++ i log_options |
---|
| 87 | friend class UI; |
---|
| 88 | |
---|
[737] | 89 | protected: |
---|
| 90 | |
---|
[789] | 91 | /*! |
---|
| 92 | \brief Read instance properties according the data stored in the Setting structure. |
---|
| 93 | It has to be called only through UI class, therefore it is protected |
---|
| 94 | |
---|
| 95 | At the begining of this method, it is obligatory to call |
---|
| 96 | the corresponding base::from_setting method. Sometimes, there can be |
---|
| 97 | an exception from this rule. If so, the programmer is encouraged |
---|
| 98 | to describe the reasons for this exception in the documentation in detail. |
---|
| 99 | |
---|
| 100 | Then, all the configuration components should be read through the UI mechanism. |
---|
| 101 | Those with UI::SettingPresence set to optional shoud have their defaults fulfilled |
---|
| 102 | within the body of this method. |
---|
| 103 | |
---|
| 104 | For instance, declaring a class \c trunk derived from our #root class, |
---|
| 105 | the implementation of the from_setting method should look like this |
---|
| 106 | |
---|
| 107 | \code |
---|
| 108 | |
---|
| 109 | public trunk : public root { |
---|
| 110 | |
---|
| 111 | anytype xxx, yyy; |
---|
| 112 | |
---|
| 113 | virtual void from_setting ( const Setting &set ) { |
---|
| 114 | root::from_setting( set ); |
---|
| 115 | |
---|
| 116 | UI::get ( xxx, set, "xxx", UI::compulsory ); |
---|
| 117 | |
---|
| 118 | if ( !UI::get ( yyy, set, "yyy", UI::optional ) ) { |
---|
| 119 | ... // here, it is necessary to set the default of attribute yyy |
---|
| 120 | } |
---|
| 121 | |
---|
| 122 | ... // another stuff related to trunk class |
---|
| 123 | } |
---|
| 124 | |
---|
| 125 | ... // other members of this class |
---|
| 126 | }; |
---|
| 127 | |
---|
| 128 | \endcode |
---|
| 129 | */ |
---|
[756] | 130 | virtual void from_setting ( const Setting &set ) { |
---|
| 131 | } |
---|
| 132 | |
---|
[789] | 133 | /*! |
---|
| 134 | \brief Save all the instance properties into the Setting structure. |
---|
| 135 | It has to be called only through UI class, therefore it is protected |
---|
| 136 | |
---|
| 137 | The only obligatory rule concerning the body of this method is to call |
---|
| 138 | the corresponding base::to_setting method first. Sometimes, there can |
---|
| 139 | be an exception from this rule. If so, the programmer is encouraged |
---|
| 140 | to describe the reasons for this exception in the documentation in detail. |
---|
| 141 | |
---|
| 142 | For instance, declaring |
---|
| 143 | a class \c trunk derived from our #root class, the implementation of |
---|
| 144 | the to_setting method should look like this |
---|
| 145 | |
---|
| 146 | \code |
---|
| 147 | public trunk : public root { |
---|
| 148 | |
---|
| 149 | anytype xxx; |
---|
| 150 | |
---|
| 151 | virtual void to_setting ( const Setting &set ) { |
---|
| 152 | root::to_setting( set ); |
---|
| 153 | |
---|
| 154 | UI::save ( xxx, set, "xxx" ); |
---|
| 155 | |
---|
| 156 | ... // another stuff related directly to trunk class |
---|
| 157 | |
---|
| 158 | } |
---|
| 159 | |
---|
| 160 | ... // other members of this class |
---|
| 161 | |
---|
| 162 | }; |
---|
| 163 | |
---|
| 164 | \endcode |
---|
| 165 | */ |
---|
[756] | 166 | virtual void to_setting ( Setting &set ) const { |
---|
| 167 | } |
---|
| 168 | |
---|
[706] | 169 | public: |
---|
[907] | 170 | |
---|
[706] | 171 | //!default constructor |
---|
[910] | 172 | root() {}; |
---|
[737] | 173 | |
---|
[706] | 174 | //! make sure this is a virtual object |
---|
| 175 | virtual ~root() { |
---|
| 176 | } |
---|
| 177 | |
---|
[769] | 178 | //! Returns basic textual info about the current instance |
---|
[706] | 179 | virtual string to_string() const { |
---|
[769] | 180 | Config C; |
---|
| 181 | Setting &set=C.getRoot(); |
---|
| 182 | this->to_setting(set); |
---|
| 183 | ostringstream os; |
---|
| 184 | UI_DBG(set, "", os); |
---|
| 185 | return os.str(); |
---|
[706] | 186 | } |
---|
[910] | 187 | //! Register log levels of each inheritance layer to a logger, i.e. allocate space for data from this class |
---|
[737] | 188 | virtual void log_register ( logger &L, const string &prefix ) { |
---|
[706] | 189 | } |
---|
[737] | 190 | |
---|
| 191 | //! Write current information into the given logger |
---|
[706] | 192 | virtual void log_write() const { |
---|
[907] | 193 | } |
---|
[706] | 194 | |
---|
[789] | 195 | /*! |
---|
| 196 | \brief This method checks that all internal structures has been set up correctly. |
---|
| 197 | |
---|
| 198 | It is called automatically after the call of the #from_setting method by the mechanism of the UI class. |
---|
| 199 | However, it can be called in any other situation to assure the correctness of an instance. |
---|
| 200 | |
---|
| 201 | The only obligatory rule concerning the body of this method is to call |
---|
| 202 | the corresponding base::validate method first. Sometimes, there can be |
---|
| 203 | an exception from this rule. If so, the programmer is encouraged |
---|
| 204 | to describe the reasons for this exception in the documentation in detail. |
---|
| 205 | |
---|
| 206 | Then, only those checks which are not implemented in the base method |
---|
| 207 | are implemented here. For instance, declaring a class \c trunk derived from our |
---|
| 208 | #root class, the implementation of the method validate should |
---|
| 209 | look like this |
---|
| 210 | |
---|
| 211 | \code |
---|
| 212 | public trunk : public root { |
---|
| 213 | |
---|
| 214 | virtual void validate ( ) { |
---|
| 215 | root::validate( ); |
---|
| 216 | |
---|
| 217 | ... // checks related directly to trunk class |
---|
| 218 | } |
---|
| 219 | |
---|
| 220 | ... // other members of this class |
---|
| 221 | |
---|
| 222 | }; |
---|
| 223 | |
---|
| 224 | \endcode |
---|
| 225 | |
---|
| 226 | */ |
---|
[706] | 227 | virtual void validate() { |
---|
| 228 | } |
---|
[766] | 229 | |
---|
| 230 | //! Virtual method providing deep copy of instances |
---|
| 231 | virtual root* _copy() const NOT_IMPLEMENTED(NULL); |
---|
| 232 | |
---|
[706] | 233 | }; |
---|
| 234 | |
---|
| 235 | }; //namespace |
---|
| 236 | #endif // root_H |
---|