<?xml version="1.0"?>

<!--
  Copyright (c) 1997-2009
  Ewgenij Gawrilow, Michael Joswig (Technische Universitaet Darmstadt, Germany)
  http://www.polymake.de

  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, or (at your option) any
  later version: http://www.gnu.org/licenses/gpl.txt.

  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.
===============================================================================
  $Project: polymake $$Id: upgrade-2.9.8.xslt 9437 2010-02-03 14:25:35Z sherrmann $

  1. TRIANGULATION as a subobject and TRIANGULATION_BOUNDARY a subobject TRIANGULATION.BOUNDARY in application polytope.
  2. BOUNDARY_AS_PSEUDO_MANIFOLD as subobject BOUNDARY in application topaz.
  3. ODD_SUBCOMPLEX as subobject in application topaz.
  
-->

<xsl:transform version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
			     xmlns:p="http://www.math.tu-berlin.de/polymake/#3">

<xsl:output method="xml" encoding="utf-8" cdata-section-elements="p:description" indent="yes" />


<!-- 1 -->

<!-- We search for objects with property TRIANGULATION or TRIANGULATION_BOUNDARY -->

<xsl:template match="p:object">
   <xsl:variable name="has_TRIANGULATION" select="./p:property[@name='TRIANGULATION']" />
   <xsl:variable name="has_TRIANGULATION_BOUNDARY" select="./p:property[@name='TRIANGULATION_BOUNDARY']" />
   <xsl:element name="object" namespace="{namespace-uri()}">
     <xsl:copy-of select="@*[name()!='tm' and name()!='version']" />
     <xsl:choose>
       <xsl:when test="(count($has_TRIANGULATION)>=1) or (count($has_TRIANGULATION_BOUNDARY)>=1)">
        
	  <!-- most properties stay in the object -->
	
	 <xsl:for-each select="*">
           <xsl:variable name="T-props">
              <xsl:apply-templates mode="T-props" select="." />
           </xsl:variable>
	   <xsl:variable name="TB-props">
              <xsl:apply-templates mode="TB-props" select="." />
           </xsl:variable>
           <xsl:if test="($T-props='') and ($TB-props='')">
             <xsl:apply-templates select="." />
           </xsl:if>   
	 </xsl:for-each>
	    
	    <!-- copy some properties into the triangulation-object -->
	    	    
	 <xsl:element name="property" namespace="{namespace-uri()}">
           <xsl:attribute name="name">TRIANGULATION</xsl:attribute>
           <xsl:element name="object" namespace="{namespace-uri()}">
             <xsl:for-each select="*">
               <xsl:variable name="T-props">
                 <xsl:apply-templates mode="T-props" select="." />
               </xsl:variable>
	       <xsl:variable name="TB-props">
                 <xsl:apply-templates mode="TB-props" select="." />
               </xsl:variable>
               <xsl:if test="($T-props!='')">
                 <xsl:element name="property" namespace="{namespace-uri()}">
                   <xsl:attribute name="name">
                     <xsl:value-of select="$T-props" />
                   </xsl:attribute>
                   <xsl:copy-of select="@*[name()!='name']" />
                   <xsl:apply-templates select="./node()" />
                 </xsl:element>
               </xsl:if>
	       
	       <!-- TRIANGULATION_BOUNDARY is copied to TRIANGULATION.BOUNDARY.FACETS  (without facet information) -->
	       
	       <xsl:if test="$TB-props!=''">
                 <xsl:element name="property" namespace="{namespace-uri()}">
		   <xsl:attribute name="name">BOUNDARY</xsl:attribute>
                   <xsl:element name="object" namespace="{namespace-uri()}">
                     <xsl:element name="property" namespace="{namespace-uri()}">
		       <xsl:attribute name="name">
                         <xsl:value-of select="$TB-props" />
                       </xsl:attribute>
                       <xsl:copy-of select="@*[name()!='name']" />
		       <xsl:element name="m" namespace="{namespace-uri()}">
	                 <xsl:for-each select="./node()">
		           <xsl:for-each select="./node()">
	                     <xsl:apply-templates select="./node()" />
		           </xsl:for-each>
	                 </xsl:for-each>
                       </xsl:element>
                     </xsl:element>
		   </xsl:element>
		 </xsl:element>
		 
		 <!-- FACET_TRIANGULATION is computed and stored in TRIANGULATION -->
		 
		 <xsl:element name="property" namespace="{namespace-uri()}">
                   <xsl:attribute name="name">FACET_TRIANGULATIONS</xsl:attribute>
	           <xsl:element name="m" namespace="{namespace-uri()}">
	             <xsl:variable name="facet_n_simplices">
                       <xsl:apply-templates mode="facet_n_simplices" select="./node()" />
                     </xsl:variable>
		     <xsl:call-template name="count_array">
                        <xsl:with-param name="values" select="$facet_n_simplices" />
                     </xsl:call-template>
                   </xsl:element>
                 </xsl:element>
               </xsl:if>
             </xsl:for-each>
           </xsl:element>
         </xsl:element>	    

      </xsl:when>
      <xsl:otherwise>
         <xsl:apply-templates  />
      </xsl:otherwise>
   </xsl:choose>
  </xsl:element>
</xsl:template>



<!-- These properties will be moved into TRIANGULATION  -->

<xsl:template mode="T-props" match="p:property[@name='SPLITS_IN_TRIANGULATION']">
   <xsl:text>REFINED_SPLITS</xsl:text>
</xsl:template>

<xsl:template mode="T-props" match="p:property[@name='GKZ_VECTOR']">
    <xsl:text>GKZ_VECTOR</xsl:text>
</xsl:template>

<xsl:template mode="T-props" match="p:property[@name='TRIANGULATION']">
    <xsl:text>FACETS</xsl:text>
</xsl:template>

<xsl:template mode="T-props" match="*" />



<!-- This properties will be moved into TRIANGULATION.BOUNDARY  -->

<xsl:template mode="TB-props" match="p:property[@name='TRIANGULATION_BOUNDARY']">
    <xsl:text>FACETS</xsl:text>
</xsl:template>

<xsl:template mode="TB-props" match="*" />



<!-- utilities to compute FACET_TRIANGULATIONS -->

<xsl:template mode="facet_n_simplices" match="p:property[@name='TRIANGULATION_BOUNDARY']/p:m">
   <xsl:for-each select="./node()">
	<xsl:variable name="n_simplices" select="./p:v" />
	    <xsl:if test="count($n_simplices)>0">
                <xsl:value-of select="count($n_simplices)" />
		<xsl:text> </xsl:text>
            </xsl:if>
   </xsl:for-each>
</xsl:template>


<xsl:template name="count_array">
        <xsl:param name="values"/>
	<xsl:param name="from">0</xsl:param>
	<xsl:variable name="n_values" select="normalize-space($values)"/>
	<xsl:variable name="first" select="substring-before($n_values,' ')" />
        <xsl:variable name="remaining" select="substring-after($n_values, ' ')" />
	<!--<xsl:value-of select="$first" /><xsl:value-of select="$remaining" />-->
  	<xsl:choose>
         <xsl:when test="$first!=''">
	    <xsl:element name="v" namespace="{namespace-uri()}">
	       <xsl:call-template name="count">
	           <xsl:with-param name="range" select="$first"/>
                   <xsl:with-param name="from" select="$from"/>
               </xsl:call-template>
	    </xsl:element>
	    <xsl:call-template name="count_array">
	        <xsl:with-param name="values" select="$remaining"/>
                <xsl:with-param name="from" select="$from+$first"/>
            </xsl:call-template>
	 </xsl:when>
	 <xsl:when test="$remaining=''">
	 <xsl:element name="v" namespace="{namespace-uri()}">
	       <xsl:call-template name="count">
	           <xsl:with-param name="range" select="$n_values"/>
                   <xsl:with-param name="from" select="$from"/>
               </xsl:call-template>
	    </xsl:element>
	  </xsl:when>
      </xsl:choose>
</xsl:template>


<!-- simple function that counts from $from to $from+$range -->
<xsl:template name="count">
        <xsl:param name="from"></xsl:param>
	<xsl:param name="range"></xsl:param>
        <xsl:value-of select="$from"/>
	
        <xsl:if test="$range!=1">
            <xsl:text> </xsl:text>
            <xsl:call-template name="count">
                <xsl:with-param name="from" select="$from+1"/>
                <xsl:with-param name="range" select="$range -1"/>
            </xsl:call-template>
        </xsl:if>
</xsl:template>


<!-- 2. -->

<xsl:template match="p:property[@name='BOUNDARY_OF_PSEUDO_MANIFOLD']">
   <xsl:element name="property" namespace="{namespace-uri()}">
      <xsl:attribute name="name">BOUNDARY</xsl:attribute>
      <xsl:element name="object" namespace="{namespace-uri()}">
	 <xsl:element name="property" namespace="{namespace-uri()}">
            <xsl:attribute name="name">FACETS</xsl:attribute>
	    <xsl:copy-of select="@*[name()!='name']" />
            <xsl:apply-templates select="./node()" />
          </xsl:element>
       </xsl:element>
   </xsl:element>
</xsl:template>

<!-- 3. -->

<xsl:template match="p:property[@name='ODD_SUBCOMPLEX']">
   <xsl:element name="property" namespace="{namespace-uri()}">
      <xsl:attribute name="name">ODD_SUBCOMPLEX</xsl:attribute>
      <xsl:element name="object" namespace="{namespace-uri()}">
	 <xsl:element name="property" namespace="{namespace-uri()}">
            <xsl:attribute name="name">FACETS</xsl:attribute>
	    <xsl:copy-of select="@*[name()!='name']" />
            <xsl:apply-templates select="./node()" />
          </xsl:element>
       </xsl:element>
   </xsl:element>
</xsl:template>


<xsl:include href="trivial-copy.xslt" />


</xsl:transform>
