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 ex: <http://example.org/>

TEMPLATE <term/main.rqg>( ?term ) {  

  TEMPLATE <header.rqg>( TEMPLATE <term/label.rqg>( ?term ) . , ?pathToStatic , false ).
 
""" 
<header id="g-header">
  <div class="g-container">
    <div class="g-block size-100">
      <div class="g-content g-particle">
        <h1><a href="{?term}">{?prefix}{?localName}</a> ({ TEMPLATE <term/label.rqg>( ?term ) . })</h1>
        <p><em>Defined in <a href="{?ontoRef}">{?ontoRef}</a> as: </em></p>
        <p>{ TEMPLATE { ?description } WHERE { ?term rdfs:comment ?description } . }</p>
        <dl>
          <dt>IRI:</dt><dd><a href='{?term}'>{?term}</a></dd>
          {
           TEMPLATE { 
            before = "<dt>Is defined by:</dt><dd>"; 
            """<a href="{?onto}">{?onto}</a>"""
            ; separator = "<br/>" ; after="</dd>" } 
           WHERE { ?term rdfs:isDefinedBy ?onto } 
           ORDER BY DESC( ?onto ) .  
           }</dd>
          {
           TEMPLATE { 
            before = "<dt>Is used by:</dt><dd>"; 
            """<a href="{?onto}">{?onto}</a>""" 
            ; separator="<br/>" ; after="</dd>" } 
           WHERE { ?term ex:isUsedBy ?onto } 
           ORDER BY DESC( ?onto ) . 
           }
        </dl>
      </div>
    </div>
  </div>
</header>
<section>
  <div class="g-container">
    <div class="g-content"> 
         { TEMPLATE{ TEMPLATE <term/class.rqg>( ?term ) . }
           FROM ?ontoRef 
           WHERE { ?term a owl:Class } . 
         }
         { TEMPLATE{ TEMPLATE <term/objectProperty.rqg>( ?term ) . }
           FROM ?ontoRef 
           WHERE { ?term a owl:ObjectProperty } .
         }
         { TEMPLATE{ TEMPLATE <term/dataProperty.rqg>( ?term ) . }
           FROM ?ontoRef 
           WHERE { ?term a owl:DatatypeProperty } .
         }
         { TEMPLATE{ TEMPLATE <term/individual.rqg>( ?term ) . }
           FROM ?ontoRef 
           WHERE { ?term a owl:NamedIndividual } .
         }
    </div>
  </div>
</section>
"""

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

}
FROM ?term
WHERE {

 BIND( fun:regex( str(?term), "^https://saref.etsi.org/([a-z4]+)/([^/]+)$", 1 ) AS ?ext )
 BIND( IF( ?ext = "core" , "saref:" , "s{SUBSTR(?ext,6)}:" ) AS ?prefix )
 BIND( fun:regex( str(?term), "^https://saref.etsi.org/([a-z4]+)/([^/]+)$", 2 ) AS ?localName )
 BIND( fun:regex( str(?term), "^(https://saref.etsi.org/[a-z4]+/)[^/]+$", 1 ) AS ?namespace )
 BIND( "../static" AS ?static )

 ?term rdfs:isDefinedBy ?ontoRef

 BIND( "../static" AS ?pathToStatic )

}
ORDER BY DESC( ?ontoRef )
LIMIT 1
