forked from tonsky/datascript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquery_rules.cljc
192 lines (177 loc) · 6.14 KB
/
query_rules.cljc
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
(ns datascript.test.query-rules
(:require
#?(:cljs [cljs.test :as t :refer-macros [is are deftest testing]]
:clj [clojure.test :as t :refer [is are deftest testing]])
[datascript.core :as d]
[datascript.db :as db]
[datascript.test.core :as tdc]))
(deftest test-rules
(let [db [ [5 :follow 3]
[1 :follow 2] [2 :follow 3] [3 :follow 4] [4 :follow 6]
[2 :follow 4]]]
(is (= (d/q '[:find ?e1 ?e2
:in $ %
:where (follow ?e1 ?e2)]
db
'[[(follow ?x ?y)
[?x :follow ?y]]])
#{[1 2] [2 3] [3 4] [2 4] [5 3] [4 6]}))
(testing "Joining regular clauses with rule"
(is (= (d/q '[:find ?y ?x
:in $ %
:where [_ _ ?x]
(rule ?x ?y)
[(even? ?x)]]
db
'[[(rule ?a ?b)
[?a :follow ?b]]])
#{[3 2] [6 4] [4 2]})))
(testing "Rule context is isolated from outer context"
(is (= (d/q '[:find ?x
:in $ %
:where [?e _ _]
(rule ?x)]
db
'[[(rule ?e)
[_ ?e _]]])
#{[:follow]})))
(testing "Rule with branches"
(is (= (d/q '[:find ?e2
:in $ ?e1 %
:where (follow ?e1 ?e2)]
db
1
'[[(follow ?e2 ?e1)
[?e2 :follow ?e1]]
[(follow ?e2 ?e1)
[?e2 :follow ?t]
[?t :follow ?e1]]])
#{[2] [3] [4]})))
(testing "Recursive rules"
(is (= (d/q '[:find ?e2
:in $ ?e1 %
:where (follow ?e1 ?e2)]
db
1
'[[(follow ?e1 ?e2)
[?e1 :follow ?e2]]
[(follow ?e1 ?e2)
[?e1 :follow ?t]
(follow ?t ?e2)]])
#{[2] [3] [4] [6]}))
(is (= (d/q '[:find ?e1 ?e2
:in $ %
:where (follow ?e1 ?e2)]
[[1 :follow 2] [2 :follow 3]]
'[[(follow ?e1 ?e2)
[?e1 :follow ?e2]]
[(follow ?e1 ?e2)
(follow ?e2 ?e1)]])
#{[1 2] [2 3] [2 1] [3 2]}))
(is (= (d/q '[:find ?e1 ?e2
:in $ %
:where (follow ?e1 ?e2)]
[[1 :follow 2] [2 :follow 3] [3 :follow 1]]
'[[(follow ?e1 ?e2)
[?e1 :follow ?e2]]
[(follow ?e1 ?e2)
(follow ?e2 ?e1)]])
#{[1 2] [2 3] [3 1] [2 1] [3 2] [1 3]})))
(testing "Mutually recursive rules"
(is (= (d/q '[:find ?e1 ?e2
:in $ %
:where (f1 ?e1 ?e2)]
[[0 :f1 1]
[1 :f2 2]
[2 :f1 3]
[3 :f2 4]
[4 :f1 5]
[5 :f2 6]]
'[[(f1 ?e1 ?e2)
[?e1 :f1 ?e2]]
[(f1 ?e1 ?e2)
[?t :f1 ?e2]
(f2 ?e1 ?t)]
[(f2 ?e1 ?e2)
[?e1 :f2 ?e2]]
[(f2 ?e1 ?e2)
[?t :f2 ?e2]
(f1 ?e1 ?t)]])
#{[0 1] [0 3] [0 5]
[1 3] [1 5]
[2 3] [2 5]
[3 5]
[4 5]})))
(testing "Passing ins to rule"
(is (= (d/q '[:find ?x ?y
:in $ % ?even
:where
(match ?even ?x ?y)]
db
'[[(match ?pred ?e ?e2)
[?e :follow ?e2]
[(?pred ?e)]
[(?pred ?e2)]]]
even?)
#{[4 6] [2 4]})))
(testing "Using built-ins inside rule"
(is (= (d/q '[:find ?x ?y
:in $ %
:where (match ?x ?y)]
db
'[[(match ?e ?e2)
[?e :follow ?e2]
[(even? ?e)]
[(even? ?e2)]]])
#{[4 6] [2 4]})))
(testing "Calling rule twice (#44)"
(d/q '[:find ?p
:in $ % ?fn
:where (rule ?p ?fn "a")
(rule ?p ?fn "b")]
[[1 :attr "a"]]
'[[(rule ?p ?fn ?x)
[?p :attr ?x]
[(?fn ?x)]]]
(constantly true)))
)
(testing "Specifying db to rule"
(is (= (d/q '[:find ?n
:in $sexes $ages %
:where ($sexes male ?n)
($ages adult ?n) ]
[["Ivan" :male] ["Darya" :female] ["Oleg" :male] ["Igor" :male]]
[["Ivan" 15] ["Oleg" 66] ["Darya" 32]]
'[[(male ?x)
[?x :male]]
[(adult ?y)
[?y ?a]
[(>= ?a 18)]]])
#{["Oleg"]})))
(testing "Rule name validation #319"
(is (thrown-msg? "Unknown rule 'wat in (wat ?x)"
(d/q '[:find ?x
:in $ %
:where (wat ?x)]
[] []))))
(testing "Rule vars validation"
(is (thrown-msg? "Cannot parse var, expected symbol starting with ?, got: $e1" ;; #300
(d/q '[:find ?e :in $ % :where [?e]]
(d/empty-db)
'[[(rule $e1 ?e2)
[?e1 :ref ?e2]]])))))
;; https://github.com/tonsky/datascript/issues/218
(deftest test-false-arguments
(let [db (d/db-with (d/empty-db)
[[:db/add 1 :attr true]
[:db/add 2 :attr false]])
rules '[[(is ?id ?val)
[?id :attr ?val]]]]
(is (= #{[1]}
(d/q '[:find ?id :in $ %
:where (is ?id true)]
db rules)))
(is (= #{[2]}
(d/q '[:find ?id :in $ %
:where (is ?id false)]
db rules)))))