source: ThirdParty/mpqc_open/src/lib/util/keyval/keyval.dox

Candidate_v1.6.1
Last change on this file was 860145, checked in by Frederik Heber <heber@…>, 8 years ago

Merge commit '0b990dfaa8c6007a996d030163a25f7f5fc8a7e7' as 'ThirdParty/mpqc_open'

  • Property mode set to 100644
File size: 8.5 KB
Line 
1
2/** \page keyval The KeyVal Library
3
4The KeyVal class provides a means for users to associate keywords with
5values. ParsedKeyVal is a specialization of KeyVal that permits
6keyword/value associations in text such as an input file or a command line
7string.
8
9The package is flexible enough to allow complex structures and arrays as
10well as objects to be read from an input file.
11
12<ul>
13 <li> \ref keyvalass
14 <li> \ref keyvalgroup
15 <li> \ref keyvalarray
16 <li> \ref keyvaltab
17 <li> \ref keyvalexp
18 <li> \ref keyvalobj
19</ul>
20
21\section keyvalass Assignment
22
23As an example of the use of ParsedKeyVal, consider the following
24input:
25<pre>
26x_coordinate = 1.0
27y_coordinate = 2.0
28x_coordinate = 3.0
29</pre>
30Two assignements will be made. The keyword <tt>x_coordinate</tt> will be
31associated with the value <tt>1.0</tt> and the keyword <tt>y_coordinate</tt>
32will be assigned to <tt>2.0</tt>. The third line in the above input
33will have no effect since <tt>x_coordinate</tt> was assigned previously.
34
35\section keyvalgroup Keyword Grouping
36
37Lets imagine that we have a program which needs to read in the
38characteristics of animals. There are lots of animals so it might be
39nice to catagorize them by their family. Here is a sample format for
40such an input file:
41<pre>
42reptile: (
43 alligator: (
44 legs = 4
45 extinct = no
46 )
47 python: (
48 legs = 0
49 extinct = no
50 )
51 )
52bird: (
53 owl: (
54 flys = yes
55 extinct = no
56 )
57 )
58</pre>
59
60This sample illustrates the use of keyword <tt>=</tt> value
61assignments and the keyword grouping operators <tt>(</tt> and <tt>)</tt>.
62The keywords in this example are
63<pre>
64reptile:alligator:legs
65reptile:alligator:extinct
66reptile:alligator:legs
67reptile:python:size
68reptile:python:extinct
69bird:owl:flys
70bird:owl:extinct
71</pre>
72
73The <tt>:</tt>'s occuring in these keywords break the keywords into
74smaller logical units called keyword segments. The sole purpose of this
75is to allow persons writing input files to group the input into easy to
76read sections. In the above example there are two main sections, the
77reptile section and the bird section. The reptile section takes the
78form <tt>reptile</tt> <tt>:</tt> <tt>(</tt> keyword <tt>=</tt> value
79assignments <tt>)</tt>. Each of the keywords found between the
80parentheses has the <tt>reptile:</tt> prefix attached to it. Within each
81of these sections further keyword groupings can be used, as many and as
82deeply nested as the user wants.
83
84Keyword grouping is also useful when you need many different programs to
85read from the same input file. Each program can be assigned its own
86unique section.
87
88\section keyvalarray Array Construction
89
90Input for an array is specified in the input by forming a keyword
91group. The name of the group is the name of the array and the
92grouped keywords are the integers \f$i\f$, such that \f$0 \leq i < n\f$, where \f$n\f$
93is the number of elements in the array. For example, an array, called
94<tt>array</tt>, of length 3 could be given as follows:
95<pre>
96array: (
97 0 = 5.4
98 1 = 8.9
99 2 = 3.7
100 )
101</pre>
102The numbers <tt>0</tt>, <tt>1</tt>, and <tt>2</tt> in this example are keyword
103segments which serve as indices of <tt>array</tt>. However, this syntax
104is somewhat awkward and array construction operators have been provided
105to simplify the input for this case. The following input is equivalent
106to the above input:
107<pre>
108array = [ 5.4 8.9 3.7 ]
109</pre>
110
111More complex arrays than this can be imagined. Suppose an array of
112complex numbers is needed. For example the input
113<pre>
114carray: (
115 0: ( r = 1.0 i = 0.0 )
116 1: ( r = 0.0 i = 1.0 )
117 )
118</pre>
119could be written as
120<pre>
121carray: [
122 (r = 1.0 i = 0.0)
123 (r = 0.0 i = 1.0)
124 ]
125</pre>
126which looks a bit nicer than the example without array construction
127operators.
128
129Furthermore, the array construction operators can be nested in about
130every imaginable way. This allows multidimensional arrays of
131complicated data to be represented. Here is an example of
132input for a lower triangular array:
133<pre>
134ltriarray = [ [ 5.4 ]
135 [ 0.0 2.8 ]
136 [ 0.1 0.0 3.7 ] ]
137</pre>
138
139\section keyvaltab Table Construction
140
141Although the array construction operators will suit
142most requirements for enumerated lists of data, in some cases the input can
143still look ugly. This can, in some cases, be fixed with the table
144construction operators, <tt>{</tt> and <tt>}</tt>.
145
146Suppose a few long vectors of the same length are needed and the data in
147the <tt>i</tt>th element of each array is related or somehow belong
148together. If the arrays are so long that the width of a page is
149exceeded, then data that should be seen next to each other are no longer
150adjacent. The way this problem can be fixed is to arrange the data
151vertically side by side rather than horizontally. The table
152construction operators allows the user to achieve this in a very simple
153manner.
154<pre>
155balls: (
156 color = [ red blue red ]
157 diameter = [ 12 14 11 ]
158 material = [ rubber vinyl plastic ]
159 bounces = [ yes no no ]
160 coordinate = [[ 0.0 0.0 0.0]
161 [ 1.0 2.0 -1.0]
162 [ 1.0 -1.0 1.0]]
163 )
164</pre>
165can be written
166<pre>
167balls: (
168 { color diameter material bounces coordinate} =
169 { red 12 rubber yes [ 0.0 0.0 0.0]
170 blue 14 vinyl no [ 1.0 2.0 -1.0]
171 red 11 plastic no [ 1.0 -1.0 1.0] }
172 )
173</pre>
174The length and width of the table can be anything the user desires.
175
176<pre>Value Substitution</pre>
177
178Occasionally, a user may need to repeat some value several times in an
179input file. If the value must be changed, it would be nice to only
180change the value in one place. The value substitution feature of
181ParsedKeyVal allows the user to do this. Any place a value can
182occur the user can place a <tt>$</tt>. Following this a keyword must be
183given. This keyword must have been assigned before the attempt is made
184to use its value in a value substitution.
185
186Here is an example illustrating most of the variable substition
187features:
188<pre>
189default:linewidth = 130
190testsub: (
191 ke: (
192 ke_1 = 1
193 ke_2 = 2
194 ke_3: (
195 ke_31 = 31
196 ke_32 = 32
197 )
198 )
199 kx = $ke
200 r1 = 3.0
201 r2 = $r1
202 linewidth = $:default:linewidth
203 )
204</pre>
205is the same as specifying
206<pre>
207testsub: (
208 ke: (
209 ke_1 = 1
210 ke_3: (
211 ke_31 = 31
212 ke_32 = 32
213 )
214 ke_2 = 2
215 )
216 linewidth = 130
217 r2 = 3.0
218 r1 = 3.0
219 kx: (
220 ke_1 = 1
221 ke_2 = 2
222 ke_3: (
223 ke_31 = 31
224 ke_32 = 32
225 )
226 )
227 )
228</pre>
229It can be seen from this that value substitution can result in entire
230keyword segment hierarchies being copied, as well as simple
231substitutions.
232
233\section keyvalexp Expression Evaluation
234
235Suppose your program requires several parameters <tt>x1</tt>, <tt>x2</tt>,
236and <tt>x3</tt>. Furthermore, suppose that their ratios remain fixed for
237all the runs of the program that you desire. It would be best to
238specify some scale factor in the input that would be the only thing that
239has to be changed from run to run. If you don't want to or cannot
240modify the program, then this can be done directly with
241ParsedKeyVal as follows
242<pre>
243scale = 1.234
244x1 = ( $:scale * 1.2 )
245x2 = ( $:scale * 9.2 )
246x3 = ( $:scale * -2.0 )
247</pre>
248So we see that to the right of the ``<tt>=</tt>'' the characters
249``<tt>(</tt>'' and ``<tt>)</tt>'' are the expression construction operators.
250This is in contrast to their function when they are to the left of the
251``<tt>=</tt>'', where they are the keyword grouping operators.
252
253The expression must be binary and the data is all converted
254to double. If you use the expression construction operators to produce
255data that the program expects to be integer, you will certainly get the
256wrong answers (unless the desired value happens to be zero).
257
258\section keyvalobj Objects
259
260An instance of an object can be specified by surrounding it's
261classname with the ``<tt><</tt>'' and ``<tt>></tt>'' operators immediately
262after the keyword naming the data.
263
264A pointer to a single object can be associated with multiple keywords by
265using value substitution.
266This is accomplished by holding references to all objects once they are
267read in.
268
269Consider a linked list class, A, which reads from the keyword
270<tt>next</tt> a reference to an object of class A. Input for such an
271object, read from keyword <tt>a1</tt>, follows:
272<pre>
273a1\<A\>: (
274 next\<A\>: (
275 next\<B\>: (
276 bdata = 4
277 next\<A\>:()
278 )
279 )
280 )
281a2 = $:a1
282</pre>
283
284The <tt>a1</tt> list would contain two <tt>A</tt> objects followed by a
285<tt>B</tt> object followed by another <tt>A</tt> object. The <tt>a2</tt> list
286refers to exactly the same object as <tt>a1</tt> (not a copy of
287<tt>a1</tt>).
288
289*/
Note: See TracBrowser for help on using the repository browser.