@prefix mk: <https://about.datameesters.nl/_previews/math-semantic-lab/v3/ontology#> .
@prefix sh: <http://www.w3.org/ns/shacl#> .
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .

mk:EvidenceShape a sh:NodeShape ; sh:targetClass mk:Evidence ;
  sh:property [ sh:path mk:source ; sh:nodeKind sh:IRI ; sh:minCount 1 ; sh:maxCount 1 ] ;
  sh:property [ sh:path mk:sourceHash ; sh:pattern "^[0-9a-f]{64}$" ; sh:minCount 1 ; sh:maxCount 1 ] ;
  sh:property [ sh:path mk:startOffset ; sh:datatype xsd:integer ; sh:minInclusive 0 ; sh:minCount 1 ; sh:maxCount 1 ; sh:lessThan mk:endOffset ] ;
  sh:property [ sh:path mk:endOffset ; sh:datatype xsd:integer ; sh:minCount 1 ; sh:maxCount 1 ] ;
  sh:property [ sh:path mk:quote ; sh:datatype xsd:string ; sh:minLength 1 ; sh:minCount 1 ; sh:maxCount 1 ] ;
  sh:property [ sh:path mk:document ; sh:minCount 1 ; sh:maxCount 1 ] .
mk:NodeShape a sh:NodeShape ; sh:targetSubjectsOf mk:localId ;
  sh:property [ sh:path mk:localId ; sh:datatype xsd:string ; sh:minCount 1 ; sh:maxCount 1 ] ;
  sh:property [ sh:path mk:layer ; sh:in ("local" "concept") ; sh:minCount 1 ; sh:maxCount 1 ] ;
  sh:property [ sh:path mk:reviewStatus ; sh:hasValue "curated-source" ; sh:minCount 1 ; sh:maxCount 1 ] ;
  sh:property [ sh:path mk:evidence ; sh:class mk:Evidence ; sh:minCount 1 ] .
mk:AssertionShape a sh:NodeShape ; sh:targetClass mk:EvidenceAssertion ;
  sh:property [ sh:path mk:subject ; sh:nodeKind sh:IRI ; sh:minCount 1 ; sh:maxCount 1 ] ;
  sh:property [ sh:path mk:predicate ; sh:nodeKind sh:IRI ; sh:minCount 1 ; sh:maxCount 1 ] ;
  sh:property [ sh:path mk:object ; sh:nodeKind sh:IRI ; sh:minCount 1 ; sh:maxCount 1 ] ;
  sh:property [ sh:path mk:evidence ; sh:class mk:Evidence ; sh:minCount 1 ] ;
  sh:property [ sh:path mk:reviewStatus ; sh:hasValue "curated-source" ; sh:minCount 1 ; sh:maxCount 1 ] ;
  sh:sparql [
    sh:message "Unknown predicate or undeclared entity endpoint." ;
    sh:select """PREFIX mk: <https://about.datameesters.nl/_previews/math-semantic-lab/v3/ontology#>
      PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
      SELECT $this WHERE { $this mk:subject ?s ; mk:predicate ?p ; mk:object ?o .
        FILTER (!EXISTS { ?s mk:localId ?sid } || !EXISTS { ?o mk:localId ?oid } ||
          !EXISTS { ?p rdfs:domain ?d ; rdfs:range ?r }) }""" ] ;
  sh:sparql [
    sh:message "Assertion violates explicit predicate domain or range." ;
    sh:select """PREFIX mk: <https://about.datameesters.nl/_previews/math-semantic-lab/v3/ontology#>
      PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
      PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
      SELECT $this WHERE {
        $this mk:subject ?s ; mk:predicate ?p ; mk:object ?o .
        ?p rdfs:domain ?d ; rdfs:range ?r .
        FILTER (!EXISTS { ?s rdf:type/rdfs:subClassOf* ?d } ||
                !EXISTS { ?o rdf:type/rdfs:subClassOf* ?r })
      }""" ] ;
  sh:sparql [
    sh:message "A concept mapping must classify a compatible scoped referent, not merge it." ;
    sh:select """PREFIX mk: <https://about.datameesters.nl/_previews/math-semantic-lab/v3/ontology#>
      PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
      PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
      SELECT $this WHERE { $this mk:subject ?s ; mk:predicate mk:hasConcept ; mk:object ?o .
        FILTER (!EXISTS { ?s mk:layer "local" } || !EXISTS { ?o mk:layer "concept" ; mk:conceptKind ?k . ?s rdf:type/rdfs:subClassOf* ?k })
      }""" ] ;
  sh:sparql [
    sh:message "Proof-use assertion must retain its qualifier." ;
    sh:select """PREFIX mk: <https://about.datameesters.nl/_previews/math-semantic-lab/v3/ontology#>
      SELECT $this WHERE { $this mk:predicate mk:proofUses .
      FILTER NOT EXISTS { $this mk:qualifier ?q . FILTER(STRLEN(STR(?q)) > 0) } }""" ] ;
  sh:sparql [
    sh:message "A quantitative statement may not change from premise to conclusion or vice versa." ;
    sh:select """PREFIX mk: <https://about.datameesters.nl/_previews/math-semantic-lab/v3/ontology#>
      SELECT $this WHERE { $this mk:subject ?s ; mk:predicate mk:hasQuantitativeStatement ; mk:object ?o .
        ?s mk:role ?sr . ?o mk:role ?or .
        FILTER(?sr IN ("premise","conclusion") && ?or IN ("premise","conclusion") && ?sr != ?or)
      }""" ] .

mk:ProofBranchShape a sh:NodeShape ;
 sh:targetClass mk:ProofBranch ;
 sh:property [ sh:path mk:branchQualifier ; sh:datatype xsd:string ; sh:minCount 1 ; sh:maxCount 1 ] ;
 sh:sparql [
  sh:message "A proof branch needs a single owning claim and single scoped condition." ;
  sh:select """PREFIX mk: <https://about.datameesters.nl/_previews/math-semantic-lab/v3/ontology#>
  SELECT $this WHERE {
    FILTER (
      !EXISTS { ?owner mk:predicate mk:hasProofBranch ; mk:object $this } ||
      EXISTS { ?a mk:predicate mk:hasProofBranch ; mk:object $this .
               ?b mk:predicate mk:hasProofBranch ; mk:object $this . FILTER(?a != ?b) } ||
      !EXISTS { ?cond mk:predicate mk:branchCondition ; mk:subject $this ; mk:object ?c .
                $this mk:scope ?scope . ?c mk:scope ?scope } ||
      EXISTS { ?a mk:predicate mk:branchCondition ; mk:subject $this .
               ?b mk:predicate mk:branchCondition ; mk:subject $this . FILTER(?a != ?b) }
    )
  }""" ] ;
 sh:sparql [
  sh:message "A branch use must match the branch qualifier and a direct qualified proof-use assertion of its owning claim." ;
  sh:select """PREFIX mk: <https://about.datameesters.nl/_previews/math-semantic-lab/v3/ontology#>
  SELECT $this WHERE {
    ?use mk:predicate mk:branchUses ; mk:subject $this ; mk:object ?result .
    FILTER NOT EXISTS {
      $this mk:branchQualifier ?q .
      ?use mk:qualifier ?q .
      ?ownership mk:predicate mk:hasProofBranch ; mk:subject ?claim ; mk:object $this .
      ?direct mk:predicate mk:proofUses ; mk:subject ?claim ; mk:object ?result ; mk:qualifier ?q .
    }
  }""" ] .
