|
| 1 | +import { Parser, Writer } from 'n3'; |
| 2 | +import { materializePolicy, ODRLEngineMultipleSteps, ODRLEvaluator, resourceToOptimisedTurtle } from "../dist/index"; |
| 3 | + |
| 4 | + |
| 5 | +// Variant on test case 036: Read request from Alice to resource X returns into yes (temporal lt) (Alice Request Read X). |
| 6 | +// https://github.com/SolidLabResearch/ODRL-Test-Suite/blob/main/data/test_cases/testcase-036-alice-read-x.ttl |
| 7 | +const policy = ` |
| 8 | +@prefix odrl: <http://www.w3.org/ns/odrl/2/> . |
| 9 | +@prefix ex: <http://example.org/> . |
| 10 | +@prefix dct: <http://purl.org/dc/terms/> . |
| 11 | +@prefix odrluc: <https://w3id.org/force/odrlproposed#> . |
| 12 | +
|
| 13 | +<urn:uuid:32127a3f-5296-4cc6-b9d6-ef6c647a721d> a odrl:Set ; |
| 14 | + odrl:uid <urn:uuid:32127a3f-5296-4cc6-b9d6-ef6c647a721d> ; |
| 15 | + dct:description "ALICE may READ resource X when it is before 'ex:updateValue' (see sotw)." ; |
| 16 | + dct:source <https://github.com/SolidLabResearch/ODRL-Test-Suite/> ; |
| 17 | + odrl:permission <urn:uuid:d6ab4a38-68fb-418e-8af5-e77649a2187a> . |
| 18 | +
|
| 19 | +<urn:uuid:d6ab4a38-68fb-418e-8af5-e77649a2187a> a odrl:Permission ; |
| 20 | + odrl:assignee ex:alice ; |
| 21 | + odrl:action odrl:read ; |
| 22 | + odrl:target ex:x ; |
| 23 | + odrl:constraint <urn:uuid:constraint:86526f9b-57c2-4c94-b079-9762fec562f1> . |
| 24 | +
|
| 25 | +<urn:uuid:constraint:86526f9b-57c2-4c94-b079-9762fec562f1> odrl:leftOperand odrl:dateTime ; |
| 26 | + odrl:operator odrl:lt ; |
| 27 | + odrl:rightOperandReference ex:operandReference1 . |
| 28 | +
|
| 29 | +ex:operandReference1 a odrluc:OperandReference ; |
| 30 | + odrluc:reference ex:externalSource ; |
| 31 | + odrluc:path ex:updatedValue . |
| 32 | +` |
| 33 | + |
| 34 | +// state of the world -> with external value indicating the time |
| 35 | +const sotw = ` |
| 36 | +@prefix ex: <http://example.org/> . |
| 37 | +@prefix temp: <http://example.com/request/> . |
| 38 | +@prefix dct: <http://purl.org/dc/terms/> . |
| 39 | +
|
| 40 | +<urn:uuid:192620fa-06d9-447b-adbd-bd1ece4f9b12> a ex:Sotw ; |
| 41 | + ex:includes temp:currentTime . |
| 42 | +
|
| 43 | +temp:currentTime dct:issued "2017-02-12T11:20:10.999Z"^^<http://www.w3.org/2001/XMLSchema#dateTime> . |
| 44 | +
|
| 45 | +# external value that will be materialized in the policy |
| 46 | +ex:externalSource ex:updatedValue "2018-02-12T11:20:10.999Z"^^<http://www.w3.org/2001/XMLSchema#dateTime> . |
| 47 | +` |
| 48 | + |
| 49 | +const request = ` |
| 50 | +@prefix odrl: <http://www.w3.org/ns/odrl/2/> . |
| 51 | +@prefix ex: <http://example.org/> . |
| 52 | +@prefix dct: <http://purl.org/dc/terms/> . |
| 53 | +
|
| 54 | +<urn:uuid:1bafee59-006c-46a3-810c-5d176b4be364> a odrl:Request ; |
| 55 | + odrl:uid <urn:uuid:1bafee59-006c-46a3-810c-5d176b4be364> ; |
| 56 | + dct:description "Requesting Party ALICE requests to READ resource X." ; |
| 57 | + odrl:permission <urn:uuid:186be541-5857-4ce3-9f03-1a274f16bf59> . |
| 58 | +
|
| 59 | +<urn:uuid:186be541-5857-4ce3-9f03-1a274f16bf59> a odrl:Permission ; |
| 60 | + odrl:assignee ex:alice ; |
| 61 | + odrl:action odrl:read ; |
| 62 | + odrl:target ex:x . |
| 63 | +` |
| 64 | + |
| 65 | +async function main() { |
| 66 | + const parser = new Parser() |
| 67 | + const writer = new Writer() |
| 68 | + |
| 69 | + const odrlDynamicPolicyQuads = parser.parse(policy) |
| 70 | + |
| 71 | + const odrlRequestQuads = parser.parse(request) |
| 72 | + const stateOfTheWorldQuads = parser.parse(sotw) |
| 73 | + |
| 74 | + const instantiatedPolicyQuads = materializePolicy(odrlDynamicPolicyQuads, stateOfTheWorldQuads) |
| 75 | + |
| 76 | + console.log("Instantiated Policy:") |
| 77 | + console.log(writer.quadsToString(instantiatedPolicyQuads)); |
| 78 | + |
| 79 | + |
| 80 | + // reasoning over dynamic policy |
| 81 | + const evaluator = new ODRLEvaluator(new ODRLEngineMultipleSteps()); |
| 82 | + const reasoningResult = await evaluator.evaluate( |
| 83 | + odrlDynamicPolicyQuads, |
| 84 | + odrlRequestQuads, |
| 85 | + stateOfTheWorldQuads) |
| 86 | + |
| 87 | + const output = writer.quadsToString(reasoningResult); |
| 88 | + console.log("Compliance Report") |
| 89 | + console.log(output); |
| 90 | + |
| 91 | + const prefixes = { |
| 92 | + 'odrl': 'http://www.w3.org/ns/odrl/2/', |
| 93 | + 'ex': 'http://example.org/', |
| 94 | + 'temp': 'http://example.com/request/', |
| 95 | + 'dct': 'http://purl.org/dc/terms/', |
| 96 | + 'xsd': 'http://www.w3.org/2001/XMLSchema#', |
| 97 | + 'foaf': 'http://xmlns.com/foaf/0.1/', |
| 98 | + 'report': 'https://w3id.org/force/compliance-report#' |
| 99 | + } |
| 100 | + |
| 101 | + // created report with N3 |
| 102 | + // @ts-ignore |
| 103 | + console.log(resourceToOptimisedTurtle(reasoningResult, prefixes)); |
| 104 | + |
| 105 | +} |
| 106 | +main() |
| 107 | + |
0 commit comments