PREFIX st:   <http://ns.inria.fr/sparql-template/>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX owl: <http://www.w3.org/2002/07/owl#>
PREFIX dcterms: <http://purl.org/dc/terms/>
PREFIX dc: <http://purl.org/dc/elements/1.1/>
PREFIX fun: <http://w3id.org/sparql-generate/fn/>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX skos: <http://www.w3.org/2004/02/skos/core#>
PREFIX obo: <http://purl.obolibrary.org/obo/>
PREFIX swrl: <http://www.w3.org/2003/11/swrl#>
PREFIX prov: <http://www.w3.org/ns/prov#>
PREFIX iter: <http://w3id.org/sparql-generate/iter/>

BASE <https://saref.etsi.org/documentation/>

TEMPLATE <classes.rqg>( ?ontology, ?lang ) { 

  """\n<div id='classes'>
  { <tagi.rqg>("h2", ?undef, fun:property(<en.properties>, 'classes') ) }""" 

  BOX { 

    # toc
    TEMPLATE <ontology/toc.rqg>( ?lang, ?class )
    WHERE {
      {?class a owl:Class} UNION {?class a rdfs:Class}
      FILTER( ISURI(?class) ) 
    } ORDER BY ?class . 


    # description
    TEMPLATE {
      "\n<div id='{fun:prefixedIRI(?class)}' class='entity'>" BOX {
        
        "<a name='{?class}'></a>"
          "\n<h3>" 

            TEMPLATE { ?langLabel 
            } ITERATOR iter:call-select(<getLabel.rqg>, ?class, ?lang) AS ?langLabel .

           <sup.rqg>(<en.properties>, 'class') 
          
           <backlink.rqg>("classes", "Class") 
          "</h3>"
        
        

          "\n<p><strong>IRI:</strong> { ?class }</p>"

          TEMPLATE {
            "\n<div class='comment'><p>{?comment}</p></div>"
          } DISTINCT WHERE {
            OPTIONAL { ?class ( rdfs:comment|prov:definition|skos:definition | obo:IAO_0000115) ?comment }
            FILTER(LANG(?comment) = str(?lang) || LANG(?comment) = "" )
          } .
        
          TEMPLATE {
            "\n<div class='comment'><p>{?comment}</p></div>"
          } DISTINCT WHERE {
            OPTIONAL { ?class (dc:description|dcterms:description) ?comment }
            FILTER(LANG(?comment) = str(?lang) || LANG(?comment) = "" )
          } .

          # should add more metadata here (see LODE)
          #<xsl:call-template name="get.version" />
          #<xsl:call-template name="get.author" />
          #<xsl:call-template name="get.original.source" />
          #<xsl:call-template name="get.source" />
          #<xsl:call-template name="get.termStatus" />
          #<xsl:call-template name="get.deprecated" />
          #<xsl:call-template name="get.rationale" />
          #<xsl:call-template name="get.example" />

        
          TEMPLATE {
            "\n<div class='description'><dl>" BOX {

st:call-template(<classDescriptionSubjectObject.rqg>, ?ontology, ?lang, <en.properties> , ?class, 'isequivalentto', owl:equivalentClass)
              
st:call-template(<classDescriptionSubject.rqg>, ?ontology, ?lang, <en.properties> , ?class, 'hassuperclasses', rdfs:subClassOf)

st:call-template(<classDescriptionObject.rqg>, ?ontology, ?lang, <en.properties> , ?class, 'hassubclasses', rdfs:subClassOf)
              
st:call-template(<classDescriptionObject.rqg>, ?ontology, ?lang, <en.properties> , ?class, 'isindomainof', rdfs:domain)

st:call-template(<classDescriptionObject.rqg>, ?ontology, ?lang, <en.properties> , ?class, 'isinrangeof', rdfs:range)              
TEMPLATE {
  before = "\n<dt>{ fun:property(<en.properties>, "hasmembers") }</dt><dd>" ;
    TEMPLATE <individualAndLiteralDescription.rqg>( <en.properties> , ?member ) . 
  ; separator = ", "
  ; after = "</dd>"
} WHERE {
  ?member rdf:type  ?class
  FILTER(ISURI(?member))
} .

# keys
TEMPLATE {
  before = "\n<dt>{ fun:property(<en.properties>, "haskeys") }</dt><dd>" ;
    TEMPLATE <propertyDescription.rqg>( <en.properties> , ?key ) . 
  ; separator = ", "
  ; after = "</dd>"
} WHERE {
  ?class owl:hasKey / rdf:rest* / rdf:first  ?key
} .

st:call-template(<classDescriptionSubjectObject.rqg>, ?ontology, ?lang, <en.properties> , ?class, 'isdisjointwith', owl:disjointWith)


            } "\n</dl></div>"
          } WHERE {
            
          } .





      } "\n</div>"



    } DISTINCT WHERE {
      {?class a owl:Class} UNION {?class a rdfs:Class}
      FILTER( ISURI(?class) ) 
    } ORDER BY ?class .


  }
  "\n</div>"

}