<?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.7.xslt 9298 2009-09-02 23:10:56Z gawrilow $

  1. Polytope having POINTS and (at least one of CHIROTOPE_INT, GKZ_VECTOR_INT,
     POLYTOPAL_SUBDIVISION_INT or no VERTICES and TRIANGULATION_INT) is converted to PointConfiguration
  2. former property LP.VARIABLE_NAMES is stored as attachment now
  3. former property GROEBNER_BASIS now a multiple object
-->

<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. -->

<xsl:template match="/">
   <xsl:variable name="top_type" select="/p:object/@type" />
   <xsl:variable name="has_PC_props">
      <xsl:apply-templates mode="PC-props" select="/p:object/p:property" />
   </xsl:variable>
   <xsl:variable name="has_POINTS" select="/p:object/p:property[@name='POINTS']" />
   <xsl:variable name="has_TRIANGULATION_INT" select="/p:object/p:property[@name='TRIANGULATION_INT']" />
   <xsl:variable name="has_VERTICES" select="/p:object/p:property[@name='VERTICES']" />

   <xsl:choose>
      <xsl:when test="starts-with($top_type,'polytope::Polytope') and count($has_POINTS)=1 and ($has_PC_props!='' or (count($has_VERTICES)=0 and count($has_TRIANGULATION_INT)=1))">
         <xsl:element name="object" namespace="{namespace-uri(/p:object)}">
            <xsl:attribute name="type">
               <xsl:value-of select="concat('polytope::PointConfiguration', substring-after($top_type,'Polytope'))" />
            </xsl:attribute>
            <xsl:copy-of select="/p:object/@*[name()!='tm' and name()!='version' and name()!='type']" />

            <xsl:element name="property" namespace="{namespace-uri(/p:object)}">
               <xsl:attribute name="name">CONVEX_HULL</xsl:attribute>
               <xsl:element name="object" namespace="{namespace-uri(/p:object)}">
                  <!-- copy all children which do not belong to PointConfiguration -->
                  <xsl:for-each select="/p:object/*">
                     <xsl:variable name="keep_in_PC">
                        <xsl:apply-templates mode="keep-in-PC" select="." />
                     </xsl:variable>
                     <xsl:if test="$keep_in_PC=''">
                        <xsl:apply-templates select="." />
                     </xsl:if>
                  </xsl:for-each>
               </xsl:element>
            </xsl:element>

            <!-- Copy the PointConfiguration's top properties, taking care of possible renaming -->
            <xsl:for-each select="/p:object/*">
               <xsl:variable name="new_name">
                  <xsl:apply-templates mode="PC-props" select="." />
        	  <xsl:apply-templates mode="PC-non_change-props" select="." />
               </xsl:variable>
               <xsl:variable name="keep_in_PC">
                  <xsl:apply-templates mode="keep-in-PC" select="." />
               </xsl:variable>
               <xsl:choose>
                  <xsl:when test="$new_name!=''">
                     <xsl:element name="property" namespace="{namespace-uri(/p:object)}">
                        <xsl:attribute name="name">
                           <xsl:value-of select="$new_name" />
                        </xsl:attribute>
                        <xsl:copy-of select="@*[name()!='name']" />
                        <xsl:apply-templates select="./node()" />
                     </xsl:element>
                  </xsl:when>
                  <xsl:when test="$keep_in_PC!=''">
                     <xsl:apply-templates select="." />
                  </xsl:when>
               </xsl:choose>
            </xsl:for-each>
         </xsl:element>
      </xsl:when>
      <xsl:otherwise>
         <xsl:apply-templates />
      </xsl:otherwise>
   </xsl:choose>
</xsl:template>

<!-- The following group of rules encode the mapping between the old and new property names.
     Every property named here remains in the top-level PointConfiguration object,
     the rest goes into the CONVEX_HULL subobject, if one the properties occurs, the object
     is translated into a PointConfiguration-->

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

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

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

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

<!-- The following group of rules encode the mapping between the old and new property names.
     Every property named here remains in the top-level PointConfiguration object,
     the rest goes into the CONVEX_HULL subobject. -->
     

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

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

<!-- The following group of rules lists the children elements which should stay at the top level.
     Unlike in the first group, they are copied verbatim. -->

<xsl:template mode="keep-in-PC" match="p:property[@name='POINTS']">Yes</xsl:template>

<xsl:template mode="keep-in-PC" match="p:property[@name='N_POINTS']">Yes</xsl:template>

<xsl:template mode="keep-in-PC" match="p:property[@name='BOUNDED']">Yes</xsl:template>

<xsl:template mode="keep-in-PC" match="p:property[@name='AMBIENT_DIM']">Yes</xsl:template>

<xsl:template mode="keep-in-PC" match="p:description">Yes</xsl:template>

<xsl:template mode="keep-in-PC" match="*">
   <xsl:apply-templates mode="PC-props" select="." />
   <xsl:apply-templates mode="PC-non_change-props" select="." />
</xsl:template>


<!-- 2. -->

<xsl:template match="p:property[@name='LP']/p:object/p:property[@name='VARIABLE_NAMES']">
   <xsl:element name="attachment" namespace="{namespace-uri()}">
      <xsl:attribute name="name">VARIABLE_NAMES</xsl:attribute>
      <xsl:attribute name="type">Array&lt;String&gt;</xsl:attribute>
      <xsl:apply-templates select="./node()" />
   </xsl:element>
</xsl:template>


<!-- 3. -->

<xsl:template match="p:property[@name='GROEBNER_BASIS']">
   <xsl:variable name="applied" select="./p:object" />
   <xsl:choose>
      <xsl:when test="count($applied)=0">
         <xsl:element name="property" namespace="{namespace-uri()}">
            <xsl:attribute name="name">GROEBNER_BASIS</xsl:attribute>
            <xsl:element name="object" namespace="{namespace-uri()}">
               <xsl:element name="property" namespace="{namespace-uri()}">
                  <xsl:attribute name="name">BASIS</xsl:attribute>
                  <xsl:apply-templates select="./node()" />
               </xsl:element>
            </xsl:element>
         </xsl:element>
      </xsl:when>
      <xsl:otherwise>
         <xsl:copy-of select="." />
      </xsl:otherwise>
   </xsl:choose>
</xsl:template>


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

</xsl:transform>
