@base           <https://www.w3.org/ns/ldt/topic-hierarchy/templates> .

@prefix :	<#> .
@prefix rdf:	<http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix rdfs:	<http://www.w3.org/2000/01/rdf-schema#> .
@prefix xsd:	<http://www.w3.org/2001/XMLSchema#> .
@prefix owl:	<http://www.w3.org/2002/07/owl#> .
@prefix ldt:	<https://www.w3.org/ns/ldt#> .
@prefix ct:	<https://www.w3.org/ns/ldt/core/templates#> .
@prefix dh:	<https://www.w3.org/ns/ldt/document-hierarchy#> .
@prefix sd:	<http://www.w3.org/ns/sparql-service-description#> .
@prefix sp:	<http://spinrdf.org/sp#> .
@prefix dct:	<http://purl.org/dc/terms/> .
@prefix vann:	<http://purl.org/vocab/vann/> .
@prefix sioc:	<http://rdfs.org/sioc/ns#> .
@prefix foaf:	<http://xmlns.com/foaf/0.1/> .

: a ldt:Ontology, owl:Ontology ;
    owl:imports ct:, sp: ;
    owl:versionInfo "1.1.4" ;
    rdfs:label "Topic hierarchy templates" ;
    rdfs:comment "Document hierarchy with topic resources" ;
    dct:created "2016-08-31T21:54:00+02:00"^^xsd:dateTime ;
    vann:preferredNamespaceUri "https://www.w3.org/ns/ldt/topic-hierarchy/templates#" ;
    vann:preferredNamespacePrefix "tht";
    dct:license <http://www.opendatacommons.org/licenses/pddl/1.0/> .

# TEMPLATES

:Document a ldt:Template ;
    ldt:extends ct:Document ;
    ldt:query :DescribeWithTopic ;
    ldt:update :DeleteWithTopic ;
    rdfs:label "Document" ;
    rdfs:isDefinedBy : .

# COMMANDS

:DescribeWithTopic a sp:Describe, sp:Query ;
    sp:text """PREFIX  foaf: <http://xmlns.com/foaf/0.1/>

DESCRIBE ?this ?primaryTopic
WHERE
  { ?this ?p ?o
    OPTIONAL
      { ?this foaf:primaryTopic ?primaryTopic }
  }""" ;
    rdfs:label "Describe with topic" ;
    rdfs:isDefinedBy : .

:DeleteWithTopic a sp:DeleteWhere, sp:Update ;
      sp:text """PREFIX foaf: <http://xmlns.com/foaf/0.1/>
DELETE {
    ?this ?p ?o .
    ?primaryTopic ?p ?o .
}
WHERE {
    {
        ?this ?p ?o .
    }
    UNION
    {
        ?primaryTopic foaf:isPrimaryTopicOf ?this .
        ?primaryTopic ?p ?o .
    }
}""" ;
    rdfs:label "Delete with topic" ;
    rdfs:isDefinedBy : .