# ==========================================================================
#  W14  Gate 2: secured land rights
# ==========================================================================
#
#  FROM      Down the Line, "Rules as shapes"
#            https://www.semantechs.co.uk/articles/down-the-line/
#
#  CHECKS
#    The land route of the connections reform's readiness test: an
#    application must show secured land rights -- an option of at least three
#    years, ownership, or a lease with at least 20 years left.
#
#  HOW IT WORKS
#    All SHACL Core. en:LandRouteShape says an application has land rights,
#    and that each set of rights conforms to at least one of three shapes,
#    with sh:or. Each of the three pairs a class with its own condition: an
#    option agreement needs en:termYears of 3 or more, ownership a title
#    number, a lease en:remainingTermYears of 20 or more. A record-level check
#    like this can run on every change.
#
#  DIAGRAM
#    APP-0101   wind farm   ownership, title QED00001        OwnershipShape   ok
#    APP-0102   battery     lease, 25 years left             LeaseShape       ok
#    APP-0103   solar farm  lease, 12 years left             none of the three -> violation
#
#  WHAT TO TAKE AWAY
#    - sh:or is how a rule with alternatives ("an option, or ownership, or a
#      lease") is written down.
#    - Title QED00001 is the same title as in W05 to W07: the electricity and
#      land examples could share one identifier for it.
#
#  TRY IT
#    In the data tab, give APP-0103's lease 21 years and validate again. It
#    conforms.
#
#  DATA      connection-applications.ttl
#  LOAD IT   https://semantechs.co.uk/turtle-editor-viewer/?dot=https%3A%2F%2Fwww.semantechs.co.uk%2Farticles%2Fshow-your-working%2Fdemos%2F05-down-the-line%2Fconnection-applications.ttl&shapes=https%3A%2F%2Fwww.semantechs.co.uk%2Farticles%2Fshow-your-working%2Fdemos%2F05-down-the-line%2Fw14-gate-2-land-rights.ttl
#  SPEC      SHACL 4.6.3 sh:or
#            https://www.w3.org/TR/shacl/#OrConstraintComponent
#  REPORTS   does not conform -- 1 violations, 0 warnings, 0 info; 8 shapes
# ==========================================================================

@prefix en:  <https://example.org/energy/> .
@prefix sh:  <http://www.w3.org/ns/shacl#> .
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .

en:LandRouteShape a sh:NodeShape ;
    sh:targetClass en:ConnectionApplication ;
    sh:property [
        sh:path en:landRights ;
        sh:minCount 1 ;
        sh:or ( en:OptionShape en:OwnershipShape en:LeaseShape ) ;
        sh:message "An application must show secured land rights: an option of at least three years, ownership, or a lease with at least 20 years left"
    ] .

en:OptionShape a sh:NodeShape ;
    sh:class en:OptionAgreement ;
    sh:property [ sh:path en:termYears ; sh:minCount 1 ; sh:minInclusive 3 ] .

en:OwnershipShape a sh:NodeShape ;
    sh:class en:Ownership ;
    sh:property [ sh:path en:titleNumber ; sh:minCount 1 ; sh:datatype xsd:string ] .

en:LeaseShape a sh:NodeShape ;
    sh:class en:Lease ;
    sh:property [ sh:path en:remainingTermYears ; sh:minCount 1 ; sh:minInclusive 20 ] .
