root/library/bdm/bdmroot.h @ 850

Revision 850, 6.7 kB (checked in by smidl, 14 years ago)

changes in Loggers!

  • 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//! level of details that will be logged to a logger
42// DULEZITE TODO - zde musi respektovat aktualne ulozene hodnoty, tj. nacist je, pak pridat
43// ty co se maji v tomto kroku ulozit a pak vse ulozit.. protoze to delame kompozici a ne dedenim  DODELAT
44// ALE HOUBY, TAKHLE TO NEJDE, musime nechat jako samostatny objekt, kazda uroven tedy zvlast,
45// jednoznacne jmeno atd..
46//
47//! information about connection to a logger
48template<class T> class logged_options {
49private:
50        friend class UI;
51
52        //! boolean flags related indicating which details will be logged to a logger
53        bitset<32> values;
54
55        virtual const Array<string> &names() const
56        {
57                return T::__option_names();
58        }
59
60public:
61       
62        bool any() const
63        {
64                return values.any();
65        }
66
67        bool operator [] (const enum T::possible_options &option ) const
68        {
69                return values[option];
70        }
71
72        bitset<32>::reference operator [] (const enum T::possible_options &option )
73        {
74                return values[option];
75        }
76};
77
78// MUZEME INTERNE POUZIVAT ENUMY, A KLIDNE MENIT JEJICH PORADI, DIKY TOMUHLE MAKRU SE VZDY NAMAPUJI NA TY SPRAVNE STRINGY
79#define LOG_LEVEL(CLASSNAME,...) private: friend class logged_options<CLASSNAME>; static const Array<string> &__option_names() { static const Array<string> option_names( "{"#__VA_ARGS__"}" ); return option_names; }; public: enum possible_options { __VA_ARGS__ }; logged_options<CLASSNAME> log_level;
80
81//forward declaration
82class logger;
83
84//! information about connection to a logger
85class log_record {
86public:
87        //!remember which logger is registered
88        logger &L;
89        //! vector of log IDs - one element for each entry
90        ivec ids;
91
92        //!default constructor
93        log_record ( logger &L0 ) : L ( L0 ), ids ( 0 ) {}
94};
95
96//! Root class of BDM objects
97class root {
98protected:
99        //! record of connections to the logger
100        log_record* logrec;
101
102        //! It is necessary to allow calling of from_setting and to_setting within the UI class
103        friend class UI;
104
105        /*!     
106        \brief Read instance properties according the data stored in the Setting structure.
107        It has to be called only through UI class, therefore it is protected
108
109        At the begining of this method, it is obligatory to call
110        the corresponding base::from_setting method. Sometimes, there can be
111        an exception from this rule. If so, the programmer is encouraged
112        to describe the reasons for this exception in the documentation in detail.
113       
114        Then, all the configuration     components should be read through the UI mechanism.
115        Those with UI::SettingPresence set to optional shoud have their defaults fulfilled
116        within the body of this method.
117
118        For instance, declaring a class \c trunk derived from our #root class,
119        the implementation of the from_setting method should look like this
120
121        \code
122
123        public trunk : public root {
124
125                anytype xxx, yyy;
126
127                virtual void from_setting ( const Setting &set ) {
128                        root::from_setting( set );
129                       
130                        UI::get ( xxx, set, "xxx", UI::compulsory );
131
132                        if ( !UI::get ( yyy, set, "yyy", UI::optional ) ) {
133                                ... // here, it is necessary to set the default of attribute yyy                                               
134                        }
135
136                        ... // another stuff related to trunk class
137                }
138
139                ... // other members of this class
140        };
141
142        \endcode
143        */
144        virtual void from_setting ( const Setting &set ) {
145        }
146
147        /*!     
148        \brief  Save all the instance properties into the Setting structure.
149        It has to be called only through UI class, therefore it is protected
150
151        The only obligatory rule concerning the body of this method is to call
152        the corresponding base::to_setting method first.        Sometimes, there can
153        be an exception from this rule. If so, the programmer is encouraged
154        to describe the reasons for this exception in the documentation in detail.
155       
156        For instance, declaring
157        a class \c trunk derived from our #root class, the implementation of
158        the to_setting method should look like this
159
160        \code
161        public trunk : public root {
162
163                anytype xxx;
164
165                virtual void to_setting ( const Setting &set ) {
166                        root::to_setting( set );
167
168                        UI::save ( xxx, set, "xxx" );
169                       
170                        ... // another stuff related directly to trunk class
171
172                }
173
174                ... // other members of this class
175
176        };
177
178        \endcode
179        */
180        virtual void to_setting ( Setting &set ) const {
181        }
182
183public:
184        enum possible_options{};
185        //!default constructor
186        root() : logrec ( NULL ) {};
187
188        //! make sure this is a virtual object
189        virtual ~root() {
190                if ( logrec ) delete logrec;
191        }
192
193        //! Returns basic textual info about the current instance
194        virtual string to_string() const {
195                Config C;
196                Setting &set=C.getRoot();
197                this->to_setting(set);
198                ostringstream os;
199                UI_DBG(set, "", os);
200                return os.str();
201        }
202        //! Register itself in a logger, i.e. allocate space for data from this class
203        //! The level of details (parameter \c level ) is individual for each class.
204        virtual void log_register ( logger &L, const string &prefix ) {
205                logrec = new log_record ( L );
206        }
207
208        //! Write current information into the given logger
209        virtual void log_write() const {
210        }
211
212        /*!     
213        \brief  This method checks that all internal structures has been set up correctly.
214
215        It is called automatically after the call of the #from_setting method by the mechanism of the UI class.
216        However, it can be called in any other situation to assure the correctness of an instance.
217       
218        The only obligatory rule concerning the body of this method is to call
219        the corresponding base::validate method first. Sometimes, there can be
220        an exception from this rule. If so, the programmer is encouraged
221        to describe the reasons for this exception in the documentation in detail.     
222       
223        Then, only those checks which are not implemented in the base method
224        are implemented here. For instance, declaring a class \c trunk derived from our
225        #root class, the implementation of the method validate should
226        look like this
227
228        \code
229        public trunk : public root {
230
231                virtual void validate ( ) {
232                        root::validate( );
233
234                        ... // checks related directly to trunk class
235                }
236
237                ... // other members of this class
238
239        };
240
241        \endcode
242
243        */
244        virtual void validate() {
245        }
246
247        //! Virtual method providing deep copy of instances
248        virtual root* _copy() const NOT_IMPLEMENTED(NULL);
249       
250};
251
252}; //namespace
253#endif // root_H
Note: See TracBrowser for help on using the browser.