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/>

 
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>"  ?source "</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 { 
              before="<dt>{fun:property(<en.properties>, 'creators')}</dt><dd>\n" ;
              TEMPLATE <ontology/contributor.rqg>(?uri) .
              ; separator = "<br/>\n"
              ; after = "</dd>"
            } WHERE {
              ?onto dcterms:creator ?uri 
              OPTIONAL { 
                { ?uri schema:familyName ?contributorName }
                UNION { ?uri foaf:name ?contributorName  }
              }
            } ORDER BY ?contributorName ?uri .
          }
          {
            TEMPLATE { 
              before="<dt>{fun:property(<en.properties>, 'contributors')}</dt><dd>\n" ;
              TEMPLATE <ontology/contributor.rqg>(?uri) .
              ; separator = "<br/>\n"
              ; after = "</dd>"
            } WHERE {
              ?onto dcterms:contributor ?uri 
              OPTIONAL { 
                { ?uri schema:familyName ?contributorName }
                UNION { ?uri foaf:name ?contributorName  }
              }
            } ORDER BY ?contributorName ?uri .
          }
          {

            TEMPLATE {
              before="<dt>{fun:property(<en.properties>, 'importedontologies')}</dt><dd>\n" ;
              """<a href="{?o}">&lt;{?o}&ht;</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>License</dt><dd><a href='https://forge.etsi.org/etsi-software-license'>https://forge.etsi.org/etsi-software-license</a></dd>
        </dl>
      </div>
    </div>
  </div>
</header> 
<section>
  <div class="container">
 {
TEMPLATE {

    TEMPLATE {
      before = "<h2>{ fun:property(<en.properties>, 'abstract') }</h2>\n" ;
      TEMPLATE <function/formatText.rqg>(?abstract) .
      ; separator = "\n"
      ; after = "\n"
    }
    WHERE {
      ?subject dcterms:abstract ?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>"

        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 .

          
        "\n<li><a href='#namespacedeclarations'>{ fun:property(<en.properties>, 'namespaces') }</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 <function/formatText.rqg>(?description) .
      ; separator = "\n"
      ; after = "\n"
    }
    WHERE {
      ?subject dcterms:description ?description .
    } .

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

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

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

    "\n\n\n" TEMPLATE <ontology/namespaces.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>( ?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>( ?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>( ?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" , "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
