<?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 an OWL schema into a set of JESS assertions 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 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;#"
 >
  <!-- get the namespace of the ontology e.g. 'http://sadehlab.cs.cmu.edu/mycampus#' -->
  <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 classes -->
  <xsl:template match="owl:Class">
(assert
 (triple
  (predicate "&rdf;#type")
  (subject   "<xsl:call-template name="get-ID"/>")
  (object    "&owl;#Class")
 )
)<xsl:apply-templates /></xsl:template>

  <!-- declare types -->
  <xsl:template match="rdf:type">
(assert
 (triple
  (predicate "&rdf;#type")
  (subject   "<xsl:call-template name="get-father-ID"/>")
  (object    "<xsl:call-template name="get-range-ID"/>")
 )
)<xsl:apply-templates /></xsl:template>


  <!-- declare sub classes -->
  <xsl:template match="rdfs:subClassOf">
(assert
 (triple
  (predicate "&rdfs;#subClassOf")
  (subject   "<xsl:call-template name="get-father-ID"/>")
  (object    "<xsl:call-template name="get-range-ID"/>")
 )
)<xsl:apply-templates /></xsl:template>

  <!-- declare complementOf -->
  <xsl:template match="owl:complementOf">
(assert
 (triple
  (predicate "&owl;#complementOf")
  (subject   "<xsl:call-template name="get-father-ID"/>")
  (object    "<xsl:call-template name="get-range-ID"/>")
 )
)<xsl:apply-templates /></xsl:template>

  <!-- declare sameAs -->
  <xsl:template match="owl:sameAs">
(assert
 (triple
  (predicate "&owl;#sameAs")
  (subject   "<xsl:call-template name="get-father-ID"/>")
  (object    "<xsl:call-template name="get-range-ID"/>")
 )
)<xsl:apply-templates /></xsl:template>

  <!-- declare sameIndividualAs -->
  <xsl:template match="owl:sameIndividualAs">
(assert
 (triple
  (predicate "&owl;#sameIndividualAs")
  (subject   "<xsl:call-template name="get-father-ID"/>")
  (object    "<xsl:call-template name="get-range-ID"/>")
 )
)<xsl:apply-templates /></xsl:template>

  <!-- declare differentFrom -->
  <xsl:template match="owl:differentFrom">
(assert
 (triple
  (predicate "&owl;#differentFrom")
  (subject   "<xsl:call-template name="get-father-ID"/>")
  (object    "<xsl:call-template name="get-range-ID"/>")
 )
)<xsl:apply-templates /></xsl:template>

  <!-- declare disjointWith -->
  <xsl:template match="owl:disjointWith">
(assert
 (triple
  (predicate "&owl;#disjointWith")
  (subject   "<xsl:call-template name="get-father-ID"/>")
  (object    "<xsl:call-template name="get-range-ID"/>")
 )
)<xsl:apply-templates /></xsl:template>

  <!-- declare equivalentClass -->
  <xsl:template match="owl:equivalentClass">
(assert
 (triple
  (predicate "&owl;#equivalentClass")
  (subject   "<xsl:call-template name="get-father-ID"/>")
  (object    "<xsl:call-template name="get-range-ID"/>")
 )
)<xsl:apply-templates /></xsl:template>


  <!-- declare Properties -->
  <xsl:template match="rdf:Property">
(assert
 (triple
  (predicate "&rdf;#type")
  (subject   "<xsl:call-template name="get-ID"/>")
  (object    "&rdf;#Property")
 )
)<xsl:apply-templates /></xsl:template>

  <!-- declare Object Properties -->
  <xsl:template match="owl:ObjectProperty">
(assert
 (triple
  (predicate "&rdf;#type")
  (subject   "<xsl:call-template name="get-ID"/>")
  (object    "&owl;#ObjectProperty")
 )
)<xsl:apply-templates /></xsl:template>

  <!-- declare Functional Properties -->
  <xsl:template match="owl:FunctionalProperty">
(assert
 (triple
  (predicate "&rdf;#type")
  (subject   "<xsl:call-template name="get-ID"/>")
  (object    "&owl;#FunctionalProperty")
 )
)<xsl:apply-templates /></xsl:template>

  <!-- declare Inverse Functional Properties -->
  <xsl:template match="owl:InverseFunctionalProperty">
(assert
 (triple
  (predicate "&rdf;#type")
  (subject   "<xsl:call-template name="get-ID"/>")
  (object    "&owl;#InverseFunctionalProperty")
 )
)<xsl:apply-templates /></xsl:template>

  <!-- declare Symmetric Properties -->
  <xsl:template match="owl:SymmetricProperty">
(assert
 (triple
  (predicate "&rdf;#type")
  (subject   "<xsl:call-template name="get-ID"/>")
  (object    "&owl;#SymmetricProperty")
 )
)<xsl:apply-templates /></xsl:template>

  <!-- declare Datatype Properties -->
  <xsl:template match="owl:DatatypeProperty">
(assert
 (triple
  (predicate "&rdf;#type")
  (subject   "<xsl:call-template name="get-ID"/>")
  (object    "&owl;#DatatypeProperty")
 )
)<xsl:apply-templates /></xsl:template>

  <!-- declare sub properties -->
  <xsl:template match="rdfs:subPropertyOf">
(assert
 (triple
  (predicate "&rdfs;#subPropertyOf")
  (subject   "<xsl:call-template name="get-father-ID"/>")
  (object    "<xsl:call-template name="get-range-ID"/>")
 )
)<xsl:apply-templates /></xsl:template>

  <!-- declare inverseOf -->
  <xsl:template match="owl:inverseOf">
(assert
 (triple
  (predicate "&owl;#inverseOf")
  (subject   "<xsl:call-template name="get-father-ID"/>")
  (object    "<xsl:call-template name="get-range-ID"/>")
 )
)<xsl:apply-templates /></xsl:template>

  <!-- declare equivalentProperty -->
  <xsl:template match="owl:equivalentProperty">
(assert
 (triple
  (predicate "&owl;#equivalentProperty")
  (subject   "<xsl:call-template name="get-father-ID"/>")
  (object    "<xsl:call-template name="get-range-ID"/>")
 )
)<xsl:apply-templates /></xsl:template>

  <!-- declare domain -->
  <xsl:template match="rdfs:domain">
(assert
 (triple
  (predicate "&rdfs;#domain")
  (subject   "<xsl:call-template name="get-father-ID"/>")
  (object    "<xsl:call-template name="get-range-ID"/>")
 )
)<xsl:apply-templates /></xsl:template>

  <!-- declare range -->
  <xsl:template match="rdfs:range">
(assert
 (triple
  (predicate "&rdfs;#range")
  (subject   "<xsl:call-template name="get-father-ID"/>")
  (object    "<xsl:call-template name="get-range-ID"/>")
 )
)<xsl:apply-templates /></xsl:template>


  <!-- named templates - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -->
  
  <!-- 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>


  <!-- get ID or About of the current node -->
  <xsl:template name="get-ID" >
    
    <xsl:choose>
      <xsl:when 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:when>
      <xsl:when 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:when>
      <xsl:otherwise>
        <xsl:value-of select="concat($namespace,generate-id(.))"/>
      </xsl:otherwise>
    </xsl:choose> 
  </xsl:template>
  
  <!-- get the ID or About of the parent node -->
  <xsl:template name="get-father-ID" >
    
    <xsl:choose>
      <xsl:when 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:when>
      <xsl:when 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:when>
      <xsl:otherwise>
        <xsl:value-of select="concat($namespace,generate-id(..))"/>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>
  
  <!-- get the ID or About of the first child  -->
  <xsl:template name="get-range-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>
   
  <xsl:template match="*" />
  
</xsl:stylesheet>