Archive for December 2011
invariant invariant update automation
In my everyday job, I often solve the following problem. Due to my contract, I cannot tell you what the exact task is, so I abstracted away the details with the help of category theory.
Let the objects e, i, w be given along with arrows T:w->w, W:e->w, I:e->i. The task is to find an arrow U for which the following diagrams commute:
Let me just name the objects and arrows with real names:
- U – update
- I – invariant projection
- W – window projection
- T – transformation
- w – window
- i – invariant
- e – entity
In other words, I write small automation scripts (U) to update entities represented in quite complex (and distributed) ways, at the same time respecting their consistency and other extra constraints. The task specification (T) usually operates with only a projection/subspace of the real entity space.
check set inclusion with XPath
In the XPath specification, Booleans section, you can read the following sentence about the = operator (in tests):
“If both objects to be compared are node-sets, then the comparison will be true if and only if there is a node in the first node-set and a node in the second node-set such that the result of performing the comparison on the string-values of the two nodes is true. [...]“
Hence you can test for an object being a member of a set of objects:
$ cat data.xml
<root>
<nodes>
<node>3</node>
<node>8</node>
<node>9</node>
<node>2</node>
</nodes>
<ptrs>
<ptr>1</ptr>
<ptr>3</ptr>
</ptrs>
</root>
$ cat selected.xsl
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml"/>
<xsl:template match="//root">
<nodes>
<xsl:for-each select="//root/nodes/node">
<selected>
<xsl:value-of select="position() = //root/ptrs/ptr/text()" />
</selected>
</xsl:for-each>
</nodes>
</xsl:template>
</xsl:stylesheet>
$ xsltproc selected.xsl data.xml | xmllint --format -
<?xml version="1.0"?>
<nodes>
<selected>true</selected>
<selected>false</selected>
<selected>true</selected>
<selected>false</selected>
</nodes>
$
