<?xml version='1.0'?>

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes"/>
<xsl:template match="/">
<nta>
<declaration>

	<!--Se mantiene lo que ya había-->
	<xsl:value-of select="//declaration"/>

</declaration>

<xsl:for-each select="nta/template">  
  
 <template>
 <name>
   <xsl:value-of select="name"/>
 </name> 
 
 <!--Se mantienen los LOCATION que haya sin modificación salvo los inicios de un choice, a los que se les puede añadir un invariante--> 
 <xsl:for-each select="location"> 
      
  <xsl:if test="not(name(following-sibling::*[1])='Choice_inv')">    
	  <xsl:element name="location">
	   <xsl:attribute name="id">	   
	    
	    <xsl:if test = "contains(@id,'End_') and contains(@id,'Init_') ">
	    	<xsl:value-of select="concat('Init_',substring-after(@id,'Init_'))"/>
	    </xsl:if>
	    
	    <xsl:if test = "not(contains(@id,'End_') and contains(@id,'Init_'))">
	    	<xsl:value-of select="@id"/>
	    </xsl:if>
		
	   </xsl:attribute>
	   <name>
	   <xsl:if test = "contains(name/@id,'End_') and contains(name/@id,'Init_') ">
	    	<xsl:value-of select="concat('Init_',substring-after(name/@id,'Init_'))"/>
	    </xsl:if>
	    
	    <xsl:if test = "not(contains(name/@id,'End_') and contains(name/@id,'Init_'))">
	    	<xsl:value-of select="name/@id"/>
	    </xsl:if>
	   </name> 
	   
	    <!--Si tiene un invariante se debe mantener-->
	  	<xsl:for-each select="label">
	  	
	  		<xsl:element name="label">
		  		<xsl:attribute name="kind">		 
			     	<xsl:value-of  select="@kind"/>
			    </xsl:attribute>
				<xsl:value-of select="."/>
			</xsl:element>
		 
		</xsl:for-each>
	    
	  </xsl:element>
  </xsl:if>  
  
  <!--Se mira si añadir invariante--> 
  <xsl:if test="name(following-sibling::*[1])='Choice_inv'">    
	  <xsl:element name="location">
	   <xsl:attribute name="id">	   
	    
	    <xsl:if test = "contains(@id,'End_') and contains(@id,'Init_') ">
	    	<xsl:value-of select="concat('Init_',substring-after(@id,'Init_'))"/>
	    </xsl:if>
	    
	    <xsl:if test = "not(contains(@id,'End_') and contains(@id,'Init_'))">
	    	<xsl:value-of select="@id"/>
	    </xsl:if>
		
	   </xsl:attribute>
	   <name>
	   <xsl:if test = "contains(name/@id,'End_') and contains(name/@id,'Init_') ">
	    	<xsl:value-of select="concat('Init_',substring-after(name/@id,'Init_'))"/>
	    </xsl:if>
	    
	    <xsl:if test = "not(contains(name/@id,'End_') and contains(name/@id,'Init_'))">
	    	<xsl:value-of select="name/@id"/>
	    </xsl:if>
	   </name>  
	   
	    <!--Si tiene un invariante se debe mantener-->
	  	<xsl:if test="not(following-sibling::*[1]/inv='Inf')">	  		
	  		  			  		
	  			<xsl:for-each select="following-sibling::*[1]/inv">
	  				
	  				<xsl:variable name="clock" select="normalize-space(substring-before(.,'&lt;='))"/>
	  				<xsl:variable name="timeout" select="normalize-space(substring-after(.,'&lt;='))"/>
	  				
	  				<xsl:if test="not(following-sibling::*[(normalize-space(substring-before(.,'&lt;='))=$clock) and (normalize-space(substring-after(.,'&lt;='))>$timeout)])
	  						and not(preceding-sibling::*[(normalize-space(substring-before(.,'&lt;='))=$clock) and (normalize-space(substring-after(.,'&lt;='))>$timeout)])">
	  				
	  					<xsl:element name="label">
					  		<xsl:attribute name="kind">		 
						     	<xsl:value-of  select="'invariant'"/>
		  					</xsl:attribute>	  					
	  						<xsl:value-of select="$clock"/> &lt;= <xsl:value-of select="$timeout + 1"/>	  						
	  					</xsl:element>
	  				
	  				</xsl:if>					
					
				</xsl:for-each>			
			
		</xsl:if>
	    
	  </xsl:element>
	  
  </xsl:if>
 </xsl:for-each>
  
  
 
 
 <!--Se mantienen los TRANSITION pero sustituyendo aquellos location que ya no existan por aquellos en los que se han unificado--> 
 <xsl:for-each select="transition">
 
  <xsl:element name="transition">
  
  	<!--Se añaden los label que tenga, salvo los assignment y los guard--> 
  	<xsl:for-each select="label[@kind!='assignment' and @kind!='guard']">	  
  		<xsl:element name="label">
  		<xsl:attribute name="kind">		 
	     	<xsl:value-of  select="@kind"/>
	    </xsl:attribute>
	    <xsl:value-of select="."/>
	    </xsl:element>
	</xsl:for-each>
	
	<!--Se añaden todas las asignaciones separadas por comas-->
	<xsl:variable name="source" select="source/@ref"/>	
	<xsl:variable name="target" select="target/@ref"/>
	
	<!--Se debe añadir un reseteo de reloj si el siguiente location tiene un invariante de reloj-->
  	<xsl:if test="(../location[contains(@id,$target) and contains(label,'&lt;=')])">
  		<xsl:element name="label">
  		<xsl:attribute name="kind">		 
	     	<xsl:value-of  select="'assignment'"/>
	    </xsl:attribute>
	    <xsl:value-of select="normalize-space(substring-before(../location[contains(@id,$target)]/label,'&lt;='))"/><xsl:text>=0</xsl:text>
	    
	    <xsl:for-each select="label[@kind='assignment']">
	    	<xsl:text>,</xsl:text><xsl:value-of select="."/>
	    </xsl:for-each>
	    
	    </xsl:element>
	</xsl:if>
	
	<xsl:if test="not(../location[contains(@id,$target) and contains(label,'&lt;=')]) and label[@kind='assignment']">		
		
  		<xsl:element name="label">
	  		<xsl:attribute name="kind">		 
		     	<xsl:value-of  select="'assignment'"/>
		    </xsl:attribute>	    
	    	
	    	<xsl:for-each select="label[@kind='assignment']">
	    	
		    	<xsl:if test="position()=1">	    	    
		    		<xsl:value-of select="."/>
		    	</xsl:if>
		    	
		    	<xsl:if test="not(position()=1)">	    	    
		    		<xsl:text>,</xsl:text><xsl:value-of select="."/>
		    	</xsl:if>
	    	
	    	</xsl:for-each>	
	    	
	    	<!--Se debe añadir una asignacion a 1 si el destino es un Exception y se encuentra dentro de un parallel-->
	    	<xsl:if test="($target='Exception') and (../location[contains(@id,$source)]/label[contains(.,'var_parallel') and contains(.,'==')])">
	    		
		    	<xsl:for-each select="../location[contains(@id,$source)]/label[contains(.,'var_parallel') and contains(.,'==')]">	    	
		    		
		    		<xsl:text>,</xsl:text><xsl:value-of select="concat(substring-before(.,'=='),'=1')"/>
		    	
		    	</xsl:for-each>
	    	
	    	</xsl:if>
	    		    	
	    </xsl:element>
	    
	</xsl:if>
	
	<!--Se deben añadir las guardas, incluyendo que las variables de control de los parallel sean igual a 0-->
	<xsl:for-each select="label[@kind='guard']">	  
  		<xsl:element name="label">
  		<xsl:attribute name="kind">		 
	     	<xsl:value-of  select="@kind"/>
	    </xsl:attribute>
	    <xsl:text>(</xsl:text><xsl:value-of select="normalize-space(.)"/><xsl:text>)</xsl:text>
	    
	    <xsl:if test="($target!='Exception') and (../location[contains(@id,$source)]/label[contains(.,'var_parallel') and contains(.,'==')])">	  
	    	    
		    <xsl:for-each select="../location[contains(@id,$source)]/label[contains(.,'var_parallel') and contains(.,'==')]">
		    
		    	<xsl:text> and (</xsl:text><xsl:value-of select="."/><xsl:text>)</xsl:text>
		    
		    </xsl:for-each>
	    
	    </xsl:if>
	    
	    </xsl:element>
	    
	</xsl:for-each>
	
	<xsl:if test="not(label[@kind='guard']) and ($target!='Exception') and (../location[contains(@id,$source)]/label[contains(.,'var_parallel') and contains(.,'==')])">	  
  		<xsl:element name="label">
  		<xsl:attribute name="kind">		 
	     	<xsl:value-of  select="'guard'"/>
	    </xsl:attribute>
	    
	    <xsl:for-each select="../location[contains(@id,$source)]/label[contains(.,'var_parallel') and contains(.,'==')]">
	    	
	    	<xsl:if test="position()=1">	    	    
	    		<xsl:text>(</xsl:text><xsl:value-of select="."/><xsl:text>)</xsl:text>
	    	</xsl:if>
	    
	    	<xsl:if test="not(position()=1)">	    	    
	    		<xsl:text>and (</xsl:text><xsl:value-of select="."/><xsl:text>)</xsl:text>
	    	</xsl:if>	    	
	    
	    </xsl:for-each>
	    
	    </xsl:element>
	    
	</xsl:if>
	
	
	<xsl:element name="source">
      <xsl:attribute name="ref">
      	
      	<!--Si existe un location con ese nombre se deja como esta--> 
      	<xsl:if test="../location[@id=$source]">
        	<xsl:value-of  select="$source"/>
        </xsl:if>
        
        <!--Si no existe un location con ese nombre se busca el que lo contenga--> 
        <xsl:if test="not(../location[@id=$source])">
        	
        	<xsl:if test = "contains(../location[contains(@id,$source)]/@id,'End_') and contains(../location[contains(@id,$source)]/@id,'Init_') ">
		    	<xsl:value-of select="concat('Init_',substring-after(../location[contains(@id,$source)]/@id,'Init_'))"/>
		    </xsl:if>
		    
		    <xsl:if test = "not(contains(../location[contains(@id,$source)]/@id,'End_') and contains(../location[contains(@id,$source)]/@id,'Init_'))">
		    	<xsl:value-of select="../location[contains(@id,$source)]/@id"/>
		    </xsl:if>			
		           	
        </xsl:if>        
     
      </xsl:attribute>
    </xsl:element>
   <xsl:element name="target">
      <xsl:attribute name="ref">        
              	
      	<!--Si existe un location con ese nombre se deja como esta--> 
      	<xsl:if test="../location[@id=$target]">
        	<xsl:value-of  select="$target"/>
        </xsl:if>
        
        <!--Si no existe un location con ese nombre se busca el que lo contenga--> 
        <xsl:if test="not(../location[@id=$target])">
        	
        	<xsl:if test = "contains(../location[contains(@id,$target)]/@id,'End_') and contains(../location[contains(@id,$target)]/@id,'Init_') ">
		    	<xsl:value-of select="concat('Init_',substring-after(../location[contains(@id,$target)]/@id,'Init_'))"/>
		    </xsl:if>
		    
		    <xsl:if test = "not(contains(../location[contains(@id,$target)]/@id,'End_') and contains(../location[contains(@id,$target)]/@id,'Init_'))">
		    	<xsl:value-of select="../location[contains(@id,$target)]/@id"/>
		    </xsl:if>

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

<instantiation></instantiation>
<system>

  <xsl:text>system </xsl:text>
 
 <!--Se deben añadir los templates que se han usado en los parallel además del principal-->
 
 <xsl:for-each select="nta/template">
 
 	<xsl:variable name="nombre" select="name"/>
 	<xsl:variable name="string_after" select="substring-after(//system,$nombre)"/>
	<xsl:variable name="string_before" select="substring-before(//system,$nombre)"/> 
	<xsl:variable name="char_after" select="substring($string_after,1,1)"/>
	<xsl:variable name="char_before" select="substring($string_before,string-length($string_before)-1,1)"/>
	 	
 	<xsl:if test="not(contains(//system,name)) or ( $char_before=' ' and ( $char_after=',' or $char_after=';') )">
		 
		 <xsl:value-of select="name"/>
		 <xsl:text>, </xsl:text>
		 
	</xsl:if>

 </xsl:for-each>
 
 <xsl:value-of select="//system"/>
</system>
</nta>
</xsl:template>

</xsl:stylesheet>
