# ==========================================================================
#  W02  Checking the goods coming in
# ==========================================================================
#
#  FROM      Joining the Dots, "Checking the goods coming in"
#            https://www.semantechs.co.uk/articles/joining-the-dots/
#
#  CHECKS
#    The article's three examples: every payment has a date, an amount and
#    a currency; every claim points to the document it came from; a
#    directorship cannot end before it starts.
#
#  HOW IT WORKS
#    inv:PaymentShape is SHACL Core: three property shapes, each with
#    sh:minCount 1, and a datatype for the date. inv:ClaimShape targets
#    inv:Claim, the class every directorship, shareholding and ownership
#    record carries, and asks for a dct:source. The directorships are RDF
#    1.2 reifiers -- the {| ... |} after each directorship triple in the
#    data -- so the shape is checking statements about statements, and it
#    needs nothing special to do so: a reifier is a node like any other.
#    inv:DirectorshipDatesShape compares two values on the same node,
#    which Core can do with sh:lessThanOrEquals on the start date.
#
#  DIAGRAM
#       inv:pay-02     amount 18000, date 2025-07-01, currency ?        -> violation
#       inv:dir-03     John Smith isDirectorOf Blue Bay, source ?       -> violation
#       inv:dir-02     from 2021-06-01   until 2019-06-30               -> violation
#                      sh:path inv:from ; sh:lessThanOrEquals inv:until
#
#  WHAT TO TAKE AWAY
#    - A shape will not say whether a fact is true. It says where the
#      evidence is incomplete or contradicts itself.
#    - The open world holds until a shape closes it: the graph not saying
#      who the source was is not a fault until you say a source must be
#      there.
#    - Statements about statements are validated like anything else.
#
#  TRY IT
#    In the data tab, give inv:pay-02 a currency (inv:currency "GBP")
#    and press Validate again. Two violations are left. Then put a
#    dct:source on inv:dir-03 and correct inv:dir-02's dates.
#
#  DATA      investigation.ttl
#  LOAD IT   https://semantechs.co.uk/turtle-editor-viewer/?dot=https%3A%2F%2Fwww.semantechs.co.uk%2Farticles%2Fshow-your-working%2Fdemos%2F01-joining-the-dots%2Finvestigation.ttl&shapes=https%3A%2F%2Fwww.semantechs.co.uk%2Farticles%2Fshow-your-working%2Fdemos%2F01-joining-the-dots%2Fw02-checking-the-goods-coming-in.ttl
#  SPEC      SHACL 4.2.1 sh:minCount; 4.5.4 sh:lessThanOrEquals
#            https://www.w3.org/TR/shacl/#LessThanOrEqualsConstraintComponent
#  REPORTS   does not conform -- 3 violations, 0 warnings, 0 info; 8 shapes
# ==========================================================================

@prefix inv:  <https://example.org/investigation/> .
@prefix sh:   <http://www.w3.org/ns/shacl#> .
@prefix xsd:  <http://www.w3.org/2001/XMLSchema#> .
@prefix dct:  <http://purl.org/dc/terms/> .

inv:PaymentShape
    a              sh:NodeShape ;
    sh:targetClass inv:Payment ;
    sh:property [ sh:path inv:date ;     sh:minCount 1 ; sh:datatype xsd:date ;
                  sh:message "A payment must have a date." ] ;
    sh:property [ sh:path inv:amount ;   sh:minCount 1 ;
                  sh:message "A payment must have an amount." ] ;
    sh:property [ sh:path inv:currency ; sh:minCount 1 ;
                  sh:message "A payment must have a currency." ] .

inv:ClaimShape
    a              sh:NodeShape ;
    sh:targetClass inv:Claim ;
    sh:property [ sh:path dct:source ; sh:minCount 1 ;
                  sh:message "Every claim must point to the document it came from." ] .

inv:DirectorshipDatesShape
    a              sh:NodeShape ;
    sh:targetClass inv:Directorship ;
    sh:property [ sh:path inv:from ; sh:minCount 1 ; sh:lessThanOrEquals inv:until ;
                  sh:message "A directorship cannot end before it starts." ] .
