<?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="/">
		<package>		    
			<xsl:attribute name="author">
				<xsl:value-of select="package/@author"/>
			</xsl:attribute>
			<xsl:attribute name="name">
				<xsl:value-of select="package/@name"/>
			</xsl:attribute>
			<xsl:attribute name="version">
				<xsl:value-of select="package/@version"/>
			</xsl:attribute>
			<xsl:comment>Declaracion de tipos</xsl:comment>
			
             <xsl:for-each select="package/informationType">
               <!--Comprobar que tenga nombre y que sea distinto del nombre del hermano siguiente-->
			   <xsl:if test="((@name!='') and not(@name=following-sibling::informationType/@name ))">			   
			   	 <informationType>
			      <xsl:attribute name="name">
			        <xsl:value-of  select="@name"/>
			      </xsl:attribute>
		   	      <xsl:attribute name="type">
			        <xsl:value-of select="@type"/>
			      </xsl:attribute>
		         </informationType>
			   </xsl:if>
			 </xsl:for-each>

			<xsl:comment>Declaracion de Tokens</xsl:comment>
			<xsl:for-each select="package/token">
			  <xsl:if test="@name!='UMLprofRef'">
				<token>
					<xsl:attribute name="name">
						<xsl:value-of select="@name"/>
					</xsl:attribute>
					<xsl:attribute name="informationType">
						<xsl:value-of select="@informationType"/>
					</xsl:attribute>
				</token>
			  </xsl:if>
			</xsl:for-each>
			<xsl:comment>Declaracion de Roles</xsl:comment>
			<xsl:for-each select="package/roleType">
			  <xsl:if test="@name!='UMLprofRoleType'">
				<roleType>
					<xsl:attribute name="name">
						<xsl:value-of select="@name"/>
					</xsl:attribute>
					<behaviour>
						<xsl:attribute name="name">
							<xsl:value-of select="behaviour/@name"/>
						</xsl:attribute>
					</behaviour>
				</roleType>
			   </xsl:if>
			</xsl:for-each>
			<xsl:comment>Declaracion de Participants</xsl:comment>
			<xsl:for-each select="package/participantType">
			  <xsl:if test="@name!='UMLprofRoleType'">
				<participantType>
					<xsl:attribute name="name">
						<xsl:value-of select="@name"/>
					</xsl:attribute>
					<role>
						<xsl:attribute name="type">
							<xsl:value-of select="role/@type"/>
						</xsl:attribute>
					</role>
				</participantType>
			   </xsl:if>
			</xsl:for-each>
			<xsl:comment>Declaracion de Relationship</xsl:comment>
             <relation>
		     <xsl:for-each select="package/relation">
		      
		       <xsl:for-each select="child::*">
			    
			   
			    <xsl:variable name="relation" select="@name"/> 
				
                <rel>
			      <relationship><xsl:value-of select="$relation"/></relationship>
				  <xsl:variable name="rol1" select="role1/@type"/>
				  <xsl:variable name="rol2" select="role2/@type"/>
				  <role1><xsl:value-of select="role1/@type"/></role1>
				  <role2><xsl:value-of select="role2/@type"/></role2>
			      <xsl:variable name="repe"/>
				  <xsl:for-each select="preceding-sibling::*">
				
				     
                    <xsl:if test="($rol2=role1/@type) and ($rol1=role2/@type)">
					 <j>
			          <xsl:value-of select="$rol2"/>
					  <xsl:value-of select="role1/@type"/>
					  </j>
					  <canalrep>
					   si
					  </canalrep>
					</xsl:if>
			
                    <xsl:if test="$relation=@name">
				       <repe>si</repe>
                 
                 			
				    </xsl:if>
			
			
				  </xsl:for-each>
			   
                </rel>
			   </xsl:for-each>
		 
		         
		     </xsl:for-each>
	
		    </relation>
			<xsl:comment>Declaracion de los canales</xsl:comment>

			<xsl:for-each select="package/channelType">
				<xsl:variable name="canal" select="@name"/>
				<xsl:if test="not($canal=following-sibling::*/@name)">


					<channelType>
						<xsl:attribute name="name">
							<xsl:value-of select="@name"/>
						</xsl:attribute>
						<xsl:for-each select="role">
							<role>
								<xsl:attribute name="type">
									<xsl:value-of select="@type"/>
								</xsl:attribute>
							</role>
						</xsl:for-each>
						<xsl:for-each select="reference">
							<reference>
								<token>
									<xsl:attribute name="name">
										<xsl:value-of select="token/@name"/>
									</xsl:attribute>
								</token>
							</reference>
						</xsl:for-each>
					</channelType>
				</xsl:if>
			</xsl:for-each>
			<xsl:comment>Declaracion de la Coreografia</xsl:comment>
			<choreography>
				<xsl:attribute name="name">
					<xsl:value-of select="package/choreography/@name"/>
				</xsl:attribute>
				<description>
					<xsl:attribute name="type">
						<xsl:value-of select="package/choreography/description/@type"/>
					</xsl:attribute>
				</description>				
				
				<variableDefinitions>
				<xsl:comment>Declaracion de las variables intercambiadas en los interaction y excepciones</xsl:comment>
				
				<xsl:call-template name="VariableInteractions"/>				
				
				<xsl:comment>Declaracion de la variable de reloj</xsl:comment>
				
				<xsl:call-template name="VariableClock"/>				
				</variableDefinitions>
				
				<xsl:comment>Asignacion de los valores iniciales de las variables</xsl:comment>
				
				<xsl:call-template name="VariableAssign"/>
				
				<sequence>
				
				<xsl:for-each select="package/choreography/sequence/*">
					
					<xsl:sort select="orden" data-type="number"/>
															
					<!--Ver si se trata de un mensaje o de un frame-->
					
					<xsl:if test="name()='interaction'">
																
						<xsl:value-of select="orden"/>
						<interaction>
							<xsl:attribute name="name">
								<xsl:value-of select="@name"/>
							</xsl:attribute>

							<xsl:attribute name="operation">
								<xsl:value-of select="@operation"/>
							</xsl:attribute>
							<xsl:attribute name="channelVariable">
								<xsl:value-of select="@channelVariable"/>
							</xsl:attribute>
							
							<xsl:attribute name="parent">
								<xsl:value-of select="@parent"/>
							</xsl:attribute>
							
							<xsl:if test="@parseq">
								<xsl:attribute name="parseq">
									<xsl:value-of select="@parseq"/>
								</xsl:attribute>
							</xsl:if>
							
							<xsl:if test="@altseq">
								<xsl:attribute name="altseq">
									<xsl:value-of select="@altseq"/>
								</xsl:attribute>
							</xsl:if>
							
							<xsl:if test="@altelseseq">
								<xsl:attribute name="altelseseq">
									<xsl:value-of select="@altelseseq"/>
								</xsl:attribute>
							</xsl:if>							
														
							<participate>
								<xsl:attribute name="relationshipType">
									<xsl:value-of select="participate/@relationshipType"/>
								</xsl:attribute>
								<xsl:attribute name="fromRole">
									<xsl:value-of select="participate/@fromRole"/>
								</xsl:attribute>
								<xsl:attribute name="toRole">
									<xsl:value-of select="participate/@toRole"/>
								</xsl:attribute>
							</participate>
							
							<!--Añadir la información de los exchanges propios-->
							<xsl:for-each select="exchange">
								<exchange>		
									<xsl:attribute name="name">
										<xsl:value-of select="@name"/>
									</xsl:attribute>
									
									<xsl:if test="@faultName">
										<xsl:attribute name="faultName">
											<xsl:value-of select="@faultName"/>
										</xsl:attribute>
									</xsl:if>
									
									<xsl:attribute name="action">
										<xsl:value-of select="@action"/>
									</xsl:attribute>
									
									<xsl:if test="send">
									<send>															
										<xsl:attribute name="variable">
											<xsl:value-of select="send/@variable"/>
										</xsl:attribute>
										
										<xsl:if test="send/@causeException">
											<xsl:attribute name="causeException">
												<xsl:value-of select="send/@causeException"/>
											</xsl:attribute>
										</xsl:if>
									
									</send>
									</xsl:if>
									
									<xsl:if test="receive">
									<receive>															
										<xsl:attribute name="variable">
											<xsl:value-of select="receive/@variable"/>
										</xsl:attribute>
										
										<xsl:if test="receive/@causeException">
											<xsl:attribute name="causeException">
												<xsl:value-of select="receive/@causeException"/>
											</xsl:attribute>
										</xsl:if>
									
									</receive>
									</xsl:if>													
								</exchange>
							</xsl:for-each>
													
							<!--Añadir las actualizaciones de las variables-->
							<xsl:for-each select="record">
							
								<record>		
									<xsl:attribute name="name">
										<xsl:value-of select="@name"/>
									</xsl:attribute>
									<xsl:attribute name="when">
										<xsl:value-of select="@when"/>
									</xsl:attribute>
																			
									<source>
										<xsl:attribute name="variable">
											<xsl:value-of select="source/@variable"/>
										</xsl:attribute>
									</source>
									
									<target>
										<xsl:attribute name="variable">
											<xsl:value-of select="target/@variable"/>
										</xsl:attribute>
									</target>
									
								</record>
																
							</xsl:for-each>
							
							<!--Añadir el timeout si lo haya-->
							<xsl:if test="timeout">	
	
								<timeout>									
									<xsl:value-of select="timeout"/>									
						        </timeout>
							</xsl:if>
						
						</interaction>
												
					</xsl:if>
					
					<xsl:if test="name()='frames'">						
							<xsl:value-of select="orden"/>
							<frames>
								<xsl:attribute name="name">
									<xsl:value-of select="@name"/>
								</xsl:attribute>
								
								<xsl:attribute name="idframe">
									<xsl:value-of select="@idframe"/>
								</xsl:attribute>
								
								<xsl:if test="@guard">
									<xsl:attribute name="guard">
										<xsl:value-of select="@guard"/>
									</xsl:attribute>
								</xsl:if>
								
								<xsl:if test="@repeat">
									<xsl:attribute name="repeat">
										<xsl:value-of select="@repeat"/>
									</xsl:attribute>
								</xsl:if>
								
								<xsl:if test="@guard_invert">
									<xsl:attribute name="guard_invert">
										<xsl:value-of select="@guard_invert"/>
									</xsl:attribute>
								</xsl:if>
								
								<xsl:if test="@repeat_invert">
									<xsl:attribute name="repeat_invert">
										<xsl:value-of select="@repeat_invert"/>
									</xsl:attribute>
								</xsl:if>
								
								<xsl:if test="@exception">
									<xsl:attribute name="exception">
										<xsl:value-of select="@exception"/>
									</xsl:attribute>
								</xsl:if>
								
								<xsl:attribute name="elements">
									<xsl:value-of select="@elements"/>
								</xsl:attribute>
								
								<xsl:attribute name="parent">
									<xsl:value-of select="@parent"/>
								</xsl:attribute>
								
								<xsl:if test="@parseq">
									<xsl:attribute name="parseq">
										<xsl:value-of select="@parseq"/>
									</xsl:attribute>
								</xsl:if>
								
								<xsl:if test="@altseq">
									<xsl:attribute name="altseq">
										<xsl:value-of select="@altseq"/>
									</xsl:attribute>
								</xsl:if>
								
								<xsl:if test="@altelseseq">
									<xsl:attribute name="altelseseq">
										<xsl:value-of select="@altelseseq"/>
									</xsl:attribute>
								</xsl:if>
								
								<xsl:attribute name="block">
									<xsl:value-of select="@block"/>
								</xsl:attribute>
								
							</frames>	
					</xsl:if>
					
					<xsl:if test="name()='frames_end'">						
							<xsl:value-of select="orden"/>
							<frames_end>
								<xsl:attribute name="name">
									<xsl:value-of select="@name"/>
								</xsl:attribute>
								
								<xsl:attribute name="idframe">
									<xsl:value-of select="@idframe"/>
								</xsl:attribute>
								
								<xsl:if test="@repeat">
									<xsl:attribute name="repeat">
										<xsl:value-of select="@repeat"/>
									</xsl:attribute>
								</xsl:if>
	
								<xsl:if test="@guard">
									<xsl:attribute name="guard">
										<xsl:value-of select="@guard"/>
									</xsl:attribute>
								</xsl:if>
								
							</frames_end>
					</xsl:if>
					
				</xsl:for-each>
				
				</sequence>
				
			</choreography>
		</package>
	</xsl:template>
	
	<xsl:template name="VariableInteractions">			
			<xsl:for-each select="package/choreography/variableDefinitions/variable">
				<xsl:if test="(@informationType) and (@informationType!='tns:Clock')">
					<variable>
						<xsl:attribute name="name">
							<xsl:value-of select="@name"/>
						</xsl:attribute>
	
						<xsl:attribute name="informationType">
								<xsl:value-of select="@informationType"/>                                         
						</xsl:attribute>
						
						<xsl:if test="@roleTypes">							
							<xsl:attribute name="roleTypes">
								<xsl:value-of select="@roleTypes"/>                                         
							</xsl:attribute>
						</xsl:if>
						
					</variable>	
				</xsl:if>	
			</xsl:for-each>			
	</xsl:template>
		
	<xsl:template name="VariableClock">			
			<xsl:if test="(package/choreography/variableDefinitions/variable/@informationType='tns:Clock') and (package/choreography/variableDefinitions/variable/@name='Clock1')">				
					<variable>
						<xsl:attribute name="name">
							<xsl:value-of select="'Clock1'"/>
						</xsl:attribute>
	
						<xsl:attribute name="informationType">
								<xsl:text>tns:</xsl:text>
								<xsl:value-of select="'Clock'"/>                                         
						</xsl:attribute>		
					</variable>	
			</xsl:if>	
			
			<!--Declarar los relojes locales utilizados para los delay-->		
			<xsl:for-each select="package/choreography/variableDefinitions/variable">
				<xsl:if test="(@informationType='tns:Clock') and (@name!='Clock1')">
					<variable>
						<xsl:attribute name="name">
							<xsl:value-of select="@name"/>
						</xsl:attribute>	
						<xsl:attribute name="informationType">
								<xsl:value-of select="@informationType"/>                                         
						</xsl:attribute>		
						<xsl:if test="@role">
							<xsl:attribute name="role">
								<xsl:value-of select="@role"/>
							</xsl:attribute>							
						</xsl:if>
						<xsl:if test="@roleTypes">							
							<xsl:attribute name="roleTypes">
								<xsl:value-of select="@roleTypes"/>                                         
							</xsl:attribute>
						</xsl:if>
					</variable>	
				</xsl:if>	
			</xsl:for-each>	
	</xsl:template>
	
	<xsl:template name="VariableAssign">			
			<xsl:for-each select="package/choreography/assign">
				
					<assign>
						<xsl:attribute name="roleType">
							<xsl:value-of select="@roleType"/>
						</xsl:attribute>
	
						<copy>
						
							<xsl:attribute name="name">
								<xsl:value-of select="copy/@name"/>
							</xsl:attribute>
							
							<source>
								<xsl:attribute name="expression">
									<xsl:value-of select="copy/source/@expression"/>
								</xsl:attribute>
							</source>
							
							<target>
								<xsl:attribute name="variable">
									<xsl:value-of select="copy/target/@variable"/>
								</xsl:attribute>
							</target>
						
						</copy>						
					</assign>	
				
			</xsl:for-each>			
	</xsl:template>
	
</xsl:stylesheet>