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, ?langProperties ) { 
  "<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>("Version Info", ?o , ", ")
      WHERE { ?ontology owl:versionInfo ?o } .

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

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

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

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

      TEMPLATE #<templates/definitionFoaf.rqg>( fun:property(?langProperties, 'contributors'), ?uri, "<br/>" )
      {
          before = "\n<dt>{ ?dt }:</dt><dd>" ;
          <function/href.rqg>(?value, ?value)
          ; separator = ?sep ; after = "</dd>"

      }
      WHERE {
        ?ontology dcterms:contributor ?uri 
      } .
 
      TEMPLATE <templates/definitionFoaf.rqg>( fun:property(?langProperties, 'publisher'), ?uri, "<br/>" )
      DISTINCT WHERE {
        ?ontology dcterms:publisher ?uri 
      } .
 
      TEMPLATE <templates/definition.rqg>( fun:property(?langProperties, '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(?langProperties, '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(?langProperties, 'toc') }</h2>"
      "\n<ol>" BOX {

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

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

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

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

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

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

        TEMPLATE {
          "\n<li><a href='#generalaxioms'>{ fun:property(?langProperties, '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(?langProperties, 'swrlrules') }</a></li>"
        }
        WHERE {
          { swrl:Imp ?p ?swrl } UNION { ?swrl a swrl:Imp }
        } LIMIT 1 .

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

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


    TEMPLATE {
      before = "<h2 id='introduction'>{ fun:property(?langProperties, '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, ?langProperties) 
    WHERE {
      {?c a owl:Class} UNION {?c a rdfs:Class}
    } LIMIT 1 . 
    
    "\n\n\n" TEMPLATE <objectProperties.rqg>(?ontology, ?lang, ?langProperties) . 

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