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