root/library/bdm/bdmroot.h @ 942

Revision 942, 4.9 kB (checked in by mido, 14 years ago)

the functionality of user info was improved, it supports an initialization of root descendant via UI::get directly, however it is save only for static attributes, for dynamically allocated attributes UI::build should be called to handle with intahrence issues

  • 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//! Root class of BDM objects
45class root {
46private:
47        //! It is necessary to allow calling of from_setting and to_setting within the UI class ++ i log_options
48        friend class UI;
49
50protected:
51
52        /*!     
53        \brief Read instance properties according the data stored in the Setting structure.
54        It has to be called only through UI class, therefore it is protected
55
56        At the begining of this method, it is obligatory to call
57        the corresponding base::from_setting method. Sometimes, there can be
58        an exception from this rule. If so, the programmer is encouraged
59        to describe the reasons for this exception in the documentation in detail.
60       
61        Then, all the configuration     components should be read through the UI mechanism.
62        Those with UI::SettingPresence set to optional shoud have their defaults fulfilled
63        within the body of this method.
64
65        For instance, declaring a class \c trunk derived from our #root class,
66        the implementation of the from_setting method should look like this
67
68        \code
69
70        public trunk : public root {
71
72                anytype xxx, yyy;
73
74                virtual void from_setting ( const Setting &set ) {
75                        root::from_setting( set );
76                       
77                        UI::get ( xxx, set, "xxx", UI::compulsory );
78
79                        if ( !UI::get ( yyy, set, "yyy", UI::optional ) ) {
80                                ... // here, it is necessary to set the default of attribute yyy                                               
81                        }
82
83                        ... // another stuff related to trunk class
84                }
85
86                ... // other members of this class
87        };
88
89        \endcode
90        */
91        virtual void from_setting ( const Setting &set ) {
92        }
93
94        /*!     
95        \brief  Save all the instance properties into the Setting structure.
96        It has to be called only through UI class, therefore it is protected
97
98        The only obligatory rule concerning the body of this method is to call
99        the corresponding base::to_setting method first.        Sometimes, there can
100        be an exception from this rule. If so, the programmer is encouraged
101        to describe the reasons for this exception in the documentation in detail.
102       
103        For instance, declaring
104        a class \c trunk derived from our #root class, the implementation of
105        the to_setting method should look like this
106
107        \code
108        public trunk : public root {
109
110                anytype xxx;
111
112                virtual void to_setting ( const Setting &set ) {
113                        root::to_setting( set );
114
115                        UI::save ( xxx, set, "xxx" );
116                       
117                        ... // another stuff related directly to trunk class
118
119                }
120
121                ... // other members of this class
122
123        };
124
125        \endcode
126        */
127        virtual void to_setting ( Setting &set ) const {
128        }
129
130public:
131       
132        //!default constructor
133        root() {};
134
135        //! make sure this is a virtual object
136        virtual ~root() {
137        }
138
139        //! Returns basic textual info about the current instance
140        virtual string to_string() const {
141                Config C;
142                Setting &set=C.getRoot();
143                this->to_setting(set);
144                ostringstream os;
145                UI_DBG(set, "", os);
146                return os.str();
147        }
148        //! Register log levels of each inheritance layer to a logger, i.e. allocate space for data from this class
149        virtual void log_register ( logger &L, const string &prefix ) {
150        }
151
152        //! Write current information into the given logger
153        virtual void log_write() const {
154        } 
155
156        /*!     
157        \brief  This method checks that all internal structures has been set up correctly.
158
159        It is called automatically after the call of the #from_setting method by the mechanism of the UI class.
160        However, it can be called in any other situation to assure the correctness of an instance.
161       
162        The only obligatory rule concerning the body of this method is to call
163        the corresponding base::validate method first. Sometimes, there can be
164        an exception from this rule. If so, the programmer is encouraged
165        to describe the reasons for this exception in the documentation in detail.     
166       
167        Then, only those checks which are not implemented in the base method
168        are implemented here. For instance, declaring a class \c trunk derived from our
169        #root class, the implementation of the method validate should
170        look like this
171
172        \code
173        public trunk : public root {
174
175                virtual void validate ( ) {
176                        root::validate( );
177
178                        ... // checks related directly to trunk class
179                }
180
181                ... // other members of this class
182
183        };
184
185        \endcode
186
187        */
188        virtual void validate() {
189        }
190
191        //! Virtual method providing deep copy of instances
192        virtual root* _copy() const NOT_IMPLEMENTED(NULL);
193       
194};
195
196}; //namespace
197#endif // root_H
Note: See TracBrowser for help on using the browser.