<?xml version="1.0" encoding="ISO-8859-1" standalone="no"?>
<!-- ##############################################################
                            cellml_connection.xsl

############################################################## -->
<!--=================================================-->
<!--        "SBML" to "CellML" 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 inquiry regarding this file, please contact Biomodels.net support (biomodels-net-support at lists.sourceforge.net)

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


<!--xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx-->
<!-- name  Template:     wConnection                 -->
<!--xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx-->
<!--connection:    information network

all simple component, which is not defined by rule or reaction 
will just contain itself, with interface out and time with interface in
the following will just discuss the complicate type.

component variable with interface in, just can have one out source
component variable out can give information to one or more in acceptor
and in interface should be fixed

	component		interface_out		interface_in
compartment, defined by         itself			unique ci in rule
rule.

global parameter                itself			unique ci in rule
by rule

species:
	by rule			variable, itself		unique ci in rule

	by reaction		itself			delta variable
							compartment

strategy:
	0 map_component component1:  compartment id
					species_id
					global_parameter_id
			component2: environment
	 map_variable: time    time
	1 search in rule, (assignmentRule and rateRule).
	rule is contained in the component with the name from @variable. (accept component)
		all UNIQUE and non-@variable ci		(in variable)
	corresponding name component (give component)
		corresponding name variable (out variable)
		
	2 search in reaction 
	reaction component, named as the reaction id (accept component)
		chemical: reactant
			product
			modifier
			
		global parameter (non species_localPara)
		
		(above all in variable)
	corresponding to in variable id' component  (give component)
		corresponding to in variable id's variable (out variable)
		
	combine with species id out variable, and reaction delta-variable
	
	strategy: 1 set up variable for reactant and product
		  2 search in reactant and product 
		  map_component is  reaction_id and species_id
		  map_variable is species_id and species_id
		  if this species is not in variable assign_rateRule, it do have delta variable so have following
		  map_variable is  delta-variable and delta|_variable

		  3 search in modifier but not reactant nor product (pure modifier)
		  map_component is reaction_id and species_id
		  map_vara is species_id and species_id
		
	3 species (by reaction)(!!!! first test if it is by rule, or not)
	map_component is species_id  and compartment_id
	map_variable is compartment_id and compartment_id
-->

<xsl:template name="wConnection">
<!--connection for time-->
	<xsl:for-each select="descendant::sbml:listOfCompartments/sbml:compartment|descendant::sbml:listOfSpecies/sbml:species|descendant::sbml:model/sbml:listOfParameters/sbml:parameter">
		<xsl:element name="connection">
			<xsl:element name="map_components">
				<xsl:attribute name="component_1">
					<xsl:value-of select="@id"/>
				</xsl:attribute>
				<xsl:attribute name="component_2">
					<xsl:value-of select="'environment'"/>
				</xsl:attribute>
			</xsl:element>
			<xsl:element name="map_variables">
				<xsl:attribute name="variable_1">
					<xsl:value-of select="'time'"/>
				</xsl:attribute>
				<xsl:attribute name="variable_2">
					<xsl:value-of select="'time'"/>
				</xsl:attribute>
			</xsl:element>
		</xsl:element>
	</xsl:for-each>

<!--connection, search in rule-->
<xsl:comment>this is the connection from rule search</xsl:comment>
<xsl:value-of select="$newline"/>
	<xsl:for-each select="descendant::sbml:listOfRules/sbml:assignmentRule|descendant::sbml:listOfRules/sbml:rateRule">
		<xsl:variable name="vara">
			<xsl:value-of select="@variable"/>
		</xsl:variable>
		<xsl:for-each select="math:math/descendant::math:ci">
			<xsl:variable name="CI">
				<xsl:value-of select="normalize-space(.)"/>
			</xsl:variable>
			<xsl:variable name="po">
				<xsl:value-of select="position()"/>
			</xsl:variable>
				<xsl:if test="not(ancestor::math:math/descendant::math:ci[normalize-space(.)=$CI and position()&lt;$po])">
		   			<xsl:element name="connection">
						<xsl:element name="map_components">
							<xsl:attribute name="component_1">
								<xsl:value-of select="$vara"/>
							</xsl:attribute>
							<xsl:attribute name="component_2">
								<xsl:value-of select="$CI"/>
							</xsl:attribute>
						</xsl:element>
						<xsl:element name="map_variables">
							<xsl:attribute name="variable_1">
								<xsl:value-of select="$CI"/>
							</xsl:attribute>
							<xsl:attribute name="variable_2">
								<xsl:value-of select="$CI"/>
							</xsl:attribute>
						</xsl:element>
		   			</xsl:element>
		   		</xsl:if>
		</xsl:for-each>
	</xsl:for-each>
	
<!--connection from reaction search-->
<xsl:comment>this is the connection from reaction search</xsl:comment>
<xsl:value-of select="$newline"/>
	<xsl:for-each select="descendant::sbml:listOfReactions/sbml:reaction">
		<xsl:variable name="reac_id">
			<xsl:value-of select="@id"/>
		</xsl:variable>
		<xsl:variable name="reactant_product">
			<xsl:for-each select="sbml:listOfReactants/sbml:speciesReference|sbml:listOfProducts/sbml:speciesReference">
				<xsl:element name="lu">
					<xsl:attribute name="species">
						<xsl:value-of select="@species"/>
					</xsl:attribute>
				</xsl:element>
			</xsl:for-each>
		</xsl:variable>
		<xsl:for-each select="sbml:listOfReactants/sbml:speciesReference|sbml:listOfProducts/sbml:speciesReference">
			<xsl:variable name="spID">
				<xsl:value-of select="@species"/>
			</xsl:variable>
		   			<xsl:element name="connection">
						<xsl:element name="map_components">
							<xsl:attribute name="component_1">
								<xsl:value-of select="$reac_id"/>
							</xsl:attribute>
							<xsl:attribute name="component_2">
								<xsl:value-of select="@species"/>
							</xsl:attribute>
						</xsl:element>
						<xsl:element name="map_variables">
							<xsl:attribute name="variable_1">
								<xsl:value-of select="@species"/>
							</xsl:attribute>
							<xsl:attribute name="variable_2">
								<xsl:value-of select="@species"/>
							</xsl:attribute>
						</xsl:element>
						<xsl:if test="not(exsl:node-set($assign_rateRule)/lu[@variable=$spID])">
						<xsl:element name="map_variables">
							<xsl:attribute name="variable_1">
								<xsl:value-of select="concat('delta_',@species,'_',$reac_id)"/>
							</xsl:attribute>
							<xsl:attribute name="variable_2">
								<xsl:value-of select="concat('delta_',@species,'_',$reac_id)"/>
							</xsl:attribute>
						</xsl:element>
						</xsl:if>
		   			</xsl:element>
		</xsl:for-each>
		
		<xsl:for-each select="sbml:listOfModifiers/sbml:modifierSpeciesReference">
			<xsl:variable name="modifier">
				<xsl:value-of select="@species"/>
			</xsl:variable>
			<xsl:if test="not(exsl:node-set($reactant_product)/lu[@species=$modifier])">

		   			<xsl:element name="connection">
						<xsl:element name="map_components">
							<xsl:attribute name="component_1">
								<xsl:value-of select="$reac_id"/>
							</xsl:attribute>
							<xsl:attribute name="component_2">
								<xsl:value-of select="$modifier"/>
							</xsl:attribute>
						</xsl:element>
						<xsl:element name="map_variables">
							<xsl:attribute name="variable_1">
								<xsl:value-of select="$modifier"/>
							</xsl:attribute>
							<xsl:attribute name="variable_2">
								<xsl:value-of select="$modifier"/>
							</xsl:attribute>
						</xsl:element>
		   			</xsl:element>
			</xsl:if>
		</xsl:for-each>
	</xsl:for-each>
	
<!--connection search in species-->
<xsl:comment>this is the connection from species</xsl:comment>
<xsl:value-of select="$newline"/>
	<xsl:for-each select="descendant::sbml:listOfSpecies/sbml:species">
		<xsl:variable name="spID">
			<xsl:value-of select="@species"/>
		</xsl:variable>
			<xsl:if test="not(exsl:node-set($assign_rateRule)/lu[@variable=$spID])">
				<xsl:if test="key('reactant',$spID) or key('product',$spID)">
		   			<xsl:element name="connection">
						<xsl:element name="map_components">
							<xsl:attribute name="component_1">
								<xsl:value-of select="@id"/>
							</xsl:attribute>
							<xsl:attribute name="component_2">
								<xsl:value-of select="@compartment"/>
							</xsl:attribute>
						</xsl:element>
						<xsl:element name="map_variables">
							<xsl:attribute name="variable_1">
								<xsl:value-of select="@compartment"/>
							</xsl:attribute>
							<xsl:attribute name="variable_2">
								<xsl:value-of select="@compartment"/>
							</xsl:attribute>
						</xsl:element>
		   			</xsl:element>
				</xsl:if>
			</xsl:if>
	</xsl:for-each>
</xsl:template>

<!--xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx-->
<!-- name  Template:     wGroup                      -->
<!--xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx-->
<!--sbml compartment concept need cellML group to realise-->
<!--step:
		each group for one compartment
		group:  
			relationship_ref: @relationship="containment"
			component_ref: component="compartment_id"
				component_ref:component="species_id"-->
<!--use key speciesCompartment-->
<xsl:template name="wGroup">
	<xsl:for-each select="descendant::sbml:listOfCompartments/sbml:compartment">
		<xsl:variable name="compartment">
			<xsl:value-of select="@id"/>
		</xsl:variable>
		<xsl:element name="group">
			<xsl:element name="relationship_ref">
				<xsl:attribute name="relationship">
					<xsl:value-of select="'containment'"/>
				</xsl:attribute>
			</xsl:element>
			<xsl:element name="component_ref">
				<xsl:attribute name="component">
					<xsl:value-of select="$compartment"/>
				</xsl:attribute>
				<xsl:for-each select="key('speciesCompartment',$compartment)">
					<xsl:element name="component_ref">
						<xsl:attribute name="component">
							<xsl:value-of select="@id"/>
						</xsl:attribute>
					</xsl:element>
				</xsl:for-each>
			

			</xsl:element>

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

</xsl:stylesheet>

