Mercurial > code > home > repos > homeauto
annotate service/reasoning/escapeoutputstatements.py @ 1732:3f4b447d65f5
port to starlette/asyncio
author | drewp@bigasterisk.com |
---|---|
date | Mon, 10 Jul 2023 17:37:58 -0700 |
parents | 95f72a22965d |
children |
rev | line source |
---|---|
284
95f72a22965d
rules become simple-looking again; fix the ambiguity in memory after loading them.
drewp@bigasterisk.com
parents:
diff
changeset
|
1 """ |
95f72a22965d
rules become simple-looking again; fix the ambiguity in memory after loading them.
drewp@bigasterisk.com
parents:
diff
changeset
|
2 Why? |
95f72a22965d
rules become simple-looking again; fix the ambiguity in memory after loading them.
drewp@bigasterisk.com
parents:
diff
changeset
|
3 |
95f72a22965d
rules become simple-looking again; fix the ambiguity in memory after loading them.
drewp@bigasterisk.com
parents:
diff
changeset
|
4 Consider these rules: |
95f72a22965d
rules become simple-looking again; fix the ambiguity in memory after loading them.
drewp@bigasterisk.com
parents:
diff
changeset
|
5 { :button1 :state :press . :lights :brightness 0 } => { :lights :brightness 1 } |
95f72a22965d
rules become simple-looking again; fix the ambiguity in memory after loading them.
drewp@bigasterisk.com
parents:
diff
changeset
|
6 { :button1 :state :press . :lights :brightness 1 } => { :lights :brightness 0 } |
95f72a22965d
rules become simple-looking again; fix the ambiguity in memory after loading them.
drewp@bigasterisk.com
parents:
diff
changeset
|
7 { :room1 :sees :motion } => { :house :sees :motion } |
95f72a22965d
rules become simple-looking again; fix the ambiguity in memory after loading them.
drewp@bigasterisk.com
parents:
diff
changeset
|
8 { :room2 :sees :motion } => { :house :sees :motion } |
95f72a22965d
rules become simple-looking again; fix the ambiguity in memory after loading them.
drewp@bigasterisk.com
parents:
diff
changeset
|
9 { :house :sees :motion } => { :outsideLights :brightness 1 } |
95f72a22965d
rules become simple-looking again; fix the ambiguity in memory after loading them.
drewp@bigasterisk.com
parents:
diff
changeset
|
10 |
95f72a22965d
rules become simple-looking again; fix the ambiguity in memory after loading them.
drewp@bigasterisk.com
parents:
diff
changeset
|
11 Suppose we ran with these inputs: |
95f72a22965d
rules become simple-looking again; fix the ambiguity in memory after loading them.
drewp@bigasterisk.com
parents:
diff
changeset
|
12 :lights :brightness 0 . |
95f72a22965d
rules become simple-looking again; fix the ambiguity in memory after loading them.
drewp@bigasterisk.com
parents:
diff
changeset
|
13 :button1 :state :press . |
95f72a22965d
rules become simple-looking again; fix the ambiguity in memory after loading them.
drewp@bigasterisk.com
parents:
diff
changeset
|
14 Those will trigger the first *two* rules, since we run rules forward |
95f72a22965d
rules become simple-looking again; fix the ambiguity in memory after loading them.
drewp@bigasterisk.com
parents:
diff
changeset
|
15 until no more statements are produced. |
95f72a22965d
rules become simple-looking again; fix the ambiguity in memory after loading them.
drewp@bigasterisk.com
parents:
diff
changeset
|
16 |
95f72a22965d
rules become simple-looking again; fix the ambiguity in memory after loading them.
drewp@bigasterisk.com
parents:
diff
changeset
|
17 The problem here is that (:lights :brightness ?x) is both an input |
95f72a22965d
rules become simple-looking again; fix the ambiguity in memory after loading them.
drewp@bigasterisk.com
parents:
diff
changeset
|
18 statement and an output statement, but it's the kind of output that is |
95f72a22965d
rules become simple-looking again; fix the ambiguity in memory after loading them.
drewp@bigasterisk.com
parents:
diff
changeset
|
19 not meant to cascade into more rules. A more precise way to read the |
95f72a22965d
rules become simple-looking again; fix the ambiguity in memory after loading them.
drewp@bigasterisk.com
parents:
diff
changeset
|
20 first rule is "if button1 is pressed and lights WERE at brightness 0, |
95f72a22965d
rules become simple-looking again; fix the ambiguity in memory after loading them.
drewp@bigasterisk.com
parents:
diff
changeset
|
21 then the lights SHOULD BE at brightness 1". |
95f72a22965d
rules become simple-looking again; fix the ambiguity in memory after loading them.
drewp@bigasterisk.com
parents:
diff
changeset
|
22 |
95f72a22965d
rules become simple-looking again; fix the ambiguity in memory after loading them.
drewp@bigasterisk.com
parents:
diff
changeset
|
23 Can we just stop running the rules when we get the first :brightness |
95f72a22965d
rules become simple-looking again; fix the ambiguity in memory after loading them.
drewp@bigasterisk.com
parents:
diff
changeset
|
24 output and not run the second rule? Not in general. Consider the third |
95f72a22965d
rules become simple-looking again; fix the ambiguity in memory after loading them.
drewp@bigasterisk.com
parents:
diff
changeset
|
25 rule, which generates (:house :sees :motion). That output triple is |
95f72a22965d
rules become simple-looking again; fix the ambiguity in memory after loading them.
drewp@bigasterisk.com
parents:
diff
changeset
|
26 meant as an input to the last rule. There's no clear difference |
95f72a22965d
rules become simple-looking again; fix the ambiguity in memory after loading them.
drewp@bigasterisk.com
parents:
diff
changeset
|
27 between (:lights :brightness 1) and (:house :sees :motion) in this |
95f72a22965d
rules become simple-looking again; fix the ambiguity in memory after loading them.
drewp@bigasterisk.com
parents:
diff
changeset
|
28 graph. Only with external knowledge do I know that (:lights |
95f72a22965d
rules become simple-looking again; fix the ambiguity in memory after loading them.
drewp@bigasterisk.com
parents:
diff
changeset
|
29 :brightness 1) shouldn't cascade. |
95f72a22965d
rules become simple-looking again; fix the ambiguity in memory after loading them.
drewp@bigasterisk.com
parents:
diff
changeset
|
30 |
95f72a22965d
rules become simple-looking again; fix the ambiguity in memory after loading them.
drewp@bigasterisk.com
parents:
diff
changeset
|
31 Possible fixes: |
95f72a22965d
rules become simple-looking again; fix the ambiguity in memory after loading them.
drewp@bigasterisk.com
parents:
diff
changeset
|
32 |
95f72a22965d
rules become simple-looking again; fix the ambiguity in memory after loading them.
drewp@bigasterisk.com
parents:
diff
changeset
|
33 1. Make the :brightness predicate more clear, like |
95f72a22965d
rules become simple-looking again; fix the ambiguity in memory after loading them.
drewp@bigasterisk.com
parents:
diff
changeset
|
34 (:lights :was_brightness 0) or (:lights :new_brightness 1). Dealing |
95f72a22965d
rules become simple-looking again; fix the ambiguity in memory after loading them.
drewp@bigasterisk.com
parents:
diff
changeset
|
35 with multiple predicates for different "tenses" seems like a pain. |
95f72a22965d
rules become simple-looking again; fix the ambiguity in memory after loading them.
drewp@bigasterisk.com
parents:
diff
changeset
|
36 |
95f72a22965d
rules become simple-looking again; fix the ambiguity in memory after loading them.
drewp@bigasterisk.com
parents:
diff
changeset
|
37 2. Put input statements in a subgraph and match them specifically in there: |
95f72a22965d
rules become simple-looking again; fix the ambiguity in memory after loading them.
drewp@bigasterisk.com
parents:
diff
changeset
|
38 { |
95f72a22965d
rules become simple-looking again; fix the ambiguity in memory after loading them.
drewp@bigasterisk.com
parents:
diff
changeset
|
39 :button1 :state :press . GRAPH :input { :lights :brightness 0 } |
95f72a22965d
rules become simple-looking again; fix the ambiguity in memory after loading them.
drewp@bigasterisk.com
parents:
diff
changeset
|
40 } => { |
95f72a22965d
rules become simple-looking again; fix the ambiguity in memory after loading them.
drewp@bigasterisk.com
parents:
diff
changeset
|
41 :lights :brightness 1 |
95f72a22965d
rules become simple-looking again; fix the ambiguity in memory after loading them.
drewp@bigasterisk.com
parents:
diff
changeset
|
42 } |
95f72a22965d
rules become simple-looking again; fix the ambiguity in memory after loading them.
drewp@bigasterisk.com
parents:
diff
changeset
|
43 |
95f72a22965d
rules become simple-looking again; fix the ambiguity in memory after loading them.
drewp@bigasterisk.com
parents:
diff
changeset
|
44 (:button1 :state :press) is allowed to match anywhere, but (:lights |
95f72a22965d
rules become simple-looking again; fix the ambiguity in memory after loading them.
drewp@bigasterisk.com
parents:
diff
changeset
|
45 :brightness 0) must be found in the :input graph. How do you say |
95f72a22965d
rules become simple-looking again; fix the ambiguity in memory after loading them.
drewp@bigasterisk.com
parents:
diff
changeset
|
46 this in N3? My example is half SPARQL. Also, how do you make rule |
95f72a22965d
rules become simple-looking again; fix the ambiguity in memory after loading them.
drewp@bigasterisk.com
parents:
diff
changeset
|
47 authors remember to do this? The old mistake is still possible. |
95f72a22965d
rules become simple-looking again; fix the ambiguity in memory after loading them.
drewp@bigasterisk.com
parents:
diff
changeset
|
48 |
95f72a22965d
rules become simple-looking again; fix the ambiguity in memory after loading them.
drewp@bigasterisk.com
parents:
diff
changeset
|
49 3. (current choice) RDF-reify output statements so they don't cascade, |
95f72a22965d
rules become simple-looking again; fix the ambiguity in memory after loading them.
drewp@bigasterisk.com
parents:
diff
changeset
|
50 then recover them after the rule run is done. |
95f72a22965d
rules become simple-looking again; fix the ambiguity in memory after loading them.
drewp@bigasterisk.com
parents:
diff
changeset
|
51 |
95f72a22965d
rules become simple-looking again; fix the ambiguity in memory after loading them.
drewp@bigasterisk.com
parents:
diff
changeset
|
52 { |
95f72a22965d
rules become simple-looking again; fix the ambiguity in memory after loading them.
drewp@bigasterisk.com
parents:
diff
changeset
|
53 :button1 :state :press . :lights :brightness 0 |
95f72a22965d
rules become simple-looking again; fix the ambiguity in memory after loading them.
drewp@bigasterisk.com
parents:
diff
changeset
|
54 } => { |
95f72a22965d
rules become simple-looking again; fix the ambiguity in memory after loading them.
drewp@bigasterisk.com
parents:
diff
changeset
|
55 :output :statement [ :subj :lights; :pred :brightness; :obj 1 ] |
95f72a22965d
rules become simple-looking again; fix the ambiguity in memory after loading them.
drewp@bigasterisk.com
parents:
diff
changeset
|
56 } |
95f72a22965d
rules become simple-looking again; fix the ambiguity in memory after loading them.
drewp@bigasterisk.com
parents:
diff
changeset
|
57 |
95f72a22965d
rules become simple-looking again; fix the ambiguity in memory after loading them.
drewp@bigasterisk.com
parents:
diff
changeset
|
58 This works since the output statement definitely won't trigger more |
95f72a22965d
rules become simple-looking again; fix the ambiguity in memory after loading them.
drewp@bigasterisk.com
parents:
diff
changeset
|
59 rules that match on :lights :brightness. It's easy to recover the true |
95f72a22965d
rules become simple-looking again; fix the ambiguity in memory after loading them.
drewp@bigasterisk.com
parents:
diff
changeset
|
60 output statement after the rules run. Like #2 above, it's still easy |
95f72a22965d
rules become simple-looking again; fix the ambiguity in memory after loading them.
drewp@bigasterisk.com
parents:
diff
changeset
|
61 to forget to reify the output statement. We can automate the |
95f72a22965d
rules become simple-looking again; fix the ambiguity in memory after loading them.
drewp@bigasterisk.com
parents:
diff
changeset
|
62 reification, though: given patterns like (?s :brightness ?o), we can |
95f72a22965d
rules become simple-looking again; fix the ambiguity in memory after loading them.
drewp@bigasterisk.com
parents:
diff
changeset
|
63 rewrite the appropriate statements in implied graphs to their reified |
95f72a22965d
rules become simple-looking again; fix the ambiguity in memory after loading them.
drewp@bigasterisk.com
parents:
diff
changeset
|
64 versions. escapeOutputStatements does this. |
95f72a22965d
rules become simple-looking again; fix the ambiguity in memory after loading them.
drewp@bigasterisk.com
parents:
diff
changeset
|
65 |
95f72a22965d
rules become simple-looking again; fix the ambiguity in memory after loading them.
drewp@bigasterisk.com
parents:
diff
changeset
|
66 4. Reify input statements. Just like #3, but alter the input |
95f72a22965d
rules become simple-looking again; fix the ambiguity in memory after loading them.
drewp@bigasterisk.com
parents:
diff
changeset
|
67 statements instead of outputs. |
95f72a22965d
rules become simple-looking again; fix the ambiguity in memory after loading them.
drewp@bigasterisk.com
parents:
diff
changeset
|
68 |
95f72a22965d
rules become simple-looking again; fix the ambiguity in memory after loading them.
drewp@bigasterisk.com
parents:
diff
changeset
|
69 This seems more expensive than #3 since there are lots of input |
95f72a22965d
rules become simple-looking again; fix the ambiguity in memory after loading them.
drewp@bigasterisk.com
parents:
diff
changeset
|
70 statements that are given to the rules engine, including many that are |
95f72a22965d
rules become simple-looking again; fix the ambiguity in memory after loading them.
drewp@bigasterisk.com
parents:
diff
changeset
|
71 never used in any rules, but they'd all have to get reified into 4x as |
95f72a22965d
rules become simple-looking again; fix the ambiguity in memory after loading them.
drewp@bigasterisk.com
parents:
diff
changeset
|
72 many statements. And, even within the patterns that do appear in the |
95f72a22965d
rules become simple-looking again; fix the ambiguity in memory after loading them.
drewp@bigasterisk.com
parents:
diff
changeset
|
73 rules, a given triple probably appears in more input graphs than |
95f72a22965d
rules become simple-looking again; fix the ambiguity in memory after loading them.
drewp@bigasterisk.com
parents:
diff
changeset
|
74 output graphs. |
95f72a22965d
rules become simple-looking again; fix the ambiguity in memory after loading them.
drewp@bigasterisk.com
parents:
diff
changeset
|
75 """ |
95f72a22965d
rules become simple-looking again; fix the ambiguity in memory after loading them.
drewp@bigasterisk.com
parents:
diff
changeset
|
76 import unittest |
95f72a22965d
rules become simple-looking again; fix the ambiguity in memory after loading them.
drewp@bigasterisk.com
parents:
diff
changeset
|
77 from rdflib.parser import StringInputSource |
95f72a22965d
rules become simple-looking again; fix the ambiguity in memory after loading them.
drewp@bigasterisk.com
parents:
diff
changeset
|
78 from rdflib import Graph, URIRef, Namespace, BNode |
95f72a22965d
rules become simple-looking again; fix the ambiguity in memory after loading them.
drewp@bigasterisk.com
parents:
diff
changeset
|
79 from rdflib.compare import isomorphic |
95f72a22965d
rules become simple-looking again; fix the ambiguity in memory after loading them.
drewp@bigasterisk.com
parents:
diff
changeset
|
80 |
95f72a22965d
rules become simple-looking again; fix the ambiguity in memory after loading them.
drewp@bigasterisk.com
parents:
diff
changeset
|
81 NS = Namespace('http://projects.bigasterisk.com/room/') |
95f72a22965d
rules become simple-looking again; fix the ambiguity in memory after loading them.
drewp@bigasterisk.com
parents:
diff
changeset
|
82 |
95f72a22965d
rules become simple-looking again; fix the ambiguity in memory after loading them.
drewp@bigasterisk.com
parents:
diff
changeset
|
83 def escapeOutputStatements(graph, outputPatterns): |
95f72a22965d
rules become simple-looking again; fix the ambiguity in memory after loading them.
drewp@bigasterisk.com
parents:
diff
changeset
|
84 """ |
95f72a22965d
rules become simple-looking again; fix the ambiguity in memory after loading them.
drewp@bigasterisk.com
parents:
diff
changeset
|
85 Rewrite |
95f72a22965d
rules become simple-looking again; fix the ambiguity in memory after loading them.
drewp@bigasterisk.com
parents:
diff
changeset
|
86 {} => { :s :p :o } . |
95f72a22965d
rules become simple-looking again; fix the ambiguity in memory after loading them.
drewp@bigasterisk.com
parents:
diff
changeset
|
87 to |
95f72a22965d
rules become simple-looking again; fix the ambiguity in memory after loading them.
drewp@bigasterisk.com
parents:
diff
changeset
|
88 {} => { :output :statement [ :subj :s; :pred :p; :obj :o ] } . |
95f72a22965d
rules become simple-looking again; fix the ambiguity in memory after loading them.
drewp@bigasterisk.com
parents:
diff
changeset
|
89 |
95f72a22965d
rules become simple-looking again; fix the ambiguity in memory after loading them.
drewp@bigasterisk.com
parents:
diff
changeset
|
90 if outputPatterns contains an element matching (:s, :p, :o) with |
95f72a22965d
rules become simple-looking again; fix the ambiguity in memory after loading them.
drewp@bigasterisk.com
parents:
diff
changeset
|
91 None as wildcards. |
95f72a22965d
rules become simple-looking again; fix the ambiguity in memory after loading them.
drewp@bigasterisk.com
parents:
diff
changeset
|
92 |
95f72a22965d
rules become simple-looking again; fix the ambiguity in memory after loading them.
drewp@bigasterisk.com
parents:
diff
changeset
|
93 Operates in-place on graph. |
95f72a22965d
rules become simple-looking again; fix the ambiguity in memory after loading them.
drewp@bigasterisk.com
parents:
diff
changeset
|
94 """ |
95f72a22965d
rules become simple-looking again; fix the ambiguity in memory after loading them.
drewp@bigasterisk.com
parents:
diff
changeset
|
95 for s, p, o in graph: |
95f72a22965d
rules become simple-looking again; fix the ambiguity in memory after loading them.
drewp@bigasterisk.com
parents:
diff
changeset
|
96 if isinstance(o, Graph): |
95f72a22965d
rules become simple-looking again; fix the ambiguity in memory after loading them.
drewp@bigasterisk.com
parents:
diff
changeset
|
97 o = escapeOutputStatements(o, outputPatterns) |
95f72a22965d
rules become simple-looking again; fix the ambiguity in memory after loading them.
drewp@bigasterisk.com
parents:
diff
changeset
|
98 variants = {(s, p, o), |
95f72a22965d
rules become simple-looking again; fix the ambiguity in memory after loading them.
drewp@bigasterisk.com
parents:
diff
changeset
|
99 (s, p, None), |
95f72a22965d
rules become simple-looking again; fix the ambiguity in memory after loading them.
drewp@bigasterisk.com
parents:
diff
changeset
|
100 (s, None, o), |
95f72a22965d
rules become simple-looking again; fix the ambiguity in memory after loading them.
drewp@bigasterisk.com
parents:
diff
changeset
|
101 (s, None, None), |
95f72a22965d
rules become simple-looking again; fix the ambiguity in memory after loading them.
drewp@bigasterisk.com
parents:
diff
changeset
|
102 (None, p, o), |
95f72a22965d
rules become simple-looking again; fix the ambiguity in memory after loading them.
drewp@bigasterisk.com
parents:
diff
changeset
|
103 (None, p, None), |
95f72a22965d
rules become simple-looking again; fix the ambiguity in memory after loading them.
drewp@bigasterisk.com
parents:
diff
changeset
|
104 (None, None, o), |
95f72a22965d
rules become simple-looking again; fix the ambiguity in memory after loading them.
drewp@bigasterisk.com
parents:
diff
changeset
|
105 (None, None, None)} |
95f72a22965d
rules become simple-looking again; fix the ambiguity in memory after loading them.
drewp@bigasterisk.com
parents:
diff
changeset
|
106 |
95f72a22965d
rules become simple-looking again; fix the ambiguity in memory after loading them.
drewp@bigasterisk.com
parents:
diff
changeset
|
107 if not variants.isdisjoint(outputPatterns): |
95f72a22965d
rules become simple-looking again; fix the ambiguity in memory after loading them.
drewp@bigasterisk.com
parents:
diff
changeset
|
108 graph.remove((s, p, o)) |
95f72a22965d
rules become simple-looking again; fix the ambiguity in memory after loading them.
drewp@bigasterisk.com
parents:
diff
changeset
|
109 stmt = BNode() |
95f72a22965d
rules become simple-looking again; fix the ambiguity in memory after loading them.
drewp@bigasterisk.com
parents:
diff
changeset
|
110 graph.add((stmt, NS['subj'], s)) |
95f72a22965d
rules become simple-looking again; fix the ambiguity in memory after loading them.
drewp@bigasterisk.com
parents:
diff
changeset
|
111 graph.add((stmt, NS['pred'], p)) |
95f72a22965d
rules become simple-looking again; fix the ambiguity in memory after loading them.
drewp@bigasterisk.com
parents:
diff
changeset
|
112 graph.add((stmt, NS['obj'], o)) |
95f72a22965d
rules become simple-looking again; fix the ambiguity in memory after loading them.
drewp@bigasterisk.com
parents:
diff
changeset
|
113 graph.add((NS['output'], NS['statement'], stmt)) |
95f72a22965d
rules become simple-looking again; fix the ambiguity in memory after loading them.
drewp@bigasterisk.com
parents:
diff
changeset
|
114 |
95f72a22965d
rules become simple-looking again; fix the ambiguity in memory after loading them.
drewp@bigasterisk.com
parents:
diff
changeset
|
115 |
95f72a22965d
rules become simple-looking again; fix the ambiguity in memory after loading them.
drewp@bigasterisk.com
parents:
diff
changeset
|
116 def unquoteOutputStatements(graph): |
95f72a22965d
rules become simple-looking again; fix the ambiguity in memory after loading them.
drewp@bigasterisk.com
parents:
diff
changeset
|
117 """ |
95f72a22965d
rules become simple-looking again; fix the ambiguity in memory after loading them.
drewp@bigasterisk.com
parents:
diff
changeset
|
118 graph can contain structures like |
95f72a22965d
rules become simple-looking again; fix the ambiguity in memory after loading them.
drewp@bigasterisk.com
parents:
diff
changeset
|
119 |
95f72a22965d
rules become simple-looking again; fix the ambiguity in memory after loading them.
drewp@bigasterisk.com
parents:
diff
changeset
|
120 :output :statement [:subj ?s; :pred ?p; :obj ?o] |
95f72a22965d
rules become simple-looking again; fix the ambiguity in memory after loading them.
drewp@bigasterisk.com
parents:
diff
changeset
|
121 |
95f72a22965d
rules become simple-looking again; fix the ambiguity in memory after loading them.
drewp@bigasterisk.com
parents:
diff
changeset
|
122 which simply mean the statements (?s ?p ?o) are meant to be in |
95f72a22965d
rules become simple-looking again; fix the ambiguity in memory after loading them.
drewp@bigasterisk.com
parents:
diff
changeset
|
123 the output, but they had to be quoted since they look like |
95f72a22965d
rules become simple-looking again; fix the ambiguity in memory after loading them.
drewp@bigasterisk.com
parents:
diff
changeset
|
124 input statements and we didn't want extra input rules to fire. |
95f72a22965d
rules become simple-looking again; fix the ambiguity in memory after loading them.
drewp@bigasterisk.com
parents:
diff
changeset
|
125 |
95f72a22965d
rules become simple-looking again; fix the ambiguity in memory after loading them.
drewp@bigasterisk.com
parents:
diff
changeset
|
126 This function returns the graph of (?s ?p ?o) statements found |
95f72a22965d
rules become simple-looking again; fix the ambiguity in memory after loading them.
drewp@bigasterisk.com
parents:
diff
changeset
|
127 on :output. |
95f72a22965d
rules become simple-looking again; fix the ambiguity in memory after loading them.
drewp@bigasterisk.com
parents:
diff
changeset
|
128 |
95f72a22965d
rules become simple-looking again; fix the ambiguity in memory after loading them.
drewp@bigasterisk.com
parents:
diff
changeset
|
129 Todo: use the standard schema for the escaping, or eliminate |
95f72a22965d
rules become simple-looking again; fix the ambiguity in memory after loading them.
drewp@bigasterisk.com
parents:
diff
changeset
|
130 it in favor of n3 graph literals. |
95f72a22965d
rules become simple-looking again; fix the ambiguity in memory after loading them.
drewp@bigasterisk.com
parents:
diff
changeset
|
131 """ |
95f72a22965d
rules become simple-looking again; fix the ambiguity in memory after loading them.
drewp@bigasterisk.com
parents:
diff
changeset
|
132 out = Graph() |
95f72a22965d
rules become simple-looking again; fix the ambiguity in memory after loading them.
drewp@bigasterisk.com
parents:
diff
changeset
|
133 for qs in graph.objects(NS['output'], NS['statement']): |
95f72a22965d
rules become simple-looking again; fix the ambiguity in memory after loading them.
drewp@bigasterisk.com
parents:
diff
changeset
|
134 out.add((graph.value(qs, NS['subj']), |
95f72a22965d
rules become simple-looking again; fix the ambiguity in memory after loading them.
drewp@bigasterisk.com
parents:
diff
changeset
|
135 graph.value(qs, NS['pred']), |
95f72a22965d
rules become simple-looking again; fix the ambiguity in memory after loading them.
drewp@bigasterisk.com
parents:
diff
changeset
|
136 graph.value(qs, NS['obj']))) |
95f72a22965d
rules become simple-looking again; fix the ambiguity in memory after loading them.
drewp@bigasterisk.com
parents:
diff
changeset
|
137 return out |
95f72a22965d
rules become simple-looking again; fix the ambiguity in memory after loading them.
drewp@bigasterisk.com
parents:
diff
changeset
|
138 |
95f72a22965d
rules become simple-looking again; fix the ambiguity in memory after loading them.
drewp@bigasterisk.com
parents:
diff
changeset
|
139 |
95f72a22965d
rules become simple-looking again; fix the ambiguity in memory after loading them.
drewp@bigasterisk.com
parents:
diff
changeset
|
140 ################################################################ |
95f72a22965d
rules become simple-looking again; fix the ambiguity in memory after loading them.
drewp@bigasterisk.com
parents:
diff
changeset
|
141 # tests |
95f72a22965d
rules become simple-looking again; fix the ambiguity in memory after loading them.
drewp@bigasterisk.com
parents:
diff
changeset
|
142 |
95f72a22965d
rules become simple-looking again; fix the ambiguity in memory after loading them.
drewp@bigasterisk.com
parents:
diff
changeset
|
143 def fromN3(n3): |
95f72a22965d
rules become simple-looking again; fix the ambiguity in memory after loading them.
drewp@bigasterisk.com
parents:
diff
changeset
|
144 g = Graph(identifier=URIRef('http://example.org/graph')) |
95f72a22965d
rules become simple-looking again; fix the ambiguity in memory after loading them.
drewp@bigasterisk.com
parents:
diff
changeset
|
145 g.parse(StringInputSource(('@prefix : %s .\n' % URIRef(NS).n3()) + n3), |
95f72a22965d
rules become simple-looking again; fix the ambiguity in memory after loading them.
drewp@bigasterisk.com
parents:
diff
changeset
|
146 format='n3') |
95f72a22965d
rules become simple-looking again; fix the ambiguity in memory after loading them.
drewp@bigasterisk.com
parents:
diff
changeset
|
147 return g |
95f72a22965d
rules become simple-looking again; fix the ambiguity in memory after loading them.
drewp@bigasterisk.com
parents:
diff
changeset
|
148 |
95f72a22965d
rules become simple-looking again; fix the ambiguity in memory after loading them.
drewp@bigasterisk.com
parents:
diff
changeset
|
149 def impliedGraph(g): |
95f72a22965d
rules become simple-looking again; fix the ambiguity in memory after loading them.
drewp@bigasterisk.com
parents:
diff
changeset
|
150 if len(g) != 1: raise NotImplementedError |
95f72a22965d
rules become simple-looking again; fix the ambiguity in memory after loading them.
drewp@bigasterisk.com
parents:
diff
changeset
|
151 stmt = list(g)[0] |
95f72a22965d
rules become simple-looking again; fix the ambiguity in memory after loading them.
drewp@bigasterisk.com
parents:
diff
changeset
|
152 return stmt[2] |
95f72a22965d
rules become simple-looking again; fix the ambiguity in memory after loading them.
drewp@bigasterisk.com
parents:
diff
changeset
|
153 |
95f72a22965d
rules become simple-looking again; fix the ambiguity in memory after loading them.
drewp@bigasterisk.com
parents:
diff
changeset
|
154 class TestEscapeOutputStatements(unittest.TestCase): |
95f72a22965d
rules become simple-looking again; fix the ambiguity in memory after loading them.
drewp@bigasterisk.com
parents:
diff
changeset
|
155 def testPassThrough(self): |
95f72a22965d
rules become simple-looking again; fix the ambiguity in memory after loading them.
drewp@bigasterisk.com
parents:
diff
changeset
|
156 g = fromN3(''' { :a :b :c } => { :d :e :f } . ''') |
95f72a22965d
rules become simple-looking again; fix the ambiguity in memory after loading them.
drewp@bigasterisk.com
parents:
diff
changeset
|
157 escapeOutputStatements(g, []) |
95f72a22965d
rules become simple-looking again; fix the ambiguity in memory after loading them.
drewp@bigasterisk.com
parents:
diff
changeset
|
158 self.assertEqual(fromN3(''' { :a :b :c } => { :d :e :f } . '''), g) |
95f72a22965d
rules become simple-looking again; fix the ambiguity in memory after loading them.
drewp@bigasterisk.com
parents:
diff
changeset
|
159 |
95f72a22965d
rules become simple-looking again; fix the ambiguity in memory after loading them.
drewp@bigasterisk.com
parents:
diff
changeset
|
160 def testMatchCompletePattern(self): |
95f72a22965d
rules become simple-looking again; fix the ambiguity in memory after loading them.
drewp@bigasterisk.com
parents:
diff
changeset
|
161 g = fromN3(''' { :a :b :c } => { :d :e :f } . ''') |
95f72a22965d
rules become simple-looking again; fix the ambiguity in memory after loading them.
drewp@bigasterisk.com
parents:
diff
changeset
|
162 escapeOutputStatements(g, [(NS['d'], NS['e'], NS['f'])]) |
95f72a22965d
rules become simple-looking again; fix the ambiguity in memory after loading them.
drewp@bigasterisk.com
parents:
diff
changeset
|
163 expected = fromN3(''' |
95f72a22965d
rules become simple-looking again; fix the ambiguity in memory after loading them.
drewp@bigasterisk.com
parents:
diff
changeset
|
164 { :a :b :c } => |
95f72a22965d
rules become simple-looking again; fix the ambiguity in memory after loading them.
drewp@bigasterisk.com
parents:
diff
changeset
|
165 { :output :statement [ :subj :d; :pred :e; :obj :f ] } . ''') |
95f72a22965d
rules become simple-looking again; fix the ambiguity in memory after loading them.
drewp@bigasterisk.com
parents:
diff
changeset
|
166 self.assert_(isomorphic(impliedGraph(expected), impliedGraph(g))) |
95f72a22965d
rules become simple-looking again; fix the ambiguity in memory after loading them.
drewp@bigasterisk.com
parents:
diff
changeset
|
167 |
95f72a22965d
rules become simple-looking again; fix the ambiguity in memory after loading them.
drewp@bigasterisk.com
parents:
diff
changeset
|
168 def testMatchWildcardPatternOnObject(self): |
95f72a22965d
rules become simple-looking again; fix the ambiguity in memory after loading them.
drewp@bigasterisk.com
parents:
diff
changeset
|
169 g = fromN3(''' { :a :b :c } => { :d :e :f } . ''') |
95f72a22965d
rules become simple-looking again; fix the ambiguity in memory after loading them.
drewp@bigasterisk.com
parents:
diff
changeset
|
170 escapeOutputStatements(g, [(NS['d'], NS['e'], None)]) |
95f72a22965d
rules become simple-looking again; fix the ambiguity in memory after loading them.
drewp@bigasterisk.com
parents:
diff
changeset
|
171 expected = fromN3(''' |
95f72a22965d
rules become simple-looking again; fix the ambiguity in memory after loading them.
drewp@bigasterisk.com
parents:
diff
changeset
|
172 { :a :b :c } => |
95f72a22965d
rules become simple-looking again; fix the ambiguity in memory after loading them.
drewp@bigasterisk.com
parents:
diff
changeset
|
173 { :output :statement [ :subj :d; :pred :e; :obj :f ] } . ''') |
95f72a22965d
rules become simple-looking again; fix the ambiguity in memory after loading them.
drewp@bigasterisk.com
parents:
diff
changeset
|
174 self.assert_(isomorphic(impliedGraph(expected), impliedGraph(g))) |
95f72a22965d
rules become simple-looking again; fix the ambiguity in memory after loading them.
drewp@bigasterisk.com
parents:
diff
changeset
|
175 |
95f72a22965d
rules become simple-looking again; fix the ambiguity in memory after loading them.
drewp@bigasterisk.com
parents:
diff
changeset
|
176 def testWildcardAndNonMatchingStatements(self): |
95f72a22965d
rules become simple-looking again; fix the ambiguity in memory after loading them.
drewp@bigasterisk.com
parents:
diff
changeset
|
177 g = fromN3(''' { :a :b :c } => { :d :e :f . :g :e :f . } . ''') |
95f72a22965d
rules become simple-looking again; fix the ambiguity in memory after loading them.
drewp@bigasterisk.com
parents:
diff
changeset
|
178 escapeOutputStatements(g, [(NS['d'], NS['e'], NS['f'])]) |
95f72a22965d
rules become simple-looking again; fix the ambiguity in memory after loading them.
drewp@bigasterisk.com
parents:
diff
changeset
|
179 expected = fromN3(''' |
95f72a22965d
rules become simple-looking again; fix the ambiguity in memory after loading them.
drewp@bigasterisk.com
parents:
diff
changeset
|
180 { :a :b :c } => |
95f72a22965d
rules become simple-looking again; fix the ambiguity in memory after loading them.
drewp@bigasterisk.com
parents:
diff
changeset
|
181 { :output :statement [ :subj :d; :pred :e; :obj :f ] . |
95f72a22965d
rules become simple-looking again; fix the ambiguity in memory after loading them.
drewp@bigasterisk.com
parents:
diff
changeset
|
182 :g :e :f } . ''') |
95f72a22965d
rules become simple-looking again; fix the ambiguity in memory after loading them.
drewp@bigasterisk.com
parents:
diff
changeset
|
183 self.assert_(isomorphic(impliedGraph(expected), impliedGraph(g))) |
95f72a22965d
rules become simple-looking again; fix the ambiguity in memory after loading them.
drewp@bigasterisk.com
parents:
diff
changeset
|
184 |
95f72a22965d
rules become simple-looking again; fix the ambiguity in memory after loading them.
drewp@bigasterisk.com
parents:
diff
changeset
|
185 def testTwoMatchingStatements(self): |
95f72a22965d
rules become simple-looking again; fix the ambiguity in memory after loading them.
drewp@bigasterisk.com
parents:
diff
changeset
|
186 g = fromN3(''' { :a :b :c } => { :d :e :f . :g :e :f } . ''') |
95f72a22965d
rules become simple-looking again; fix the ambiguity in memory after loading them.
drewp@bigasterisk.com
parents:
diff
changeset
|
187 escapeOutputStatements(g, [(None, NS['e'], None)]) |
95f72a22965d
rules become simple-looking again; fix the ambiguity in memory after loading them.
drewp@bigasterisk.com
parents:
diff
changeset
|
188 expected = fromN3(''' |
95f72a22965d
rules become simple-looking again; fix the ambiguity in memory after loading them.
drewp@bigasterisk.com
parents:
diff
changeset
|
189 { :a :b :c } => |
95f72a22965d
rules become simple-looking again; fix the ambiguity in memory after loading them.
drewp@bigasterisk.com
parents:
diff
changeset
|
190 { :output :statement [ :subj :d; :pred :e; :obj :f ], |
95f72a22965d
rules become simple-looking again; fix the ambiguity in memory after loading them.
drewp@bigasterisk.com
parents:
diff
changeset
|
191 [ :subj :g; :pred :e; :obj :f ] } . ''') |
95f72a22965d
rules become simple-looking again; fix the ambiguity in memory after loading them.
drewp@bigasterisk.com
parents:
diff
changeset
|
192 self.assert_(isomorphic(impliedGraph(expected), impliedGraph(g))) |
95f72a22965d
rules become simple-looking again; fix the ambiguity in memory after loading them.
drewp@bigasterisk.com
parents:
diff
changeset
|
193 |
95f72a22965d
rules become simple-looking again; fix the ambiguity in memory after loading them.
drewp@bigasterisk.com
parents:
diff
changeset
|
194 def testDontReplaceSourceStatements(self): |
95f72a22965d
rules become simple-looking again; fix the ambiguity in memory after loading them.
drewp@bigasterisk.com
parents:
diff
changeset
|
195 g = fromN3(''' { :a :b :c } => { :a :b :c } . ''') |
95f72a22965d
rules become simple-looking again; fix the ambiguity in memory after loading them.
drewp@bigasterisk.com
parents:
diff
changeset
|
196 escapeOutputStatements(g, [(NS['a'], NS['b'], NS['c'])]) |
95f72a22965d
rules become simple-looking again; fix the ambiguity in memory after loading them.
drewp@bigasterisk.com
parents:
diff
changeset
|
197 expected = fromN3(''' |
95f72a22965d
rules become simple-looking again; fix the ambiguity in memory after loading them.
drewp@bigasterisk.com
parents:
diff
changeset
|
198 { :a :b :c } => |
95f72a22965d
rules become simple-looking again; fix the ambiguity in memory after loading them.
drewp@bigasterisk.com
parents:
diff
changeset
|
199 { :output :statement [ :subj :a; :pred :b; :obj :c ] } . ''') |
95f72a22965d
rules become simple-looking again; fix the ambiguity in memory after loading them.
drewp@bigasterisk.com
parents:
diff
changeset
|
200 self.assert_(isomorphic(impliedGraph(expected), impliedGraph(g))) |
95f72a22965d
rules become simple-looking again; fix the ambiguity in memory after loading them.
drewp@bigasterisk.com
parents:
diff
changeset
|
201 |