# baseURI: https://w3id.org/constructx/shapes/contract/core
@prefix cs: <https://w3id.org/constructx/shapes/contract/core#> .
@prefix c: <https://w3id.org/constructx/contract#> .
@prefix part: <https://w3id.org/constructx/participation#> .
@prefix role: <https://www.construct-x.org/vocab/participation-role#> .
@prefix skos: <http://www.w3.org/2004/02/skos/core#> .
@prefix sh: <http://www.w3.org/ns/shacl#> .
@prefix dcterms: <http://purl.org/dc/terms/> .
@prefix owl: <http://www.w3.org/2002/07/owl#> .
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .

<https://w3id.org/constructx/shapes/contract/core> a owl:Ontology ;
    dcterms:title "Construct-X Contract Core Shapes"@en, "Construct-X Prüfregeln für Verträge"@de ;
    dcterms:description "Validates bilateral parties, project references, contract types and direct subcontract chains."@en, "Prüft Parteien, Projektbezüge, Vertragsarten und Untervertragsketten."@de ;
    dcterms:created "2026-11-19"^^xsd:date ;
    dcterms:modified "2026-12-04"^^xsd:date ;
    dcterms:license <https://creativecommons.org/licenses/by/4.0/> ;
    dcterms:publisher <https://www.construct-x.org/id/organization/ecbe8336-94ab-5c11-8220-94f260963eb2/revision/1> ;
    owl:imports <https://w3id.org/constructx/shapes/participation/core/0.3.0> ;
    owl:versionIRI <https://w3id.org/constructx/shapes/contract/core/0.2.0> ;
    owl:versionInfo "0.2.0" .

cs:ContractShape a sh:NodeShape ;
    sh:targetClass c:Contract ;
    sh:targetSubjectsOf c:hasProjectContext, c:hasContractParticipation, c:hasSubcontract, c:isSubcontractOf, c:hasContractType ;
    sh:class c:Contract ;
    sh:message "[CONTRACT-001] Contract links and classifications must originate from a Contract."@en,
               "[CONTRACT-001] Vertragsbeziehungen und Vertragsarten müssen von einem Vertrag ausgehen."@de ;
    sh:property [
        sh:path [ sh:alternativePath ( c:hasSubcontract c:isSubcontractOf ) ] ;
        sh:class c:Contract ;
        sh:message "[CONTRACT-011] A direct subcontract link must identify another Contract."@en,
                   "[CONTRACT-011] Eine Untervertragsbeziehung muss einen anderen Vertrag benennen."@de
    ], [
        sh:path c:hasContractType ;
        sh:nodeKind sh:IRI ;
        sh:class c:ContractType ;
        sh:node [ sh:property [ sh:path skos:inScheme ;
            sh:hasValue <https://www.construct-x.org/vocab/contract-type> ] ] ;
        sh:message "[CONTRACT-013] Use a ContractType IRI from the contract type scheme."@en,
                   "[CONTRACT-013] Die Vertragsart muss eine ContractType-IRI aus dem Vertragsarten-Vokabular sein."@de
    ], [
        sh:path c:hasProjectContext ;
        sh:nodeKind sh:IRI ;
        sh:message "[CONTRACT-002] Identify each referenced project by an IRI."@en,
                   "[CONTRACT-002] Jedes referenzierte Projekt benötigt eine IRI."@de
    ], [
        sh:path c:hasContractParticipation ;
        sh:nodeKind sh:IRI ;
        sh:class c:ContractParticipation ;
        sh:message "[CONTRACT-009] hasContractParticipation requires the IRI of a ContractParticipation."@en,
                   "[CONTRACT-009] hasContractParticipation benötigt die IRI einer Vertragsbeteiligung."@de
    ], [
        sh:path [ sh:inversePath part:hasContext ] ;
        sh:qualifiedValueShape [ sh:class c:ContractParticipation ] ;
        sh:qualifiedMinCount 2 ; sh:qualifiedMaxCount 2 ;
        sh:message "[CONTRACT-003] The contract needs exactly two contract participations."@en,
                   "[CONTRACT-003] Der Vertrag benötigt genau zwei Vertragsbeteiligungen."@de
    ], [
        sh:path [ sh:inversePath part:hasContext ] ;
        sh:qualifiedValueShape [ sh:property [ sh:path part:hasRole ; sh:hasValue role:CommissioningPartyRole ] ] ;
        sh:qualifiedMinCount 1 ; sh:qualifiedMaxCount 1 ;
        sh:message "[CONTRACT-004] Exactly one participation must carry CommissioningPartyRole."@en,
                   "[CONTRACT-004] Genau eine Beteiligung muss die Auftraggeberrolle tragen."@de
    ], [
        sh:path [ sh:inversePath part:hasContext ] ;
        sh:qualifiedValueShape [ sh:property [ sh:path part:hasRole ; sh:hasValue role:ContractorRole ] ] ;
        sh:qualifiedMinCount 1 ; sh:qualifiedMaxCount 1 ;
        sh:message "[CONTRACT-005] Exactly one participation must carry ContractorRole."@en,
                   "[CONTRACT-005] Genau eine Beteiligung muss die Auftragnehmerrolle tragen."@de
    ] ;
    sh:sparql [
        sh:message "[CONTRACT-012] A contract cannot be its own direct or indirect subcontract."@en,
                   "[CONTRACT-012] Ein Vertrag darf weder direkt noch indirekt sein eigener Untervertrag sein."@de ;
        sh:select """
            PREFIX c: <https://w3id.org/constructx/contract#>
            SELECT $this WHERE {
                $this (c:hasSubcontract|^c:isSubcontractOf)+ $this .
            }
        """
    ], [
        sh:message "[CONTRACT-010] The linked contract participation must explicitly identify this contract as its context."@en,
                   "[CONTRACT-010] Die verknüpfte Vertragsbeteiligung muss diesen Vertrag ausdrücklich als Kontext nennen."@de ;
        sh:select """
            PREFIX c: <https://w3id.org/constructx/contract#>
            PREFIX part: <https://w3id.org/constructx/participation#>
            SELECT $this ?value WHERE {
                $this c:hasContractParticipation ?value .
                FILTER NOT EXISTS { ?value part:hasContext $this }
            }
        """
    ], [
        sh:message "[CONTRACT-006] The commissioning party and contractor must reference different agents."@en,
                   "[CONTRACT-006] Auftraggeber und Auftragnehmer müssen auf verschiedene Akteure verweisen."@de ;
        sh:select """
            PREFIX part: <https://w3id.org/constructx/participation#>
            PREFIX role: <https://www.construct-x.org/vocab/participation-role#>
            SELECT DISTINCT $this ?value WHERE {
                ?commission part:hasContext $this ; part:hasRole role:CommissioningPartyRole ; part:hasParticipant ?value .
                ?contractor part:hasContext $this ; part:hasRole role:ContractorRole ; part:hasParticipant ?value .
            }
        """
    ] .

cs:ContractParticipationShape a sh:NodeShape ;
    sh:targetClass c:ContractParticipation ;
    sh:property [
        sh:path part:hasContext ;
        sh:class c:Contract ;
        sh:message "[CONTRACT-007] The context of a contract participation must be a Contract."@en,
                   "[CONTRACT-007] Der Kontext einer Vertragsbeteiligung muss ein Vertrag sein."@de
    ], [
        sh:path part:hasRole ;
        sh:in (role:CommissioningPartyRole role:ContractorRole) ;
        sh:message "[CONTRACT-008] Choose the commissioning party or contractor role for a contract party."@en,
                   "[CONTRACT-008] Eine Vertragspartei benötigt die Auftraggeber- oder Auftragnehmerrolle."@de
    ] .
