root/library/bdm/bdmroot.h @ 915

Revision 915, 6.2 kB (checked in by mido, 14 years ago)

optional parameter enum_subindex added to all the methods related to logging

  • Property svn:eol-style set to native
Line 
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>
18#include <bitset>
19
20#include "itpp_ext.h"
21#include "bdmerror.h"
22
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
31using namespace libconfig;
32using namespace itpp;
33using namespace std;
34
35namespace bdm {
36       
37//! auxiliary function for debugging
38void UI_DBG (const Setting &S, const string &spc, ostream &out );
39
40
41//! Forward class declaration
42class 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
48class log_level_base {
49private:
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
52        friend class UI;
53
54        //! this is necessary to allow logger to set ids vector appropriately and also to set registered_logger
55        friend class logger; 
56
57protected:
58        //! boolean flags related indicating which details will be logged to a logger
59        bitset<32> values;
60       
61        //! vector of vectors of log IDs - one element for each entry and multiple entries can be stored on the position of one enum
62        Vec<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
69public:
70        //! default constructor
71        log_level_base() {
72                registered_logger = NULL;
73        }
74
75
76        //! a general utility transforming a comma-separated sequence of strings into an instance of Array<strings>
77        static Array<string> string2Array( const string &input );
78
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;
81};
82
83//! Root class of BDM objects
84class root {
85private:
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
89protected:
90
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        */
130        virtual void from_setting ( const Setting &set ) {
131        }
132
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        */
166        virtual void to_setting ( Setting &set ) const {
167        }
168
169public:
170       
171        //!default constructor
172        root() {};
173
174        //! make sure this is a virtual object
175        virtual ~root() {
176        }
177
178        //! Returns basic textual info about the current instance
179        virtual string to_string() const {
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();
186        }
187        //! Register log levels of each inheritance layer to a logger, i.e. allocate space for data from this class
188        virtual void log_register ( logger &L, const string &prefix ) {
189        }
190
191        //! Write current information into the given logger
192        virtual void log_write() const {
193        } 
194
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        */
227        virtual void validate() {
228        }
229
230        //! Virtual method providing deep copy of instances
231        virtual root* _copy() const NOT_IMPLEMENTED(NULL);
232       
233};
234
235}; //namespace
236#endif // root_H
Note: See TracBrowser for help on using the browser.