<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" 
	xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
	xmlns:xs="http://www.w3.org/2001/XMLSchema" 
	xmlns:fn="http://www.w3.org/2004/07/xpath-functions" 
	xmlns:xdt="http://www.w3.org/2004/07/xpath-datatypes"
	xmlns:p="http://protege.stanford.edu/xml">
	
	<xsl:function name="p:slotvalue">
		<xsl:param name="slot"/>
		<xsl:param name="name"/>
		<xsl:sequence select="$slot/p:own_slot_value[p:slot_reference=$name]/p:value"/>
	</xsl:function>

	<xsl:template name="collectslots">
		<xsl:param name="class" required="yes"/>
		<xsl:variable name="mine">
			<xsl:call-template name="concatslots">
				<xsl:with-param name="list" select="$class/p:template_slot"/>
			</xsl:call-template>
		</xsl:variable>		
		<xsl:variable name="superslots">
			<xsl:call-template name="superslots">
				<xsl:with-param name="supers" select="$class/p:superclass"/>
			</xsl:call-template>
		</xsl:variable>
		<xsl:sequence select="$mine | $superslots"/>
	</xsl:template>
	
	<xsl:template name="superslots">
		<xsl:param name="supers" required="yes"/>		
		<xsl:choose>
			<xsl:when test="not(empty($supers))">
				<xsl:variable name="first">
					<xsl:call-template name="collectslots">
						<xsl:with-param name="class" select="key('classes', $supers[1])"/>
					</xsl:call-template>
				</xsl:variable>				
				<xsl:variable name="rest">
					<xsl:call-template name="superslots">
						<xsl:with-param name="supers" select="$supers[position() != 1]"/>
					</xsl:call-template>
				</xsl:variable>
				<xsl:sequence select="$first | $rest"/>
			</xsl:when>
			<xsl:otherwise></xsl:otherwise>
		</xsl:choose>
	</xsl:template>

	<xsl:template name="concatslots">
		<xsl:param name="list" required="yes"/>
		<xsl:param name="one" tunnel="yes"/>
		<xsl:choose>
			<xsl:when test="not(empty($list))">
				<xsl:variable name="first">
					<xsl:choose>
						<xsl:when test="$one"><xsl:sequence select="key('slots', $list[1])[p:own_slot_value[p:slot_reference = ':SLOT-MAXIMUM-CARDINALITY']/p:value = $one]"/></xsl:when>
						<xsl:otherwise><xsl:sequence select="key('slots', $list[1])"/></xsl:otherwise>
					</xsl:choose>
				</xsl:variable>
				<xsl:variable name="rest">
					<xsl:call-template name="concatslots">
						<xsl:with-param name="list" select="$list[position() != 1]"/>
					</xsl:call-template>
				</xsl:variable>
				<xsl:sequence select="$first | $rest"/>
			</xsl:when>
			<xsl:otherwise></xsl:otherwise>
		</xsl:choose>
	</xsl:template>

	<xsl:template name="modelquality">
		<xsl:for-each select="//p:slot">
			<xsl:sort select="p:name"/>
			<xsl:variable name="name" select="p:name"/>
			<xsl:variable name="class" select="p:slotvalue(., ':SLOT-VALUE-TYPE')[@value_type = 'class']"/>
			<xsl:variable name="inverse" select="p:slotvalue(., ':SLOT-INVERSE')"/>
			<xsl:if test="not(//p:class[p:template_slot = $name])">
Unused slot: <xsl:value-of select="$name"/><xsl:text>
</xsl:text>
			</xsl:if>

			<xsl:if test="$class and not($inverse)">
No inverse for: <xsl:value-of select="$name"/><xsl:text>
</xsl:text>
			</xsl:if>
		</xsl:for-each>
	</xsl:template>
</xsl:stylesheet>
