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 | |
---|
41 | //! Forward class declaration |
---|
42 | class logger; |
---|
43 | |
---|
44 | //! Root class of BDM objects |
---|
45 | class root { |
---|
46 | private: |
---|
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 | |
---|
50 | protected: |
---|
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 | |
---|
130 | public: |
---|
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 | //! This class stores the details which will be logged to a logger |
---|
197 | //! |
---|
198 | //! This is only the first part of the whole declaration, which has to be however separated into |
---|
199 | //! two different classes for allowing the compilation of source code. For more details |
---|
200 | //! see logger::add_setting(...) method and mainly log_level_template<T>::template<class U> void store( const enum T::log_level_enums log_level_enum, const U data ) const |
---|
201 | //! method. For the reason the second one is templated, it was necessary to declare this whole class. |
---|
202 | template<class T> class log_level_base : public root { |
---|
203 | private: |
---|
204 | //! this is necessary to allow logger to set ids vector appropriately and also to set registered_logger |
---|
205 | friend class logger; |
---|
206 | |
---|
207 | |
---|
208 | //! this method adds new id to its proper position and return the name of this position |
---|
209 | string store_id_and_give_name( enum T::log_level_enums const log_level_enum, int enum_subindex, int id ) { |
---|
210 | if( ids(log_level_enum).length() <= enum_subindex ) |
---|
211 | ids(log_level_enum).set_size( enum_subindex+1, true ); |
---|
212 | ids(log_level_enum)(enum_subindex) = id; |
---|
213 | |
---|
214 | // here we remove a "log" prefix from name, i.e., for instance it transforms "logevidence" to "evidence" |
---|
215 | ostringstream stream; |
---|
216 | string name_with_prefix = names()(log_level_enum); |
---|
217 | string possible_log_prefix = name_with_prefix.substr(0,3); |
---|
218 | if( possible_log_prefix == "log" ) |
---|
219 | stream << name_with_prefix.substr(3,name_with_prefix.length()-3); |
---|
220 | else |
---|
221 | stream << name_with_prefix; |
---|
222 | |
---|
223 | // add number to name only in the case there are more registered vectors with the same log_level_enum |
---|
224 | if( ids(log_level_enum).length() > 1 ) |
---|
225 | stream << "*" << enum_subindex; |
---|
226 | |
---|
227 | return stream.str(); |
---|
228 | } |
---|
229 | |
---|
230 | protected: |
---|
231 | //! internal pointer to the logger to which this log_level is registered |
---|
232 | //! |
---|
233 | //! it is set to NULL at the beginning |
---|
234 | logger * registered_logger; |
---|
235 | |
---|
236 | //! vector of vectors of log IDs - one element for each entry and multiple entries can be stored on the position of one enum |
---|
237 | Vec<ivec> ids; |
---|
238 | |
---|
239 | //! string equivalents of the used enumerations which are filled with a help of #LOG_LEVEL macro within class T |
---|
240 | const Array<string> &names() const |
---|
241 | { |
---|
242 | return T::log_level_names(); |
---|
243 | } |
---|
244 | |
---|
245 | //! default constructor�which is intentionaly declared as protected |
---|
246 | log_level_base( ) { |
---|
247 | registered_logger = NULL; |
---|
248 | int len = names().length(); |
---|
249 | ids.set_size( len ); |
---|
250 | for( int i = 0; i<len; i++ ) |
---|
251 | { |
---|
252 | ids(i).set_size ( 1 ); |
---|
253 | ids(i) = -1; |
---|
254 | } |
---|
255 | } |
---|
256 | |
---|
257 | public: |
---|
258 | //! a general utility transforming a comma-separated sequence of strings into an instance of Array<strings> |
---|
259 | static Array<string> string2Array( const string &input ) |
---|
260 | { |
---|
261 | string result = input; |
---|
262 | string::size_type loc; |
---|
263 | while( loc = result.find( ',' ), loc != string::npos ) |
---|
264 | result[loc] = ' '; |
---|
265 | return Array<string>("{ " + result + " }" ); |
---|
266 | } |
---|
267 | }; |
---|
268 | //UIREGISTER IS FORBIDDEN FOR THIS CLASS, AS IT SHOULD BE LOADED ONLY THROUGH THE SPECIALIZED UI::GET(...) METHOD |
---|
269 | |
---|
270 | }; //namespace |
---|
271 | #endif // root_H |
---|