@prefix rc: <https://ontology.inferal.com/profiles/commerce-repeat-cohorts/> .
@prefix pc: <https://ontology.inferal.com/modules/commerce-purchase-classification/> .
@prefix ctx: <https://ontology.inferal.com/modules/commerce-transaction/> .
@prefix prov: <http://www.w3.org/ns/prov#> .
@prefix time: <http://www.w3.org/2006/time#> .
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
@prefix owl: <http://www.w3.org/2002/07/owl#> .
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
@prefix sh: <http://www.w3.org/ns/shacl#> .
rc:SnapshotShape a sh:NodeShape ; sh:targetClass rc:EvidenceSnapshot ; sh:nodeKind sh:IRI ;
 sh:property [ sh:path pc:merchant ; sh:minCount 1 ; sh:maxCount 1 ; sh:nodeKind sh:IRI ] ;
 sh:property [ sh:path pc:policy ; sh:minCount 1 ; sh:maxCount 1 ; sh:nodeKind sh:IRI ] ;
 sh:property [ sh:path pc:asOf ; sh:minCount 1 ; sh:maxCount 1 ; sh:datatype xsd:dateTimeStamp ] ;
 sh:property [ sh:path prov:hadMember ; sh:minCount 1 ; sh:nodeKind sh:IRI ; sh:class pc:PurchaseClassification ; sh:node rc:ClassificationShape ] ;
 sh:property [ sh:path prov:wasDerivedFrom ; sh:minCount 1 ; sh:nodeKind sh:IRI ] ;
 sh:sparql [ sh:message "Selected assessments must share snapshot merchant/policy and use an evidence cutoff no later than the snapshot cutoff." ; sh:select """
 PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
 PREFIX pc: <https://ontology.inferal.com/modules/commerce-purchase-classification/>
 PREFIX ctx: <https://ontology.inferal.com/modules/commerce-transaction/>
 PREFIX time: <http://www.w3.org/2006/time#>
 PREFIX prov: <http://www.w3.org/ns/prov#>
 SELECT $this WHERE { $this pc:merchant ?merchant ; pc:policy ?policy ; pc:asOf ?asOf ; prov:hadMember ?member .
 ?member pc:merchant ?m ; pc:policy ?p ; pc:asOf ?assessedAsOf . FILTER (?merchant != ?m || ?policy != ?p || xsd:dateTime(STR(?assessedAsOf)) > xsd:dateTime(STR(?asOf))) }
 """ ] ;
 sh:sparql [ sh:message "Resolve repeated order assessments and ambiguous source keys before cohort counting." ; sh:select """
 PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
 PREFIX pc: <https://ontology.inferal.com/modules/commerce-purchase-classification/>
 PREFIX prov: <http://www.w3.org/ns/prov#>
 SELECT $this WHERE { $this prov:hadMember ?left, ?right . FILTER (?left != ?right)
 ?left pc:order ?lo ; pc:buyer ?lb . ?right pc:order ?ro ; pc:buyer ?rb .
 ?lo pc:purchaseKey ?lk . ?ro pc:purchaseKey ?rk . FILTER (?lo = ?ro || (?lb = ?rb && ?lk = ?rk)) }
 """ ] .
rc:ClassificationShape a sh:NodeShape ;
 sh:property [ sh:path pc:order ; sh:minCount 1 ; sh:maxCount 1 ; sh:nodeKind sh:IRI ; sh:class ctx:Order ; sh:node rc:OrderShape ] ;
 sh:property [ sh:path pc:buyer ; sh:minCount 1 ; sh:maxCount 1 ; sh:nodeKind sh:IRI ] ;
 sh:property [ sh:path pc:merchant ; sh:minCount 1 ; sh:maxCount 1 ; sh:nodeKind sh:IRI ] ;
 sh:property [ sh:path pc:policy ; sh:minCount 1 ; sh:maxCount 1 ; sh:nodeKind sh:IRI ] ;
 sh:property [ sh:path pc:asOf ; sh:minCount 1 ; sh:maxCount 1 ; sh:datatype xsd:dateTimeStamp ] ;
 sh:property [ sh:path pc:eligibilityStanding ; sh:minCount 1 ; sh:maxCount 1 ; sh:in (pc:Eligible pc:NotEligible) ] ;
 sh:property [ sh:path pc:context ; sh:minCount 1 ; sh:maxCount 1 ; sh:nodeKind sh:IRI ; sh:class pc:ClassificationContext ; sh:node rc:ContextShape ] ;
 sh:property [ sh:path prov:wasDerivedFrom ; sh:minCount 1 ; sh:nodeKind sh:IRI ] ;
 sh:sparql [ sh:message "Stable identity and selected order must belong to this assessment's exact context and evidence cutoff." ; sh:select """
 PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
 PREFIX pc: <https://ontology.inferal.com/modules/commerce-purchase-classification/>
 PREFIX ctx: <https://ontology.inferal.com/modules/commerce-transaction/>
 PREFIX time: <http://www.w3.org/2006/time#>
 SELECT $this WHERE { $this pc:buyer ?buyer ; pc:merchant ?merchant ; pc:policy ?policy ; pc:asOf ?asOf ; pc:order ?order ; pc:context ?context .
 ?context pc:buyer ?cb ; pc:merchant ?cm ; pc:policy ?cp ; pc:asOf ?ca .
 ?order pc:capturedAt ?capture ; ctx:placedAt/time:inXSDDateTimeStamp ?placed .
 FILTER (?buyer != ?cb || ?merchant != ?cm || ?policy != ?cp || xsd:dateTime(STR(?asOf)) != xsd:dateTime(STR(?ca)) || NOT EXISTS { ?context pc:selectedOrder ?order } || xsd:dateTime(STR(?capture)) > xsd:dateTime(STR(?asOf)) || xsd:dateTime(STR(?placed)) > xsd:dateTime(STR(?asOf))) }
 """ ] ;
 sh:sparql [ sh:message "The assessment and retained order must share exactly the supplied buyer and merchant scope." ; sh:select """
 PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
 PREFIX pc: <https://ontology.inferal.com/modules/commerce-purchase-classification/>
 SELECT $this WHERE { $this pc:buyer ?buyer ; pc:merchant ?merchant ; pc:order ?order .
 FILTER (NOT EXISTS { ?order pc:buyer ?buyer ; pc:merchant ?merchant } || EXISTS { ?order pc:buyer ?other . FILTER (?other != ?buyer) } || EXISTS { ?order pc:merchant ?other . FILTER (?other != ?merchant) }) }
 """ ] .
rc:ContextShape a sh:NodeShape ;
 sh:property [ sh:path pc:buyer ; sh:minCount 1 ; sh:maxCount 1 ; sh:nodeKind sh:IRI ] ;
 sh:property [ sh:path pc:merchant ; sh:minCount 1 ; sh:maxCount 1 ; sh:nodeKind sh:IRI ] ;
 sh:property [ sh:path pc:policy ; sh:minCount 1 ; sh:maxCount 1 ; sh:nodeKind sh:IRI ] ;
 sh:property [ sh:path pc:asOf ; sh:minCount 1 ; sh:maxCount 1 ; sh:datatype xsd:dateTimeStamp ] ;
 sh:property [ sh:path pc:identityStanding ; sh:minCount 1 ; sh:maxCount 1 ; sh:hasValue pc:DeclaredStable ] ;
 sh:property [ sh:path pc:selectedOrder ; sh:minCount 1 ; sh:nodeKind sh:IRI ] .
rc:OrderShape a sh:NodeShape ;
 sh:property [ sh:path pc:purchaseKey ; sh:minCount 1 ; sh:maxCount 1 ; sh:datatype xsd:string ; sh:minLength 1 ] ;
 sh:property [ sh:path pc:capturedAt ; sh:minCount 1 ; sh:maxCount 1 ; sh:datatype xsd:dateTimeStamp ] ;
 sh:property [ sh:path ctx:placedAt ; sh:minCount 1 ; sh:maxCount 1 ; sh:node [ sh:property [ sh:path time:inXSDDateTimeStamp ; sh:minCount 1 ; sh:maxCount 1 ; sh:datatype xsd:dateTimeStamp ] ] ] .
rc:EntryShape a sh:NodeShape ; sh:targetClass rc:CohortEntry ; sh:nodeKind sh:IRI ;
 sh:property [ sh:path rc:snapshot ; sh:minCount 1 ; sh:maxCount 1 ; sh:nodeKind sh:IRI ; sh:class rc:EvidenceSnapshot ] ;
 sh:property [ sh:path rc:anchor ; sh:minCount 1 ; sh:maxCount 1 ; sh:nodeKind sh:IRI ; sh:class pc:PurchaseClassification ; sh:node [ sh:property [ sh:path pc:basis ; sh:minCount 1 ; sh:maxCount 1 ; sh:hasValue pc:FirstObserved ] ; sh:property [ sh:path pc:eligibilityStanding ; sh:hasValue pc:Eligible ] ] ] ;
 sh:property [ sh:path rc:windowEnd ; sh:minCount 1 ; sh:maxCount 1 ; sh:datatype xsd:dateTimeStamp ] ;
 sh:property [ sh:path rc:followupHistory ; sh:minCount 1 ; sh:maxCount 1 ; sh:nodeKind sh:IRI ; sh:class pc:HistoryDeclaration ; sh:node rc:FollowupShape ] ;
 sh:property [ sh:path prov:wasDerivedFrom ; sh:minCount 1 ; sh:nodeKind sh:IRI ] ;
 sh:sparql [ sh:message "Anchor must be a selected member; follow-up must have the same buyer/merchant and an ordered, available interval." ; sh:select """
 PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
 PREFIX rc: <https://ontology.inferal.com/profiles/commerce-repeat-cohorts/>
 PREFIX pc: <https://ontology.inferal.com/modules/commerce-purchase-classification/>
 PREFIX ctx: <https://ontology.inferal.com/modules/commerce-transaction/>
 PREFIX time: <http://www.w3.org/2006/time#>
 PREFIX prov: <http://www.w3.org/ns/prov#>
 SELECT $this WHERE { $this rc:snapshot ?snapshot ; rc:anchor ?anchor ; rc:windowEnd ?end ; rc:followupHistory ?history .
 ?snapshot pc:asOf ?asOf . ?anchor pc:buyer ?buyer ; pc:merchant ?merchant ; pc:order/ctx:placedAt/time:inXSDDateTimeStamp ?start .
 ?history pc:buyer ?hb ; pc:merchant ?hm ; pc:historyStart ?from ; pc:historyThrough ?through .
 FILTER (NOT EXISTS { ?snapshot prov:hadMember ?anchor } || ?buyer != ?hb || ?merchant != ?hm || xsd:dateTime(STR(?end)) <= xsd:dateTime(STR(?start)) || xsd:dateTime(STR(?through)) < xsd:dateTime(STR(?from)) || xsd:dateTime(STR(?through)) > xsd:dateTime(STR(?asOf))) }
 """ ] ;
 sh:sparql [ sh:message "An earlier or tied selected eligible purchase contradicts the chosen first-observed anchor." ; sh:select """
 PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
 PREFIX rc: <https://ontology.inferal.com/profiles/commerce-repeat-cohorts/>
 PREFIX pc: <https://ontology.inferal.com/modules/commerce-purchase-classification/>
 PREFIX ctx: <https://ontology.inferal.com/modules/commerce-transaction/>
 PREFIX time: <http://www.w3.org/2006/time#>
 PREFIX prov: <http://www.w3.org/ns/prov#>
 SELECT $this WHERE { $this rc:snapshot ?snapshot ; rc:anchor ?anchor . ?anchor pc:buyer ?buyer ; pc:order/ctx:placedAt/time:inXSDDateTimeStamp ?start .
 ?snapshot prov:hadMember ?other . FILTER (?other != ?anchor) ?other pc:buyer ?buyer ; pc:eligibilityStanding pc:Eligible ; pc:order/ctx:placedAt/time:inXSDDateTimeStamp ?otherTime . FILTER (xsd:dateTime(STR(?otherTime)) <= xsd:dateTime(STR(?start))) }
 """ ] ;
 sh:sparql [ sh:message "A cohort cannot count the same snapshot and buyer twice." ; sh:select """
 PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
 PREFIX rc: <https://ontology.inferal.com/profiles/commerce-repeat-cohorts/>
 PREFIX pc: <https://ontology.inferal.com/modules/commerce-purchase-classification/>
 SELECT $this WHERE { $this rc:snapshot ?snapshot ; rc:anchor/pc:buyer ?buyer . ?other a rc:CohortEntry ; rc:snapshot ?snapshot ; rc:anchor/pc:buyer ?buyer . FILTER ($this != ?other) }
 """ ] .
rc:FollowupShape a sh:NodeShape ;
 sh:property [ sh:path pc:buyer ; sh:minCount 1 ; sh:maxCount 1 ; sh:nodeKind sh:IRI ] ;
 sh:property [ sh:path pc:merchant ; sh:minCount 1 ; sh:maxCount 1 ; sh:nodeKind sh:IRI ] ;
 sh:property [ sh:path pc:historyCoverage ; sh:minCount 1 ; sh:maxCount 1 ; sh:in (pc:CompleteDeclared pc:Partial) ] ;
 sh:property [ sh:path pc:historyStart ; sh:minCount 1 ; sh:maxCount 1 ; sh:datatype xsd:dateTimeStamp ] ;
 sh:property [ sh:path pc:historyThrough ; sh:minCount 1 ; sh:maxCount 1 ; sh:datatype xsd:dateTimeStamp ] ;
 sh:property [ sh:path prov:wasDerivedFrom ; sh:minCount 1 ; sh:nodeKind sh:IRI ] .
