# SHACL shapes for user stories graphs in the terms of the C4 user stories ontology (c4-stories.ttl).
#
# A stories graph is checked on its own; its links into the architecture graph (c4us:actor, c4us:portrays,
# c4us:scenario, c4us:coversStep, c4us:seenAtStep, c4us:architecture) are only required to be IRIs here. That they land
# on the right resources is checked by the writer, which builds the architecture graph from the explorer's model.js:
#   node skills/user-stories/scripts/stories-rdf.mjs communications/user-stories/index.html --check
# Validated together with the architecture graph, c4-shapes.ttl still holds: nothing here is a schema:ImageObject.
#
# Only SHACL Core, and no logical constraints (sh:or, sh:and, sh:not, sh:xone): some engines, rete-graph 0.7.4 among
# them, skip those silently.
#
# Sections: collection and personas · stories · criteria and code · screens and views · notes and pins.

@prefix c4uss:   <https://caviri.github.io/c4-model-agentic-plugin/ontology/c4-stories-shapes.ttl#> .
@prefix c4us:    <https://caviri.github.io/c4-model-agentic-plugin/ontology/c4-stories#> .
@prefix c4:      <https://caviri.github.io/c4-model-agentic-plugin/ontology/c4#> .
@prefix sh:      <http://www.w3.org/ns/shacl#> .
@prefix owl:     <http://www.w3.org/2002/07/owl#> .
@prefix rdf:     <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix rdfs:    <http://www.w3.org/2000/01/rdf-schema#> .
@prefix xsd:     <http://www.w3.org/2001/XMLSchema#> .
@prefix dcterms: <http://purl.org/dc/terms/> .
@prefix schema:  <https://schema.org/> .

<https://caviri.github.io/c4-model-agentic-plugin/ontology/c4-stories-shapes.ttl>
    a owl:Ontology ;
    dcterms:title "SHACL shapes for the C4 user stories ontology"@en ;
    dcterms:description "Constraints that every user stories graph meets: the collection and its personas, stories with their journey, acceptance criteria and code references, screens and their views, and readers' notes with their pins."@en ;
    dcterms:creator "caviri" ;
    dcterms:license <https://www.apache.org/licenses/LICENSE-2.0> ;
    owl:versionInfo "0.1.0" ;
    rdfs:seeAlso <https://caviri.github.io/c4-model-agentic-plugin/ontology/c4-stories> .

#################################################################
#  Collection and personas
#################################################################

c4uss:StoryCollectionShape a sh:NodeShape ;
    sh:targetClass c4us:StoryCollection ;
    sh:property [ sh:path rdfs:label ; sh:minCount 1 ; sh:maxCount 1 ; sh:datatype xsd:string ; sh:minLength 1 ] ;
    sh:property [ sh:path dcterms:identifier ; sh:minCount 1 ; sh:maxCount 1 ; sh:datatype xsd:string ;
                  sh:message "A story collection has exactly one identifier: the page's key, under which readers' browsers keep their notes." ] ;
    sh:property [ sh:path dcterms:description ; sh:maxCount 1 ; sh:datatype xsd:string ] ;
    sh:property [ sh:path dcterms:conformsTo ; sh:hasValue <https://caviri.github.io/c4-model-agentic-plugin/ontology/c4-stories> ] ;
    sh:property [ sh:path c4us:withFeedback ; sh:minCount 1 ; sh:maxCount 1 ; sh:datatype xsd:boolean ;
                  sh:message "A story collection says whether the graph holds readers' notes (c4us:withFeedback)." ] ;
    sh:property [ sh:path c4us:architecture ; sh:maxCount 1 ; sh:nodeKind sh:IRI ] ;
    sh:property [ sh:path c4us:persona ; sh:class c4us:Persona ] ;
    sh:property [ sh:path dcterms:hasPart ; sh:minCount 1 ; sh:class c4us:UserStory ; sh:message "A story collection has at least one story, and its parts are stories." ] ;
    sh:property [ sh:path c4us:hasNote ; sh:class c4us:Note ] .

# notes only in a graph that says it holds them
c4uss:NotesNeedFeedbackShape a sh:NodeShape ;
    sh:targetSubjectsOf c4us:hasNote ;
    sh:property [ sh:path c4us:withFeedback ; sh:hasValue true ;
                  sh:message "A graph with readers' notes says so: c4us:withFeedback true." ] .

c4uss:PersonaShape a sh:NodeShape ;
    sh:targetClass c4us:Persona ;
    sh:property [ sh:path rdfs:label ; sh:minCount 1 ; sh:maxCount 1 ; sh:datatype xsd:string ; sh:minLength 1 ] ;
    sh:property [ sh:path dcterms:identifier ; sh:minCount 1 ; sh:maxCount 1 ; sh:datatype xsd:string ] ;
    sh:property [ sh:path dcterms:description ; sh:maxCount 1 ; sh:datatype xsd:string ] ;
    sh:property [ sh:path c4us:personaGoal ; sh:datatype xsd:string ; sh:minLength 1 ] ;
    sh:property [ sh:path c4us:portrays ; sh:maxCount 1 ; sh:nodeKind sh:IRI ] ;
    sh:property [ sh:path schema:position ; sh:maxCount 1 ; sh:datatype xsd:integer ; sh:minInclusive 1 ] .

#################################################################
#  Stories
#################################################################

c4uss:UserStoryShape a sh:NodeShape ;
    sh:targetClass c4us:UserStory ;
    sh:property [ sh:path rdfs:label ; sh:minCount 1 ; sh:maxCount 1 ; sh:datatype xsd:string ; sh:minLength 1 ;
                  sh:message "A story has exactly one title (rdfs:label)." ] ;
    sh:property [ sh:path dcterms:identifier ; sh:minCount 1 ; sh:maxCount 1 ; sh:datatype xsd:string ; sh:pattern "^[a-z0-9][a-z0-9-]*$" ;
                  sh:message "A story has exactly one identifier, lowercase-kebab: readers' notes point at it." ] ;
    sh:property [ sh:path dcterms:isPartOf ; sh:minCount 1 ; sh:maxCount 1 ; sh:class c4us:StoryCollection ] ;
    sh:property [ sh:path schema:position ; sh:minCount 1 ; sh:maxCount 1 ; sh:datatype xsd:integer ; sh:minInclusive 1 ] ;
    sh:property [ sh:path c4us:actor ; sh:minCount 1 ; sh:maxCount 1 ; sh:nodeKind sh:IRI ;
                  sh:message "A story is told for exactly one actor (As a …): a person of the architecture or a persona." ] ;
    sh:property [ sh:path c4us:persona ; sh:maxCount 1 ; sh:class c4us:Persona ] ;
    sh:property [ sh:path c4us:want ; sh:minCount 1 ; sh:maxCount 1 ; sh:datatype xsd:string ; sh:minLength 1 ;
                  sh:message "A story says what its actor wants (I want …: exactly one c4us:want)." ] ;
    sh:property [ sh:path c4us:soThat ; sh:maxCount 1 ; sh:datatype xsd:string ; sh:minLength 1 ] ;
    sh:property [ sh:path c4us:status ; sh:maxCount 1 ; sh:in ( "implemented" "partial" "planned" ) ] ;
    sh:property [ sh:path c4us:note ; sh:maxCount 1 ; sh:datatype xsd:string ] ;
    sh:property [ sh:path schema:keywords ; sh:datatype xsd:string ; sh:minLength 1 ] ;
    sh:property [ sh:path c4us:goal ; sh:maxCount 1 ; sh:datatype xsd:string ] ;
    sh:property [ sh:path c4us:trigger ; sh:maxCount 1 ; sh:datatype xsd:string ] ;
    sh:property [ sh:path c4us:precondition ; sh:datatype xsd:string ] ;
    sh:property [ sh:path c4us:outcome ; sh:maxCount 1 ; sh:datatype xsd:string ] ;
    sh:property [ sh:path c4us:scenario ; sh:maxCount 1 ; sh:nodeKind sh:IRI ] ;
    sh:property [ sh:path c4us:firstStep ; sh:maxCount 1 ; sh:nodeKind sh:IRI ] ;
    sh:property [ sh:path c4us:lastStep ; sh:maxCount 1 ; sh:nodeKind sh:IRI ] ;
    sh:property [ sh:path c4us:coversStep ; sh:nodeKind sh:IRI ] ;
    sh:property [ sh:path c4us:acceptanceCriterion ; sh:class c4us:AcceptanceCriterion ] ;
    sh:property [ sh:path c4us:evidence ; sh:class c4us:CodeReference ] ;
    sh:property [ sh:path c4us:screen ; sh:class c4us:Screen ] .

# a journey is a range of one scenario's steps: its ends need the scenario, and both ends are steps it covers
c4uss:JourneyShape a sh:NodeShape ;
    sh:targetSubjectsOf c4us:coversStep ;
    sh:property [ sh:path c4us:scenario ; sh:minCount 1 ; sh:message "A story that covers steps names their scenario (c4us:scenario)." ] ;
    sh:property [ sh:path c4us:firstStep ; sh:minCount 1 ; sh:message "A story with a journey has a first step (c4us:firstStep)." ] ;
    sh:property [ sh:path c4us:lastStep ; sh:minCount 1 ; sh:message "A story with a journey has a last step (c4us:lastStep)." ] .

#################################################################
#  Criteria and code
#################################################################

c4uss:AcceptanceCriterionShape a sh:NodeShape ;
    sh:targetClass c4us:AcceptanceCriterion ;
    sh:property [ sh:path c4us:statement ; sh:minCount 1 ; sh:maxCount 1 ; sh:datatype xsd:string ; sh:minLength 1 ;
                  sh:message "An acceptance criterion has exactly one text (c4us:statement)." ] ;
    sh:property [ sh:path schema:position ; sh:minCount 1 ; sh:maxCount 1 ; sh:datatype xsd:integer ; sh:minInclusive 1 ] ;
    sh:property [ sh:path c4us:given ; sh:maxCount 1 ; sh:datatype xsd:string ] ;
    sh:property [ sh:path c4us:when ; sh:maxCount 1 ; sh:datatype xsd:string ] ;
    sh:property [ sh:path c4us:then ; sh:maxCount 1 ; sh:datatype xsd:string ] ;
    sh:property [ sh:path [ sh:inversePath c4us:acceptanceCriterion ] ; sh:minCount 1 ; sh:maxCount 1 ;
                  sh:message "An acceptance criterion belongs to exactly one story." ] .

c4uss:CodeReferenceShape a sh:NodeShape ;
    sh:targetClass c4us:CodeReference ;
    sh:property [ sh:path c4:path ; sh:minCount 1 ; sh:maxCount 1 ; sh:datatype xsd:string ; sh:minLength 1 ;
                  sh:message "A code reference names exactly one file (c4:path, relative to the repository root)." ] ;
    sh:property [ sh:path c4:line ; sh:maxCount 1 ; sh:datatype xsd:integer ; sh:minInclusive 1 ] ;
    sh:property [ sh:path rdfs:label ; sh:maxCount 1 ; sh:datatype xsd:string ] ;
    sh:property [ sh:path rdfs:seeAlso ; sh:maxCount 1 ; sh:nodeKind sh:IRI ] ;
    sh:property [ sh:path schema:position ; sh:minCount 1 ; sh:maxCount 1 ; sh:datatype xsd:integer ; sh:minInclusive 1 ] ;
    sh:property [ sh:path [ sh:inversePath c4us:evidence ] ; sh:minCount 1 ; sh:maxCount 1 ;
                  sh:message "A code reference belongs to exactly one story." ] .

#################################################################
#  Screens and views
#################################################################

c4uss:ScreenShape a sh:NodeShape ;
    sh:targetClass c4us:Screen ;
    sh:property [ sh:path dcterms:identifier ; sh:minCount 1 ; sh:maxCount 1 ; sh:datatype xsd:string ; sh:pattern "^[A-Za-z0-9_-]{1,40}$" ;
                  sh:message "A screen has exactly one identifier (letters, digits, - or _): readers' notes point at it." ] ;
    sh:property [ sh:path rdfs:label ; sh:maxCount 1 ; sh:datatype xsd:string ] ;
    sh:property [ sh:path schema:position ; sh:maxCount 1 ; sh:datatype xsd:integer ; sh:minInclusive 1 ] ;
    sh:property [ sh:path c4us:seenAtStep ; sh:maxCount 1 ; sh:nodeKind sh:IRI ] ;
    sh:property [ sh:path c4us:screenKind ; sh:minCount 1 ; sh:maxCount 1 ; sh:in ( "screenshot" "wireframe" "mockup" "diagram" "illustration" "photo" ) ] ;
    sh:property [ sh:path c4us:illustrative ; sh:maxCount 1 ; sh:datatype xsd:boolean ] ;
    sh:property [ sh:path c4us:hasView ; sh:minCount 1 ; sh:class c4us:ScreenView ; sh:message "A screen has at least one view." ] ;
    sh:property [ sh:path c4us:defaultView ; sh:minCount 1 ; sh:maxCount 1 ; sh:class c4us:ScreenView ;
                  sh:message "A screen has exactly one default view (the one the page shows first)." ] ;
    sh:property [ sh:path [ sh:inversePath c4us:screen ] ; sh:minCount 1 ; sh:maxCount 1 ;
                  sh:message "A screen belongs to exactly one story." ] .

c4uss:ScreenViewShape a sh:NodeShape ;
    sh:targetClass c4us:ScreenView ;
    sh:property [ sh:path c4us:viewName ; sh:minCount 1 ; sh:maxCount 1 ; sh:datatype xsd:string ; sh:pattern "^[A-Za-z0-9][\\w-]{0,23}$" ;
                  sh:message "A view has exactly one short name (desktop, tablet, phone, or a name of the project's own)." ] ;
    sh:property [ sh:path schema:position ; sh:minCount 1 ; sh:maxCount 1 ; sh:datatype xsd:integer ; sh:minInclusive 1 ] ;
    sh:property [ sh:path schema:contentUrl ; sh:maxCount 1 ; sh:nodeKind sh:IRIOrLiteral ] ;
    sh:property [ sh:path schema:caption ; sh:maxCount 1 ; sh:datatype xsd:string ; sh:minLength 1 ] ;
    sh:property [ sh:path c4us:altText ; sh:maxCount 1 ; sh:datatype xsd:string ; sh:minLength 1 ] ;
    sh:property [ sh:path c4us:pixelWidth ; sh:maxCount 1 ; sh:datatype xsd:integer ; sh:minInclusive 1 ] ;
    sh:property [ sh:path c4us:pixelHeight ; sh:maxCount 1 ; sh:datatype xsd:integer ; sh:minInclusive 1 ] ;
    sh:property [ sh:path [ sh:inversePath c4us:hasView ] ; sh:minCount 1 ; sh:maxCount 1 ;
                  sh:message "A view belongs to exactly one screen." ] .

#################################################################
#  Notes and pins
#################################################################

c4uss:NoteShape a sh:NodeShape ;
    sh:targetClass c4us:Note ;
    sh:property [ sh:path dcterms:identifier ; sh:minCount 1 ; sh:maxCount 1 ; sh:datatype xsd:string ] ;
    sh:property [ sh:path dcterms:isPartOf ; sh:minCount 1 ; sh:maxCount 1 ; sh:class c4us:StoryCollection ] ;
    sh:property [ sh:path c4us:aboutStory ; sh:minCount 1 ; sh:maxCount 1 ; sh:class c4us:UserStory ;
                  sh:message "A note is about exactly one story of the collection." ] ;
    sh:property [ sh:path c4us:onScreen ; sh:maxCount 1 ; sh:class c4us:Screen ] ;
    sh:property [ sh:path c4us:onView ; sh:maxCount 1 ; sh:class c4us:ScreenView ] ;
    sh:property [ sh:path dcterms:creator ; sh:maxCount 1 ; sh:datatype xsd:string ] ;
    sh:property [ sh:path dcterms:created ; sh:minCount 1 ; sh:maxCount 1 ; sh:datatype xsd:dateTime ] ;
    sh:property [ sh:path dcterms:modified ; sh:minCount 1 ; sh:maxCount 1 ; sh:datatype xsd:dateTime ] ;
    sh:property [ sh:path c4us:noteStatus ; sh:minCount 1 ; sh:maxCount 1 ; sh:in ( "open" "resolved" ) ] ;
    sh:property [ sh:path c4us:comment ; sh:maxCount 1 ; sh:datatype xsd:string ] ;
    sh:property [ sh:path c4us:markCount ; sh:maxCount 1 ; sh:datatype xsd:integer ; sh:minInclusive 0 ] ;
    sh:property [ sh:path c4us:pin ; sh:class c4us:Pin ] ;
    sh:property [ sh:path c4us:hasRecording ; sh:maxCount 1 ; sh:in ( true ) ] ;
    sh:property [ sh:path c4us:recordingDuration ; sh:maxCount 1 ; sh:datatype xsd:decimal ; sh:minInclusive 0 ] ;
    sh:property [ sh:path c4us:recordingFile ; sh:maxCount 1 ; sh:datatype xsd:string ; sh:pattern "^audio/" ;
                  sh:message "A voice note is named by its file (audio/ and the note's id); the audio itself is never in the graph." ] ;
    sh:property [ sh:path c4us:transcript ; sh:maxCount 1 ; sh:datatype xsd:string ] .

c4uss:PinShape a sh:NodeShape ;
    sh:targetClass c4us:Pin ;
    sh:property [ sh:path schema:position ; sh:minCount 1 ; sh:maxCount 1 ; sh:datatype xsd:integer ; sh:minInclusive 1 ] ;
    sh:property [ sh:path c4us:x ; sh:minCount 1 ; sh:maxCount 1 ; sh:datatype xsd:decimal ; sh:minInclusive 0 ; sh:maxInclusive 1 ] ;
    sh:property [ sh:path c4us:y ; sh:minCount 1 ; sh:maxCount 1 ; sh:datatype xsd:decimal ; sh:minInclusive 0 ; sh:maxInclusive 1 ] ;
    sh:property [ sh:path c4us:pinText ; sh:maxCount 1 ; sh:datatype xsd:string ] ;
    sh:property [ sh:path [ sh:inversePath c4us:pin ] ; sh:minCount 1 ; sh:maxCount 1 ; sh:message "A pin belongs to exactly one note." ] .
