Mike Gerwitz

Activist for User Freedom

aboutsummaryrefslogtreecommitdiffstats
blob: 9149ddd42ce1500cb0718657f7b4c54e00dcb3d1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
<?xml version="1.0" encoding="ISO-8859-1"?>
<!--
  Compiles rater XML into JavaScript

  This stylesheet should be included by whatever is doing the processing and is
  responsible for outputting the generated code in whatever manner is
  appropriate (inline JS, a file, etc).
-->

<stylesheet version="2.0"
  xmlns="http://www.w3.org/1999/XSL/Transform"
  xmlns:xs="http://www.w3.org/2001/XMLSchema"
  xmlns:lv="http://www.lovullo.com/rater"
  xmlns:w="http://www.lovullo.com/rater/worksheet"
  xmlns:_w="http://www.lovullo.com/rater/worksheet/_priv"
  xmlns:c="http://www.lovullo.com/calc"
  xmlns:lvmc="http://www.lovullo.com/rater/map/compiler"
  xmlns:wc="http://www.lovullo.com/rater/worksheet/compiler"
  xmlns:preproc="http://www.lovullo.com/rater/preproc"
  xmlns:util="http://www.lovullo.com/util">

<variable name="wc:lc" select="'abcdefghijklmnopqrstuvwxyz'" />
<variable name="wc:uc" select="'ABCDEFGHIJKLMNOPQRSTUVWXYZ'" />

<!-- lexemes to be converted to a human-readable format -->
<!-- TODO: move this into an external file so it may be easily configurable -->
<variable name="wc:hlex">
  <!-- prefix to suffix -->
  <wc:lex prefix="prem" to="Premium" />
  <wc:lex prefix="rate" to="Rate" />
  <wc:lex prefix="credit" to="Credit" />
  <wc:lex prefix="surcharge" to="Surcharge" />

  <wc:lex str="gl" to="GL" />
  <wc:lex str="prop" to="Property" />
  <wc:lex str="equip" to="Equipment" />
  <wc:lex str="adjust" to="Adjustment" />
  <wc:lex str="adj" to="Adjustment" />
  <wc:lex str="ded" to="Deductible" />
  <wc:lex str="dw" to="Dwelling" />
  <wc:lex str="fam" to="Family" />
  <wc:lex str="tiv" to="TIV" />

  <!-- *Each is used for generators -->
  <wc:lex str="each" to="" />
</variable>


<!-- we expect that the worksheet will have been preprocessed into the rater
     document -->
<template match="w:worksheet" mode="w:compile" priority="10">
  <param name="corder" />

  <variable name="displays" as="element( w:display )*"
                select="w:display" />

  <variable name="package" as="element( lv:package )"
                select="_w:load-package( @package, . )" />

  <variable name="syms" as="element( preproc:sym )*"
                select="_w:filter-needed-symbols(
                          _w:load-symbols( $package ),
                          $displays )" />

  <lv:package name="{@name}"
               lvmc:type="worksheet">
    <!-- we provide one special symbol -->
    <preproc:symtable>
      <preproc:sym name="___worksheet"
                   type="worksheet" />
    </preproc:symtable>

    <!-- TODO -->
    <preproc:sym-deps>
      <preproc:sym-dep name="___worksheet" />
    </preproc:sym-deps>

    <copy-of select="node()" />

    <preproc:fragments>
      <preproc:fragment id="___worksheet">
        <text>rater.worksheet = </text>

        <call-template name="util:json">
          <with-param name="obj">
            <for-each select="$displays">
              <sequence select="_w:compile-display( ., $syms )" />
            </for-each>

            <variable name="yield" as="element( lv:rate )?"
                          select="$package/lv:rate[ @yields = '___yield' ]" />

            <!-- always include yield -->
            <if test="$yield">
              <util:value>
                <call-template name="util:json">
                  <with-param name="id" select="'yield'" />
                  <with-param name="array">
                    <util:value>
                      <call-template name="util:json">
                        <with-param name="value" select="'Yields'" />
                      </call-template>
                    </util:value>

                    <util:value>
                      <apply-templates mode="wc:compile"
                                           select="$yield/c:*" />
                    </util:value>
                  </with-param>
                </call-template>
              </util:value>
            </if>
          </with-param>
        </call-template>

        <text>;</text>
      </preproc:fragment>
    </preproc:fragments>
  </lv:package>
</template>


<function name="_w:compile-display" as="element( util:value )">
  <param name="display" as="element( w:display )" />
  <param name="syms"    as="element( preproc:sym )*" />

  <variable name="name" as="xs:string"
                select="$display/@name" />

  <variable name="sym" as="element( preproc:sym )?"
                select="$syms[ @name = $name ]" />

  <!-- terminate on unknown references -->
  <if test="empty( $sym )">
    <message terminate="yes"
                 select="'Reference to unknown symbol:', $name" />
  </if>

  <util:value>
    <call-template name="util:json">
      <with-param name="id" select="$name" />

      <with-param name="array">
        <util:value>
          <call-template name="util:json">
            <with-param name="value">
              <call-template name="wc:var-to-hstr">
                <with-param name="var" select="$name" />
              </call-template>
            </with-param>
          </call-template>
        </util:value>

        <util:value>
          <choose>
            <when test="$display/@collapse = 'true'">
              <sequence select="''" />
            </when>

            <otherwise>
              <variable name="rate-block" as="element( lv:rate )"
                        select="_w:get-src-node( $sym )" />

              <apply-templates mode="wc:compile"
                               select="$rate-block/c:*">
                <with-param name="display" select="$display"
                            tunnel="yes" />
              </apply-templates>
            </otherwise>
          </choose>
        </util:value>

        <util:value>
          <call-template name="util:json">
            <with-param name="value">
              <value-of select="$display/@always" />
            </with-param>
          </call-template>
        </util:value>
      </with-param>
    </call-template>
  </util:value>
</function>


<function name="_w:load-package" as="element( lv:package )">
  <param name="path"    as="xs:string" />
  <param name="context" as="node()" />

  <!-- TODO: function to provide xmlo extension -->
  <variable name="package-uri" as="xs:anyURI"
                select="resolve-uri(
                          concat( $path, '.xmlo' ),
                          base-uri( $context ) )" />

  <if test="not( doc-available( $package-uri ) )">
    <message terminate="yes"
                 select="concat( 'fatal: package ',
                                 $path,
                                 ' not found' )" />
  </if>

  <sequence select="doc( $package-uri )/lv:package" />
</function>


<!-- TODO: some of this logic can be factored out into a common
     library -->
<function name="_w:load-symbols" as="element( preproc:sym )+">
  <param name="package" as="element( lv:package )" />

  <sequence select="$package/preproc:symtable/preproc:sym" />
</function>



<function name="_w:filter-needed-symbols" as="element( preproc:sym )*">
  <param name="syms"     as="element( preproc:sym )*" />
  <param name="displays" as="element( w:display )*" />

  <sequence select="$syms[ @name = $displays/@name ]" />
</function>


<function name="_w:get-src-package" as="element( lv:package )">
  <param name="sym" as="element( preproc:sym )" />

  <variable name="sym-path" as="xs:string?"
                select="$sym/@src" />

  <variable name="context-uri" as="xs:anyURI"
                select="base-uri( $sym )" />

  <!-- TODO: function to provide xmlo extension -->
  <variable name="src-uri" as="xs:anyURI"
                select="if ( $sym-path and not( $sym-path = '' ) ) then
                          resolve-uri(
                            concat( $sym-path, '.xmlo' ),
                            $context-uri )
                        else
                          $context-uri" />

  <if test="not( doc-available( $src-uri ) )">
    <message terminate="yes"
                 select="concat( 'fatal: package ',
                                 $sym-path,
                                 ' not found; required by symbol ',
                                 $sym/@name )" />
  </if>

  <sequence select="doc( $src-uri )/lv:package" />
</function>



<function name="_w:get-src-node" as="element( lv:rate )">
  <param name="sym"  as="element( preproc:sym )" />

  <variable name="package" as="element( lv:package )"
                select="_w:get-src-package( $sym )" />

  <variable name="rate-name" as="xs:string"
                select="if ( $sym/@parent ) then
                          $sym/@parent
                        else
                          $sym/@name" />

  <sequence select="$package/lv:rate[ @yields = $rate-name ]" />
</function>


<template match="c:sum[@of='_CMATCH_']" mode="wc:compile" priority="9">
  <apply-templates select="./c:*" mode="wc:compile" />
</template>

<template match="c:sum[@of='_CMATCH_']/c:product[c:value-of[@name='_CMATCH_']]"
  mode="wc:compile" priority="9">

  <!-- ignore the product and continue with the 2nd node -->
  <apply-templates select="./c:*[2]" mode="wc:compile" />
</template>


<template match="c:apply" priority="7" mode="wc:compile">
  <param name="display" as="element( w:display )"
         tunnel="yes" />

  <choose>
    <!-- do not expand -->
    <when test="
        not(
          @name = $display/ancestor::w:worksheet
            /w:expand-function/@name
          or (
            @name = $display/w:expand-function/@name
          )
        )
      ">

      <call-template name="wc:compile-calc">
        <with-param name="nochildren" select="true()" />
        <with-param name="runtime" select="true()" />
      </call-template>
    </when>

    <!-- expand -->
    <otherwise>
      <call-template name="wc:compile-calc" />
    </otherwise>
  </choose>
</template>


<template match="c:value-of[c:index]" mode="wc:compile" priority="5">
  <call-template name="wc:compile-calc">
    <with-param name="nochildren" select="true()" />
    <with-param name="runtime" select="true()" />
  </call-template>
</template>

<!-- we need to take into account constants that are compiled in place (so we
     cannot determine their value by name at runtime) -->
<template match="c:value-of[ @name=//lv:const[ not( * ) ]/@name ]" mode="wc:compile" priority="5">
  <variable name="name" select="@name" />

  <call-template name="wc:compile-calc">
    <with-param name="include-value">
      <value-of select="//lv:const[ @name=$name ]/@value" />
    </with-param>
  </call-template>
</template>


<!--
  Will output JSON of the following structure:

  [ "type", {desc}, [subnodes] ]

  The subnodes are recursively generated in the same format as above.
-->
<template name="wc:compile-calc" match="c:*" mode="wc:compile" priority="4">
  <param name="nochildren" as="xs:boolean" select="false()" />
  <param name="runtime" select="false()" />
  <param name="include-value" />

  <call-template name="util:json">
    <with-param name="array">
      <!-- output node type -->
      <util:value>
        <call-template name="util:json">
          <with-param name="value" select="local-name()" />
        </call-template>
      </util:value>

      <!-- description -->
      <util:value>
        <call-template name="util:json">
          <with-param name="obj">

            <!-- build each attribute into the description -->
            <for-each select="@*">
              <util:value>
                <call-template name="util:json">
                  <with-param name="id" select="local-name()" />
                  <with-param name="value" select="." />
                </call-template>
              </util:value>
            </for-each>

            <!-- certain values should be calculated at runtime -->
            <if test="$runtime = true()">
              <util:value>
                <call-template name="util:json">
                  <with-param name="id"    select="'runtime'" />
                  <with-param name="value" select="'true'" />
                </call-template>
              </util:value>
            </if>

          </with-param>
        </call-template>
      </util:value>

      <!-- children -->
      <util:value>
        <call-template name="util:json">
          <with-param name="array">

            <if test="not( $nochildren = true() )">
              <!-- sub-nodes (recursive) -->
              <for-each select="c:*">
                <util:value>
                  <apply-templates select="." mode="wc:compile" />
                </util:value>
              </for-each>
            </if>

          </with-param>
        </call-template>
      </util:value>

      <!-- optional value (if we can determine compile-time) -->
      <if test="$include-value">
        <util:value>
          <call-template name="util:json">
            <with-param name="value" select="$include-value" />
          </call-template>
        </util:value>
      </if>
    </with-param>
  </call-template>
</template>


<template name="wc:var-to-hstr">
  <param name="var" />

  <!-- string separators (TODO: make configurable) -->
  <variable name="pre" select="substring-before( $var, '4' )" />

  <choose>
    <when test="not( $pre = '' )">
      <!-- before separator -->
      <call-template name="wc:var-to-hstr">
        <with-param name="var" select="$pre" />
      </call-template>

      <text> for </text>

      <!-- after -->
      <call-template name="wc:var-to-hstr">
        <with-param name="var" select="substring-after( $var, '4' )" />
      </call-template>
    </when>

    <!-- no separator; continue -->
    <otherwise>
      <call-template name="wc:_var-to-hstr">
        <with-param name="var" select="$var" />
      </call-template>
    </otherwise>
  </choose>
</template>

<!-- var to human-readable string -->
<template name="wc:_var-to-hstr">
  <param name="var" />

  <!-- start by grabbing the prefix -->
  <variable name="prefix">
    <call-template name="wc:str-until-uc">
      <with-param name="str" select="$var" />
    </call-template>
  </variable>

  <!-- and grab the rest of the string after the prefix -->
  <variable name="remain" select="substring-after( $var, $prefix )" />

  <!-- convert the first char to lowercase so that we do not screw up the uc
       prefix substr on the next call -->
  <variable name="remain-recurse" select="
      concat(
        translate( substring( $remain, 1, 1 ), $wc:uc, $wc:lc ),
        substring( $remain, 2 )
      )
    " />

  <variable name="prelex" select="$wc:hlex//wc:lex[ @prefix=$prefix ]" />

  <choose>
    <when test="$prelex">
      <if test="not( $remain-recurse = '' )">
        <call-template name="wc:var-to-hstr">
          <with-param name="var" select="$remain-recurse" />
        </call-template>
      </if>

      <text> </text>
      <value-of select="$prelex/@to" />
    </when>

    <!-- no knowledge of prefix; output it and then recurse -->
    <otherwise>
      <variable name="strlex" select="$wc:hlex//wc:lex[ @str=$prefix ]" />

      <choose>
        <!-- if we recognize this text as a lexeme to be replaced, then do so
             -->
        <when test="$strlex">
          <value-of select="$strlex/@to" />
        </when>

        <!-- just output the text as-is -->
        <otherwise>
          <!-- ucfirst -->
          <value-of select="
              concat(
                translate( substring( $prefix, 1, 1 ), $wc:lc, $wc:uc ),
                substring( $prefix, 2 )
              )
            " />
        </otherwise>
      </choose>

      <if test="not( $remain-recurse = '' )">
        <text> </text>

        <call-template name="wc:var-to-hstr">
          <with-param name="var" select="$remain-recurse" />
        </call-template>
      </if>
    </otherwise>
  </choose>
</template>

<!-- get string prefix until reaching a upper-case char -->
<template name="wc:str-until-uc">
  <param name="str" />

  <variable name="char" select="substring( $str, 1, 1 )" />

  <choose>
    <when test="$str = ''">
      <!-- done; nothing else to do -->
    </when>

    <!-- did we find an upper-case char? -->
    <when test="translate( $char, $wc:uc, '' ) = ''">
      <!-- we're done; do nothing and do not output -->
    </when>

    <otherwise>
      <!-- output the char and recurse -->
      <value-of select="$char" />

      <call-template name="wc:str-until-uc">
        <with-param name="str" select="substring( $str, 2 )" />
      </call-template>
    </otherwise>
  </choose>
</template>

</stylesheet>