<?xml version="1.0" encoding="ISO-8859-1"?>
<!--=================================================-->
<!--        "CellML" to "SBML" XML stylesheet        -->
<!--                 (c) EBI 2005-2010                  -->
<!--=================================================-->

<!--

    This program 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
    (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, write to the Free Software
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA


    For any enquiry regarding this file, please contact "BioModels.net Support" <biomodels-net-support@lists.sourceforge.net>

-->
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"
xmlns:cellml="http://www.cellml.org/cellml/1.0#"
xmlns:mathml="http://www.w3.org/1998/Math/MathML"
xmlns:math="http://www.w3.org/1998/Math/MathML"
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:cmeta="http://www.cellml.org/metadata/1.0#"
xmlns:bqs="http://www.cellml.org/bqs/1.0#"
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:dcterms="http://purl.org/dc/terms/"
xmlns:vCard="http://www.w3.org/2001/vcard-rdf/3.0#"
xmlns:sbml="http://www.sbml.org/sbml/level2"
xmlns:exsl="http://exslt.org/common" extension-element-prefixes="exsl"
xmlns:stsim="http://www.sbml.org/2001/ns/stochsim"
xmlns="http://www.sbml.org/sbml/level2"
>

<xsl:output method="xml" indent="yes" encoding="UTF-8"/>
<xsl:strip-space elements="*"/>

<xsl:key name="model_unit" match="cellml:model/cellml:units" use="@name" />
<xsl:key name="component_unit" match="cellml:model/cellml:component/cellml:units" use="@name"/>
<xsl:key name="group" match="cellml:model/cellml:group//cellml:component_ref" use="@component"/>
<xsl:key name="mathml_bvar" match="//mathml:bvar" use="normalize-space(./mathml:ci)"/>
<xsl:key name="variable" match="cellml:model/cellml:component/cellml:variable" use="@name"/>
<xsl:key name="component" match="cellml:model/cellml:component" use="@name"/>

<xsl:include href="cellml_template.xsl"/>
<xsl:include href="cellml_math.xsl"/>
<xsl:include href="cellml_variable.xsl"/>

<!--newline variable-->
<xsl:variable name="newline">
	<xsl:text>
	</xsl:text>
</xsl:variable>


<!--===============================================-->
<!--                 MAIN                          -->
<!--===============================================-->
<xsl:template match="/">
<xsl:variable name="no_import">
	<xsl:choose>
		<xsl:when test="cellml:model/cellml:import">
			<xsl:value-of select="'false'"/>
		</xsl:when>
		<xsl:otherwise>
			<xsl:value-of select="'true'"/>
		</xsl:otherwise>
	</xsl:choose>
</xsl:variable>

<xsl:variable name="no_base">
	<xsl:choose>
		<xsl:when test="cellml:model/cellml:units[@base_units='yes'] or cellml:model/cellml:component/cellml:units[@base_units='yes']">
			<xsl:value-of select="'false'"/>
		</xsl:when>
		<xsl:otherwise>
			<xsl:value-of select="'true'"/>
		</xsl:otherwise>
	</xsl:choose>
</xsl:variable>

<xsl:variable name="one_time">
	<xsl:choose>
		<xsl:when test="count(exsl:node-set($time_variable)/lu[not(@ori_unit_id=preceding-sibling::lu/@ori_unit_id)])='1' or '0'">
			<xsl:value-of select="'true'"/>
		</xsl:when>
		<xsl:otherwise>
			<xsl:value-of select="'false'"/>
		</xsl:otherwise>
	</xsl:choose>
</xsl:variable>
<xsl:variable name="waste">
	<xsl:for-each select="exsl:node-set($species_2)/lu">
		<xsl:element name="lu">
		   <xsl:attribute name="id">
			<xsl:value-of select="@id"/>
		   </xsl:attribute>
		</xsl:element>
	</xsl:for-each>
</xsl:variable>
<!--test dimension-->
<xsl:variable name="N">
	<xsl:choose>
		<xsl:when test="count(exsl:node-set($dimension)/lu[spaUnit/@dimension='0'])=count(exsl:node-set($dimension)/lu)">
			<xsl:value-of select="'1'"/>
		</xsl:when>
		<xsl:otherwise>
	<xsl:value-of select="count(exsl:node-set($dimension)/lu[not(spaUnit/@dimension='0') and not(spaUnit/@dimension=preceding-sibling::lu/spaUnit/@dimension)])"/>
		</xsl:otherwise>
	</xsl:choose>
</xsl:variable>
<!--test rate based substance unit-->
<!--
<xsl:variable name="N_ratesub">
	<xsl:call-template name="countDifference">
		<xsl:with-param name="vara" select="$rate_substance"/>
		<xsl:with-param name="N" select="count(exsl:node-set($rate_substance)/lu)"/>
		<xsl:with-param name="n" select="'2'"/>
		<xsl:with-param name="result" select="'0'"/>
	</xsl:call-template>
</xsl:variable>
-->

<!--test rate based time unit-->
<xsl:variable name="N_ratetime">
	<xsl:call-template name="countDifference">
		<xsl:with-param name="vara" select="$rate_time"/>
		<xsl:with-param name="N" select="count(exsl:node-set($rate_time)/lu)"/>
		<xsl:with-param name="n" select="'1'"/>
		<xsl:with-param name="result" select="'0'"/>
	</xsl:call-template>
</xsl:variable>

<xsl:choose>
	<xsl:when test="$no_import='false'">
		<xsl:value-of select="'We currently can not solve cellML import element.'"/>
	</xsl:when>

	<xsl:when test="$one_time='false'">
		<xsl:value-of select="'The boundary variable time is based on several time systems, which we currently can not solve.'"/>
	</xsl:when>
	<xsl:when test="$no_base='false'">
		<xsl:value-of select="'There are user defined base units, which we currently can not solve.'"/>
	</xsl:when>
	<xsl:when test="not($N='1')">
		<xsl:value-of select="'The spatial dimension, which the kinetic rate Law unit based on is not unique, which we currently can not solve.'"/>
	</xsl:when>
<!--
	<xsl:when test="not($N_ratesub='0')">
		<xsl:value-of select="'The substance units, which rate variables based on are not consistant.'"/>
	</xsl:when>
-->
	<xsl:when test="not($N_ratetime='0')">
		<xsl:value-of select="'The time units, which rate variable based on  are not consistant.'"/>
	</xsl:when>
	<xsl:otherwise>
		<xsl:text disable-output-escaping="yes">&lt;sbml xmlns=&quot;http://www.sbml.org/sbml/level2&quot; level=&quot;2&quot; version=&quot;1&quot;&gt;</xsl:text>
<!--
		<xsl:element name="sbml" xmlns="http://www.sbml.org/sbml/level2">
			<xsl:attribute name="level">
				<xsl:value-of select="'2'"/>
			</xsl:attribute>
			<xsl:attribute name="version">
				<xsl:value-of select="'1'"/>
			</xsl:attribute>

			<xsl:call-template name="top_level_notes"/>
			<xsl:apply-templates select="cellml:model" mode="model"/>

		</xsl:element>
-->
		<xsl:call-template name="top_level_notes"/>
		<xsl:apply-templates select="cellml:model" mode="model"/>
		


		<xsl:value-of select="$newline"/>

		<xsl:text disable-output-escaping="yes">&lt;/sbml&gt;</xsl:text>
	</xsl:otherwise>

</xsl:choose>

</xsl:template>

<!--================================================-->
<!--             cellml:model                       -->
<!--================================================-->

<xsl:template match="cellml:model" mode="model">
	<xsl:element name="model">
		<xsl:attribute name="id">
			<xsl:value-of select="@name"/>
		</xsl:attribute>
			
		<xsl:call-template name="wUnit"/>
		<xsl:call-template name="wCompartment"/>
		<xsl:call-template name="wSpecies"/>
		<xsl:call-template name="wParameter"/>
		<xsl:call-template name="wRule"/>
		<xsl:call-template name="wReaction"/>
		
<!--
		<xsl:comment>This is all annotation</xsl:comment>
		<xsl:value-of select="$newline"/>
		<xsl:element name="annotation">
		<xsl:for-each select=".//rdf:RDF">
			<xsl:apply-templates select="."/>
		</xsl:for-each>

		</xsl:element>
-->
	</xsl:element>
</xsl:template>

</xsl:stylesheet>



