# ==========================================================================
#  W07  Who stands behind a piece of ground
# ==========================================================================
#
#  FROM      Who Owns the Glen?, "Rules from the statute book"
#            https://www.semantechs.co.uk/articles/who-owns-the-glen/
#
#  DERIVES
#    The article's rule: if A has a controlled interest in B, and B owns C,
#    then A has an interest in C -- applied through as many layers as the
#    corporate structure has.
#
#  HOW IT WORKS
#    Two SHACL-AF SPARQL rules on every organisation with a controlled
#    interest. The first is the article's rule as written, with "B owns C"
#    read as "B is the current proprietor of title C". The second carries
#    an interest one layer further up: if A has a controlled interest in B,
#    and B has an interest in C, then A has an interest in C. Run to a
#    fixpoint ("rules, iterated"), the second rule climbs every layer there
#    is. land:InterestProbe lists each inferred land:hasInterestIn as an
#    Info result.
#
#  DIAGRAM
#       Morven Foundation --controlled interest--> The Àrd-ìre Trust
#                         --controlled interest--> Glenmeadhanach Estates Ltd
#                                                    --proprietor of--> QED00001, QED00002
#
#       rule 1   Àrd-ìre Trust      has an interest in QED00001, QED00002
#                Morven Foundation  has an interest in QED00004 (Strathbeag), QED00006 (Corsemill)
#       rule 2   Morven Foundation  has an interest in QED00001, QED00002     (through the trust)
#                                                                  total  6 info
#
#  WHAT TO TAKE AWAY
#    - A rule written once applies to every layer of every structure in
#      the graph, including ones nobody has looked at yet.
#    - The rule reads "owns" as "is the current proprietor of": choosing
#      that reading is the policy decision, and it is written down where
#      anyone can see and change it.
#    - Achnagairn Holdings SA has no controller in the data, so nothing is
#      inferred for it. The open world again: not recorded is not none.
#
#  TRY IT
#    In the data tab, delete the line saying the Morven Foundation has a
#    controlled interest in the Àrd-ìre Trust, and validate again. Four
#    inferences are left: the foundation no longer stands behind the glen.
#
#  DATA      glen-titles.ttl
#  INFERENCE rules-iterated   (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%2F02-who-owns-the-glen%2Fglen-titles.ttl&shapes=https%3A%2F%2Fwww.semantechs.co.uk%2Farticles%2Fshow-your-working%2Fdemos%2F02-who-owns-the-glen%2Fw07-who-stands-behind-the-ground.ttl&inference=rules-iterated
#  SPEC      SHACL-AF 8.6 SPARQL Rules
#            https://www.w3.org/TR/shacl-af/#SPARQLRule
#  REPORTS   does not conform -- 0 violations, 0 warnings, 6 info; 3 shapes
# ==========================================================================

@prefix land: <https://example.org/land/> .
@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#> .

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

land:InterestRules
    a                   sh:NodeShape ;
    sh:targetSubjectsOf land:hasControlledInterestIn ;
    sh:rule [
        a            sh:SPARQLRule ;
        sh:order     0 ;
        sh:prefixes  land:prefixes ;
        sh:construct """
            CONSTRUCT { $this land:hasInterestIn ?title }
            WHERE {
                $this land:hasControlledInterestIn ?b .
                ?p land:proprietor ?b ; land:title ?title .
                FILTER NOT EXISTS { ?p land:until ?ended }
            }
        """ ;
    ] ;
    sh:rule [
        a            sh:SPARQLRule ;
        sh:order     1 ;
        sh:prefixes  land:prefixes ;
        sh:construct """
            CONSTRUCT { $this land:hasInterestIn ?title }
            WHERE { $this land:hasControlledInterestIn ?b . ?b land:hasInterestIn ?title . }
        """ ;
    ] .

land:InterestProbe
    a                   sh:NodeShape ;
    sh:targetSubjectsOf land:hasInterestIn ;
    sh:sparql [
        sh:prefixes  land:prefixes ;
        sh:severity  sh:Info ;
        sh:message   "inferred: {?who} has an interest in title {?number}" ;
        sh:select    """
            SELECT $this ?value ?who ?number WHERE {
                $this land:hasInterestIn ?value ; rdfs:label ?who .
                ?value land:titleNumber ?number .
            }
        """ ;
    ] .
