# Deriving a research-data permission with evidence
#
# An ODRL request and supporting DPV facts describe a proposed research
# use. N3 rules derive the agreement and its conditions; the proof can be
# inspected by the companion audit example.
#
# See <https://www.w3.org/TR/odrl-model/>
# See <https://www.w3.org/TR/odrl-vocab/>
# See <https://w3id.org/dpv/2.3>

@prefix : <https://example.org/access#> .
@prefix odrl: <http://www.w3.org/ns/odrl/2/> .
@prefix dpv: <https://w3id.org/dpv#> .

# Intake: the request proposes one use, by one researcher, for one purpose.
:request-42 a odrl:Request ;
    odrl:profile :research-profile ;
    odrl:permission :research-use-42 .

:research-use-42 a odrl:Permission ;
    odrl:assigner :hospital ;
    odrl:assignee :dr-okafor ;
    odrl:target :cohort-b ;
    odrl:action odrl:use ;
    odrl:constraint :purpose-42 .

# odrl:purpose is a constraint operand, not a predicate. DPV supplies its value.
:purpose-42 a odrl:Constraint ;
    odrl:leftOperand odrl:purpose ;
    odrl:operator odrl:eq ;
    odrl:rightOperand dpv:ScientificResearch .

# Ethics: this accepted approval covers the same dataset and purpose.
:dr-okafor :holdsApproval :eth-2026-118 .
:eth-2026-118 dpv:hasData :cohort-b ;
    dpv:hasPurpose dpv:ScientificResearch .

# Consent: a simplified, cohort-level assertion, scoped to dataset and purpose.
# The audit separately identifies the document recording these assertions.
:consent-b a dpv:Consent ;
    dpv:hasData :cohort-b ;
    dpv:hasPurpose dpv:ScientificResearch ;
    dpv:hasConsentStatus dpv:ConsentGiven .

# Registry: a recorded safeguard, not an assertion that the data is anonymous.
:cohort-b dpv:hasTechnicalOrganisationalMeasure dpv:Deidentification .

# Check 1: the requesting researcher holds an approval for this data and purpose.
{
    ?request a odrl:Request ;
        odrl:permission ?use .
    ?use odrl:assignee ?who ;
        odrl:target ?data ;
        odrl:constraint ?constraint .
    ?constraint a odrl:Constraint ;
        odrl:leftOperand odrl:purpose ;
        odrl:operator odrl:eq ;
        odrl:rightOperand ?purpose .
    ?who :holdsApproval ?approval .
    ?approval dpv:hasData ?data ;
        dpv:hasPurpose ?purpose .
} => {
    ?use dpv:hasJustification ?approval .
} .

# Check 2: given consent covers the same data and purpose.
{
    ?request a odrl:Request ;
        odrl:permission ?use .
    ?use odrl:target ?data ;
        odrl:constraint ?constraint .
    ?constraint a odrl:Constraint ;
        odrl:leftOperand odrl:purpose ;
        odrl:operator odrl:eq ;
        odrl:rightOperand ?purpose .
    ?consent a dpv:Consent ;
        dpv:hasData ?data ;
        dpv:hasPurpose ?purpose ;
        dpv:hasConsentStatus dpv:ConsentGiven .
} => {
    ?use dpv:hasLegalBasis ?consent .
} .

# Check 3: the requested dataset has the policy's required safeguard.
{
    ?request a odrl:Request ;
        odrl:permission ?use .
    ?use odrl:target ?data .
    ?data dpv:hasTechnicalOrganisationalMeasure dpv:Deidentification .
} => {
    ?use dpv:hasTechnicalOrganisationalMeasure dpv:Deidentification .
} .

# This demonstration handles request 42. Its Agreement grants the proposed
# use only after all three checks; the Request itself remains a proposal.
{
    :request-42 a odrl:Request ;
        odrl:profile :research-profile ;
        odrl:permission ?use .
    ?use a odrl:Permission ;
        odrl:assigner ?holder ;
        odrl:assignee ?researcher ;
        odrl:target ?data ;
        odrl:action odrl:use ;
        odrl:constraint ?constraint ;
        dpv:hasJustification ?approval ;
        dpv:hasLegalBasis ?consent ;
        dpv:hasTechnicalOrganisationalMeasure dpv:Deidentification .
} => {
    :agreement-42 a odrl:Agreement ;
        odrl:profile :research-profile ;
        odrl:assigner ?holder ;
        odrl:assignee ?researcher ;
        odrl:permission ?use .
    :request-42 dpv:hasOutcome :agreement-42 .
} .
