# C4 coherence shapes: the explorer's coherence rules as SHACL-SPARQL constraints.
# Written by skills/architecture-explorer/scripts/check-coherence.mjs --write-shapes from the rules in the explorer template (its coherence block):
# do not edit by hand. Run with any SHACL engine that supports SPARQL constraints, e.g. pyshacl -s c4-coherence-shapes.ttl architecture.ttl
@prefix sh: <http://www.w3.org/ns/shacl#> .
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
@prefix owl: <http://www.w3.org/2002/07/owl#> .
@prefix c4c: <https://caviri.github.io/c4-model-agentic-plugin/ontology/c4-coherence#> .
@prefix c4: <https://caviri.github.io/c4-model-agentic-plugin/ontology/c4#> .
@prefix c4v: <https://caviri.github.io/c4-model-agentic-plugin/ontology/c4-view#> .
@prefix c4d: <https://caviri.github.io/c4-model-agentic-plugin/ontology/c4-data#> .
@prefix dcterms: <http://purl.org/dc/terms/> .
@prefix schema: <https://schema.org/> .
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .

<https://caviri.github.io/c4-model-agentic-plugin/ontology/c4-coherence> a owl:Ontology ;
    rdfs:label "C4 coherence shapes"@en ;
    rdfs:comment "Coherence rules for C4 architecture models (flows, data, infrastructure, lifecycles) as SHACL-SPARQL constraints. The same rules the explorer, check-coherence.mjs and the MCP server run with rete-graph."@en ;
    sh:declare [ sh:prefix "c4" ; sh:namespace "https://caviri.github.io/c4-model-agentic-plugin/ontology/c4#"^^xsd:anyURI ] ,
        [ sh:prefix "c4v" ; sh:namespace "https://caviri.github.io/c4-model-agentic-plugin/ontology/c4-view#"^^xsd:anyURI ] ,
        [ sh:prefix "c4d" ; sh:namespace "https://caviri.github.io/c4-model-agentic-plugin/ontology/c4-data#"^^xsd:anyURI ] ,
        [ sh:prefix "dcterms" ; sh:namespace "http://purl.org/dc/terms/"^^xsd:anyURI ] ,
        [ sh:prefix "schema" ; sh:namespace "https://schema.org/"^^xsd:anyURI ] ,
        [ sh:prefix "rdfs" ; sh:namespace "http://www.w3.org/2000/01/rdf-schema#"^^xsd:anyURI ] ,
        [ sh:prefix "rdf" ; sh:namespace "http://www.w3.org/1999/02/22-rdf-syntax-ns#"^^xsd:anyURI ] .

c4c:flow-step-without-arrow a sh:NodeShape ;
    rdfs:label "A step that no arrow carries"@en ;
    rdfs:comment "The map and the flows disagree: a reader who follows the arrows cannot see this interaction, and the flow on the map can only draw it as a dotted line. Fix: Add the relationship between the two boxes, at the most specific level that is true, or correct the step's ends."@en ;
    sh:targetClass c4:Step ;
    sh:severity sh:Warning ;
    sh:sparql [
        a sh:SPARQLConstraint ;
        sh:prefixes <https://caviri.github.io/c4-model-agentic-plugin/ontology/c4-coherence> ;
        sh:message "Step {?n} of {?scName}: {?aName} → {?bName}, but no arrow joins these boxes (nor the boxes around or inside them)." ;
        sh:select """SELECT DISTINCT $this ?a ?b ?sc ?n ?scName ?aName ?bName WHERE {
$this a c4:Step ; c4:source ?a ; c4:target ?b ; c4:inScenario ?sc ; schema:position ?n . FILTER(?a != ?b) FILTER NOT EXISTS { ?a dcterms:isPartOf+ ?b } FILTER NOT EXISTS { ?b dcterms:isPartOf+ ?a } FILTER NOT EXISTS { ?a dcterms:isPartOf* ?k . ?k a c4:Container . ?b dcterms:isPartOf* ?k }
FILTER NOT EXISTS { ?r a c4:Relationship ; (c4:source|c4:target) ?rx ; (c4:source|c4:target) ?ry . FILTER(?rx != ?ry) FILTER NOT EXISTS { ?r a c4:Transition } { ?a dcterms:isPartOf* ?rx } UNION { ?rx dcterms:isPartOf* ?a } { ?b dcterms:isPartOf* ?ry } UNION { ?ry dcterms:isPartOf* ?b } }
OPTIONAL { ?sc rdfs:label ?scName }
OPTIONAL { ?a rdfs:label ?aName }
OPTIONAL { ?b rdfs:label ?bName }
}""" ;
    ] .

c4c:flow-call-without-arrow a sh:NodeShape ;
    rdfs:label "A call inside a container that no arrow shows"@en ;
    rdfs:comment "Calls inside one container are often left out of the arrows on purpose; opened at that level, the map then misses them. Fix: Add the relationship if the dependency matters at that level, or dismiss this finding (for instance: code-level calls are not drawn)."@en ;
    sh:targetClass c4:Step ;
    sh:severity sh:Info ;
    sh:sparql [
        a sh:SPARQLConstraint ;
        sh:prefixes <https://caviri.github.io/c4-model-agentic-plugin/ontology/c4-coherence> ;
        sh:message "Step {?n} of {?scName}: {?aName} → {?bName}, inside {?kName}, with no arrow between them." ;
        sh:select """SELECT DISTINCT $this ?a ?b ?sc ?k ?n ?scName ?aName ?bName ?kName WHERE {
$this a c4:Step ; c4:source ?a ; c4:target ?b ; c4:inScenario ?sc ; schema:position ?n . FILTER(?a != ?b) FILTER NOT EXISTS { ?a dcterms:isPartOf+ ?b } FILTER NOT EXISTS { ?b dcterms:isPartOf+ ?a } ?a dcterms:isPartOf* ?k . ?k a c4:Container . ?b dcterms:isPartOf* ?k .
FILTER NOT EXISTS { ?r a c4:Relationship ; (c4:source|c4:target) ?rx ; (c4:source|c4:target) ?ry . FILTER(?rx != ?ry) FILTER NOT EXISTS { ?r a c4:Transition } { ?a dcterms:isPartOf* ?rx } UNION { ?rx dcterms:isPartOf* ?a } { ?b dcterms:isPartOf* ?ry } UNION { ?ry dcterms:isPartOf* ?b } }
OPTIONAL { ?sc rdfs:label ?scName }
OPTIONAL { ?a rdfs:label ?aName }
OPTIONAL { ?b rdfs:label ?bName }
OPTIONAL { ?k rdfs:label ?kName }
}""" ;
    ] .

c4c:flow-step-against-arrow a sh:NodeShape ;
    rdfs:label "A step against its arrow"@en ;
    rdfs:comment "An arrow points from the box that starts the interaction. A step the other way is a reply (then mark it) or the arrow is reversed. Fix: Mark the step as a reply (reply: true), reverse the arrow, or add the arrow in the step's direction."@en ;
    sh:targetClass c4:Step ;
    sh:severity sh:Warning ;
    sh:sparql [
        a sh:SPARQLConstraint ;
        sh:prefixes <https://caviri.github.io/c4-model-agentic-plugin/ontology/c4-coherence> ;
        sh:message "Step {?n} of {?scName}: {?aName} → {?bName} runs against the only arrow between them ({?bName} → {?aName}), and is not marked as a reply." ;
        sh:select """SELECT DISTINCT $this ?a ?b ?sc ?n ?scName ?aName ?bName WHERE {
$this a c4:Step ; c4:source ?a ; c4:target ?b ; c4:inScenario ?sc ; schema:position ?n . FILTER(?a != ?b) FILTER NOT EXISTS { ?a dcterms:isPartOf+ ?b } FILTER NOT EXISTS { ?b dcterms:isPartOf+ ?a } FILTER NOT EXISTS { $this c4:isReply true } FILTER NOT EXISTS { ?a a c4:DataStore } FILTER EXISTS { ?q a c4:Relationship ; c4:source ?qs ; c4:target ?qt . FILTER NOT EXISTS { ?q a c4:Transition } { ?b dcterms:isPartOf* ?qs } UNION { ?qs dcterms:isPartOf* ?b } { ?a dcterms:isPartOf* ?qt } UNION { ?qt dcterms:isPartOf* ?a } }
FILTER NOT EXISTS { ?r a c4:Relationship ; c4:source ?rs ; c4:target ?rt . FILTER NOT EXISTS { ?r a c4:Transition } { ?a dcterms:isPartOf* ?rs } UNION { ?rs dcterms:isPartOf* ?a } { ?b dcterms:isPartOf* ?rt } UNION { ?rt dcterms:isPartOf* ?b } }
OPTIONAL { ?sc rdfs:label ?scName }
OPTIONAL { ?a rdfs:label ?aName }
OPTIONAL { ?b rdfs:label ?bName }
}""" ;
    ] .

c4c:flow-broken-chain a sh:NodeShape ;
    rdfs:label "A step that nothing earlier leads to"@en ;
    rdfs:comment "Each step should follow from the ones before it: someone or something hands control to the sender. A sender out of nowhere often means a missing step, or the wrong sender. Fix: Add the step that triggers it, correct the sender, or name the scenario's actor."@en ;
    sh:targetClass c4:Step ;
    sh:severity sh:Warning ;
    sh:sparql [
        a sh:SPARQLConstraint ;
        sh:prefixes <https://caviri.github.io/c4-model-agentic-plugin/ontology/c4-coherence> ;
        sh:message "Step {?n} of {?scName}: {?aName} acts, but nothing earlier in the flow reaches it: it is not the actor, and no earlier step involves it, its container or a box it is connected to." ;
        sh:select """SELECT DISTINCT $this ?a ?sc ?n ?scName ?aName WHERE {
$this a c4:Step ; c4:inScenario ?sc ; schema:position ?n ; c4:source ?a . FILTER(?n > 1) FILTER NOT EXISTS { ?sc c4:primaryActor ?a } FILTER NOT EXISTS { ?prev c4:source ?a ; c4:inScenario ?sc ; schema:position ?m . $this schema:position ?n0 . FILTER(?m < ?n0) } FILTER NOT EXISTS { ?prev c4:target ?a ; c4:inScenario ?sc ; schema:position ?m . $this schema:position ?n0 . FILTER(?m < ?n0) } FILTER NOT EXISTS { ?a dcterms:isPartOf* ?k . ?k a c4:Container . ?p dcterms:isPartOf* ?k . ?prev (c4:source|c4:target) ?p ; c4:inScenario ?sc ; schema:position ?m . $this schema:position ?n0 . FILTER(?m < ?n0) }
FILTER NOT EXISTS { ?prev c4:inScenario ?sc ; schema:position ?m ; (c4:source|c4:target) ?p . $this schema:position ?n0 . FILTER(?m < ?n0) ?r a c4:Relationship ; (c4:source|c4:target) ?rx ; (c4:source|c4:target) ?ry . FILTER(?rx != ?ry) FILTER NOT EXISTS { ?r a c4:Transition } { ?a dcterms:isPartOf* ?rx } UNION { ?rx dcterms:isPartOf* ?a } { ?p dcterms:isPartOf* ?ry } UNION { ?ry dcterms:isPartOf* ?p } }
OPTIONAL { ?sc rdfs:label ?scName }
OPTIONAL { ?a rdfs:label ?aName }
}""" ;
    ] .

c4c:flow-actor-absent a sh:NodeShape ;
    rdfs:label "An actor who takes no step"@en ;
    rdfs:comment "The actor of a use case starts it or takes part in it; a scenario that never involves its actor tells a different story than its use case. Fix: Add the actor's first step (what they do to start it), or correct the actor."@en ;
    sh:targetClass c4:Scenario ;
    sh:severity sh:Warning ;
    sh:sparql [
        a sh:SPARQLConstraint ;
        sh:prefixes <https://caviri.github.io/c4-model-agentic-plugin/ontology/c4-coherence> ;
        sh:message "{?thisName} names {?pName} as its actor, but {?pName} takes no step in it." ;
        sh:select """SELECT DISTINCT $this ?p ?thisName ?pName WHERE {
$this a c4:Scenario ; c4:primaryActor ?p . FILTER NOT EXISTS { ?st c4:inScenario $this ; c4:source ?p } FILTER NOT EXISTS { ?st c4:inScenario $this ; c4:target ?p }
OPTIONAL { $this rdfs:label ?thisName }
OPTIONAL { ?p rdfs:label ?pName }
}""" ;
    ] .

c4c:flow-data-out-of-reach a sh:NodeShape ;
    rdfs:label "Data a step cannot reach"@en ;
    rdfs:comment "Data in a store is reached through a relationship. A step that touches it from boxes with no path to that store puts the access in the wrong place, or an arrow is missing. Fix: Move the read or write to the step that reaches the store (usually the one that calls it), or add the relationship to the store."@en ;
    sh:targetClass c4:Step ;
    sh:severity sh:Warning ;
    sh:sparql [
        a sh:SPARQLConstraint ;
        sh:prefixes <https://caviri.github.io/c4-model-agentic-plugin/ontology/c4-coherence> ;
        sh:message "Step {?n} of {?scName} reads or writes {?dName} in {?sName}, but neither {?aName} nor {?bName} (nor their containers) is connected to {?sName}." ;
        sh:select """SELECT DISTINCT $this ?a ?b ?sc ?d ?s ?n ?scName ?dName ?sName ?aName ?bName WHERE {
$this a c4:Step ; c4:source ?a ; c4:target ?b ; c4:inScenario ?sc ; schema:position ?n . $this (c4d:reads|c4d:writes) ?d . ?d (c4d:inSchema|c4d:inEntity/c4d:inSchema) ?sch . ?sch c4d:livesIn ?s . ?s a c4:DataStore . FILTER NOT EXISTS { ?a dcterms:isPartOf* ?s } FILTER NOT EXISTS { ?b dcterms:isPartOf* ?s }
FILTER NOT EXISTS { $this (c4:source|c4:target) ?e . ?e dcterms:isPartOf* ?k . ?k a c4:Container . ?r a c4:Relationship ; (c4:source|c4:target) ?rx ; (c4:source|c4:target) ?ry . FILTER(?rx != ?ry) ?rx dcterms:isPartOf* ?k . ?ry dcterms:isPartOf* ?s . }
OPTIONAL { ?sc rdfs:label ?scName }
OPTIONAL { ?d rdfs:label ?dName }
OPTIONAL { ?s rdfs:label ?sName }
OPTIONAL { ?a rdfs:label ?aName }
OPTIONAL { ?b rdfs:label ?bName }
}""" ;
    ] .

c4c:data-read-never-written a sh:NodeShape ;
    rdfs:label "Data that is read but never written"@en ;
    rdfs:comment "Data comes from somewhere. When no flow writes what flows read, either it is written outside the modelled flows, or a step is missing. Fix: Add the step that writes it, or dismiss this finding with where it comes from (migrations, an import, another system)."@en ;
    sh:targetClass c4d:Entity ;
    sh:severity sh:Info ;
    sh:sparql [
        a sh:SPARQLConstraint ;
        sh:prefixes <https://caviri.github.io/c4-model-agentic-plugin/ontology/c4-coherence> ;
        sh:message "{?thisName} in {?sName} is read in a scenario, but no scenario writes it." ;
        sh:select """SELECT DISTINCT $this ?s ?thisName ?sName WHERE {
$this a c4d:Entity ; c4d:inSchema/c4d:livesIn ?s . ?s a c4:DataStore . ?st c4d:reads ?d . ?d (c4d:inEntity?) $this . FILTER NOT EXISTS { ?st2 c4d:writes ?w . ?w (c4d:inEntity?) $this }
OPTIONAL { $this rdfs:label ?thisName }
OPTIONAL { ?s rdfs:label ?sName }
}""" ;
    ] .

c4c:data-written-never-read a sh:NodeShape ;
    rdfs:label "Data that is written but never read"@en ;
    rdfs:comment "Data is kept to be used. When no flow reads what flows write, either it is read outside the modelled flows, or it is dead weight. Fix: Add the step that reads it, or dismiss this finding with who reads it (a download, another system, an audit)."@en ;
    sh:targetClass c4d:Entity ;
    sh:severity sh:Info ;
    sh:sparql [
        a sh:SPARQLConstraint ;
        sh:prefixes <https://caviri.github.io/c4-model-agentic-plugin/ontology/c4-coherence> ;
        sh:message "{?thisName} in {?sName} is written in a scenario, but no scenario reads it." ;
        sh:select """SELECT DISTINCT $this ?s ?thisName ?sName WHERE {
$this a c4d:Entity ; c4d:inSchema/c4d:livesIn ?s . ?s a c4:DataStore . ?st c4d:writes ?d . ?d (c4d:inEntity?) $this . FILTER NOT EXISTS { ?st2 c4d:reads ?w . ?w (c4d:inEntity?) $this }
OPTIONAL { $this rdfs:label ?thisName }
OPTIONAL { ?s rdfs:label ?sName }
}""" ;
    ] .

c4c:data-store-untouched a sh:NodeShape ;
    rdfs:label "A store no flow uses"@en ;
    rdfs:comment "The flows are where a reader learns what a store is for; a store none of them uses is either missing from them or no longer used. Fix: Add the steps that use it, or dismiss this finding if it is used outside the modelled flows."@en ;
    sh:targetClass c4:DataStore ;
    sh:severity sh:Info ;
    sh:sparql [
        a sh:SPARQLConstraint ;
        sh:prefixes <https://caviri.github.io/c4-model-agentic-plugin/ontology/c4-coherence> ;
        sh:message "{?thisName} holds data (a schema lives in it), but no scenario reaches it, reads or writes its data." ;
        sh:select """SELECT DISTINCT $this ?thisName WHERE {
$this a c4:DataStore . FILTER EXISTS { ?sch c4d:livesIn $this } FILTER NOT EXISTS { ?st a c4:Step ; (c4:source|c4:target) ?x . ?x dcterms:isPartOf* $this } FILTER NOT EXISTS { ?st2 (c4d:reads|c4d:writes) ?d . ?d (c4d:inSchema|c4d:inEntity/c4d:inSchema) ?sc2 . ?sc2 c4d:livesIn $this }
OPTIONAL { $this rdfs:label ?thisName }
}""" ;
    ] .

c4c:infra-queue-one-sided a sh:NodeShape ;
    rdfs:label "A queue with one side"@en ;
    rdfs:comment "A queue joins a producer and a consumer. With only one of them in the model, its messages lead nowhere, or come from nowhere. Fix: Add the relationship of the missing side (producers and consumers both point at the queue), or change its tag and technology if it is not a queue."@en ;
    sh:targetClass c4:DataStore ;
    sh:severity sh:Warning ;
    sh:sparql [
        a sh:SPARQLConstraint ;
        sh:prefixes <https://caviri.github.io/c4-model-agentic-plugin/ontology/c4-coherence> ;
        sh:message "{?thisName} looks like a queue, but fewer than two parties connect to it: messages go in and nobody takes them out, or the other way round." ;
        sh:select """SELECT DISTINCT $this ?thisName WHERE {
$this a c4:DataStore . FILTER EXISTS { { $this c4:tag ?t } UNION { $this c4:technology ?t } FILTER(REGEX(?t, "queue|kafka|rabbit|amqp|\\\\bsqs\\\\b|pub.?sub|\\\\bnats\\\\b|\\\\brq\\\\b|celery|bullmq|kinesis|event.?hub|service.?bus|mqtt|zeromq|\\\\bmq\\\\b", "i")) }
FILTER NOT EXISTS { ?r1 a c4:Relationship ; (c4:source|c4:target) ?x1 ; (c4:source|c4:target) ?y1 . FILTER(?x1 != ?y1) ?x1 dcterms:isPartOf* $this . FILTER NOT EXISTS { ?y1 dcterms:isPartOf* $this } ?y1 dcterms:isPartOf* ?k1 . ?k1 a ?kt . FILTER(?kt IN (c4:Container, c4:Person, c4:ExternalSystem, c4:SiblingSystem)) ?r2 a c4:Relationship ; (c4:source|c4:target) ?x2 ; (c4:source|c4:target) ?y2 . FILTER(?x2 != ?y2) ?x2 dcterms:isPartOf* $this . FILTER NOT EXISTS { ?y2 dcterms:isPartOf* $this } ?y2 dcterms:isPartOf* ?k2 . ?k2 a ?kt2 . FILTER(?kt2 IN (c4:Container, c4:Person, c4:ExternalSystem, c4:SiblingSystem)) FILTER(?k1 != ?k2) }
OPTIONAL { $this rdfs:label ?thisName }
}""" ;
    ] .

c4c:infra-sync-cycle a sh:NodeShape ;
    rdfs:label "Two containers that call each other"@en ;
    rdfs:comment "Two containers that wait on each other are tightly coupled: a slow or failing one takes the other with it, and neither can be deployed alone. Fix: Make one direction asynchronous (a queue or an event: a dashed arrow), merge the two, or move the part they share into a third container."@en ;
    sh:targetClass c4:Container ;
    sh:severity sh:Warning ;
    sh:sparql [
        a sh:SPARQLConstraint ;
        sh:prefixes <https://caviri.github.io/c4-model-agentic-plugin/ontology/c4-coherence> ;
        sh:message "{?thisName} and {?otherName} call each other synchronously (solid arrows both ways)." ;
        sh:select """SELECT DISTINCT $this ?other ?thisName ?otherName WHERE {
?r a c4:Relationship ; c4:source ?rs ; c4:target ?rt . FILTER NOT EXISTS { ?r c4v:dashed true } FILTER NOT EXISTS { ?r a c4:Transition } ?rs dcterms:isPartOf* $this . $this a c4:Container . FILTER NOT EXISTS { $this a c4:DataStore } ?rt dcterms:isPartOf* ?other . ?other a c4:Container . FILTER NOT EXISTS { ?other a c4:DataStore } FILTER($this != ?other && STR($this) < STR(?other)) FILTER EXISTS { ?q a c4:Relationship ; c4:source ?qs ; c4:target ?qt . FILTER NOT EXISTS { ?q c4v:dashed true } FILTER NOT EXISTS { ?q a c4:Transition } ?qs dcterms:isPartOf* ?other . ?qt dcterms:isPartOf* $this }
OPTIONAL { $this rdfs:label ?thisName }
OPTIONAL { ?other rdfs:label ?otherName }
}""" ;
    ] .

c4c:infra-frontend-to-database a sh:NodeShape ;
    rdfs:label "A front end that talks straight to a database"@en ;
    rdfs:comment "A browser or a mobile app that reaches a database (or a queue) holds its credentials and depends on its schema; such access usually goes through an API. Fix: Route the access through the API container. Object storage reached with signed URLs is fine: tag or name the store as such (S3, object storage) and the rule leaves it alone."@en ;
    sh:targetClass c4:Relationship ;
    sh:severity sh:Warning ;
    sh:sparql [
        a sh:SPARQLConstraint ;
        sh:prefixes <https://caviri.github.io/c4-model-agentic-plugin/ontology/c4-coherence> ;
        sh:message "{?feName}, a front end, talks straight to {?dbName}: {?thisName}." ;
        sh:select """SELECT DISTINCT $this ?fe ?db ?feName ?dbName ?thisName WHERE {
$this a c4:Relationship ; c4:source ?s ; c4:target ?t . FILTER NOT EXISTS { $this a c4:Transition } ?s dcterms:isPartOf* ?fe . ?fe a c4:Container . FILTER NOT EXISTS { ?fe a c4:DataStore } FILTER EXISTS { { ?fe c4:tag ?ft } UNION { ?fe c4:technology ?ft } FILTER(REGEX(?ft, "react|\\\\bvue\\\\b|angular|svelte|\\\\bspa\\\\b|browser|front.?end|single.page|mobile|\\\\bios\\\\b|android|flutter|next\\\\.js|swiftui|kotlin multiplatform", "i")) } ?t dcterms:isPartOf* ?db . ?db a c4:DataStore . FILTER NOT EXISTS { { ?db c4:tag ?dt } UNION { ?db c4:technology ?dt } UNION { ?db rdfs:label ?dt } FILTER(REGEX(?dt, "s3|minio|object|bucket|blob|cdn|\\\\bgcs\\\\b|static", "i")) }
OPTIONAL { ?fe rdfs:label ?feName }
OPTIONAL { ?db rdfs:label ?dbName }
OPTIONAL { $this rdfs:label ?thisName }
}""" ;
    ] .

c4c:state-no-initial a sh:NodeShape ;
    rdfs:label "A lifecycle without an initial state"@en ;
    rdfs:comment "Without a state to start in, nothing says where a new item begins, and which states can be reached cannot be told. Fix: Mark the state a new item starts in with initial: true."@en ;
    sh:targetClass c4:Element ;
    sh:severity sh:Violation ;
    sh:sparql [
        a sh:SPARQLConstraint ;
        sh:prefixes <https://caviri.github.io/c4-model-agentic-plugin/ontology/c4-coherence> ;
        sh:message "The lifecycle in {?thisName} has no initial state." ;
        sh:select """SELECT DISTINCT $this ?thisName WHERE {
$this a c4:Element . FILTER EXISTS { ?s a c4:State ; dcterms:isPartOf $this } FILTER NOT EXISTS { ?i a c4:State ; dcterms:isPartOf $this ; c4:initial true }
OPTIONAL { $this rdfs:label ?thisName }
}""" ;
    ] .

c4c:state-unreachable a sh:NodeShape ;
    rdfs:label "A state no transition reaches"@en ;
    rdfs:comment "A state no transition leads to is never entered: either a transition is missing, or the state is left over. Fix: Add the transition that leads to it, or remove the state."@en ;
    sh:targetClass c4:State ;
    sh:severity sh:Warning ;
    sh:sparql [
        a sh:SPARQLConstraint ;
        sh:prefixes <https://caviri.github.io/c4-model-agentic-plugin/ontology/c4-coherence> ;
        sh:message "{?thisName} cannot be reached from the initial state of {?oName}." ;
        sh:select """SELECT DISTINCT $this ?o ?thisName ?oName WHERE {
$this a c4:State ; dcterms:isPartOf ?o . FILTER EXISTS { ?i a c4:State ; dcterms:isPartOf ?o ; c4:initial true } FILTER NOT EXISTS { ?i0 a c4:State ; dcterms:isPartOf ?o ; c4:initial true . ?i0 c4:transitionsTo* $this }
OPTIONAL { $this rdfs:label ?thisName }
OPTIONAL { ?o rdfs:label ?oName }
}""" ;
    ] .

c4c:state-final-exits a sh:NodeShape ;
    rdfs:label "A final state with a way out"@en ;
    rdfs:comment "Final means an item stays there; a transition out of it contradicts that. Fix: Remove final: true, or the transition."@en ;
    sh:targetClass c4:State ;
    sh:severity sh:Warning ;
    sh:sparql [
        a sh:SPARQLConstraint ;
        sh:prefixes <https://caviri.github.io/c4-model-agentic-plugin/ontology/c4-coherence> ;
        sh:message "{?thisName} is final, but has a transition to {?xName}." ;
        sh:select """SELECT DISTINCT $this ?x ?thisName ?xName WHERE {
$this a c4:State ; c4:final true ; c4:transitionsTo ?x . FILTER(?x != $this)
OPTIONAL { $this rdfs:label ?thisName }
OPTIONAL { ?x rdfs:label ?xName }
}""" ;
    ] .

c4c:state-dead-end a sh:NodeShape ;
    rdfs:label "A state an item cannot leave"@en ;
    rdfs:comment "A state without a way out is final in fact; if it is not meant to be, a transition is missing. Fix: Add the transition out of it, or mark it final: true."@en ;
    sh:targetClass c4:State ;
    sh:severity sh:Warning ;
    sh:sparql [
        a sh:SPARQLConstraint ;
        sh:prefixes <https://caviri.github.io/c4-model-agentic-plugin/ontology/c4-coherence> ;
        sh:message "{?thisName} is not final, but no transition leaves it: an item there is stuck." ;
        sh:select """SELECT DISTINCT $this ?thisName WHERE {
$this a c4:State . FILTER NOT EXISTS { $this c4:final true } FILTER NOT EXISTS { $this c4:transitionsTo ?x . FILTER(?x != $this) }
OPTIONAL { $this rdfs:label ?thisName }
}""" ;
    ] .
