<?xml version="1.0" encoding="ISO-8859-1"?>
<!-- Transforms the SWRL rules into a set of JESS assertions -->

<!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 swrl "http://www.w3.org/2003/11/swrl"> 
  <!ENTITY xsd "http://www.w3.org/2000/10/XMLSchema">
  <!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:swrl="&swrl;#"
 xmlns:xsd="&xsd;#"
 >

  <!-- get the namespace of the ontology  -->
  <xsl:param name='namespace' select="concat(/rdf:RDF/@xml:base,'#')"/>
  
  <xsl:output method="text" media-type="text/plain" encoding="ISO-8859-1" omit-xml-declaration="yes"/>

  <xsl:template match="/"><xsl:apply-templates /></xsl:template>
  <xsl:template match="rdf:RDF"><xsl:apply-templates /></xsl:template>
  
  <!-- transform ontology into Jess triples - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -->

  <!-- descriptions are ignored -->
  <xsl:template match="rdf:Description"><xsl:apply-templates /></xsl:template>


  <!-- declare implication -->

<xsl:template match="swrl:Imp">
(defrule rule-<xsl:number/>  <xsl:apply-templates select="swrl:body" />
  =>  <xsl:apply-templates select="swrl:head" />
)
</xsl:template>


  <!-- declare body -->
<xsl:template match="swrl:body">
  <xsl:for-each select="."><xsl:apply-templates /></xsl:for-each>
</xsl:template>  

  <!-- declare head -->
<xsl:template match="swrl:head">
  <xsl:for-each select=".">
(assert  
  <xsl:apply-templates />
) </xsl:for-each>
</xsl:template>  

  <!-- declare AtomList -->
<xsl:template match="swrl:AtomList">
  <xsl:for-each select="."><xsl:apply-templates /></xsl:for-each>
</xsl:template>  

  <!-- declare first -->
<xsl:template match="rdf:first">
  <xsl:apply-templates />
</xsl:template>  

  <!-- declare rest -->
<xsl:template match="rdf:rest">
  <xsl:apply-templates />
</xsl:template>  

  <!-- declare class atom -->
<xsl:template match="swrl:ClassAtom">
  (triple
    (predicate "&rdf;#type")
    (subject    <xsl:apply-templates select="swrl:argument1" />)
    (object   "<xsl:apply-templates select="swrl:classPredicate" />")
  )</xsl:template>  

  <!-- declare individual property atom -->
<xsl:template match="swrl:IndividualPropertyAtom">
  (triple
    (predicate "<xsl:apply-templates select="swrl:propertyPredicate" />")
    (subject    <xsl:apply-templates select="swrl:argument1" />)
    (object   <xsl:apply-templates select="swrl:argument2" />)
  )</xsl:template>  

  <!-- declare datavalued property atom -->
<xsl:template match="swrl:DatavaluedPropertyAtom">
  (triple
    (predicate "<xsl:apply-templates select="swrl:propertyPredicate" />")
    (subject    "<xsl:apply-templates select="swrl:argument1" />")
    (object   "<xsl:apply-templates select="swrl:argument2" />")
  )</xsl:template> 

  <!-- declare same individual atom -->
<xsl:template match="swrl:SameIndividualAtom">
  (triple
    (predicate "&owl;#sameIndividualAs")
    (subject    "<xsl:apply-templates select="swrl:argument1" />")
    (object   "<xsl:apply-templates select="swrl:argument2" />")
  )</xsl:template> 

  <!-- declare different individual atom -->
<xsl:template match="swrl:DifferentIndividualAtom">
  (triple
    (predicate "&owl;#differentFrom")
    (subject    "<xsl:apply-templates select="swrl:argument1" />")
    (object   "<xsl:apply-templates select="swrl:argument2" />")
  )</xsl:template> 

  <!-- declare class predicate -->
<xsl:template match="swrl:classPredicate">
    <xsl:call-template name="get-child-ID"/>
</xsl:template>  

  <!-- declare property predicate -->
<xsl:template match="swrl:propertyPredicate">
    <xsl:call-template name="get-child-ID"/>
</xsl:template>   

  <!-- declare argument 1 -->
<xsl:template match="swrl:argument1">
    <xsl:call-template name="get-variable"/>
</xsl:template>  
  
  <!-- declare argument 2 -->
<xsl:template match="swrl:argument2">
    <xsl:call-template name="get-variable"/>
</xsl:template> 



  <!-- named templates - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -->
  
  <!-- normalize variable -->
  <xsl:template name="get-variable" >
    <xsl:choose>
      <xsl:when test="@rdf:resource">
        <xsl:value-of select="concat('?',substring-after(@rdf:resource,'#'))"/>
      </xsl:when>
      <xsl:when test="*[position()=1]/@rdf:ID">
        <xsl:value-of select="concat('?',*[position()=1]/@rdf:ID)"/>
      </xsl:when>
      <xsl:when test="*[position()=1]/@rdf:about">
        <xsl:value-of select="concat('?',*[position()=1]/@rdf:about)"/>
      </xsl:when>
      <xsl:otherwise>
        <xsl:value-of select="concat('?',generate-id(*[position()=1]))"/>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>
  
  <!-- get the ID or About of the first child  -->

  <xsl:template name="get-child-ID" >
    <xsl:choose>
      <xsl:when test="@rdf:resource">
        <xsl:call-template name="local-ID"><xsl:with-param name="id"><xsl:value-of select="@rdf:resource"/></xsl:with-param></xsl:call-template> 
      </xsl:when>
      <xsl:when 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:when>
      <xsl:when 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:when>
      <xsl:otherwise>
        <xsl:value-of select="concat($namespace,generate-id(*[position()=1]))"/>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

  
  <!-- normalize local URIs -->
  <xsl:template name="local-ID" >
    <xsl:param name="id" />
    <xsl:choose>
      <xsl:when test="starts-with($id,'#')">
        <xsl:value-of select="concat($namespace,substring-after($id,'#'))"/>
      </xsl:when>
      <xsl:when test="contains($id,'/') or contains($id,'#') or contains($id,':')">
        <xsl:value-of select="$id" />
      </xsl:when>
      <xsl:otherwise>
        <xsl:value-of select="concat($namespace,$id)"/>
      </xsl:otherwise>
    </xsl:choose> 
  </xsl:template>
     
  <xsl:template match="*"/>


   
</xsl:stylesheet>