# ==========================================================================
#  W11  A rule anyone can argue with
# ==========================================================================
#
#  FROM      Field to Fork and Back Again, "What's in it, and is it good for me?"
#            https://www.semantechs.co.uk/articles/field-to-fork/
#
#  DERIVES
#    The article's example: this product contains an ingredient from this
#    crop; this crop contains gluten; therefore this product is not
#    suitable for a coeliac diet -- unless it is certified gluten-free.
#
#  HOW IT WORKS
#    One SHACL-AF SPARQL rule on every prepacked product. It walks down
#    what the product is made of, (gist:isMadeUpOf | food:milledFrom)+,
#    to any lot that is a batch of a crop marked as containing gluten, and
#    infers food:notSuitableFor food:CoeliacDiet. The exception is part of
#    the rule: FILTER NOT EXISTS skips a product certified gluten-free.
#    (Oats are among the cereals UK labelling law lists as containing
#    gluten; oats milled for a gluten-free line, and certified, are the
#    exception the rule allows for.) food:CoeliacProbe lists what the
#    rule inferred.
#
#  DIAGRAM
#       product --(isMadeUpOf | milledFrom)+--> lot --isBatchOf--> crop   crop containsGluten true
#       and NOT product certifiedGlutenFree true
#       =>  product notSuitableFor CoeliacDiet
#
#       Glen porridge pot           RO-2611 > OAT-0934 (oats)    not suitable
#       Glen oatcakes               RO-2612 > OAT-0935 (oats)    not suitable
#       Glen gluten-free porridge   RO-2650 > OAT-0940 (oats)    certified: excepted
#
#  WHAT TO TAKE AWAY
#    - Because the rule is written down, anyone can see why the answer
#      came out as it did, and argue with the rule rather than the result.
#    - Exceptions belong in the rule, where they can be seen, rather than
#      in somebody's head.
#
#  TRY IT
#    In the data tab, delete food:certifiedGlutenFree true from the
#    gluten-free porridge and validate again. The rule now flags it too:
#    three inferences.
#
#  DATA      porridge.ttl
#  INFERENCE rules   (the Inference dropdown in the editor)
#  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%2Fw11-a-rule-anyone-can-argue-with.ttl&inference=rules
#  SPEC      SHACL-AF 8.6 SPARQL Rules
#            https://www.w3.org/TR/shacl-af/#SPARQLRule
#  REPORTS   does not conform -- 0 violations, 0 warnings, 2 info; 3 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 "gist" ; sh:namespace "https://w3id.org/semanticarts/ns/ontology/gist/"^^xsd:anyURI ] ;
    sh:declare [ sh:prefix "rdfs" ; sh:namespace "http://www.w3.org/2000/01/rdf-schema#"^^xsd:anyURI ] .

food:CoeliacRule
    a              sh:NodeShape ;
    sh:targetClass food:PrepackedProduct ;
    sh:rule [
        a            sh:SPARQLRule ;
        sh:prefixes  food:prefixes ;
        sh:construct """
            CONSTRUCT { $this food:notSuitableFor food:CoeliacDiet }
            WHERE {
                $this (gist:isMadeUpOf | food:milledFrom)+ ?lot .
                ?lot food:isBatchOf ?crop .
                ?crop food:containsGluten true .
                FILTER NOT EXISTS { $this food:certifiedGlutenFree true }
            }
        """ ;
    ] .

food:CoeliacProbe
    a              sh:NodeShape ;
    sh:targetClass food:PrepackedProduct ;
    sh:sparql [
        sh:prefixes  food:prefixes ;
        sh:severity  sh:Info ;
        sh:message   "inferred: {?product} is not suitable for a coeliac diet" ;
        sh:select    """
            SELECT $this ?product WHERE { $this food:notSuitableFor food:CoeliacDiet ; rdfs:label ?product . }
        """ ;
    ] .
