Mike Gerwitz

Activist for User Freedom

aboutsummaryrefslogtreecommitdiffstats
blob: cf71c670bddcd5ff86a5b6209e6820702f451c00 (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
<?xml version="1.0" encoding="ISO-8859-1"?>
<!--
  No, not "odorizer".

  Copyright (C) 2016 LoVullo Associates, Inc.

    This file is part of TAME.

    TAME is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see
    <http://www.gnu.org/licenses/>.

  This is a powerful system that takes an immensely complex (and insurmountable)
  task out of the programmer's hands. In particular, the system:

    - Uses the map to recognize the order in which params appear in a UI
    - Using that order, determines which fields in the UI could potentially
      cause fields that appear previous to them to become invalid (e.g. hide)
      due to classification criteria.
    - Based on those conflicts, constructs a custom classification that ignores
      only the conflicting portions, allowing a great deal of precision in
      controlling field validity up to that particular point in the UI.

  The result is highly-refined, custom-generated classifications per-question
  for only the criteria that are needed to ensure that fields cannot hide fields
  previous to them: A task that would otherwise be prohibitively complicated (or
  would otherwise have to be far too coarse) if done manually in systems with
  highly sophisticated classification schemes. Furthermore, the result is wholly
  deterministic.

  It should be noted that futher benefit can be realized when the map is altered
  or questions in the UI are reordered; the system will simply re-calculate new
  classifications that yield desirable results.
-->
<xsl:stylesheet version="2.0"
  xmlns="http://www.w3.org/1999/xhtml"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:lv="http://www.lovullo.com/rater"
  xmlns:lvp="http://www.lovullo.com"
  xmlns:lvm="http://www.lovullo.com/rater/map"
  xmlns:lvmc="http://www.lovullo.com/rater/map/compiler"
  xmlns:gc="http://www.lovullo.com/calc/global-classifier"
  xmlns:c="http://www.lovullo.com/calc"
  xmlns:o="http://www.lovullo.com/rater/compiler/orderizer"
  xmlns:preproc="http://www.lovullo.com/rater/preproc">



<!--
  Determines param conflicts based on UI ordering
-->
<xsl:template name="o:analyze">
  <xsl:param name="class-deps" />
  <xsl:param name="ui" />
  <xsl:param name="sdmap" />
  <xsl:param name="param-classes" />

  <xsl:variable name="rater" select="." />

  <!-- put fields in the order that they appear in the UI -->
  <!-- TODO: duplicate check (error) -->
  <xsl:variable name="ordered">
    <o:ordered>
      <xsl:for-each select="$ui//lvp:question">
        <xsl:variable name="id" select="@id" />
        <xsl:copy-of select="$sdmap/lvmc:map[ @from=$id ]" />
      </xsl:for-each>
    </o:ordered>
  </xsl:variable>

  <!-- the param-class list gives us the classes that are directly used; let's
       get a list of all classes that are used by those classes as well, which
       will simplify the queries to come -->
  <xsl:message>[orderizer] recursively discovering param classes...</xsl:message>
  <xsl:variable name="param-classes-expanded">
    <xsl:apply-templates select="$param-classes" mode="o:gen-param-reflist">
      <xsl:with-param name="class-deps" select="$class-deps" />
    </xsl:apply-templates>
  </xsl:variable>

  <xsl:variable name="initial">
    <o:analysis>
      <xsl:for-each select="$ordered//lvmc:*">
        <xsl:variable name="cur" select="." />
        <xsl:variable name="pref" select="@to" />

        <!-- progress indicator -->
        <xsl:message>
          <xsl:text>[orderizer] checking previous UI siblings: </xsl:text>
          <xsl:value-of select="@to" />
        </xsl:message>

        <!-- preceding -->
        <xsl:for-each select="
          $param-classes/gc:param[
            @ref=$cur/preceding-sibling::lvmc:map/@to
          ]">

          <!-- immediate conflicts (we check these separately rather than in the
               xpath above, which would be cleaner, so that they can be
               processed -->
          <xsl:variable name="conflicts" select="
              .//gc:class[
                @ref=$param-classes-expanded/o:param-refs/o:param[
                  @ref=$pref
                ]/o:class/@ref
              ]
            " />

          <xsl:if test="$conflicts">
            <o:conflict ref="{@ref}" relative-to="{$pref}">
              <xsl:for-each select="$conflicts">
                <!-- record the immediate problem -->
                <o:class ref="{@ref}" yields="{@yields}" />
              </xsl:for-each>
            </o:conflict>
          </xsl:if>
        </xsl:for-each>
      </xsl:for-each>
    </o:analysis>
  </xsl:variable>

  <xsl:apply-templates select="$initial/o:analysis" mode="o:reduce-analysis" />
</xsl:template>


<xsl:template match="gc:param-classes" mode="o:gen-param-reflist" priority="5">
  <xsl:param name="class-deps" />

  <o:param-refs>
    <xsl:apply-templates select="gc:param" mode="o:gen-param-reflist">
      <xsl:with-param name="class-deps" select="$class-deps" />
    </xsl:apply-templates>
  </o:param-refs>
</xsl:template>

<xsl:template match="gc:param" mode="o:gen-param-reflist" priority="5">
  <xsl:param name="class-deps" />

  <o:param ref="{@ref}">
    <xsl:apply-templates select="gc:class" mode="o:gen-param-reflist">
      <xsl:with-param name="class-deps" select="$class-deps" />
    </xsl:apply-templates>
  </o:param>
</xsl:template>

<xsl:template match="gc:class" mode="o:gen-param-reflist" priority="5">
  <xsl:param name="class-deps" />

  <!-- well, this class is certainly used -->
  <o:class ref="{@ref}" />

  <xsl:variable name="ref" select="@ref" />

  <!-- but how about things that use this class? -->
  <xsl:apply-templates select="$class-deps/preproc:class[ @ref=$ref ]"
    mode="o:gen-param-reflist">
    <xsl:with-param name="class-deps" select="$class-deps" />
  </xsl:apply-templates>
</xsl:template>

<xsl:template match="preproc:class" mode="o:gen-param-reflist" priority="5">
  <xsl:param name="class-deps" />

  <o:class ref="{@ref}" />

  <xsl:apply-templates mode="o:gen-param-reflist">
    <xsl:with-param name="class-deps" select="$class-deps" />
  </xsl:apply-templates>
</xsl:template>

<xsl:template match="*" mode="o:gen-param-reflist" priority="1">
  <!-- do nothing -->
</xsl:template>


<xsl:template match="preproc:class" mode="o:get-indirect-params" priority="5">
  <xsl:apply-templates mode="o:get-indirect-params" />
</xsl:template>

<xsl:template match="preproc:class-dep" mode="o:get-indirect-params" priority="5">
  <xsl:variable name="ref" select="@ref" />

  <xsl:apply-templates
    select="ancestor::preproc:class-deps/preproc:class[ @ref=$ref ]"
    mode="o:get-indirect-params" />
</xsl:template>

<xsl:template match="preproc:rate-dep" mode="o:get-indirect-params" priority="5">
  <xsl:variable name="ref" select="@ref" />

  <xsl:apply-templates
    select="//preproc:rate-deps/preproc:rate[ @ref=$ref ]"
    mode="o:get-indirect-params" />
</xsl:template>

<xsl:template match="preproc:param" mode="o:get-indirect-params" priority="5">
  <xsl:copy-of select="." />
</xsl:template>

<xsl:template match="*" mode="o:get-indirect-params" priority="1">
  <!-- do nothing -->
</xsl:template>


<!--
  Combines initial analysis results, merging common refs and de-duplicating
  conflicts.
-->
<xsl:template match="o:analysis" mode="o:reduce-analysis">
  <!-- start by combining the contents of all unique refs -->
  <xsl:variable name="combined">
    <xsl:for-each select="
        o:conflict[
          not( @ref=preceding-sibling::o:conflict/@ref )
        ]
      ">

      <xsl:variable name="ref" select="@ref" />
      <xsl:variable name="dups" select="
          following-sibling::o:conflict[ @ref=$ref ]
        " />

      <o:conflict ref="{@ref}">
        <!-- add relativity nodes -->
        <xsl:for-each select="$dups|.">
          <o:relative-to ref="{@relative-to}" />
        </xsl:for-each>

        <!-- add class deps -->
        <o:reasons>
          <xsl:copy-of select="($dups|.)/o:class" />
        </o:reasons>
      </o:conflict>
    </xsl:for-each>
  </xsl:variable>

  <!-- now, remove duplicates resulting from the above operation -->
  <xsl:copy>
    <xsl:copy-of select="@*" />

    <xsl:apply-templates select="$combined/o:conflict"
      mode="o:reduce-analysis" />
  </xsl:copy>
</xsl:template>

<xsl:template match="o:conflict" mode="o:reduce-analysis">
  <xsl:copy>
    <xsl:copy-of select="@*" />

    <!-- unique relative-to nodes -->
    <xsl:copy-of select="
        o:relative-to[
          not( @ref=preceding-sibling::o:relative-to/@ref )
        ]
      " />

    <xsl:apply-templates select="o:reasons" mode="o:reduce-analysis" />
  </xsl:copy>
</xsl:template>

<xsl:template match="o:reasons" mode="o:reduce-analysis">
  <xsl:copy>
    <xsl:copy-of select="@*" />

    <!-- unique reasons -->
    <xsl:copy-of select="
        o:class[
          not( @ref=preceding-sibling::o:class/@ref )
        ]
      " />
  </xsl:copy>
</xsl:template>

</xsl:stylesheet>