forked from tonsky/datascript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser_where.cljc
237 lines (196 loc) · 7.8 KB
/
parser_where.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
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
(ns datascript.test.parser-where
(: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.parser :as dp]
[datascript.test.core :as tdc])
#?(:clj
(:import [clojure.lang ExceptionInfo])))
(deftest pattern
(are [clause pattern] (= (dp/parse-clause clause) pattern)
'[?e ?a ?v]
(dp/->Pattern (dp/->DefaultSrc) [(dp/->Variable '?e) (dp/->Variable '?a) (dp/->Variable '?v)])
'[_ ?a _ _]
(dp/->Pattern (dp/->DefaultSrc) [(dp/->Placeholder) (dp/->Variable '?a) (dp/->Placeholder) (dp/->Placeholder)])
'[$x _ ?a _ _]
(dp/->Pattern (dp/->SrcVar '$x) [(dp/->Placeholder) (dp/->Variable '?a) (dp/->Placeholder) (dp/->Placeholder)])
'[$x _ :name ?v]
(dp/->Pattern (dp/->SrcVar '$x) [(dp/->Placeholder) (dp/->Constant :name) (dp/->Variable '?v)]))
(is (thrown-with-msg? ExceptionInfo #"Pattern could not be empty"
(dp/parse-clause '[])))
)
(deftest test-pred
(are [clause res] (= (dp/parse-clause clause) res)
'[(pred ?a 1)]
(dp/->Predicate (dp/->PlainSymbol 'pred) [(dp/->Variable '?a) (dp/->Constant 1)])
'[(pred)]
(dp/->Predicate (dp/->PlainSymbol 'pred) [])
'[(?custom-pred ?a)]
(dp/->Predicate (dp/->Variable '?custom-pred) [(dp/->Variable '?a)])
))
(deftest test-fn
(are [clause res] (= (dp/parse-clause clause) res)
'[(fn ?a 1) ?x]
(dp/->Function (dp/->PlainSymbol 'fn) [(dp/->Variable '?a) (dp/->Constant 1)] (dp/->BindScalar (dp/->Variable '?x)))
'[(fn) ?x]
(dp/->Function (dp/->PlainSymbol 'fn) [] (dp/->BindScalar (dp/->Variable '?x)))
'[(?custom-fn) ?x]
(dp/->Function (dp/->Variable '?custom-fn) [] (dp/->BindScalar (dp/->Variable '?x)))
'[(?custom-fn ?arg) ?x]
(dp/->Function (dp/->Variable '?custom-fn) [(dp/->Variable '?arg)] (dp/->BindScalar (dp/->Variable '?x)))))
(deftest rule-expr
(are [clause res] (= (dp/parse-clause clause) res)
'(friends ?x ?y)
(dp/->RuleExpr (dp/->DefaultSrc) (dp/->PlainSymbol 'friends) [(dp/->Variable '?x) (dp/->Variable '?y)])
'(friends "Ivan" _)
(dp/->RuleExpr (dp/->DefaultSrc) (dp/->PlainSymbol 'friends) [(dp/->Constant "Ivan") (dp/->Placeholder)])
'($1 friends ?x ?y)
(dp/->RuleExpr (dp/->SrcVar '$1) (dp/->PlainSymbol 'friends) [(dp/->Variable '?x) (dp/->Variable '?y)]))
(is (thrown-with-msg? ExceptionInfo #"rule-expr requires at least one argument"
(dp/parse-clause '(friends))))
(is (thrown-with-msg? ExceptionInfo #"Cannot parse rule-expr arguments"
(dp/parse-clause '(friends something)))))
(deftest not-clause
(are [clause res] (= (dp/parse-clause clause) res)
'(not [?e :follows ?x])
(dp/->Not
(dp/->DefaultSrc)
[(dp/->Variable '?e) (dp/->Variable '?x)]
[ (dp/->Pattern
(dp/->DefaultSrc)
[(dp/->Variable '?e) (dp/->Constant :follows) (dp/->Variable '?x)]) ])
'(not
[?e :follows ?x]
[?x _ ?y])
(dp/->Not
(dp/->DefaultSrc)
[(dp/->Variable '?e) (dp/->Variable '?x) (dp/->Variable '?y)]
[ (dp/->Pattern
(dp/->DefaultSrc)
[(dp/->Variable '?e) (dp/->Constant :follows) (dp/->Variable '?x)])
(dp/->Pattern
(dp/->DefaultSrc)
[(dp/->Variable '?x) (dp/->Placeholder) (dp/->Variable '?y)])])
'($1 not [?x])
(dp/->Not
(dp/->SrcVar '$1)
[(dp/->Variable '?x)]
[ (dp/->Pattern (dp/->DefaultSrc) [(dp/->Variable '?x)]) ])
'(not-join [?e ?y]
[?e :follows ?x]
[?x _ ?y])
(dp/->Not
(dp/->DefaultSrc)
[(dp/->Variable '?e) (dp/->Variable '?y)]
[ (dp/->Pattern
(dp/->DefaultSrc)
[(dp/->Variable '?e) (dp/->Constant :follows) (dp/->Variable '?x)])
(dp/->Pattern
(dp/->DefaultSrc)
[(dp/->Variable '?x) (dp/->Placeholder) (dp/->Variable '?y)])])
'($1 not-join [?e] [?e :follows ?x])
(dp/->Not
(dp/->SrcVar '$1)
[(dp/->Variable '?e)]
[ (dp/->Pattern
(dp/->DefaultSrc)
[(dp/->Variable '?e) (dp/->Constant :follows) (dp/->Variable '?x)]) ])
)
(is (thrown-with-msg? ExceptionInfo #"Join variables should not be empty"
(dp/parse-clause '(not-join [] [?y]))))
(is (thrown-with-msg? ExceptionInfo #"Join variables should not be empty"
(dp/parse-clause '(not [_]))))
(is (thrown-with-msg? ExceptionInfo #"Cannot parse 'not-join' clause"
(dp/parse-clause '(not-join [?x]))))
(is (thrown-with-msg? ExceptionInfo #"Cannot parse 'not' clause"
(dp/parse-clause '(not))))
)
(deftest or-clause
(are [clause res] (= (dp/parse-clause clause) res)
'(or [?e :follows ?x])
(dp/->Or
(dp/->DefaultSrc)
(dp/->RuleVars nil [(dp/->Variable '?e) (dp/->Variable '?x)])
[ (dp/->Pattern
(dp/->DefaultSrc)
[(dp/->Variable '?e) (dp/->Constant :follows) (dp/->Variable '?x)]) ])
'(or
[?e :follows ?x]
[?e :friend ?x])
(dp/->Or
(dp/->DefaultSrc)
(dp/->RuleVars nil [(dp/->Variable '?e) (dp/->Variable '?x)])
[ (dp/->Pattern
(dp/->DefaultSrc)
[(dp/->Variable '?e) (dp/->Constant :follows) (dp/->Variable '?x)])
(dp/->Pattern
(dp/->DefaultSrc)
[(dp/->Variable '?e) (dp/->Constant :friend) (dp/->Variable '?x)])])
'(or
[?e :follows ?x]
(and
[?e :friend ?x]
[?x :friend ?e]))
(dp/->Or
(dp/->DefaultSrc)
(dp/->RuleVars nil [(dp/->Variable '?e) (dp/->Variable '?x)])
[ (dp/->Pattern
(dp/->DefaultSrc)
[(dp/->Variable '?e) (dp/->Constant :follows) (dp/->Variable '?x)])
(dp/->And
[(dp/->Pattern
(dp/->DefaultSrc)
[(dp/->Variable '?e) (dp/->Constant :friend) (dp/->Variable '?x)])
(dp/->Pattern
(dp/->DefaultSrc)
[(dp/->Variable '?x) (dp/->Constant :friend) (dp/->Variable '?e)])]) ])
'($1 or [?x])
(dp/->Or
(dp/->SrcVar '$1)
(dp/->RuleVars nil [(dp/->Variable '?x)])
[ (dp/->Pattern (dp/->DefaultSrc) [(dp/->Variable '?x)]) ])
'(or-join [?e]
[?e :follows ?x]
[?e :friend ?y])
(dp/->Or
(dp/->DefaultSrc)
(dp/->RuleVars nil [(dp/->Variable '?e)])
[ (dp/->Pattern
(dp/->DefaultSrc)
[(dp/->Variable '?e) (dp/->Constant :follows) (dp/->Variable '?x)])
(dp/->Pattern
(dp/->DefaultSrc)
[(dp/->Variable '?e) (dp/->Constant :friend) (dp/->Variable '?y)])])
'(or-join [[?e]]
(and [?e :follows ?x]
[?e :friend ?y]))
(dp/->Or
(dp/->DefaultSrc)
(dp/->RuleVars [(dp/->Variable '?e)] nil)
[ (dp/->And
[(dp/->Pattern
(dp/->DefaultSrc)
[(dp/->Variable '?e) (dp/->Constant :follows) (dp/->Variable '?x)])
(dp/->Pattern
(dp/->DefaultSrc)
[(dp/->Variable '?e) (dp/->Constant :friend) (dp/->Variable '?y)])]) ])
'($1 or-join [[?e] ?x]
[?e :follows ?x])
(dp/->Or
(dp/->SrcVar '$1)
(dp/->RuleVars [(dp/->Variable '?e)] [(dp/->Variable '?x)])
[ (dp/->Pattern
(dp/->DefaultSrc)
[(dp/->Variable '?e) (dp/->Constant :follows) (dp/->Variable '?x)]) ])
)
(is (thrown-with-msg? ExceptionInfo #"Cannot parse rule-vars"
(dp/parse-clause '(or-join [] [?y]))))
(is (thrown-with-msg? ExceptionInfo #"Join variables should not be empty"
(dp/parse-clause '(or [_]))))
(is (thrown-with-msg? ExceptionInfo #"Cannot parse 'or-join' clause"
(dp/parse-clause '(or-join [?x]))))
(is (thrown-with-msg? ExceptionInfo #"Cannot parse 'or' clause"
(dp/parse-clause '(or))))
)