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