root/library/doc/tutorial/ui.dox @ 600

Revision 600, 5.4 kB (checked in by mido, 15 years ago)

presun globalnich veci v RV dovnitr tridy kde byly zadefinovany jako staticke promenne
(plus drobne zacisteni dokumentace a adresarove struktury)

Line 
1/*!
2\page ui_page User Infos and their use
3 
4\sa #bdm::UI
5
6\sa #UIREGISTER
7
8\sa #bdm::UIFile
9
10\sa <a href="http://www.hyperrealm.com/libconfig/">The web page of the libconfig library.</a>
11
12\section ui_intro Introduction
13
14For easier interaction with users, experiments can be configured via structures called <b>User Info</b>. These structures contain information about details of the experiment that is to be performed. Since experiments involve the use of basic BDM classes and their compositions, the experiment description is also hierarchical with specific user info for each object or class.
15
16The User Infos are designed using customized version of the libconfig library (www.hyperrealm.com/libconfig). It is specialized for BDM so as to recognize basic mathematical objects, such as vectors and matrices, see details below.
17 
18Technically it can be made compatible with matlab structures.
19\todo it will be good to create a more detailed paragraph about the intraction with Matlab
20
21\section ui_example Example
22 
23For example, a simple experiment can be configures in the following way:
24\code
25ndat = 100;                //number of data points
26prior = { class="enorm";
27        mu = [1, 2, 3];
28        R = [1, 0, 0,
29             0, 1, 0,
30             0, 0, 1];
31};
32\endcode
33The exact meaning of the root fields in this structure (i.e. \c ndat and \c prior) is defined by the application (or mex file) that is using this configuration file. It will look for expected fields and it will ignore any other structures. When it does not find what it is looking for, it terminates with an appropriate error message.
34 
35A structure with field \c class="identifier" is special. Such a structure will be parsed by an appropriate #bdm::UI::build method which will construct the desired object, in this instance of an object of the class bdm::enorm.
36 
37For a detailed example how this mechanism works in practice see \ref arx_ui. To learn about the possinility of making <b> internal or external links</b> in configuration files, see the documentation of #bdm::SettingResolver.
38
39\section ui_implementation Implementation of user info in a custom class
40 
41In accordance with the previous example of the configuration file, consider the class defined as follows:
42
43\code
44
45class newbee : public bdm::root { 
46  ...
47   
48  bdm::enorm prior;
49  int ndat;
50 
51  ...   
52}
53
54\endcode
55 
56There are few obligatory steps to implement user info mechanism in this class.  At first, the class has to be \b inherited (directly or indirectly) from #bdm::root, as you can see in our example. What is hidden behind the scene is the use of a <b>parameterless constructor.</b> Each class using User infos has to have one.
57
58Next, <b>two virtual methods</b> #bdm::root::from_setting and #bdm::root::to_setting defined in the #bdm::root class <b>should be overriden </b> in accordance with the content of the current class. In fact, you can override just method #bdm::root::from_setting in the case you are interested only in loading from configuration files, which is quite common. It is valid also in the other way round, i.e., in the case you are interested just in saving, override only #bdm::root::to_setting.
59
60How should look the bodies of these methods? It is important not to forget to call their <b>implementation in the base class</b> (in our case, it is the #bdm::root class). Then, they should contain loading of all the necessary class attributes in the case of #bdm::root::from_setting method and saving of them in the other case. To implement these operation, you are encouraged to use static methods of #bdm::UI class, namely #bdm::UI::get, #bdm::UI::build and #bdm::UI::save.
61
62It can look like this:
63
64\code
65 
66class newbee : public bdm::root { 
67  ...
68   
69  bdm::enorm prior;
70  int ndat;
71 
72  ...
73
74  virtual void from_setting(const Setting &set) {
75    root::from_setting(set);
76    if( !UI::get( ndat, set, "ndat" ) )
77       ndat = 1;         
78    prior = UI::build<enorm>( set, "prior", compulsory );
79  }
80
81  virtual void to_setting(Setting &set) const {
82    root::to_setting(set);
83    UI::save(ndat, set, "ndat");
84    UI::save(prior, set, "prior");
85  }
86
87  ...
88}
89
90\endcode
91
92As you can see, the presence of a concrete Setting in the configuration file can be tested by the return value of these methods and the code initializing the
93default values can follow immediately. Imagine, for example, that the first attribute \c ndat is optional. Thereore, the default value is filled in the case that there is not any other in the configuration file (and so #bdm::UI::get method returs \c false). The second atribute, \c prior, is intended to be compulsory. This fact is specified by the last parameter of the templated #bdm::UI::build method. In this case, the method throws an exception if there is not proper data in the configuration file.
94
95The only difference between #bdm::UI::build and #bdm::UI::get method is in the types of variables they are prepared to. The #bdm::UI::build<T> method is used to initialize instances of classes derived from #bdm::root. It allocates them dynamically and return just an pointer to the new instance. This way it is possible even to load instances of inherited classes without aneven knowing about it. Oppositely, all scalar values of types int, double, string, vec, ivec or mat are loaded by the #bdm::UI::get method with a static memory management. It is also capable to load arrays of templated type #itpp::Array<T>.
96
97Saving is much more easier. For all the variable types, use the #bdm::UI::save method.
98*/
Note: See TracBrowser for help on using the browser.