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/terms/>
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 foaf: <http://xmlns.com/foaf/0.1/>
PREFIX schema: <http://schema.org/>

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

TEMPLATE <ontology/main.rqg>( ?versionIRI ) { 

TEMPLATE <header.rqg>( st:call-template(<ontology/title.rqg>, ?onto ) , ?pathToStatic , true ) .

""" 
<header id="g-header">
  <div class="g-container">
    <div class="g-block size-100">
      <div class="g-content g-particle">
        <h1><a href="{?onto}">{ st:call-template(<ontology/title.rqg>, ?onto ) }</a></h1>
        <dl>
          <dt>Latest version:</dt><dd><a href='{?onto}'>{?onto}</a></dd>
          {
            TEMPLATE { "<dt>Permanent IRI for this version (" ?versionInfo ")</dt><dd><a href='{?versionIRI}'>{?versionIRI}</a></dd>" }
            WHERE { ?onto owl:versionInfo ?versionInfo ;  owl:versionIRI ?versionIRI } . 
          }
          {
            TEMPLATE { "<dt>" fun:property(<en.properties>, 'previousversion') "</dt><dd><a href='{?onto}'>{?onto}</a></dd>" }
            WHERE {
              ?onto owl:priorVersion ?o  
            } .
          }
          {
            TEMPLATE { "<dt>" fun:property(<en.properties>, 'etsiSource') "</dt><dd><a href='{?source}'>{?source}</a></dd>" }
            WHERE {
              ?onto rdfs:seeAlso ?source 
            } ORDER BY DESC(?source) .
          }
          {
            TEMPLATE { "<dt>" fun:property(<en.properties>, 'forgeSource') "</dt><dd><a href='{?source}'>{?source}</a></dd>" }
            WHERE {
              ?onto dcterms:source ?source 
            } ORDER BY DESC(?source) .
          }
          {
            TEMPLATE { "<dt>" fun:property(<en.properties>, 'publicationDate') "</dt><dd>"  ?date "</dd>" }
            WHERE {
              ?onto dcterms:issued ?date 
            } ORDER BY DESC(?date) .
          }
          {
            TEMPLATE { "<dt>" fun:property(<en.properties>, 'modificationDate') "</dt><dd>"  ?date "</dd>" }
            WHERE {
              ?onto dcterms:modified ?date 
            } ORDER BY DESC(?date) .
          }
          {
            TEMPLATE <ontology/creators.rqg>( ?onto ) .
          }
          {
            TEMPLATE <ontology/contributors.rqg>( ?onto ) .
          }
          <dt>Ontology requirements and tests</dt><dd><a href='requirements'>requirements</a> and <a href='tests'>tests</a></dd>
          {

            TEMPLATE {
              before="<dt>{fun:property(<en.properties>, 'importedontologies')}</dt><dd>\n" ;
              """<a href="{?o}">&lt;{?o}&gt;</a>""" 
              ; separator="\n"
              ; after = "</dd>"
            } WHERE {
              ?onto owl:imports ?o
            } .
          }
          <dt>Prefix and namespace declaration:<dt>
          <dd>Turtle: <code>@prefix {?prefix} &lt;{?onto}&gt; .</code><dd>
          <dd>SPARQL: <code>PREFIX {?prefix} &lt;{?onto}&gt;</code><dd>
           <dt>Download serialization:</dt><dd>
            <span><a href="{ ?filename }.jsonld" target="_blank"><img src="https://img.shields.io/badge/Format-JSON--LD-blue.svg"/></a></span>
            <span><a href="{ ?filename }.n3" target="_blank"><img src="https://img.shields.io/badge/Format-N3-blue.svg"/></a></span>
            <span><a href="{ ?filename }.nt" target="_blank"><img src="https://img.shields.io/badge/Format-N--Triples-blue.svg"/></a></span>
            <span><a href="{ ?filename }.rdf" target="_blank"><img src="https://img.shields.io/badge/Format-RDF/XML-blue.svg"/></a></span>
            <span><a href="{ ?filename }.ttl" target="_blank"><img src="https://img.shields.io/badge/Format-Turtle-blue.svg"/></a></span>
           </dd>
           <dt>License:</dt><dd>
            <span><a href="https://forge.etsi.org/etsi-software-license" target="_blank"><img src="https://img.shields.io/badge/License-https://forge.etsi.org/etsi--software--license-blue.svg"/></a></span>
           </dd>
        </dl>
      </div>
    </div>
  </div>
</header> 
<section>
  <div class="container">
 {
TEMPLATE {

    TEMPLATE {
      IF( ?abstract != "" , "<h2>{ fun:property(<en.properties>, 'abstract') }</h2>\n{?abstract}\n" , "" )
    } WHERE { 
      BIND( st:call-template(<ontology/abstract.rqg>, ?onto) AS ?abstract )
    }.

    "<div id='toc2'>" BOX { 
      "\n<h2>{ fun:property(<en.properties>, 'toc') }</h2>"
      "\n<ol>" BOX {

        "\n<li><a href='#introduction'>{ fun:property(<en.properties>, 'introduction') }</a></li>"

        IF( st:call-template(<ontology/examples.rqg> ) != "" , "<li><a href='#examples'>{ fun:property(<en.properties>, 'examples') }</a></li>" , "" )
          
        "\n<li><a href='#namespacedeclarations'>{ fun:property(<en.properties>, 'namespaces') }</a></li>"

        TEMPLATE {
          "\n<li><a href='#classes'>{ fun:property(<en.properties>, 'classes') }</a></li>"
        }
        WHERE {
          {?class a owl:Class} UNION {?class a rdfs:Class}
        } LIMIT 1 .

        TEMPLATE {
          "\n<li><a href='#objectproperties'>{ fun:property(<en.properties>, 'objectproperties') }</a></li>"
        }
        WHERE {
          ?op a owl:ObjectProperty
        } LIMIT 1 .

        TEMPLATE {
          "\n<li><a href='#dataproperties'>{ fun:property(<en.properties>, 'dataproperties') }</a></li>"
        }
        WHERE {
          ?dp a owl:DatatypeProperty
        } LIMIT 1 .

        TEMPLATE {
          "\n<li><a href='#namedindividuals'>{ fun:property(<en.properties>, 'namedindividuals') }</a></li>"
        }
        WHERE {
          ?ni a owl:NamedIndividual
        } LIMIT 1 .

        TEMPLATE {
          "\n<li><a href='#annotationproperties'>{ fun:property(<en.properties>, 'annotationproperties') }</a></li>"
        }
        WHERE {
          ?ap a owl:AnnotationProperty
        } LIMIT 1 .

        TEMPLATE {
          "\n<li><a href='#generalaxioms'>{ fun:property(<en.properties>, 'generalaxioms') }</a></li>"
        }
        WHERE {
          {?ga a owl:AllDisjointClasses}
           UNION {
            { ?class a owl:Class}
            UNION { ?class a rdfs:Class}
            UNION { ?class a owl:Restriction }
            FILTER( ISBLANK( ?class) )
            { ?class rdfs:subClassOf []}
            UNION { ?class owl:equivalentClass []}
            }
        } LIMIT 1 .

        IF( st:call-template(<ontology/references.rqg> ) != "" , "<li><a href='#references'>{ fun:property(<en.properties>, 'references') }</a></li>" , "" )

        IF( st:call-template(<ontology/acknowledgements.rqg> ) != "" , "<li><a href='#acknowledgements'>{ fun:property(<en.properties>, 'acknowledgements') }</a></li>" , "" )

        "\n<li><a href='#toc'>Hierarchies of Terms</a></li>"
        

      } "\n</ol>"
    } "\n</div>"


    TEMPLATE {
      before = "<h2>{ fun:property(<en.properties>, 'introduction') }</h2>\n" ;
      TEMPLATE <ontology/description.rqg>( ?onto ) .
      ; after = "\n"
    } .

    "\n\n\n" TEMPLATE <ontology/examples.rqg>( ) .

    "\n\n\n" TEMPLATE <ontology/namespaces.rqg>() .

    "\n\n\n" TEMPLATE <ontology/classes.rqg>( ?onto  ) 
    WHERE {
      {?c a owl:Class} UNION {?c a rdfs:Class}
    } LIMIT 1 . 
    
    "\n\n\n" TEMPLATE <ontology/objectProperties.rqg>( ?onto  ) 
    WHERE {
      ?op a owl:ObjectProperty
    } LIMIT 1 . 

    "\n\n\n" TEMPLATE <ontology/dataProperties.rqg>( ?onto  )
    WHERE {
      ?op a owl:DatatypeProperty
    } LIMIT 1 . 

    "\n\n\n" TEMPLATE <ontology/namedIndividuals.rqg>( ?onto  ) . 

    "\n\n\n" TEMPLATE <ontology/references.rqg>( ) .

    "\n\n\n" TEMPLATE <ontology/acknowledgements.rqg>( ) .

}.
         }
  </div>
</section>
<section>
  <div class="container">
    <nav id="toc">
      <h2 class="introductory" id="table-of-contents">Term hierarchy</h2>
        <ul class="nav nav-tabs" id="myTab" role="tablist">
          <li class="nav-item">
            <a class="nav-link active" id="classes-toc-tab" data-toggle="tab" href="#classes-toc" role="tab" aria-controls="classes-toc" aria-selected="true">Classes</a>
          </li>
          <li class="nav-item">
            <a class="nav-link" id="objectproperties-toc-tab" data-toggle="tab" href="#objectproperties-toc" role="tab" aria-controls="profile" aria-selected="false">ObjectProperties</a>
          </li>
          <li class="nav-item">
            <a class="nav-link" id="dataproperties-toc-tab" data-toggle="tab" href="#dataproperties-toc" role="tab" aria-controls="profile" aria-selected="false">DataProperties</a>
          </li>
        </ul>
        <div class="tab-content" id="myTabContent">
          <div class="tab-pane show active" id="classes-toc" role="tabpanel" aria-labelledby="classes-toc-tab">
{ 
    # toc
TEMPLATE <ontology/toc-bar.rqg>( ?onto,  ?class , rdfs:subClassOf ) 
DISTINCT WHERE {
  ?class a owl:Class 
  FILTER( ISURI(?class) ) 
  FILTER NOT EXISTS { ?class rdfs:subClassOf ?super FILTER( ISURI(?super) )  }
  FILTER( ?class != owl:Thing )
} ORDER BY ?class 
. 
}
          </div>
          <div class="tab-pane" id="objectproperties-toc" role="tabpanel" aria-labelledby="objectproperties-toc-tab">
{ 
    # toc
TEMPLATE <ontology/toc-bar.rqg>( ?onto,  ?property , rdfs:subPropertyOf )
WHERE {
  ?property a owl:ObjectProperty
  FILTER( ISURI(?property) ) 
  FILTER NOT EXISTS { ?property rdfs:subPropertyOf ?super FILTER( ISURI(?super) )  }
} ORDER BY ?property . 
}
          </div>
          <div class="tab-pane" id="dataproperties-toc" role="tabpanel" aria-labelledby="dataproperties-toc-tab">
{ 
    # toc
TEMPLATE <ontology/toc-bar.rqg>( ?onto,  ?property , rdfs:subPropertyOf )
WHERE {
  ?property a owl:DatatypeProperty
  FILTER( ISURI(?property) ) 
  FILTER NOT EXISTS { ?property rdfs:subPropertyOf ?super FILTER( ISURI(?super) )  }
} ORDER BY ?property . 
}
          </div>
        </div>
    </nav>
  </div>
</section>
"""


  TEMPLATE <footer.rqg>( ?pathToStatic , true  ) .

} FROM ?versionIRI
WHERE {
 BIND( fun:regex( str(?versionIRI), "^https://saref.etsi.org/([a-z4]+)/([^/]+)/$", 1 ) AS ?ext )
 BIND( IF( ?ext = "core" , "saref" , ?ext ) AS ?filename )
 BIND( IF( ?ext = "core" , "saref" , "s{SUBSTR(?ext,6)}" ) AS ?short )
 BIND( UCASE( ?short ) AS ?name )
 BIND( "{?short}:" AS ?prefix )
 BIND( fun:regex( str(?versionIRI), "^https://saref.etsi.org/([a-z4]+)/(v[0-9]+\\.[0-9]+\\.[0-9]+)/$", 2 ) AS ?versionInfo )
 BIND( IRI( fun:regex( str(?versionIRI), "^(https://saref.etsi.org/[a-z4]+/)", 1 ) ) AS ?onto )
 BIND( "../../static" AS ?pathToStatic )
 ?onto a owl:Ontology .
}
#LIMIT 1
