-
Notifications
You must be signed in to change notification settings - Fork 160
/
sparql.clj
346 lines (307 loc) · 13.2 KB
/
sparql.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
(ns crux.sparql
"SPARQL to Datalog compiler. Only handles a subset of SPARQL.
https://www.w3.org/TR/2013/REC-sparql11-query-20130321/
See http://docs.rdf4j.org/"
(:require [clojure.walk :as w]
[clojure.set :as set]
[crux.rdf :as rdf])
(:import [org.eclipse.rdf4j.query BindingSet QueryLanguage]
org.eclipse.rdf4j.query.parser.QueryParserUtil
org.eclipse.rdf4j.query.algebra.helpers.AbstractQueryModelVisitor
[org.eclipse.rdf4j.query.algebra
And ArbitraryLengthPath BindingSetAssignment Compare Difference Distinct Extension ExtensionElem Exists
Filter FunctionCall Join LeftJoin ListMemberOperator MathExpr Not Or Order OrderElem Projection QueryModelNode
Regex SameTerm Slice StatementPattern TupleExpr Union ValueConstant Var ZeroLengthPath]))
(defn- str->sparql-var [s]
(symbol (str "?" s)))
(defn- arbitrary-length-path-rule-head [s p o min-length]
(when (coll? p)
(throw (UnsupportedOperationException. "Only single predicate arbitrary length paths supported.")))
(let [path-type (case (long min-length)
1 "PLUS"
0 "STAR")]
(list (symbol (namespace p)
(str (name p) "-" path-type)) s o)))
(defn blank-or-anonymous-var? [x]
(re-find #"^\??_" x))
(extend-protocol rdf/RDFToClojure
And
(rdf->clj [this]
(vec (concat (rdf/rdf->clj (.getLeftArg this))
(rdf/rdf->clj (.getRightArg this)))))
ArbitraryLengthPath
(rdf->clj [this]
(when-not (instance? StatementPattern (.getPathExpression this))
(throw (UnsupportedOperationException.
"Arbitrary length paths only supported for statement patterns.")))
(let [[[s p o]] (rdf/rdf->clj (.getPathExpression this))]
[(arbitrary-length-path-rule-head s p o (.getMinLength this))]))
BindingSetAssignment
(rdf->clj [this])
Compare
(rdf->clj [this]
(let [left (rdf/rdf->clj (.getLeftArg this))
right (rdf/rdf->clj (.getRightArg this))]
(when (some list? [left right])
(throw (UnsupportedOperationException. "Nested expressions are not supported.")))
[[(list (symbol (.getSymbol (.getOperator this)))
left
right)]]))
Difference
(rdf->clj [this]
(throw (UnsupportedOperationException. "MINUS not supported, use NOT EXISTS.")))
;; NOTE: Distinct is the default due to the set semantics of
;; Datalog.
Distinct
(rdf->clj [this]
(rdf/rdf->clj (.getArg this)))
Extension
(rdf->clj [this]
(vec (apply concat
(rdf/rdf->clj (.getArg this))
(for [arg (.getElements this)]
(rdf/rdf->clj arg)))))
ExtensionElem
(rdf->clj [this]
(when-not (instance? Var (.getExpr this))
[[(rdf/rdf->clj (.getExpr this))
(str->sparql-var (.getName this))]]))
Exists
(rdf->clj [this]
(rdf/rdf->clj (.getSubQuery this)))
Filter
(rdf->clj [this]
(let [smap (when (instance? SameTerm (.getCondition this))
(let [same-term ^SameTerm (.getCondition this)]
{(rdf/rdf->clj (.getLeftArg same-term))
(rdf/rdf->clj (.getRightArg same-term))}))]
(vec (concat (w/postwalk-replace smap (rdf/rdf->clj (.getArg this)))
(rdf/rdf->clj (.getCondition this))))))
FunctionCall
(rdf->clj [this]
(let [args (for [arg (.getArgs this)]
(rdf/rdf->clj arg))]
(when (some list? args)
(throw (UnsupportedOperationException. "Nested expressions are not supported.")))
(->> (cons (symbol (.getURI this)) args)
(apply list))))
Join
(rdf->clj [this]
(vec (concat (rdf/rdf->clj (.getLeftArg this))
(rdf/rdf->clj (.getRightArg this)))))
MathExpr
(rdf->clj [this]
(let [left (rdf/rdf->clj (.getLeftArg this))
right (rdf/rdf->clj (.getRightArg this))]
(when (some list? [left right])
(throw (UnsupportedOperationException. "Nested expressions are not supported.")))
(list (symbol (.getSymbol (.getOperator this)))
left right)))
LeftJoin
(rdf->clj [this]
(let [or-join-vars (->> (.getBindingNames this)
(remove blank-or-anonymous-var?)
(set))
or-join-vars (set/intersection or-join-vars (set (.getBindingNames (.getRightArg this))))
or-join-vars (mapv str->sparql-var or-join-vars)
common-vars (set (mapv str->sparql-var (.getBindingNames (.getLeftArg this))))
condition (some-> (.getCondition this) (rdf/rdf->clj))
optional (rdf/rdf->clj (.getRightArg this))
optional-attr (second (first optional))
optional (cond-> optional
condition (concat condition))
optional (if (> (count optional) 1)
[(cons 'and optional)]
optional)
build-optional-clause (fn [var]
(if (contains? common-vars var)
(list 'not [var optional-attr])
[(list 'identity ::optional) var]))]
(conj (rdf/rdf->clj (.getLeftArg this))
(cons 'or-join (cons or-join-vars
(concat
optional
(if (> (count or-join-vars) 1)
[(apply list 'and (map build-optional-clause or-join-vars))]
[(build-optional-clause (first or-join-vars))])))))))
ListMemberOperator
(rdf->clj [this]
[[(list '==
(rdf/rdf->clj (first (.getArguments this)))
(set (map rdf/rdf->clj (rest (.getArguments this)))))]])
;; TODO: Should be possible to make this work in other cases as
;; well.
Not
(rdf->clj [this]
(when-not (and (instance? Exists (.getArg this))
(instance? Filter (.getParentNode this)))
(throw (UnsupportedOperationException. "NOT only supported in FILTER NOT EXISTS.")))
(let [not-vars (->> (.getAssuredBindingNames (.getSubQuery ^Exists (.getArg this)))
(remove blank-or-anonymous-var?)
(set))
parent-vars (set (.getAssuredBindingNames ^Filter (.getParentNode this)))
is-not? (set/subset? not-vars parent-vars)
not-join-vars (set/intersection not-vars parent-vars)
not (rdf/rdf->clj (.getArg this))]
[(if is-not?
(cons 'not not)
(cons 'not-join (cons (mapv str->sparql-var not-join-vars) not)))]))
;; TODO: To simplistic. Logical or is really short cutting, but is
;; useful to support between predicates in a filter.
Or
(rdf->clj [this]
(let [or-left (rdf/rdf->clj (.getLeftArg this))
or-left (if (> (count or-left) 1)
[(cons 'and or-left)]
or-left)
or-right (rdf/rdf->clj (.getRightArg this))
or-right (if (> (count or-right) 1)
[(cons 'and or-right)]
or-right)]
[(cons 'or (concat or-left or-right))]))
Order
(rdf->clj [this]
(rdf/rdf->clj (.getArg this)))
OrderElem
(rdf->clj [this]
[(rdf/rdf->clj (.getExpr this))
(if (.isAscending this)
:asc
:desc)])
Projection
(rdf->clj [this]
(rdf/rdf->clj (.getArg this)))
Regex
(rdf->clj [this]
[[(list 're-find
(let [flags (some-> (.getFlagsArg this) (rdf/rdf->clj))]
(re-pattern (str (when (seq flags)
(str "(?" flags ")"))
(rdf/rdf->clj (.getPatternArg this)))))
(rdf/rdf->clj (.getArg this)))]])
SameTerm
(rdf->clj [this])
Slice
(rdf->clj [this]
(rdf/rdf->clj (.getArg this)))
StatementPattern
(rdf->clj [this]
(let [s (.getSubjectVar this)
p (.getPredicateVar this)
o (.getObjectVar this)]
(when-not (.hasValue p)
(throw (UnsupportedOperationException. (str "Does not support variables in predicate position: " (rdf/rdf->clj p)))))
[(mapv rdf/rdf->clj [s p o])]))
Union
(rdf->clj [this]
(let [is-or? (= (set (.getAssuredBindingNames this))
(->> (.getBindingNames this)
(remove blank-or-anonymous-var?)
(set)))
or-join-vars (->> (.getAssuredBindingNames this)
(remove blank-or-anonymous-var?)
(mapv str->sparql-var))
or-left (rdf/rdf->clj (.getLeftArg this))
or-left (if (> (count or-left) 1)
[(cons 'and or-left)]
or-left)
or-right (rdf/rdf->clj (.getRightArg this))
or-right (if (> (count or-right) 1)
[(cons 'and or-right)]
or-right)]
[(if is-or?
(cons 'or (concat or-left or-right))
(cons 'or-join (cons or-join-vars (concat or-left or-right))))]))
Var
(rdf->clj [this]
(if (.hasValue this)
(rdf/rdf->clj (.getValue this))
(str->sparql-var (.getName this))))
ValueConstant
(rdf->clj [this]
(rdf/rdf->clj (.getValue this)))
ZeroLengthPath
(rdf->clj [this]
[[(rdf/rdf->clj (.getSubjectVar this)) :crux.db/id]
['(identity ::zero-matches) (rdf/rdf->clj (.getObjectVar this))]]))
(defn- collect-args [^TupleExpr tuple-expr]
(let [args (atom nil)]
(->> (proxy [AbstractQueryModelVisitor] []
(meetNode [node]
(when (instance? BindingSetAssignment node)
(when @args
(throw (IllegalStateException. "Only one VALUES block supported.")))
(let [bsa ^BindingSetAssignment node]
(->> (for [^BindingSet bs (.getBindingSets bsa)]
(->> (for [bn (.getBindingNames bs)]
[(str->sparql-var bn) (if (.hasBinding bs bn)
(rdf/rdf->clj (.getValue bs bn))
::undefined)])
(into {})))
(vec)
(reset! args))))
(.visitChildren ^QueryModelNode node this)))
(.visit tuple-expr))
@args))
(defn- collect-arbritrary-path-rules [^TupleExpr tuple-expr]
(let [rule-name->rules (atom nil)]
(->> (proxy [AbstractQueryModelVisitor] []
(meetNode [node]
(when (instance? ArbitraryLengthPath node)
(let [[[_ p _]] (rdf/rdf->clj (.getPathExpression ^ArbitraryLengthPath node))
s '?s
o '?o
min-length (.getMinLength ^ArbitraryLengthPath node)
[rule-name] (arbitrary-length-path-rule-head s p o min-length)]
(swap! rule-name->rules assoc rule-name
(cond-> [[(arbitrary-length-path-rule-head s p o min-length)
[s p o]]
[(arbitrary-length-path-rule-head s p o min-length)
[s p '?t]
(arbitrary-length-path-rule-head '?t p o min-length)]]
(zero? min-length) (conj [(arbitrary-length-path-rule-head s p o min-length)
[s :crux.db/id]
['(identity ::zero-matches) o]])))))
(.visitChildren ^QueryModelNode node this)))
(.visit tuple-expr))
(some->> (for [[_ rules] @rule-name->rules
rule rules]
rule)
(not-empty)
(vec))))
(defn- collect-slice [^TupleExpr tuple-expr]
(let [slice (atom nil)]
(->> (proxy [AbstractQueryModelVisitor] []
(meetNode [node]
(when (instance? Slice node)
(reset! slice (merge (when (.hasOffset ^Slice node)
{:offset (.getOffset ^Slice node)})
(when (.hasLimit ^Slice node)
{:limit (.getLimit ^Slice node)}))))
(.visitChildren ^QueryModelNode node this)))
(.visit tuple-expr))
@slice))
(defn- collect-order-by [^TupleExpr tuple-expr]
(let [order-by (atom nil)]
(->> (proxy [AbstractQueryModelVisitor] []
(meetNode [node]
(when (instance? Order node)
(reset! order-by (mapv rdf/rdf->clj (.getElements ^Order node))))
(.visitChildren ^QueryModelNode node this)))
(.visit tuple-expr))
@order-by))
(defn sparql->datalog
([sparql]
(sparql->datalog sparql nil))
([sparql base-uri]
(let [tuple-expr (.getTupleExpr (QueryParserUtil/parseQuery QueryLanguage/SPARQL sparql base-uri))
args (collect-args tuple-expr)
rules (collect-arbritrary-path-rules tuple-expr)
slice (collect-slice tuple-expr)
order-by (collect-order-by tuple-expr)]
(cond-> {:find (mapv str->sparql-var (.getBindingNames tuple-expr))
:where (rdf/use-default-language (rdf/rdf->clj tuple-expr) rdf/*default-language*)}
args (assoc :args args)
slice (merge slice)
order-by (assoc :order-by order-by)
rules (assoc :rules rules)))))