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 | |
---|
31 | using namespace libconfig; |
---|
32 | using namespace itpp; |
---|
33 | using namespace std; |
---|
34 | |
---|
35 | namespace bdm { |
---|
36 | |
---|
37 | //! auxiliary function for debugging |
---|
38 | void UI_DBG (const Setting &S, const string &spc, ostream &out ); |
---|
39 | |
---|
40 | //forward declaration |
---|
41 | class logger; |
---|
42 | |
---|
43 | //! base class for all log_levels |
---|
44 | //! |
---|
45 | //! the existence of this class is forced by the necessity of passing log_levels to user_info methods, however, the main functionality |
---|
46 | //! is located in \c log_level_template class |
---|
47 | class log_level_base |
---|
48 | { |
---|
49 | private: |
---|
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 | protected: |
---|
55 | //! boolean flags related indicating which details will be logged to a logger |
---|
56 | bitset<32> values; |
---|
57 | |
---|
58 | public: |
---|
59 | |
---|
60 | //! a general utility transforming a comma-separated sequence of strings into an instance of Array<strings> |
---|
61 | static Array<string> string2Array( const string &input ); |
---|
62 | |
---|
63 | //! string equivalents of the used enumerations which are filled with a help of #LOG_LEVEL macro within class T |
---|
64 | virtual const Array<string> &names() const = 0; |
---|
65 | }; |
---|
66 | |
---|
67 | |
---|
68 | //! Root class of BDM objects |
---|
69 | class root { |
---|
70 | private: |
---|
71 | //! It is necessary to allow calling of from_setting and to_setting within the UI class ++ i log_options |
---|
72 | friend class UI; |
---|
73 | |
---|
74 | protected: |
---|
75 | //!remember which logger is registered |
---|
76 | logger * registered_logger; |
---|
77 | |
---|
78 | /*! |
---|
79 | \brief Read instance properties according the data stored in the Setting structure. |
---|
80 | It has to be called only through UI class, therefore it is protected |
---|
81 | |
---|
82 | At the begining of this method, it is obligatory to call |
---|
83 | the corresponding base::from_setting method. Sometimes, there can be |
---|
84 | an exception from this rule. If so, the programmer is encouraged |
---|
85 | to describe the reasons for this exception in the documentation in detail. |
---|
86 | |
---|
87 | Then, all the configuration components should be read through the UI mechanism. |
---|
88 | Those with UI::SettingPresence set to optional shoud have their defaults fulfilled |
---|
89 | within the body of this method. |
---|
90 | |
---|
91 | For instance, declaring a class \c trunk derived from our #root class, |
---|
92 | the implementation of the from_setting method should look like this |
---|
93 | |
---|
94 | \code |
---|
95 | |
---|
96 | public trunk : public root { |
---|
97 | |
---|
98 | anytype xxx, yyy; |
---|
99 | |
---|
100 | virtual void from_setting ( const Setting &set ) { |
---|
101 | root::from_setting( set ); |
---|
102 | |
---|
103 | UI::get ( xxx, set, "xxx", UI::compulsory ); |
---|
104 | |
---|
105 | if ( !UI::get ( yyy, set, "yyy", UI::optional ) ) { |
---|
106 | ... // here, it is necessary to set the default of attribute yyy |
---|
107 | } |
---|
108 | |
---|
109 | ... // another stuff related to trunk class |
---|
110 | } |
---|
111 | |
---|
112 | ... // other members of this class |
---|
113 | }; |
---|
114 | |
---|
115 | \endcode |
---|
116 | */ |
---|
117 | virtual void from_setting ( const Setting &set ) { |
---|
118 | } |
---|
119 | |
---|
120 | /*! |
---|
121 | \brief Save all the instance properties into the Setting structure. |
---|
122 | It has to be called only through UI class, therefore it is protected |
---|
123 | |
---|
124 | The only obligatory rule concerning the body of this method is to call |
---|
125 | the corresponding base::to_setting method first. Sometimes, there can |
---|
126 | be an exception from this rule. If so, the programmer is encouraged |
---|
127 | to describe the reasons for this exception in the documentation in detail. |
---|
128 | |
---|
129 | For instance, declaring |
---|
130 | a class \c trunk derived from our #root class, the implementation of |
---|
131 | the to_setting method should look like this |
---|
132 | |
---|
133 | \code |
---|
134 | public trunk : public root { |
---|
135 | |
---|
136 | anytype xxx; |
---|
137 | |
---|
138 | virtual void to_setting ( const Setting &set ) { |
---|
139 | root::to_setting( set ); |
---|
140 | |
---|
141 | UI::save ( xxx, set, "xxx" ); |
---|
142 | |
---|
143 | ... // another stuff related directly to trunk class |
---|
144 | |
---|
145 | } |
---|
146 | |
---|
147 | ... // other members of this class |
---|
148 | |
---|
149 | }; |
---|
150 | |
---|
151 | \endcode |
---|
152 | */ |
---|
153 | virtual void to_setting ( Setting &set ) const { |
---|
154 | } |
---|
155 | |
---|
156 | public: |
---|
157 | |
---|
158 | //!default constructor |
---|
159 | root() : registered_logger ( NULL ) {}; |
---|
160 | |
---|
161 | //! make sure this is a virtual object |
---|
162 | virtual ~root() { |
---|
163 | } |
---|
164 | |
---|
165 | //! Returns basic textual info about the current instance |
---|
166 | virtual string to_string() const { |
---|
167 | Config C; |
---|
168 | Setting &set=C.getRoot(); |
---|
169 | this->to_setting(set); |
---|
170 | ostringstream os; |
---|
171 | UI_DBG(set, "", os); |
---|
172 | return os.str(); |
---|
173 | } |
---|
174 | //! Register itself in a logger, i.e. allocate space for data from this class |
---|
175 | //! The level of details (parameter \c level ) is individual for each class. |
---|
176 | virtual void log_register ( logger &L, const string &prefix ) { |
---|
177 | registered_logger = &L; |
---|
178 | } |
---|
179 | |
---|
180 | //! Write current information into the given logger |
---|
181 | virtual void log_write() const { |
---|
182 | } |
---|
183 | |
---|
184 | /*! |
---|
185 | \brief This method checks that all internal structures has been set up correctly. |
---|
186 | |
---|
187 | It is called automatically after the call of the #from_setting method by the mechanism of the UI class. |
---|
188 | However, it can be called in any other situation to assure the correctness of an instance. |
---|
189 | |
---|
190 | The only obligatory rule concerning the body of this method is to call |
---|
191 | the corresponding base::validate method first. Sometimes, there can be |
---|
192 | an exception from this rule. If so, the programmer is encouraged |
---|
193 | to describe the reasons for this exception in the documentation in detail. |
---|
194 | |
---|
195 | Then, only those checks which are not implemented in the base method |
---|
196 | are implemented here. For instance, declaring a class \c trunk derived from our |
---|
197 | #root class, the implementation of the method validate should |
---|
198 | look like this |
---|
199 | |
---|
200 | \code |
---|
201 | public trunk : public root { |
---|
202 | |
---|
203 | virtual void validate ( ) { |
---|
204 | root::validate( ); |
---|
205 | |
---|
206 | ... // checks related directly to trunk class |
---|
207 | } |
---|
208 | |
---|
209 | ... // other members of this class |
---|
210 | |
---|
211 | }; |
---|
212 | |
---|
213 | \endcode |
---|
214 | |
---|
215 | */ |
---|
216 | virtual void validate() { |
---|
217 | } |
---|
218 | |
---|
219 | //! Virtual method providing deep copy of instances |
---|
220 | virtual root* _copy() const NOT_IMPLEMENTED(NULL); |
---|
221 | |
---|
222 | }; |
---|
223 | |
---|
224 | }; //namespace |
---|
225 | #endif // root_H |
---|