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

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

TEMPLATE <body.rqg>( ?ontology, ?lang ) { 
  "<body>" BOX { "\n"

    "<div class='head'>" BOX { "\n"


    TEMPLATE {
      "<h1>" ?title "</h1>\n"
    } 
    ITERATOR <select/title.rqg>(?ontology, ?lang) AS ?title.

    "<dl>" BOX {

      TEMPLATE <templates/definition.rqg>("IRI", ?ontology , "<br/>").

      TEMPLATE <templates/definition.rqg>("Version IRI", ?o , ", " )
      WHERE { ?ontology owl:versionIRI ?o } .

      TEMPLATE <templates/definition.rqg>("Version Info", ?o , ", ")
      WHERE { ?ontology owl:versionInfo ?o } .

      TEMPLATE <templates/definition.rqg>( fun:property(<en.properties>, 'date'), ?o , ", ")
      WHERE {
        ?ontology (dcterms:date|dc:date) ?o 
      } ORDER BY ?o . 

      TEMPLATE <templates/definition.rqg>( fun:property(<en.properties>, 'priorVersion'), ?o , ", ")
      WHERE {
        ?ontology owl:priorVersion ?o
      } .

      TEMPLATE <templates/definition.rqg>( fun:property(<en.properties>, 'backwardcompatiblewith'), ?o , ", ")
      WHERE {
        ?ontology owl:backwardCompatibleWith ?o
      } .
 
      TEMPLATE <templates/definition.rqg>( fun:property(<en.properties>, 'incompatibleWith'), ?o , ", ")
      WHERE {
        ?ontology owl:incompatibleWith ?o
      } .

      TEMPLATE <templates/definitionFoaf.rqg>( fun:property(<en.properties>, 'authors'), ?uri, "<br/>" )
      WHERE {
        ?ontology (dc:creator|dcterms:creator) ?uri
      } .

      TEMPLATE <templates/definitionFoaf.rqg>( fun:property(<en.properties>, 'contributors'), ?uri, "<br/>" )
      WHERE {
        ?ontology (dc:contributor|dcterms:contributor) ?uri 
      } .
 
      TEMPLATE <templates/definitionFoaf.rqg>( fun:property(<en.properties>, 'publisher'), ?uri, "<br/>" )
      WHERE {
        ?ontology (dc:publisher|dcterms:publisher) ?uri 
      } .
 
      TEMPLATE <templates/definition.rqg>( fun:property(<en.properties>, 'importedontologies'), ?o , "<br/>")
      WHERE {
        ?ontology owl:imports ?o
      } .

      TEMPLATE {
        before = "<h4>Copyright</h4>";
        "<div class='copyright'>" <function/processLines.rqg>( ?copyright ) "</div>"
        ; separator = "\n"
      }
      WHERE {
        VALUES  ?property  { 
           dcterms:rights   
           dc:rights  }
        ?ontology ?property ?copyright .
      }
      LIMIT 1 .

    } "\n</dl>"


    "<hr/>"

    TEMPLATE {
      "<h2>{ fun:property(<en.properties>, 'abstract') }</h2>\n"
      "<p>{ IF( DATATYPE(?comment) = <http://www.iana.org/assignments/media-types/text/html>, ?comment, <function/processLines.rqg>( ?comment )) }</p>"
    }
    WHERE {
      ?ontology rdfs:comment ?comment .
      FILTER(LANG(?comment) = str(?lang) || LANG(?comment) = "")
    } ORDER BY DESC ( LANG(?comment) ) .


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

        TEMPLATE {
          "\n<li><a href='#introduction'>{ fun:property(<en.properties>, 'introduction') }</a></li>"
        }
        WHERE {
          ?ontology (dc:description|dcterms:description) ?obj .
        } LIMIT 1 .

        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 .

        TEMPLATE {
          "\n<li><a href='#swrlrules'>{ fun:property(<en.properties>, 'swrlrules') }</a></li>"
        }
        WHERE {
          { swrl:Imp ?p ?swrl } UNION { ?swrl a swrl:Imp }
        } LIMIT 1 .

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

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


    TEMPLATE {
      before = "<h2 id='introduction'>{ fun:property(<en.properties>, 'introduction') }</h2>" ;
      ?descriptionHTML
      ; separator = "\n"
    }
    WHERE {
      ?ontology (dcterms:description|dc:description) ?description
      BIND( <body/description-to-html.rqg>(?description) AS ?descriptionHTML )
    } ORDER BY ASC ( ?descriptionHTML ) .

    "\n\n\n" TEMPLATE <classes.rqg>(?ontology, ?lang, <en.properties>) 
    WHERE {
      {?c a owl:Class} UNION {?c a rdfs:Class}
    } LIMIT 1 . 
    
    "\n\n\n" TEMPLATE <objectProperties.rqg>(?ontology, ?lang, <en.properties>) . 

    "\n\n\n" TEMPLATE <dataProperties.rqg>(?ontology, ?lang, <en.properties>) . 
    #"\n\n\n" TEMPLATE <namedIndividuals.rqg>(?ontology, ?lang, <en.properties>) . 
    "\n\n\n" TEMPLATE <namespaces.rqg>(?ontology, ?lang, <en.properties>) .
  }
  """
  <p class="endnote">{ fun:property(<en.properties>, 'endnote') } <a href="http://w3id.org/sparql-generate/"><em>SPARQL-Generate</em></a>, { fun:property(<en.properties>, 'developedby') } <a href="http://maxime-lefrancois.info/">Maxime Lefrançois</a>.</p>"""
  } "\n" "</body>"
} 