# ==========================================================================
#  W09  Checking the goods coming in, and the open world
# ==========================================================================
#
#  FROM      Field to Fork and Back Again, "Checking the goods coming in"
#            https://www.semantechs.co.uk/articles/field-to-fork/
#
#  CHECKS
#    The article's three examples. Every lot must have a date and a place
#    of harvest or production. Every prepacked product must name its
#    allergens. An organic claim must point to a current certificate.
#
#  HOW IT WORKS
#    food:LotShape uses sh:or: a lot conforms if it has a harvest place and
#    date, or a production place and date. food:AllergenShape is the
#    article's point about the open world. A product that says nothing
#    about sesame has not said it contains none, so the shape asks, for
#    every allergen in the data, for an explicit food:contains or
#    food:freeFrom; silence is the fault. (The data lists three of the
#    fourteen allergens UK law names, to keep the report short.)
#    food:OrganicShape follows the claim to its certificate and compares
#    the certificate's expiry with the article's date, 10 November 2026.
#
#  DIAGRAM
#       Lot OAT-0934     harvestedFrom Field 12, harvestedOn 2026-08-28      ok (first branch)
#       Lot RO-2611      producedAt Glen Mill, producedOn 2026-10-02         ok (second branch)
#       Lot OAT-0935     harvestedFrom Field 14, no date                     -> violation
#
#       Glen porridge pot   contains gluten, milk    sesame: (nothing)       -> violation
#       Glen oatcakes       organic, certificate OF-1123 valid until 2026-06-30 -> violation
#
#  WHAT TO TAKE AWAY
#    - In an open world, "not recorded" and "none" are different facts.
#      A shape is where you require the difference to be written down.
#    - sh:or lets one shape accept either of two complete descriptions.
#    - A claim is only as good as what it points to; a shape can follow
#      the pointer and check the thing at the end.
#
#  TRY IT
#    In the data tab, extend the oatcakes' certificate to 2027-06-30 and
#    validate again: two violations. Then add food:freeFrom food:Sesame to
#    the porridge pot, and a harvest date to Lot OAT-0935.
#
#  DATA      porridge.ttl
#  LOAD IT   https://semantechs.co.uk/turtle-editor-viewer/?dot=https%3A%2F%2Fwww.semantechs.co.uk%2Farticles%2Fshow-your-working%2Fdemos%2F03-field-to-fork%2Fporridge.ttl&shapes=https%3A%2F%2Fwww.semantechs.co.uk%2Farticles%2Fshow-your-working%2Fdemos%2F03-field-to-fork%2Fw09-checking-the-goods-coming-in.ttl
#  SPEC      SHACL 4.6.3 sh:or; 5 SPARQL-based Constraints
#            https://www.w3.org/TR/shacl/#OrConstraintComponent
#  REPORTS   does not conform -- 3 violations, 0 warnings, 0 info; 9 shapes
# ==========================================================================

@prefix food: <https://example.org/food/> .
@prefix sh:   <http://www.w3.org/ns/shacl#> .
@prefix owl:  <http://www.w3.org/2002/07/owl#> .
@prefix xsd:  <http://www.w3.org/2001/XMLSchema#> .

food:prefixes
    a  owl:Ontology ;
    sh:declare [ sh:prefix "food" ; sh:namespace "https://example.org/food/"^^xsd:anyURI ] ;
    sh:declare [ sh:prefix "rdfs" ; sh:namespace "http://www.w3.org/2000/01/rdf-schema#"^^xsd:anyURI ] ;
    sh:declare [ sh:prefix "xsd" ;  sh:namespace "http://www.w3.org/2001/XMLSchema#"^^xsd:anyURI ] .

food:LotShape
    a              sh:NodeShape ;
    sh:targetClass food:Lot ;
    sh:message     "Every lot must have a date and a place of harvest or production." ;
    sh:or (
        [ sh:property [ sh:path food:harvestedFrom ; sh:minCount 1 ] ;
          sh:property [ sh:path food:harvestedOn ;   sh:minCount 1 ] ]
        [ sh:property [ sh:path food:producedAt ;    sh:minCount 1 ] ;
          sh:property [ sh:path food:producedOn ;    sh:minCount 1 ] ]
    ) .

food:AllergenShape
    a              sh:NodeShape ;
    sh:targetClass food:PrepackedProduct ;
    sh:sparql [
        sh:prefixes  food:prefixes ;
        sh:message   "{?product} says nothing about {?allergen}: not the same as saying it contains none." ;
        sh:select    """
            SELECT $this ?product ?allergen WHERE {
                ?a a food:Allergen ; rdfs:label ?allergen .
                FILTER NOT EXISTS { $this food:contains ?a }
                FILTER NOT EXISTS { $this food:freeFrom ?a }
                $this rdfs:label ?product .
            }
        """ ;
    ] .

food:OrganicShape
    a              sh:NodeShape ;
    sh:targetClass food:PrepackedProduct ;
    sh:sparql [
        sh:prefixes  food:prefixes ;
        sh:message   "{?product} claims to be organic, but its certificate ran out on {?until}." ;
        sh:select    """
            SELECT $this ?product ?until WHERE {
                $this food:claims food:Organic ; food:organicCertificate ?c ; rdfs:label ?product .
                ?c food:validUntil ?until .
                FILTER (?until < "2026-11-10"^^xsd:date)
            }
        """ ;
    ] .
