<?xml version="1.0" encoding="ISO-8859-1"?>
<!--COPYRIGHT (C) 2001-2004 Mobile Commerce Laboratory -  School of Computer Science - Carnegie Mellon University 5000 Forbes Avenue - Pittsburgh, PA 15213-3890 - ALL RIGHTS RESERVED.-->
<!-- Transforms the rules in OWL into rules in JESS based on the description of the meta-model of OWL -->

<!DOCTYPE uridef[
  <!ENTITY rdf "http://www.w3.org/1999/02/22-rdf-syntax-ns">
  <!ENTITY rdfs "http://www.w3.org/2000/01/rdf-schema">
  <!ENTITY owl "http://www.w3.org/2002/07/owl">
  <!ENTITY xsd "http://www.w3.org/2000/10/XMLSchema">
  <!ENTITY rowl "http://mycampus.cs.cmu.edu/ROWL">
  <!ENTITY nbsp "&#160;">
]>

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"
 xmlns:rdf="&rdf;#"
 xmlns:rdfs="&rdfs;#"
 xmlns:owl="&owl;#"
 xmlns:xsd="&xsd;#"
 xmlns:rowl="&rowl;#"
 >
  <!-- namespace of these rules e.g. http://www-2.cs.cmu.edu/rules# -->
  <xsl:param name='namespace' select="concat(/rdf:RDF/@xml:base,'#')"/>
  
  <xsl:param name="type" select="'false'"/>

  <xsl:key name="variable_lookup" match="rowl:Variable" use="@rdf:ID"/>
  
  <xsl:param name='variable_uri' select="'http://mycampus.cs.cmu.edu/variable#'"/>
  
  <xsl:output method="text" media-type="text/plain" encoding="ISO-8859-1" omit-xml-declaration="yes" indent="no"/>

  <xsl:template match="/"><xsl:apply-templates /></xsl:template>
  
  <xsl:template match="rdf:RDF"><xsl:apply-templates /></xsl:template>
  
  <!-- transform these rules into JESS rules - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -->
  <!-- templates use parameters to modify the type of triples requested/produced -->
  
  <!-- define a rule -->
  <xsl:template match="/rdf:RDF/rowl:Rule">
(defrule <xsl:value-of select="translate(normalize-space(rdfs:label),' -','__')"/> 
    <xsl:for-each select="rowl:body/*"><xsl:call-template name="process-class-instance"><xsl:with-param name="assert">false</xsl:with-param></xsl:call-template></xsl:for-each>
   => 
   <xsl:for-each select="rowl:head/*"><xsl:call-template name="process-class-instance"><xsl:with-param name="assert">true</xsl:with-param></xsl:call-template></xsl:for-each>
)
  </xsl:template>

  <xsl:template match="*" />

  <!-- named templates - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -->

  <!-- process an instance of a class -->
  <xsl:template name="process-class-instance" >
  <xsl:param name="assert" select="'false'"/>
  <xsl:if test="$assert='false' or $type!='false'"> 
  <xsl:if test="concat(namespace-uri(.),local-name(.))!='http://mycampus.cs.cmu.edu/ROWL#Variable'">
<xsl:if test="$assert='true'">
 (assert </xsl:if>
  (triple
   (predicate "&rdf;#type")
   (subject <xsl:if test="@rdf:ID">
   <xsl:call-template name="local-ID">
   <xsl:with-param name="id">
   <xsl:value-of select="@rdf:ID"/>
   </xsl:with-param>
   </xsl:call-template>
   </xsl:if>
   <xsl:if test="@rdf:about">
   <xsl:call-template name="local-ID">
   <xsl:with-param name="id">
   <xsl:value-of select="@rdf:about"/>
   </xsl:with-param></xsl:call-template>
   </xsl:if>)
   (object    "<xsl:value-of select="concat(namespace-uri(.),local-name(.))"/>")
  )

<xsl:if test="$assert='true'"> )</xsl:if>
</xsl:if>
</xsl:if>
  <xsl:for-each select="*"><xsl:call-template name="process-property-instance"><xsl:with-param name="assert"><xsl:value-of select="$assert"/></xsl:with-param></xsl:call-template></xsl:for-each> 
  </xsl:template>

  <!-- process an instance of a property -->
  <xsl:template name="process-property-instance" >
  <xsl:param name="assert" select="'false'"/>
    <xsl:choose>
      <xsl:when test='count(*)=1'> <!-- has element nodes as children -->
        <xsl:call-template name="process-objectproperty-instance"><xsl:with-param name="assert"><xsl:value-of select="$assert"/></xsl:with-param></xsl:call-template>
      </xsl:when>
      <xsl:when test='count(text())=1'> <!-- has text nodes as children -->
        <xsl:call-template name="process-dataproperty-instance"><xsl:with-param name="assert"><xsl:value-of select="$assert"/></xsl:with-param></xsl:call-template>
      </xsl:when>
      <xsl:when test='@rdf:resource'> <!-- has a reference -->
        <xsl:call-template name="process-referenceproperty-instance"><xsl:with-param name="assert"><xsl:value-of select="$assert"/></xsl:with-param></xsl:call-template>
      </xsl:when>
    </xsl:choose>
  </xsl:template>

  <!-- process an instance of a objectproperty -->
  <xsl:template name="process-objectproperty-instance" >
  <xsl:param name="assert" select="'false'"/>
<xsl:if test="$assert='true'">
 (assert </xsl:if>
  (triple
   (predicate "<xsl:value-of select="concat(namespace-uri(.),local-name(.))"/>")
   (subject  <xsl:if test="../@rdf:ID">
   <xsl:call-template name="local-ID">
   <xsl:with-param name="id">
   <xsl:value-of select="../@rdf:ID"/>
   </xsl:with-param>
   </xsl:call-template>
   </xsl:if>
   <xsl:if test="../@rdf:about">
   <xsl:call-template name="local-ID">
   <xsl:with-param name="id">
   <xsl:value-of select="../@rdf:about"/>
   </xsl:with-param>
   </xsl:call-template>
   </xsl:if>)
   (object <xsl:if test="*[position()=1]/@rdf:ID">
   <xsl:call-template name="local-ID">
   <xsl:with-param name="id">
   <xsl:value-of select="*[position()=1]/@rdf:ID"/>
   </xsl:with-param></xsl:call-template>
   </xsl:if>
   <xsl:if test="*[position()=1]/@rdf:about">
   <xsl:call-template name="local-ID">
   <xsl:with-param name="id">
   <xsl:value-of select="*[position()=1]/@rdf:about"/>
   </xsl:with-param></xsl:call-template>
   </xsl:if>)
 )
 <xsl:if test="$assert='true'"> )</xsl:if>
  <xsl:for-each select="*[position()=1]">
    <xsl:call-template name="process-class-instance"><xsl:with-param name="assert"><xsl:value-of select="$assert"/></xsl:with-param></xsl:call-template>
   </xsl:for-each>
  </xsl:template>

  <!-- process an instance of a dataproperty -->
  <xsl:template name="process-dataproperty-instance" >
  <xsl:param name="assert" select="'false'"/>
<xsl:if test="$assert='true'">
 (assert </xsl:if>
  (triple
   (predicate "<xsl:value-of select="concat(namespace-uri(.),local-name(.))"/>")
   (subject  <xsl:if test="../@rdf:ID">
   <xsl:call-template name="local-ID">
   <xsl:with-param name="id">
   <xsl:value-of select="../@rdf:ID"/>
   </xsl:with-param></xsl:call-template>
   </xsl:if>
   <xsl:if test="../@rdf:about">
   <xsl:call-template name="local-ID">
   <xsl:with-param name="id">
   <xsl:value-of select="../@rdf:about"/>
   </xsl:with-param></xsl:call-template>
   </xsl:if>)
   (object    <xsl:call-template name="data-value"><xsl:with-param name="value"><xsl:value-of select="normalize-space(.)"/></xsl:with-param></xsl:call-template>)
  )
<xsl:if test="$assert='true'"> )</xsl:if>
  </xsl:template>

  <!-- process an instance of a property referencing a resource in its range-->
  <xsl:template name="process-referenceproperty-instance" >
  <xsl:param name="assert" select="'false'"/>
<xsl:if test="$assert='true'">
 (assert </xsl:if>
  (triple
   (predicate "<xsl:value-of select="concat(namespace-uri(.),local-name(.))"/>")
   (subject  <xsl:if test="../@rdf:ID">
   <xsl:call-template name="local-ID">
   <xsl:with-param name="id">
   <xsl:value-of select="../@rdf:ID"/>
   </xsl:with-param></xsl:call-template>
   </xsl:if>
   <xsl:if test="../@rdf:about">
   <xsl:call-template name="local-ID">
   <xsl:with-param name="id">
   <xsl:value-of select="../@rdf:about"/>
   </xsl:with-param></xsl:call-template>
   </xsl:if>)
   (object    <xsl:call-template name="local-ID"><xsl:with-param name="id"><xsl:value-of select="@rdf:resource"/></xsl:with-param></xsl:call-template>)
  )
<xsl:if test="$assert='true'"> )</xsl:if>
  </xsl:template>

  
  <!-- normalize local IDs -->
  <xsl:template name="local-ID" >
    <xsl:param name="id" />
    <!--<xsl:value-of select="$id"/>-->
    <xsl:choose>
      <xsl:when test="starts-with($id,$variable_uri) or key('variable_lookup',substring-after($id,'#'))/@rdf:ID">
        <xsl:value-of select="concat('?',substring-after($id,'#'))" />
      </xsl:when>
      <xsl:when test="starts-with($id,'http://')">"<xsl:value-of select="$id" />"</xsl:when>
      <xsl:otherwise>"<xsl:value-of select="concat($namespace,$id)"/>"</xsl:otherwise>
    </xsl:choose>
  </xsl:template>

  <!-- check if the value is a variable -->
  <xsl:template name="data-value" >
    <xsl:param name="value" />
    <xsl:choose>
      <xsl:when test="starts-with($value,$variable_uri)">
        <xsl:value-of select="concat('?',substring-after($value,$variable_uri))" />
      </xsl:when>
      <xsl:otherwise>"<xsl:value-of select="$value"/>"</xsl:otherwise>
    </xsl:choose> 
  </xsl:template>

  <!-- get the list of all the variables -->
  <xsl:template name="variable-list" >
     <xsl:for-each select="//*[starts-with(@rdf:about,$variable_uri)]">
       <xsl:value-of select="concat(' ',substring-after(@rdf:about,$variable_uri),' ?',substring-after(@rdf:ID,$variable_uri))" />
     </xsl:for-each>
     <xsl:for-each select="//text()[starts-with(.,$variable_uri)]">
       <xsl:value-of select="concat(' ',substring-after(.,$variable_uri),' ?',substring-after(.,$variable_uri))" />
     </xsl:for-each>
  </xsl:template>
     
</xsl:stylesheet>