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#>
PREFIX prov: <http://www.w3.org/ns/prov#>
 
BASE <https://saref.etsi.org/documentation/>

# See https://www.w3.org/TR/owl2-quick-reference/#Data_Ranges

TEMPLATE  <ontology/dataRangeDescription.rqg>( ?onto,  ?dataRange ) {

  # named datatype
  TEMPLATE {
    "<a href='{IF( STRSTARTS( STR(?dataRange), STR(?onto)), "#{fun:prefixedIRI(?dataRange)}", ?dataRange )}' title='{?dataRange}'>{fun:prefixedIRI(?dataRange)}</a>"
  } 
  WHERE {
    FILTER(ISURI(?dataRange))
  } .

  # data range complement
  TEMPLATE {
    "( <span class='logic'>not</span> "
    TEMPLATE <ontology/dataRangeDescription.rqg>( ?onto,  ?D ).
    ")"
  } 
  WHERE {
    FILTER(ISBLANK(?dataRange))
    ?dataRange rdf:type rdfs:Datatype ;
      owl:datatypeComplementOf ?D.
  } .

  # data range intersection
  TEMPLATE {
    before="(" ; 
    TEMPLATE <ontology/dataRangeDescription.rqg>( ?onto,  ?D ).
    ; separator=" <span class='logic'>and</span> "
    ; after=")"
  } 
  WHERE {
    FILTER(ISBLANK(?dataRange))
    ?dataRange rdf:type rdfs:Datatype ;
      owl:intersectionOf / rdf:rest* / rdf:first ?D .
  } .

  # data range union
  TEMPLATE {
    before="(" ; 
    TEMPLATE <ontology/dataRangeDescription.rqg>( ?onto,  ?D ).
    ; separator=" <span class='logic'>or</span> "
    ; after=")"
  } 
  WHERE {
    FILTER(ISBLANK(?dataRange))
    ?dataRange rdf:type rdfs:Datatype ;
      owl:unionOf / rdf:rest* / rdf:first ?D .
  } .

  # literal enumeration
  TEMPLATE {
    before="\{" ; 
    TEMPLATE <ontology/individualAndLiteralDescription.rqg>( ?onto,  ?v ).
    ; separator=", "
    ; after="}"
  } 
  WHERE {
    FILTER(ISBLANK(?dataRange))
    ?dataRange rdf:type rdfs:Datatype ;
      owl:oneOf / rdf:rest* / rdf:first ?v .
  } .

  # datatype restriction
  TEMPLATE {
    "(" 
    
    TEMPLATE <ontology/dataRangeDescription.rqg>( ?onto,  ?DN ) .
    " <span class='logic'>with restrictions:</span> "

    TEMPLATE <ontology/facetDescription.rqg>( ?onto,  ?restriction )
    WHERE {
       ?dataRange owl:withRestrictions / rdf:rest* / rdf:first ?restriction
    } .

    ")"
  } 
  WHERE {
    FILTER(ISBLANK(?dataRange))
    ?dataRange rdf:type rdfs:Datatype ;
      owl:onDatatype ?DN .
  } .


}