<?xml version='1.0' encoding='ISO-8859-1'?>

<!--
    Copyright (C) 2009  INRETS / CNRS / PSU


    This file is part of Abstract XML.

    Abstract XML 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 2 of the License, or
    any later version.

    Abstract XML 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/>.

-->

<!DOCTYPE xsl:transform [
	 <!ENTITY rdf 'http://www.w3.org/1999/02/22-rdf-syntax-ns#'>
	 <!ENTITY rdfs 'http://www.w3.org/2000/01/rdf-schema#'>
	 <!ENTITY kb 'http://protege.stanford.edu/kb#'>
	 <!ENTITY a 'http://protege.stanford.edu/system#'>
   	 <!ENTITY swivt 'http://semantic-mediawiki.org/swivt/1.0#'>
	 <!ENTITY property 'http://acs.ist.psu.edu/wiki/index.php/Special:URIResolver/Property-3A'>
]>


<!-- ================================================================ 
TraceMetier2svg.xsl

ACTION:  This XSLT file transform the RDF trace into a SVG graph

INPUT:   begin :   Begin date in seconds
         end :     End date in seconds

APPLY TO:  MyTrace.rdf :   Trace in RDF format

GENERATE:  MyVisualization.svg  :  Graphical visualization of the trace in SVG format
=====================================================================   -->


<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"
	xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
	xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#"
	xmlns:kb="http://protege.stanford.edu/kb#"
	xmlns:a="http://protege.stanford.edu/system#"
    xmlns:swivt="http://semantic-mediawiki.org/swivt/1.0#"
    xmlns:property="http://acs.ist.psu.edu/wiki/index.php/Special:URIResolver/Property-3A"
    xmlns:xlink="http://www.w3.org/1999/xlink">
<xsl:output method="xml" indent="yes"/>
<xsl:param name="defaultComponentId" select="string('v11')"/>
<xsl:param name="begin" />
<xsl:param name="end" />
<xsl:param name="timeScale" select="1000 div ($end - $begin)"/> 
<xsl:param name="beginOffset" select="0"/> 
<xsl:param name="centerOffset" select="0"/> 
<xsl:param name="docrdfs" select="string('http://acs.ist.psu.edu/wiki/index.php?title=Special:ExportRDF&amp;page=Activity_Modeling&amp;recursive=1')" /> <!--   -->
<xsl:variable name="mcdoc" select="document($docrdfs)"/>

<!-- Match the XML root of the RDF file -->

<xsl:template match="rdf:RDF"> <!-- [rdf:Description]"> -->
	<svg xmlns="http://www.w3.org/2000/svg"
		 viewbox="0 0 1000 250" width="1000" height="250">
		<rect x="0" y="0" rx="10" ry="10" width="1000" height="250" stroke="#303030" fill="#FFFFFF" />
		<!-- Horizontal axis-->
		<polyline stroke="#B0B0B0" points="0,120 1000,120" stroke-dasharray="32,16,8,16" />
		<!-- Red vertical cursor -->
		<polyline stroke="#FF0000" points="{$centerOffset},0 {$centerOffset},250" id="{$defaultComponentId}svgRedLine" transform="" stroke-opacity="0.5" />
		<!-- <xsl:variable name="typesToDisplay" select="$mcdoc//swivt:Subject"/> -->
		<!-- The trace -->
		<g id="{$defaultComponentId}g" transform="translate(0,0)" stroke="#444444" stroke-width="1">
			<xsl:apply-templates select="rdf:Description[number(kb:date)&gt;=number($begin) and number(kb:date)&lt;=number($end)]">
			<!--	<xsl:sort select="kb:date" data-type="number" /> -->
			</xsl:apply-templates>
		</g>
	</svg>
</xsl:template>

<!-- Match the event having a date value  -->

<xsl:template match="rdf:Description"> <!-- le champ kb:date doit exister sinon on obtient un graphe bizarre -->
    <xsl:variable name="VarType" select="rdf:type/@rdf:resource"/>
    <xsl:variable name="VarPerpetrator" select="kb:PERPETRATOR"/>
    <xsl:variable name="VarAttack" select="kb:ATTACK"/>
    <xsl:variable name="varLevel">
        <xsl:choose>
            <xsl:when test="$mcdoc//swivt:Subject[rdfs:label=$VarPerpetrator]/property:Visu_level">
				<xsl:value-of select="- $mcdoc//swivt:Subject[rdfs:label=$VarPerpetrator]/property:Visu_level" />
            </xsl:when>
            <xsl:otherwise>
				<xsl:text>100</xsl:text>
            </xsl:otherwise>
        </xsl:choose>        
    </xsl:variable>     
	<xsl:variable name="visu_color">
		<xsl:choose>
			<xsl:when test="$mcdoc//swivt:Subject[substring(@rdf:about,59)=$VarType]/property:Visu_color">
				<xsl:value-of select="$mcdoc//swivt:Subject[substring(@rdf:about,59)=$VarType]/property:Visu_color" />
			</xsl:when>
			<xsl:otherwise>
				<xsl:text>gray</xsl:text>
			</xsl:otherwise>
		</xsl:choose>        
	</xsl:variable>     
	<xsl:variable name="icon">
		<xsl:choose>
			<xsl:when test="$mcdoc//swivt:Subject[substring(@rdf:about,59)=$VarType]/property:Visu_icon">
				<xsl:value-of select="$mcdoc//swivt:Subject[substring(@rdf:about,59)=$VarType]/property:Visu_icon" />
			</xsl:when>
			<xsl:otherwise>
				<xsl:text>none</xsl:text>
			</xsl:otherwise>
		</xsl:choose>        
	</xsl:variable>     
	<xsl:variable name="icon2">
		<xsl:choose>
			<xsl:when test="$mcdoc//swivt:Subject[substring(@rdf:about,59)=$VarAttack]/property:Visu_icon">
				<xsl:value-of select="$mcdoc//swivt:Subject[substring(@rdf:about,59)=$VarAttack]/property:Visu_icon" />
			</xsl:when>
			<xsl:otherwise>
				<xsl:text>none</xsl:text>
			</xsl:otherwise>
		</xsl:choose>        
	</xsl:variable>     
	<xsl:variable name="currentShape" select="$mcdoc//swivt:Subject[substring(@rdf:about,59)=$VarType]/property:Visu_shape"/>  
	<xsl:apply-templates select="kb:inferred|kb:begin|kb:end" mode="connexion">
	    <xsl:with-param name="varLeveli" select="$varLevel" />
	</xsl:apply-templates>
	<!-- Icon2 -->
    <xsl:choose>
		<xsl:when test="$icon2!='none'">
			<image width="16" height="16">
				<xsl:attribute name="x">
					<xsl:value-of select="kb:date*$timeScale - $beginOffset * $timeScale - 4 + $centerOffset"/>
				</xsl:attribute>
				<xsl:attribute name="y">
					<xsl:value-of select="$varLevel+116"/>
				</xsl:attribute>
				<xsl:attribute name="xlink:href">
					<xsl:value-of select="$icon2"/>
				</xsl:attribute>
			</image>
		</xsl:when>
	</xsl:choose>
    <xsl:choose>
  		<!-- Icon -->
		<xsl:when test="$icon!='none'">
			<image width="16" height="16">
				<xsl:attribute name="x">
					<xsl:value-of select="kb:date*$timeScale - $beginOffset * $timeScale - 8 + $centerOffset"/>
				</xsl:attribute>
				<xsl:attribute name="y">
					<xsl:value-of select="$varLevel+112"/>
				</xsl:attribute>
				<xsl:attribute name="xlink:href">
					<xsl:value-of select="$icon"/>
				</xsl:attribute>
			</image>
			<polygon id="{$defaultComponentId}idNode{@rdf:about}" style="fill-opacity:0;stroke-opacity:0;stroke:#ffffff">
				<!-- Set the attributes of the observés -->
				<xsl:attribute name="points">
					<xsl:value-of select="kb:date*$timeScale - 8 - $beginOffset * $timeScale + $centerOffset"/><xsl:text>,</xsl:text>
					<xsl:value-of select="$varLevel+112"/><xsl:text> </xsl:text>
					<xsl:value-of select="kb:date*$timeScale + 8 - $beginOffset * $timeScale + $centerOffset"/><xsl:text>,</xsl:text>
					<xsl:value-of select="$varLevel+112"/><xsl:text> </xsl:text>
					<xsl:value-of select="kb:date*$timeScale + 8 - $beginOffset * $timeScale + $centerOffset"/><xsl:text>, </xsl:text>
					<xsl:value-of select="$varLevel+128"/><xsl:text> </xsl:text>
					<xsl:value-of select="kb:date*$timeScale - 8 - $beginOffset * $timeScale + $centerOffset"/><xsl:text>, </xsl:text>
					<xsl:value-of select="$varLevel+128"/><xsl:text> </xsl:text>
				</xsl:attribute>
				<xsl:attribute name="class">
					<xsl:call-template name="getClass">
						<xsl:with-param name="currentClass" select="rdf:type/@rdf:resource" />
					</xsl:call-template>
				</xsl:attribute>
			</polygon>
        </xsl:when>
  		<!-- Line -->
		<xsl:when test="$currentShape='line' and $icon='none'">
			<polygon id="{$defaultComponentId}idNode{@rdf:about}">
				<!-- Set the attributes of the observés -->
				<xsl:attribute name="points">
					<xsl:value-of select="kb:date*$timeScale - $beginOffset * $timeScale + $centerOffset"/><xsl:text>,</xsl:text>
					<xsl:value-of select="0"/><xsl:text> </xsl:text>
					<xsl:value-of select="kb:date*$timeScale - $beginOffset * $timeScale + $centerOffset"/><xsl:text>,</xsl:text>
					<xsl:value-of select="200"/><xsl:text> </xsl:text>
				</xsl:attribute>
				<xsl:attribute name="class">
					<xsl:call-template name="getClass">
						<xsl:with-param name="currentClass" select="rdf:type/@rdf:resource" />
					</xsl:call-template>
				</xsl:attribute>
			</polygon>
        </xsl:when>
		<!-- Square -->
		<xsl:when test="$currentShape='square' and $icon='none'">
			<polygon id="{$defaultComponentId}idNode{@rdf:about}" style="fill:{$visu_color}">
				<!-- Set the attributes of the observés -->
				<xsl:attribute name="points">
					<xsl:value-of select="kb:date*$timeScale - 6 - $beginOffset * $timeScale + $centerOffset"/><xsl:text>,</xsl:text>
					<xsl:value-of select="$varLevel+114"/><xsl:text> </xsl:text>
					<xsl:value-of select="kb:date*$timeScale + 6 - $beginOffset * $timeScale + $centerOffset"/><xsl:text>,</xsl:text>
					<xsl:value-of select="$varLevel+114"/><xsl:text> </xsl:text>
					<xsl:value-of select="kb:date*$timeScale + 6 - $beginOffset * $timeScale + $centerOffset"/><xsl:text>, </xsl:text>
					<xsl:value-of select="$varLevel+126"/><xsl:text> </xsl:text>
					<xsl:value-of select="kb:date*$timeScale - 6 - $beginOffset * $timeScale + $centerOffset"/><xsl:text>, </xsl:text>
					<xsl:value-of select="$varLevel+126"/><xsl:text> </xsl:text>
				</xsl:attribute>
				<xsl:attribute name="class">
					<xsl:call-template name="getClass">
						<xsl:with-param name="currentClass" select="rdf:type/@rdf:resource" />
					</xsl:call-template>
				</xsl:attribute>
			</polygon>
        </xsl:when>		
        <!-- trangle backward -->
		<xsl:when test="$currentShape='back' and $icon='none'">
			<polygon id="{$defaultComponentId}idNode{@rdf:about}" style="fill:{$visu_color}">
				<!-- Set the attributes of the observés -->
				<xsl:attribute name="points">
					<xsl:value-of select="kb:date*$timeScale - 8 - $beginOffset * $timeScale + $centerOffset"/><xsl:text>,</xsl:text>
					<xsl:value-of select="$varLevel+120"/><xsl:text> </xsl:text>
					<xsl:value-of select="kb:date*$timeScale + 6 - $beginOffset * $timeScale + $centerOffset"/><xsl:text>,</xsl:text>
					<xsl:value-of select="$varLevel+112"/><xsl:text> </xsl:text>
					<xsl:value-of select="kb:date*$timeScale + 6 - $beginOffset * $timeScale + $centerOffset"/><xsl:text>, </xsl:text>
					<xsl:value-of select="$varLevel+128"/><xsl:text> </xsl:text>
				</xsl:attribute>
				<xsl:attribute name="class">
					<xsl:call-template name="getClass">
						<xsl:with-param name="currentClass" select="rdf:type/@rdf:resource" />
					</xsl:call-template>
				</xsl:attribute>
			</polygon>
        </xsl:when>		
        <!-- trangle forward -->
		<xsl:when test="$currentShape='front' and $icon='none'">
			<polygon id="{$defaultComponentId}idNode{@rdf:about}" style="fill:{$visu_color}">
				<!-- Set the attributes of the observés -->
				<xsl:attribute name="points">
					<xsl:value-of select="kb:date*$timeScale - 6 - $beginOffset * $timeScale + $centerOffset"/><xsl:text>,</xsl:text>
					<xsl:value-of select="$varLevel+112"/><xsl:text> </xsl:text>
					<xsl:value-of select="kb:date*$timeScale + 8 - $beginOffset * $timeScale + $centerOffset"/><xsl:text>,</xsl:text>
					<xsl:value-of select="$varLevel+120"/><xsl:text> </xsl:text>
					<xsl:value-of select="kb:date*$timeScale - 6 - $beginOffset * $timeScale + $centerOffset"/><xsl:text>, </xsl:text>
					<xsl:value-of select="$varLevel+128"/><xsl:text> </xsl:text>
				</xsl:attribute>
				<xsl:attribute name="class">
					<xsl:call-template name="getClass">
						<xsl:with-param name="currentClass" select="rdf:type/@rdf:resource" />
					</xsl:call-template>
				</xsl:attribute>
			</polygon>
        </xsl:when>		
        <!-- trangle up -->
		<xsl:when test="$currentShape='up' and $icon='none'">
			<polygon id="{$defaultComponentId}idNode{@rdf:about}" style="fill:{$visu_color}">
				<!-- Set the attributes of the observés -->
				<xsl:attribute name="points">
					<xsl:value-of select="kb:date*$timeScale - 8 - $beginOffset * $timeScale + $centerOffset"/><xsl:text>,</xsl:text>
					<xsl:value-of select="$varLevel+126"/><xsl:text> </xsl:text>
					<xsl:value-of select="kb:date*$timeScale - $beginOffset * $timeScale + $centerOffset"/><xsl:text>,</xsl:text>
					<xsl:value-of select="$varLevel+112"/><xsl:text> </xsl:text>
					<xsl:value-of select="kb:date*$timeScale + 8 - $beginOffset * $timeScale + $centerOffset"/><xsl:text>, </xsl:text>
					<xsl:value-of select="$varLevel+126"/><xsl:text> </xsl:text>
				</xsl:attribute>
				<xsl:attribute name="class">
					<xsl:call-template name="getClass">
						<xsl:with-param name="currentClass" select="rdf:type/@rdf:resource" />
					</xsl:call-template>
				</xsl:attribute>
			</polygon>
        </xsl:when>		
        <!-- trangle down -->
		<xsl:when test="$currentShape='down' and $icon='none'">
			<polygon id="{$defaultComponentId}idNode{@rdf:about}" style="fill:{$visu_color}">
				<!-- Set the attributes of the observés -->
				<xsl:attribute name="points">
					<xsl:value-of select="kb:date*$timeScale - 8 - $beginOffset * $timeScale + $centerOffset"/><xsl:text>,</xsl:text>
					<xsl:value-of select="$varLevel+114"/><xsl:text> </xsl:text>
					<xsl:value-of select="kb:date*$timeScale - $beginOffset * $timeScale + $centerOffset"/><xsl:text>,</xsl:text>
					<xsl:value-of select="$varLevel+128"/><xsl:text> </xsl:text>
					<xsl:value-of select="kb:date*$timeScale + 8 - $beginOffset * $timeScale + $centerOffset"/><xsl:text>, </xsl:text>
					<xsl:value-of select="$varLevel+114"/><xsl:text> </xsl:text>
				</xsl:attribute>
				<xsl:attribute name="class">
					<xsl:call-template name="getClass">
						<xsl:with-param name="currentClass" select="rdf:type/@rdf:resource" />
					</xsl:call-template>
				</xsl:attribute>
			</polygon>
        </xsl:when>		
		<xsl:otherwise>
			<circle cx="{kb:date*$timeScale - $beginOffset * $timeScale + $centerOffset}" r="4" id="{$defaultComponentId}idNode{@rdf:about}" style="fill:{$visu_color}"> <!-- onclick="toggleTip(evt,'{@rdf:about}');" -->
				<!-- Set the attributes of the observés -->
				<xsl:attribute name="cy">
					<xsl:value-of select="$varLevel+120"/>
				</xsl:attribute>
				<xsl:attribute name="class">
					<xsl:call-template name="getClass">
						<xsl:with-param name="currentClass" select="rdf:type/@rdf:resource" />
					</xsl:call-template>
				</xsl:attribute>
			</circle>  
		</xsl:otherwise>
    </xsl:choose>
</xsl:template>

<!-- return a list of all the types of the element (his type, and all his inherited types) -->
<xsl:template name="getClass">
	<xsl:param name="currentClass" />
	<xsl:variable name="parentClass" select="$mcdoc/rdf:RDF/swivt:Subject[substring(@rdf:about,59)=$currentClass]/rdfs:subClassOf/@rdf:resource" />
	<xsl:value-of select="substring($currentClass,59)" /><xsl:text> </xsl:text><!-- <xsl:value-of select="substring($parentClass,32)" /><xsl:text> </xsl:text>  -->
	<xsl:choose>
		<xsl:when test="$parentClass">
			<xsl:call-template name="getClass">
				<xsl:with-param name="currentClass" select="$parentClass" />
			</xsl:call-template>
		</xsl:when>
		<xsl:otherwise>
			<xsl:text>clickable</xsl:text>
		</xsl:otherwise>
	</xsl:choose>
</xsl:template>

<!-- Draw the inferrence lines -->
<xsl:template match="kb:inferred|kb:begin|kb:end" mode="connexion">
	<xsl:param name="inferredNode" select="@rdf:resource" />
	<xsl:param name="varLeveli" />
	<!-- check that the inferred node has a date  -->
	<xsl:param name="linkedNode" select="//rdf:Description[@rdf:about=$inferredNode]"/>
	<xsl:if test="$linkedNode">
	<polyline stroke="#000000" stroke-width="1" stroke-opacity="0.2">
		<xsl:attribute name="points">
			<xsl:value-of select="../kb:date*$timeScale - $beginOffset * $timeScale + $centerOffset"/><xsl:text>,</xsl:text>
			<xsl:value-of select="$varLeveli+120"/><xsl:text> </xsl:text>
			<xsl:value-of select="$linkedNode/kb:date*$timeScale - $beginOffset * $timeScale + $centerOffset"/><xsl:text>,</xsl:text>
            <xsl:variable name="varTypei" select="$linkedNode/rdf:type/@rdf:resource" /> 
			<xsl:choose>
				<xsl:when test="$mcdoc//a:OverridingProperty[a:domain/@rdf:resource=$varTypei][a:overriddenProperty/@rdf:resource='http://protege.stanford.edu/kb#visu_level']/@a:defaultValues">
					<xsl:value-of select="- $mcdoc//a:OverridingProperty[a:domain/@rdf:resource=$varTypei][a:overriddenProperty/@rdf:resource='http://protege.stanford.edu/kb#visu_level']/@a:defaultValues+120" />
				</xsl:when>
				<xsl:otherwise>
					<xsl:text>200</xsl:text>
				</xsl:otherwise>
			</xsl:choose>
		</xsl:attribute>
	</polyline>
	</xsl:if>
</xsl:template>

<xsl:template match="*"/>

</xsl:transform>

