Debian Codegraph @

reasoning/delfour-insight-economy/scanner.n3

raw
1 # =====================================================================
2 # Delfour — SCANNER side
3 # Inputs:
4 # - an ins:Insight (neutral envelope)
5 # - the ODRL policy (derived from the insight)
6 # - a request to use the insight with purpose=shopping_assist
7 # - "now" as xsd:dateTime (for expiry guard)
8 # - a small product catalog + a scan event (scanned product)
9 # Outputs (pass-only-new):
10 # - authorization marking (Allowed / Blocked with reason)
11 # - derived expiry flag (ex:Expired) when past expiresAt
12 # - an audit trail (act:Decision, optionally a delete_due Duty)
13 # - a runtime suggestion banner (shop:suggestedAlternative)
14 # =====================================================================
15
16 @prefix ins: <https://example.org/insight#> .
17 @prefix odrl: <http://www.w3.org/ns/odrl/2/> .
18 @prefix ex: <https://example.org/enforce#> .
19 @prefix act: <https://example.org/activity#> .
20 @prefix shop: <https://example.org/shop#> .
21 @prefix schema:<http://schema.org/> .
22 @prefix math: <http://www.w3.org/2000/10/swap/math#> .
23 @prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
24 @prefix vocab: <https://example.org/vocab/> .
25
26 # ---------------------------------------------------------------------
27 # 1) Expiry guard: mark an Insight as ex:Expired if expiresAt < now
28 # (Provide "now" using the helper triple shown below in SAMPLE INPUT.)
29 # ---------------------------------------------------------------------
30 {
31 ?ins a ins:Insight ; ins:expiresAt ?exp .
32 [] ex:now ?now .
33 ?exp math:lessThan ?now .
34 }
35 =>
36 { ?ins a ex:Expired . } .
37
38 # ---------------------------------------------------------------------
39 # 2) Enforcement:
40 # - Allowed if request matches the policy use@shopping_assist on the same target
41 # - Blocked if Allowed target is expired
42 # ---------------------------------------------------------------------
43 # Allowed
44 {
45 ?pol a odrl:Policy ;
46 odrl:permission [
47 odrl:action odrl:use ;
48 odrl:target ?ins ;
49 odrl:constraint [
50 odrl:leftOperand odrl:purpose ;
51 odrl:operator odrl:eq ;
52 odrl:rightOperand vocab:shopping_assist
53 ]
54 ] .
55 ?ins a ins:Insight .
56 ?req odrl:action odrl:use ;
57 odrl:constraint [ odrl:leftOperand odrl:purpose ; odrl:rightOperand vocab:shopping_assist ] .
58 }
59 =>
60 { ?req a ex:Allowed ; ex:target ?ins . } .
61
62 # Block on expiry
63 {
64 ?req a ex:Allowed ; ex:target ?ins .
65 ?ins a ex:Expired .
66 }
67 =>
68 { ?req a ex:Blocked ; ex:reason "expired" . } .
69
70 # ---------------------------------------------------------------------
71 # 3) Audit trail: record a Decision; also record Duty when at/after expiry.
72 # ---------------------------------------------------------------------
73 { ?req a ex:Allowed . [] ex:now ?now . }
74 =>
75 { _:a a act:Decision ; act:at ?now ; act:request ?req ; act:outcome "Allowed" . } .
76
77 { ?req a ex:Blocked ; ex:reason ?r . [] ex:now ?now . }
78 =>
79 { _:a a act:Decision ; act:at ?now ; act:request ?req ; act:outcome "Blocked" ; act:reason ?r . } .
80
81 { ?ins a ex:Expired . [] ex:now ?now . }
82 =>
83 { _:d a act:Duty ; act:type "delete_due" ; act:at ?now ; act:target ?ins . } .
84
85 # ---------------------------------------------------------------------
86 # 4) Shopping suggestion:
87 # If Allowed and scanned product sugar >= threshold, suggest an alternative
88 # with strictly lower sugarContent.
89 # ---------------------------------------------------------------------
90 {
91 ?req a ex:Allowed ; ex:target ?ins .
92 ?ins a ins:Insight ;
93 ins:metric "sugar_g_per_serving" ;
94 ins:threshold ?thr .
95
96 # A scan event points to some product
97 ?scan shop:scannedProduct ?p .
98
99 # Product under scan
100 ?p schema:name ?name ;
101 schema:sugarContent ?sug .
102
103 # Candidate alternative (any other product with lower sugar)
104 ?alt schema:name ?an ;
105 schema:sugarContent ?as .
106 ?as math:lessThan ?sug .
107 ?sug math:notLessThan ?thr .
108 }
109 =>
110 {
111 ?scan shop:note "High sugar" ;
112 shop:suggestedAlternative ?alt .
113 } .
114