@base		<https://w3id.org/atomgraph/linkeddatahub/admin> .

@prefix :	<#> .
@prefix ac:	<https://w3id.org/atomgraph/client#> .
@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 acl:	<http://www.w3.org/ns/auth/acl#> .
@prefix cert:	<http://www.w3.org/ns/auth/cert#> .
@prefix dh:	<https://w3id.org/atomgraph/linkeddatahub/document-hierarchy#> .
@prefix sh:     <http://www.w3.org/ns/shacl#> .
@prefix sp:	<http://spinrdf.org/sp#> .
@prefix spin:	<http://spinrdf.org/spin#> .
@prefix sioc:	<http://rdfs.org/sioc/ns#> .
@prefix foaf:	<http://xmlns.com/foaf/0.1/> .
@prefix lacl:	<https://w3id.org/atomgraph/linkeddatahub/admin/acl#> .
@prefix ldh:	<https://w3id.org/atomgraph/linkeddatahub#> .
@prefix dct:	<http://purl.org/dc/terms/> .

: a owl:Ontology ;
    owl:imports sioc:, ldh:, lacl:, foaf:, <http://www.w3.org/2002/07/owl> ;
    owl:versionInfo "1.1.4" ;
    rdfs:label "Admin ontology" ;
    rdfs:comment "AtomGraph admin application ontology" .

:SignUp a owl:Class ;
    rdfs:label "Sign up" ;
    rdfs:isDefinedBy : .

:RequestAccess a owl:Class ;
    rdfs:label "Request access" ;
    rdfs:isDefinedBy : .

:MissingRDFSLabel a ldh:MissingPropertyValue ;
    rdfs:label "Missing rdfs:label" ;
    sp:arg1 rdfs:label ;
    rdfs:isDefinedBy : .

### EXTERNAL ASSERTIONS

# http://spinrdf.org/sp

# sp:Query spin:constraint :MissingRDFSLabel .

# http://www.w3.org/2002/07/owl

<http://www.w3.org/2002/07/owl> rdfs:label "The OWL 2 Schema vocabulary (OWL 2)" .

# ontology

owl:Ontology spin:constructor [
        a ldh:Constructor ;
        rdfs:label "Ontology constructor" ;
        sp:text """
    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#>

    CONSTRUCT {
        $this rdfs:label [ a xsd:string ] ;
            rdfs:comment [ a xsd:string ] ;
            owl:imports [ a rdfs:Resource ] ;
            owl:versionInfo "1.0-SNAPSHOT" .
    }
    WHERE {}"""
    ] .
#    spin:constraint :MissingRDFSLabel .

rdfs:isDefinedBy ldh:inverseView :OntologyClassView .

:OntologyClassView a ldh:View ;
    dct:title "Ontology classes" ;
    spin:query :SelectOntologyClasses ;
    ac:mode ac:TableMode ;
    rdfs:isDefinedBy : .

:SelectOntologyClasses a sp:Select ;
    rdfs:label "Select classes" ;
    sp:text """
PREFIX  rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX  owl:  <http://www.w3.org/2002/07/owl#>
PREFIX  foaf: <http://xmlns.com/foaf/0.1/>

SELECT DISTINCT  ?class
WHERE
  { GRAPH ?classGraph
      {   { ?class  a  owl:Class }
        UNION
          { ?class  a  rdfs:Class }
        ?class  rdfs:isDefinedBy  $about
      }
  }
""" .

# CLASS

owl:Class spin:constructor [
        a ldh:Constructor ;
        rdfs:label "Class constructor" ;
        sp:text """
    PREFIX ldh:     <https://w3id.org/atomgraph/linkeddatahub#>
    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 spin:    <http://spinrdf.org/spin#>
    PREFIX foaf:    <http://xmlns.com/foaf/0.1/>

    CONSTRUCT {
        $this rdfs:subClassOf [ a owl:Class ] ;
            rdfs:label [ a xsd:string ] ;
            rdfs:comment [ a xsd:string ] ;
            spin:constructor [ a ldh:Constructor ] ;
            spin:constraint [ a rdfs:Resource ] .
    }
    WHERE {}"""
    ] .
#    spin:constraint :MissingRDFSLabel .

# object property

owl:ObjectProperty spin:constructor [
        a ldh:Constructor ;
        rdfs:label "Object property constructor" ;
        sp:text """
    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#>

    CONSTRUCT {
        $this rdfs:subPropertyOf [ a rdf:Property ] ;
            rdfs:label [ a xsd:string ] ;
            rdfs:comment [ a xsd:string ] ;
            rdfs:domain [ a rdfs:Resource ] ;
            rdfs:range [ a rdfs:Resource ] .
    }
    WHERE {}"""
    ] .
#    spin:constraint :MissingRDFSLabel .

# datatype property

owl:DatatypeProperty spin:constructor [
        a ldh:Constructor ;
        rdfs:label "Datatype property constructor" ;
        sp:text """
    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#>

    CONSTRUCT {
        $this rdfs:subPropertyOf [ a rdf:Property ] ;
            rdfs:label [ a xsd:string ] ;
            rdfs:comment [ a xsd:string ] ;
            rdfs:domain [ a rdfs:Resource ] ;
            rdfs:range [ a rdfs:Resource ] .
    }
    WHERE {}"""
    ] .
#    spin:constraint :MissingRDFSLabel .

# restriction

owl:Restriction spin:constructor [
        a ldh:Constructor ;
        rdfs:label "Restriction constructor" ;
        sp:text """
            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#>

            CONSTRUCT {
                $this rdfs:label [ a xsd:string ] ;
                  rdfs:comment [ a xsd:string ] ;
                  owl:onProperty [ a rdf:Property ] ;
                  owl:allValuesFrom [ a rdfs:Resource ] ;
                  owl:hasValue [ a rdfs:Resource ] ;
                  rdfs:isDefinedBy [ a owl:Ontology ] .
            }
            WHERE {}"""
    ] .
#    spin:constraint :MissingRDFSLabel .

# http://www.w3.org/ns/auth/acl

acl: rdfs:label "Basic Access Control ontology" .

# AUTHORIZATION

acl:Authorization spin:constructor [
        a ldh:Constructor ;
        rdfs:label "Authorization constructor" ;
        sp:text """
            PREFIX dct:     <http://purl.org/dc/terms/>
            PREFIX rdfs:    <http://www.w3.org/2000/01/rdf-schema#>
            PREFIX owl:     <http://www.w3.org/2002/07/owl#>
            PREFIX xsd:     <http://www.w3.org/2001/XMLSchema#>
            PREFIX acl:     <http://www.w3.org/ns/auth/acl#>
            PREFIX foaf:    <http://xmlns.com/foaf/0.1/>

            CONSTRUCT {
                $this rdfs:label [ a xsd:string ] ;
                    dct:description [ a xsd:string ] ;
                    acl:mode [ a acl:Mode ] ;
                    acl:accessTo [ a rdfs:Resource ] ;
                    acl:accessToClass [ a owl:Class ] ;
                    acl:agent [ a foaf:Agent ] ;
                    acl:agentClass [ a owl:Class ] ;
                    acl:agentGroup [ a foaf:Group ] .
            }
            WHERE {}"""
    ] ;
    spin:constraint # :MissingRDFSLabel,
    [
        a ldh:MissingPropertyValue ;
        sp:arg1 acl:mode
    ] .

acl:agent ldh:view :AuthorizationAgentView .

:AuthorizationAgentView a ldh:View ;
    dct:title "Authorization agents" ;
    spin:query :SelectAuthorizationAgents ;
    rdfs:isDefinedBy : .

:SelectAuthorizationAgents a sp:Select ;
    rdfs:label "Select agents" ;
    sp:text """
PREFIX  foaf: <http://xmlns.com/foaf/0.1/>
PREFIX  acl:  <http://www.w3.org/ns/auth/acl#>

SELECT DISTINCT  ?agent
WHERE
  { GRAPH ?authGraph
      { $about  acl:agent  ?agent }
  }
""" ;
    rdfs:isDefinedBy : .

# http://www.w3.org/ns/auth/cert

cert:Key rdfs:label "Key" .

cert:PublicKey spin:constructor [
        a ldh:Constructor ;
        rdfs:label "Public key constructor" ;
        sp:text """
            PREFIX xsd:     <http://www.w3.org/2001/XMLSchema#>
            PREFIX cert:    <http://www.w3.org/ns/auth/cert#>

            CONSTRUCT {
                $this cert:modulus [ a xsd:hexBinary ] ;
                    cert:exponent 65537 .
            }
            WHERE {}"""
    ] ;
    spin:constraint [
        a ldh:MissingPropertyValue ;
        rdfs:label "Missing cert:modulus" ;
        sp:arg1 cert:modulus
    ], [
        a ldh:MissingPropertyValue ;
        rdfs:label "Missing cert:exponent" ;
        sp:arg1 cert:exponent
    ] .

# http://www.w3.org/ns/shacl

sh:NodeShape spin:constructor :NodeShapeConstructor .

:NodeShapeConstructor a ldh:Constructor ;
    rdfs:label "Node shape constructor" ;
    sp:text """
        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 sh:      <http://www.w3.org/ns/shacl#>

        CONSTRUCT {
            $this rdfs:label [ a xsd:string ] ;
                sh:targetClass [ a owl:Class ] ;
                sh:property [ a sh:PropertyShape ] .
        }
        WHERE {}""" .

sh:PropertyShape spin:constructor :PropertyShapeConstructor .

:PropertyShapeConstructor a ldh:Constructor ;
    rdfs:label "Property shape constructor" ;
    sp:text """
        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 sh:      <http://www.w3.org/ns/shacl#>


        CONSTRUCT {
            $this sh:name [ a xsd:string ] ;
                sh:path [ a rdf:Property ] ;
                sh:minCount [ a xsd:integer ] ;
                sh:maxCount [ a xsd:integer ] ;
                sh:datatype [ a rdfs:Resource ] .
        }
        WHERE {}""" .

# http://rdfs.org/sioc/ns

sioc: rdfs:label "SIOC Core Ontology Namespace" .

sioc:UserAccount spin:constructor :UserAccountConstructor ;
    spin:constraint [
        a ldh:MissingPropertyValue ;
        rdfs:label "Missing sioc:name" ;
        sp:arg1 :name
    ] , [
        a ldh:MissingPropertyValue ;
        rdfs:label "Missing sioc:email" ;
        sp:arg1 :email
    ], [
        a ldh:MissingPropertyValue ;
        rdfs:label "Missing sioc:account_of" ;
        sp:arg1 :account_of
    ] .

:UserAccountConstructor a ldh:Constructor ;
    rdfs:label "User account constructor" ;
    sp:text """
        PREFIX rdfs:    <http://www.w3.org/2000/01/rdf-schema#>
        PREFIX foaf:    <http://xmlns.com/foaf/0.1/>
        PREFIX sioc:    <http://rdfs.org/sioc/ns#>
        PREFIX xsd:     <http://www.w3.org/2001/XMLSchema#>

        CONSTRUCT {
            $this sioc:name [ a xsd:string ] ;
                sioc:email [ a rdfs:Resource ] ;
                sioc:account_of [ a foaf:Agent ] .
        }
        WHERE {}""" .

# http://xmlns.com/foaf/0.1/

foaf: rdfs:label "Friend of a Friend (FOAF) vocabulary" .

foaf:Agent spin:constructor :AgentConstructor .
#    spin:constraint [
#        a ldh:MissingPropertyValue ;
#        rdfs:label "Missing cert:key" ;
#        sp:arg1 cert:key
#    ] ; # Agent has either PublicKey or UserAccount

:AgentConstructor a ldh:Constructor ;
    rdfs:label "Agent constructor" ;
    sp:text """
        PREFIX rdfs:    <http://www.w3.org/2000/01/rdf-schema#>
        PREFIX xsd:     <http://www.w3.org/2001/XMLSchema#>
        PREFIX acl:     <http://www.w3.org/ns/auth/acl#>
        PREFIX cert:    <http://www.w3.org/ns/auth/cert#>

        CONSTRUCT {
            $this cert:key [ a cert:PublicKey ] ;
                acl:delegates [ a rdfs:Resource ] .
        }
        WHERE {}""" .

foaf:Person spin:constructor :PersonConstructor ;
    spin:constraint [
        a ldh:MissingPropertyValue ;
        rdfs:label "Missing foaf:givenName" ;
        sp:arg1 foaf:givenName
    ] , [
        a ldh:MissingPropertyValue ;
        rdfs:label "Missing foaf:familyName" ;
        sp:arg1 foaf:familyName
    ] , [
        a ldh:MissingPropertyValue ;
        rdfs:label "Missing foaf:based_near" ;
        sp:arg1 foaf:based_near
    ] , [ 
        a ldh:MissingPropertyValue ;
        rdfs:label "Missing foaf:mbox" ;
        sp:arg1 foaf:mbox
    ] .

:PersonConstructor a ldh:Constructor ;
    rdfs:label "Person constructor" ;
    sp:text """
        PREFIX rdfs:    <http://www.w3.org/2000/01/rdf-schema#>
        PREFIX foaf:    <http://xmlns.com/foaf/0.1/>
        PREFIX xsd:     <http://www.w3.org/2001/XMLSchema#>
        PREFIX vivo:    <http://vivoweb.org/ontology/core#>

        CONSTRUCT {
            $this foaf:givenName [ a xsd:string ] ;
                foaf:familyName [ a xsd:string ] ;
                foaf:mbox [ a rdfs:Resource ] ;
                foaf:based_near [ a rdfs:Resource ] ;
                vivo:orcidId [ a rdfs:Resource ] .
        }
        WHERE {}""" .

foaf:Group spin:constructor :GroupConstructor ;
    spin:constraint [ a ldh:MissingPropertyValue ;
        rdfs:label "Missing name" ;
        sp:arg1 foaf:name
    ] .

:GroupConstructor a ldh:Constructor ;
    rdfs:label "Group constructor" ;
    sp:text """
        PREFIX xsd:     <http://www.w3.org/2001/XMLSchema#>
        PREFIX foaf:    <http://xmlns.com/foaf/0.1/>
        PREFIX dct:     <http://purl.org/dc/terms/>

        CONSTRUCT {
            $this foaf:name [ a xsd:string ] ;
                dct:description [ a xsd:string ] ;
                foaf:member [ a foaf:Agent ] .
        }
        WHERE {}""" .