# ==========================================================================
#  W17  Seasons and limits as shapes
# ==========================================================================
#
#  FROM      Off the Beaten Track, "Seasons and limits as shapes"
#            https://www.semantechs.co.uk/articles/off-the-beaten-track/
#
#  CHECKS
#    Names say which language they are in, and a place in the Gaeltacht has a
#    name in Irish; a short-term let quotes a number that is on the register,
#    for no more guests than registered; Skellig takes no more than 180
#    visitors a day and 12 to a boat, from permitted boats only.
#
#  HOW IT WORKS
#    vt:NameShape pairs a Core sh:languageIn with a SPARQL check that follows
#    a place's gist:hasPhysicalLocation and then gist:isGeoContainedIn* up to
#    the Gaeltacht: gist relates a physical thing to its location with the first,
#    and one location to another with the second. vt:LetShape looks the quoted
#    number up among the register's identifiers and compares the advertised
#    guests with the registered ones. The landing shape adds up the day's
#    passengers with GROUP BY and HAVING, and checks each boat against the
#    limit and the permit list.
#
#  DIAGRAM
#    9 published names with no language tag                     -> 9 violations
#    189 visitors would land on 2027-06-15; the limit is 180    -> violation
#    trip-b carries 13; the limit is 12 to a boat               -> violation
#    boat-p has no landing permit                               -> violation
#    EX-STL-000199 is not on the register                       -> violation
#    advertised for 6 guests; registered for 4                  -> violation
#
#  WHAT TO TAKE AWAY
#    - Nine of the fourteen results come from real data: Fáilte Ireland's
#      names carry no language tag. A shape finds a habit in a whole dataset
#      as easily as a fault in one record.
#    - An aggregate (the day's total) is as checkable as a single value.
#
#  TRY IT
#    In the data tab, advertise the third let for 4 guests instead of 6 and
#    validate again: thirteen violations.
#
#  DATA      dingle-visit.ttl
#  LOAD IT   https://semantechs.co.uk/turtle-editor-viewer/?dot=https%3A%2F%2Fwww.semantechs.co.uk%2Farticles%2Fshow-your-working%2Fdemos%2F07-off-the-beaten-track%2Fdingle-visit.ttl&shapes=https%3A%2F%2Fwww.semantechs.co.uk%2Farticles%2Fshow-your-working%2Fdemos%2F07-off-the-beaten-track%2Fw17-seasons-and-limits.ttl
#  SPEC      SHACL 4.3.1 sh:languageIn; 5 SPARQL-based Constraints
#            https://www.w3.org/TR/shacl/#LanguageInConstraintComponent
#  REPORTS   does not conform -- 14 violations, 0 warnings, 0 info; 5 shapes
# ==========================================================================

@prefix vt:     <https://example.org/visit/> .
@prefix ex:     <https://example.org/place/> .
@prefix gist:   <https://w3id.org/semanticarts/ns/ontology/gist/> .
@prefix schema: <https://schema.org/> .
@prefix sh:     <http://www.w3.org/ns/shacl#> .
@prefix xsd:    <http://www.w3.org/2001/XMLSchema#> .

vt:NameShape a sh:NodeShape ;
    sh:targetClass vt:Landmark, schema:TouristAttraction ;
    sh:property [
        sh:path schema:name ; sh:minCount 1 ;
        sh:languageIn ( "en" "ga" ) ;
        sh:message "Every name should say which language it is in"
    ] ;
    sh:sparql [
        a sh:SPARQLConstraint ;
        sh:prefixes vt:prefixes ;
        sh:message "This place is in the Gaeltacht but has no name in Irish" ;
        sh:select """
            SELECT $this WHERE {
                $this gist:hasPhysicalLocation/gist:isGeoContainedIn* ex:gaeltacht .
                FILTER NOT EXISTS { $this schema:name ?n . FILTER (LANG(?n) = "ga") }
            }
        """ ;
    ] .

vt:LetShape a sh:NodeShape ;
    sh:targetClass vt:ShortTermLet ;
    sh:property [ sh:path vt:registrationNumber ; sh:minCount 1 ; sh:maxCount 1 ;
                  sh:message "A short-term let must show its registration number" ] ;
    sh:sparql [
        a sh:SPARQLConstraint ;
        sh:prefixes vt:prefixes ;
        sh:message "Registration number {?number} is not on the register" ;
        sh:select """
            SELECT $this ?number WHERE {
                $this vt:registrationNumber ?number .
                FILTER NOT EXISTS { ?entry a vt:RegisterEntry ; gist:isIdentifiedBy/gist:uniqueText ?number }
            }
        """ ;
    ] ;
    sh:sparql [
        a sh:SPARQLConstraint ;
        sh:prefixes vt:prefixes ;
        sh:message "Advertised for {?advertised} guests; registered for {?registered}" ;
        sh:select """
            SELECT $this ?advertised ?registered WHERE {
                $this vt:registrationNumber ?number ; vt:advertisedGuests ?advertised .
                ?entry a vt:RegisterEntry ; gist:isIdentifiedBy/gist:uniqueText ?number ; vt:guests ?registered .
                FILTER (?advertised > ?registered)
            }
        """ ;
    ] .

vt:LandingShape a sh:NodeShape ;
    sh:targetClass vt:LandingSite ;
    sh:sparql [
        a sh:SPARQLConstraint ;
        sh:prefixes vt:prefixes ;
        sh:message "{?total} visitors would land on {?day}; the limit is {?max}" ;
        sh:select """
            SELECT $this ?day (SUM(?n) AS ?total) (SAMPLE(?limit) AS ?max) WHERE {
                $this vt:maxPerDay ?limit .
                ?trip vt:lands $this ; vt:passengers ?n ; gist:plannedStartDateTime ?start .
                BIND (SUBSTR(STR(?start), 1, 10) AS ?day)
            }
            GROUP BY $this ?day
            HAVING (SUM(?n) > SAMPLE(?limit))
        """ ;
    ] ;
    sh:sparql [
        a sh:SPARQLConstraint ;
        sh:prefixes vt:prefixes ;
        sh:message "{?trip} carries {?n} visitors; the limit is {?limit} to a boat" ;
        sh:select """
            SELECT $this ?trip ?n ?limit WHERE {
                $this vt:maxPerBoat ?limit .
                ?trip vt:lands $this ; vt:passengers ?n .
                FILTER (?n > ?limit)
            }
        """ ;
    ] ;
    sh:sparql [
        a sh:SPARQLConstraint ;
        sh:prefixes vt:prefixes ;
        sh:message "{?boat} has no landing permit for this site" ;
        sh:select """
            SELECT $this ?boat WHERE {
                ?trip vt:lands $this ; vt:boat ?boat .
                FILTER NOT EXISTS { ?permit a gist:Permission ; vt:forSite $this ; vt:permitsBoat ?boat }
            }
        """ ;
    ] .

vt:prefixes
    sh:declare [ sh:prefix "vt" ; sh:namespace "https://example.org/visit/"^^xsd:anyURI ] ;
    sh:declare [ sh:prefix "ex" ; sh:namespace "https://example.org/place/"^^xsd:anyURI ] ;
    sh:declare [ sh:prefix "gist" ; sh:namespace "https://w3id.org/semanticarts/ns/ontology/gist/"^^xsd:anyURI ] ;
    sh:declare [ sh:prefix "schema" ; sh:namespace "https://schema.org/"^^xsd:anyURI ] .
