<!--****************************************************************************************************-->
<!--																									-->
<!--   Converts Rhapsody XMI to Interchange3 XML                                						-->
<!--																									-->
<!--****************************************************************************************************-->
<xsl:stylesheet version="1.0"
	xmlns="http://www.tridsys.com/InterchangeSE/3.0/InterchangeXML" 
	xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xmlns:UML="omg.org/UML13" 
	xmlns:str="http://exslt.org/strings"
	extension-element-prefixes="str">

	<xsl:variable name="NOT_MAPPED">NOT_MAPPED_IN_I3</xsl:variable>
	<!--Formats the output of the transformation.-->
	<xsl:output method="xml" version="1.0" encoding="ISO-8859-1" indent="yes"/>
	<xsl:variable name="xmiContent" select="XMI.content"/>


	<!-- ******************************** -->
	<!-- BEGIN SUB-TEMPLATE SECTION -->
	<!-- ******************************** -->
	<xsl:template name="outputStandardRelation">
		<xsl:param name="relName"/>
		<xsl:param name="relationType" select="DEFAULTRELATION"/>
		<xsl:param name="sourceUName" select="@xmi.id"/>
		<xsl:param name="sourceXSIType" select="DEFAULTSOURCETYPE"/>
		<xsl:param name="targetUName" select="@xmi.id"/>
		<xsl:param name="targetXSIType" select="DEFAULTTARGETTYPE"/>
		<xsl:if test="($sourceXSIType != $NOT_MAPPED) and ($targetXSIType != $NOT_MAPPED) and not(starts-with($relationType, $NOT_MAPPED))">
			<xsl:element name="Relation">
				<xsl:attribute name="xsi:type">
					<xsl:value-of select="$relationType"/>
				</xsl:attribute>
				<xsl:element name="uniqueName">
					<xsl:value-of select="concat($sourceUName, ' ', $relationType, ' ', $targetUName)"/>
				</xsl:element>
				<xsl:if test="$relName">
					<xsl:element name="relName">
						<xsl:value-of select="$relName"/>
					</xsl:element>
				</xsl:if>
				<xsl:element name="Source">
					<xsl:attribute name="className">
						<xsl:value-of select="$sourceXSIType"/>
					</xsl:attribute>
					<xsl:element name="uniqueName">
						<xsl:value-of select="$sourceUName"/>
					</xsl:element>
				</xsl:element>
				<xsl:element name="Target">
					<xsl:attribute name="className">
						<xsl:value-of select="$targetXSIType"/>
					</xsl:attribute>
					<xsl:element name="uniqueName">
						<xsl:value-of select="$targetUName"/>
					</xsl:element>
				</xsl:element>
			</xsl:element>
		</xsl:if>
	</xsl:template>
	<!-- ******************************** -->
	<!-- ******************************** -->
	<xsl:template name="findElementTypeMapping">
		<xsl:param name="elementUniqueName" select="@xmi.id"/>
		<xsl:param name="rootNode" select="/"/>
		<xsl:variable name="elementType" select="name($rootNode//*[@xmi.id=$elementUniqueName])"/>
		<xsl:choose>
			<xsl:when test="$elementType='UML:Action'">Action</xsl:when>
			<xsl:when test="$elementType='UML:Signal'">Branch</xsl:when>
			<xsl:when test="$elementType='UML:SignalEvent'">Event</xsl:when>
			<xsl:when test="$elementType='UML:SimpleState'">ProcessActivity</xsl:when>
			<xsl:when test="$elementType='UML:State'">ProcessActivity</xsl:when>
			<xsl:otherwise>
				<xsl:value-of select="$elementType"/>
			</xsl:otherwise>
		</xsl:choose>
	</xsl:template>



	<!-- ******************************** -->
	<!-- ******************************** -->
	<xsl:template name="handleSignal_Relation">
		<xsl:comment>Signal Relation</xsl:comment>
		<xsl:for-each select="//UML:Signal">
			<xsl:if test="@occurrence">
				<xsl:variable name="SRID" select="@xmi.id"/>
				<xsl:variable name="docRoot" select="/"/>
				<xsl:for-each select="str:tokenize(@occurrence,' ')">
					<xsl:variable name="etm">
						<xsl:call-template name="findElementTypeMapping">
							<xsl:with-param name="elementUniqueName" select="."/>
							<xsl:with-param name="rootNode" select="$docRoot"/>
						</xsl:call-template>
					</xsl:variable>
					<xsl:call-template name="outputStandardRelation">
						<xsl:with-param name="relationType">TriggersTransition</xsl:with-param>
						<xsl:with-param name="sourceUName" select="."/>
						<xsl:with-param name="sourceXSIType" select="$etm"/>
						<xsl:with-param name="targetUName" select="$SRID"/>
						<xsl:with-param name="targetXSIType">Branch</xsl:with-param>
					</xsl:call-template>
					<xsl:call-template name="outputStandardRelation">
						<xsl:with-param name="relationType">CorrespondsTo</xsl:with-param>
						<xsl:with-param name="sourceUName" select="."/>
						<xsl:with-param name="sourceXSIType" select="$etm"/>
						<xsl:with-param name="targetUName" select="$SRID"/>
						<xsl:with-param name="targetXSIType">Branch</xsl:with-param>
					</xsl:call-template>
				</xsl:for-each>
			</xsl:if>
		</xsl:for-each>
	</xsl:template>

	
	<!-- ******************************** -->
	<!-- ******************************** -->
	<!-- END TEMPLATE SECTION -->
	<xsl:template name="handleModel">
		<xsl:for-each select="//UML:Model">
			<xsl:element name="TSIModel">

				<!--Object handling-->
				<xsl:element name="NodeGroup">
					<xsl:element name="Object">
						<xsl:attribute name="xsi:type">Package</xsl:attribute>
						<xsl:element name="uniqueName">
							<xsl:value-of select="concat(@xmi.id, 'PARENT_PACKAGE')"/>
						</xsl:element>
						<xsl:element name="topLevel">
							<xsl:value-of select="true()"/>
						</xsl:element>
					</xsl:element>
				</xsl:element>
				<!--Relationship handling-->
				<xsl:element name="EdgeGroup">
					<!-- TEST AREA CONTAINS DUPLICATE TEMPLATE CALLS -->
					<xsl:call-template name="handleSignal_Relation"/>
				</xsl:element>
			</xsl:element>
		</xsl:for-each>
	</xsl:template>
	<!-- ******************************** -->
	<!-- ******************************** -->
	<!--Starting template-->
	<xsl:template match="XMI">
		<InterchangeXML xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.tridsys.com/InterchangeSE/3.0/InterchangeXML">
			<xsl:call-template name="handleModel"/>
		</InterchangeXML>
	</xsl:template>
</xsl:stylesheet>