Project

Profile

Help

RE: Iter Versus Call Template; Byte-Code performance & Me... » Digraph_public_I_modified_iterate.xsl

David Rudel, 2013-12-31 08:31

 
<?xml version="1.0" encoding="UTF-8" ?>

<xsl:stylesheet version="3.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:udf="user-defined" xmlns:chain="chain"
xmlns:fn="http://www.w3.org/2005/xpath-functions" xmlns:xdt="http://www.w3.org/2005/xpath-datatypes" xmlns:err="http://www.w3.org/2005/xqt-errors"
xmlns:math="http://exslt.org/math" xmlns:saxon="http://saxon.sf.net/" xmlns:exsl="http://exslt.org/common" xmlns:random="http://exslt.org/random"
xmlns:date="http://exslt.org/dates-and-times" xmlns:map="http://www.w3.org/2005/xpath-functions/map"
exclude-result-prefixes="xs xdt udf saxon math fn err exsl date map chain random">

<xsl:key name="connection.key" match="//target" use="concat(../@x,'--',@y)"/>

<xsl:variable name="max.iterations" select="15"/>
<xsl:variable name="path" select="replace(base-uri(),'(.*/)[^/]+?.xml','$1')"/>

<!--
TG1.file and TG2.file are filenames of optional transgraphs used in the processing. These files are not needed and when absent the script
will bootstrap based on the current graph values.-->

<xsl:template match="/">
<xsl:variable name="graph">
<xsl:copy-of select="graph/from"/>
</xsl:variable>

<xsl:result-document href="{concat($path,'DigraphI_maps_iterate.xml')}">
<digraph>

<xsl:iterate select="1 to $max.iterations">
<xsl:param name="input" select="$graph"/>

<!-- <a round="{current()}">
<xsl:copy-of select="$input"/>
</a>-->

<xsl:variable name="input.map" as="map(*)">
<xsl:map>
<xsl:for-each select="$input//target">
<xsl:map-entry key="concat(../@x,'--',@y)" select="."/>
</xsl:for-each>
</xsl:map>
</xsl:variable>

<!--We determine cases where one edge loses points to other edges. Each edge is considered and
one t element i made for each other edge that will take from it. Then a t element is made to represent the overall loss to the
edge under review.-->

<xsl:variable name="mid.iteration">
<!--
If the input transgraph files exist, we use them, otherwise we use the current digraph itself.
This allows bootstrapping from a fresh input initial digraph when no transgraphs are known.
-->

<xsl:for-each select="$input/from">
<xsl:variable name="x" select="@x"/>
<xsl:variable name="targets" select="target"/>
<xsl:copy>
<xsl:copy-of select="@*"/>
<xsl:for-each select="target">

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

<xsl:choose>
<xsl:when test="@edge.score=0">
<t y="{$y}"/>
</xsl:when>
<xsl:otherwise>
<!--<t y="{$y}" d="{sum(for $m in $targets[@y ne $y][+@edge.score gt 0] return (if(+$m/@edge.score gt +current()/@edge.score) then 0.1 * (current()/@edge.score - $m/@edge.score) else 0))}"/>
-->
<xsl:variable name="to" select="current()"/>

<!-- For connections with non-zero strength, we look through all the possible intermediary points and see if any
are strong enough to take some strength away from the from-to pair under consideration.-->

<xsl:iterate select="$targets[@y ne $y][+@edge.score gt 0]">
<xsl:param name="total.transfer" select="0" as="xs:double"/>

<xsl:choose>
<xsl:when test="(+@edge.score gt +$to/@edge.score)">
<xsl:variable name="local.trans" select="0.1 * (@edge.score - $to/@edge.score)"/>

<xsl:next-iteration>
<xsl:with-param name="total.transfer" select="$total.transfer + $local.trans"/>
</xsl:next-iteration>

</xsl:when>
</xsl:choose>
<xsl:on-completion>

<!-- N.B. Here we put the total score lost to the edge in question. Note that @y is
the edge under review rather than the individual candidate edges that robbed points
from that edge.-->

<xsl:text>
</xsl:text>
<t y="{$to/@y}" d="{$total.transfer}"/>
</xsl:on-completion>
</xsl:iterate>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each>
</xsl:copy>
</xsl:for-each>
</xsl:variable>

<!-- We adjust the edges and create a new graph for the next iteration.
-->
<xsl:variable name="next">

<xsl:for-each select="$mid.iteration/from">
<xsl:text>
</xsl:text>
<xsl:copy>
<xsl:copy-of select="@*"/>
<xsl:variable name="x" select="@x"/>
<xsl:for-each-group select="t" group-by="@y">
<xsl:text>
</xsl:text>
<xsl:variable name="y" select="current-grouping-key()"/>
<xsl:variable name="connection" select="$input.map(concat($x,'--',$y))"/>

<target y="{$y}" edge.score="{$connection/@edge.score - sum(current-group()/@d)}"/>
</xsl:for-each-group>
</xsl:copy>
</xsl:for-each>
</xsl:variable>

<xsl:if test="current() = $max.iterations">
<xsl:copy-of select="$next"/>
</xsl:if>

<xsl:next-iteration>
<xsl:with-param name="input" select="$next"/>
</xsl:next-iteration>
</xsl:iterate>
</digraph>
</xsl:result-document>

</xsl:template>

</xsl:stylesheet>
(2-2/2)