Project

Profile

Help

Support #5124 » select-trains-and-movements-(proxy).xslt

Johan Gheys, 2021-10-13 10:11

 
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:cern="http://www.infrabel.be/Artemis/Timetable"
xmlns:shared="urn:shared-functions"
xmlns:local="urn:local-functions"
version="2.0">
<xsl:import href="/shared/shared-functions.xslt"/>
<xsl:param name="output-folder" as="xs:string"/>
<xsl:param name="input-file" as="xs:string"/>
<xsl:param name="output-file" as="xs:string"/>
<xsl:param name="selection" as="xs:string"/>
<xsl:strip-space elements="*"/>
<xsl:variable name="runXML" select="shared:open-file($output-folder, 'run.xml')"/>
<xsl:variable name="runs" select="$runXML/runs"/>
<xsl:variable name="creationDate" select="$runs/@creationDate"/>
<xsl:variable name="timetable-folder" select="concat($runs/@input-folder, 'timetable/')"/>
<xsl:variable name="formation-folder" select="concat($runs/@input-folder, 'formation/')"/>
<xsl:key name="by-id" match="*" use="@id"/>
<xsl:key name="by-PAV-trainNo-PAVdiscriminator" match="*" use="concat(@PAV, $separator, @trainNo, $separator, @PAVdiscriminator)"/>
<!-- *********************************************************************************************************** -->
<xsl:template name="xsl:initial-template" match="/">
<xsl:for-each select="$runs/run">
<xsl:sequence select="shared:log('DEBUG', concat('SelectionDate = ', @selectionDate, ' and selectionLayer = ', @selectionLayer))"/>
<xsl:variable name="trainXML" select="shared:open-file(concat($output-folder, @folder-name, $input-file))"/>
<xsl:variable name="fileName" select="shared:path-to-uri(concat($output-folder, @folder-name, $output-file))"/>
<xsl:result-document href="{$fileName}" method="xml">
<xsl:apply-templates select="$trainXML/trains">
<xsl:with-param name="selectionLayer" select="@selectionLayer"/>
</xsl:apply-templates>
</xsl:result-document>
</xsl:for-each>
</xsl:template>
<!-- *********************************************************************************************************** -->
<xsl:template match="trains" as="element(trains)">
<xsl:param name="selectionLayer" as="xs:string"/>
<xsl:variable name="pointIdf" select="//inPointIdf, //outPointIdf"/>
<xsl:variable name="trainXML">
<xsl:sequence select="//train"/>
</xsl:variable>
<xsl:variable name="globalProxyXML">
<xsl:sequence select="local:getGlobalProxys($pointIdf, $trainXML)"/>
</xsl:variable>
<xsl:variable name="localProxyXML">
<xsl:sequence select="local:getLocalProxys($pointIdf, $trainXML)"/>
</xsl:variable>
<xsl:copy>
<xsl:sequence select="@*"/>
<xsl:apply-templates>
<xsl:with-param name="globalProxyXML" select="$globalProxyXML"/>
<xsl:with-param name="localProxyXML" select="$localProxyXML"/>
<xsl:with-param name="selectionLayer" select="$selectionLayer"/>
</xsl:apply-templates>
</xsl:copy>
</xsl:template>
<!-- *********************************************************************************************************** -->
<xsl:template match="train" as="element(train)">
<xsl:param name="globalProxyXML" as="document-node()"/>
<xsl:param name="localProxyXML" as="document-node()"/>
<xsl:param name="selectionLayer" as="xs:string"/>
<xsl:variable name="globalProxies"
select="key('by-PAV-trainNo-PAVdiscriminator', concat(@PAV, $separator, @trainNo, $separator, @PAVdiscriminator), $globalProxyXML)"/>
<xsl:variable name="localProxies"
select="key('by-PAV-trainNo-PAVdiscriminator', concat(@PAV, $separator, @trainNo, $separator, @PAVdiscriminator), $localProxyXML)"/>
<xsl:choose>
<xsl:when test="empty($globalProxies) and empty($localProxies)">
<xsl:sequence select="."/>
</xsl:when>
<xsl:when test="exists($globalProxies) and empty($localProxies)">
<xsl:apply-templates select="." mode="global">
<xsl:with-param name="proxies" select="$globalProxies"/>
<xsl:with-param name="selectionLayer" select="$selectionLayer"/>
</xsl:apply-templates>
</xsl:when>
<xsl:when test="empty($globalProxies) and exists($localProxies)">
<xsl:apply-templates select="." mode="local">
<xsl:with-param name="proxies" select="$localProxies"/>
<xsl:with-param name="selectionLayer" select="$selectionLayer"/>
</xsl:apply-templates>
</xsl:when>
</xsl:choose>
</xsl:template>
<!-- *********************************************************************************************************** -->
<xsl:template match="train" mode="global" as="element(train)">
<xsl:param name="proxies" as="element()*"/>
<xsl:param name="selectionLayer" as="xs:string"/>
<xsl:variable name="trainXML" select="shared:open-file($timetable-folder, @href)"/>
<xsl:variable name="gtvFromFile" select="$trainXML/cern:trains/globalTrainVariant"/>
<xsl:variable name="gtv" select="local:createGlobalTrainVariantFromProxy($proxies, $gtvFromFile, $selectionLayer)"/>
<xsl:copy>
<xsl:sequence select="@*"/>
<xsl:for-each-group select="globalTrainVariant" group-by="@id">
<xsl:variable name="gtvId" select="current-grouping-key()"/>
<xsl:for-each select="current-group(), $gtv[@id = $gtvId]">
<xsl:sort select="@plannedDepartureDate"/>
<xsl:sequence select="."/>
</xsl:for-each>
</xsl:for-each-group>
</xsl:copy>
</xsl:template>
<!-- *********************************************************************************************************** -->
<xsl:template match="train" mode="local" as="element(train)">
<xsl:param name="proxies" as="element()*"/>
<xsl:param name="selectionLayer" as="xs:string"/>
<xsl:variable name="trainXML" select="shared:open-file($formation-folder, @href)"/>
<xsl:variable name="movementFromFile" select="$trainXML/cern:movements/movement"/>
<xsl:variable name="movementsFromFileWithPosition" select="local:addPosition($movementFromFile)"/>
<xsl:variable name="movement" select="local:createMovementFromProxy($proxies, $movementsFromFileWithPosition, $selectionLayer)"/>
<xsl:copy>
<xsl:sequence select="@*"/>
<xsl:for-each-group select="movement" group-by="@position">
<xsl:variable name="position" select="current-grouping-key()"/>
<xsl:for-each select="current-group(), $movement[@position = $position]">
<xsl:sort select="@plannedDepartureDate"/>
<xsl:sequence select="."/>
</xsl:for-each>
</xsl:for-each-group>
</xsl:copy>
</xsl:template>
<!-- *********************************************************************************************************** -->
<xsl:function name="local:addPosition" as="element(movement)*">
<xsl:param name="movement" as="element(movement)*"/>
<xsl:for-each select="$movement">
<xsl:sequence select="shared:add-or-update-attributes(., 'position', position())"/>
</xsl:for-each>
</xsl:function>
<!-- *********************************************************************************************************** -->
<xsl:function name="local:createGlobalTrainVariantFromProxy" as="element(globalTrainVariant)*">
<xsl:param name="proxy" as="element()*"/>
<xsl:param name="globalTrainVariantFromFile" as="element(globalTrainVariant)*"/>
<xsl:param name="selectionLayer" as="xs:string"/>
<xsl:for-each select="$proxy">
<xsl:variable name="gtvId" select="@trainId"/>
<xsl:variable name="plannedDepartureDate" select="@plannedDepartureDate"/>
<xsl:variable name="gtv" select="$globalTrainVariantFromFile[@id = $gtvId]"/>
<xsl:for-each select="$gtv">
<xsl:variable name="currentGtv" select="."/>
<xsl:variable name="departDay" as="xs:integer" select="(@departDay, 0)[1]"/>
<xsl:variable name="destDay" as="xs:integer" select="(@destDay, 0)[1]"/>
<xsl:for-each select="$currentGtv">
<xsl:variable name="gtvVectorBit" select="shared:get-vector-bit(vectors/vector, $selectionLayer, $plannedDepartureDate)"/>
<xsl:element name="globalTrainVariant">
<xsl:sequence select="@uuid, @id, @layer, @car, @per, @dateFrom"/>
<xsl:attribute name="departDay" select="$departDay"/>
<xsl:attribute name="dateTo" select="@dateTo"/>
<xsl:attribute name="destDay" select="$destDay"/>
<xsl:attribute name="plannedDepartureDate" select="$plannedDepartureDate"/>
<xsl:attribute name="resultBit" select="$gtvVectorBit"/>
<xsl:attribute name="selection" select="$selection"/>
<xsl:for-each select="train">
<xsl:variable name="ltvVectorBit" select="shared:get-vector-bit(vectors/vector, $selectionLayer, $plannedDepartureDate)"/>
<xsl:if test="$ltvVectorBit ne 0">
<xsl:element name="localTrainVariant">
<xsl:sequence select="@uuid, @layer, @dateFrom, @priority"/>
<xsl:attribute name="departDay" select="$departDay"/>
<xsl:attribute name="dateTo" select="@dateTo"/>
<xsl:attribute name="destDay" select="$destDay"/>
<xsl:attribute name="plannedDepartureDate" select="$plannedDepartureDate"/>
<xsl:attribute name="resultBit" select="$ltvVectorBit"/>
<xsl:attribute name="selection" select="$selection"/>
</xsl:element>
</xsl:if>
</xsl:for-each>
</xsl:element>
</xsl:for-each>
</xsl:for-each>
</xsl:for-each>
</xsl:function>
<!-- *********************************************************************************************************** -->
<xsl:function name="local:createMovementFromProxy" as="element(movement)*">
<xsl:param name="proxy" as="element()*"/>
<xsl:param name="movementFromFile" as="element(movement)*"/>
<xsl:param name="selectionLayer" as="xs:string"/>
<xsl:for-each select="$proxy">
<xsl:variable name="movementId" select="@movementId"/>
<xsl:variable name="plannedDepartureDate" select="@plannedDepartureDate"/>
<xsl:variable name="movement" select="$movementFromFile[@id = $movementId]"/>
<xsl:for-each select="$movement">
<xsl:variable name="movementVectorBit" select="shared:get-vector-bit(vectors/vector, $selectionLayer, $plannedDepartureDate)"/>
<xsl:if test="$movementVectorBit ne 0">
<xsl:element name="movement">
<xsl:sequence select="@position, @id"/>
<xsl:attribute name="type" select="local:getMovementType(.)"/>
<xsl:attribute name="plannedDepartureDate" select="$plannedDepartureDate"/>
<xsl:attribute name="resultBit" select="$movementVectorBit"/>
<xsl:attribute name="selection" select="$selection"/>
</xsl:element>
</xsl:if>
</xsl:for-each>
</xsl:for-each>
</xsl:function>
<!-- *********************************************************************************************************** -->
<xsl:function name="local:getGlobalProxys" as="element()*">
<xsl:param name="pointIdf" as="element()*"/>
<xsl:param name="trainXML" as="document-node()*"/>
<xsl:for-each-group select="$pointIdf" group-by="@trainId">
<xsl:variable name="gtvId" select="current-grouping-key()"/>
<xsl:variable name="gtvs" select="key('by-id', $gtvId, $trainXML)[local-name() = 'globalTrainVariant']"/>
<xsl:if test="empty($gtvs)">
<xsl:for-each select="current-group()">
<xsl:sort select="../@href"/>
<xsl:sequence select="shared:log('DEBUG', concat('Warning: ', ../@href, ': No out globalTrainVariant found with id = ', $gtvId))"/>
</xsl:for-each>
</xsl:if>
<xsl:for-each-group select="current-group()" group-by="@plannedDepartureDate">
<xsl:variable name="plannedDepartureDate" select="current-grouping-key()"/>
<xsl:if test="empty($gtvs[@plannedDepartureDate = $plannedDepartureDate])">
<xsl:sequence select="current-group()[1]"/>
</xsl:if>
</xsl:for-each-group>
</xsl:for-each-group>
</xsl:function>
<!-- *********************************************************************************************************** -->
<xsl:function name="local:getLocalProxys" as="element()*">
<xsl:param name="pointIdf" as="element()*"/>
<xsl:param name="trainXML" as="document-node()"/>
<xsl:for-each-group select="$pointIdf" group-by="@movementId">
<xsl:variable name="movementId" select="current-grouping-key()"/>
<xsl:variable name="movements" select="key('by-id', $movementId, $trainXML)[local-name() = 'movement']"/>
<xsl:if test="empty($movements)">
<xsl:for-each select="current-group()">
<xsl:sort select="../@href"/>
<xsl:sequence select="shared:log('DEBUG', concat('Warning: ', ../@href, ': No out movement found with id = ', $movementId))"/>
</xsl:for-each>
</xsl:if>
<xsl:for-each-group select="current-group()" group-by="@plannedDepartureDate">
<xsl:variable name="plannedDepartureDate" select="current-grouping-key()"/>
<xsl:if test="empty($movements[@plannedDepartureDate = $plannedDepartureDate])">
<xsl:sequence select="current-group()[1]"/>
</xsl:if>
</xsl:for-each-group>
</xsl:for-each-group>
</xsl:function>
<!-- *********************************************************************************************************** -->
<xsl:function name="local:getMovementType" as="xs:string">
<xsl:param name="movement" as="element()"/>
<xsl:for-each select="$movement">
<xsl:variable name="movementPoint" select=".//movementPoint"/>
<xsl:choose>
<xsl:when test="@type eq 'F'">
<xsl:sequence select="'formation'"/>
</xsl:when>
<xsl:when test="@type eq 'S'">
<xsl:sequence select="'sorting'"/>
</xsl:when>
<xsl:when test="$movementPoint[1]/@ptdesId eq $movementPoint[last()]/@ptdesId">
<xsl:sequence select="'shunting'"/>
</xsl:when>
<xsl:otherwise>
<xsl:sequence select="'transfer'"/>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each>
</xsl:function>
<!-- *********************************************************************************************************** -->
</xsl:stylesheet>
(3-3/3)