/
matching.clj
408 lines (373 loc) · 16.5 KB
/
matching.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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
(ns
^{:doc "Matching strategies for snippet-driven querying."
:author "Coen De Roover, Siltvani"}
damp.ekeko.snippets.matching
(:require [clojure.core.logic :as cl])
(:require [damp.ekeko.snippets
[util :as util]
[representation :as representation]
[querying :as querying]
[runtime :as runtime]])
(:require
[damp.ekeko [logic :as el]]
[damp.ekeko.jdt
[astnode :as astnode]
[reification :as reification]]))
;; for each grounding or constraining function maker, there are additional arguments beside snippet-ast,
;; which can be accessed in snippet :ast2groundf or :ast2constrainf via getter function
;; snippet-grounder-args-for-node and snippet-constrainer-args-for-node (see representation.clj)
(defn
make-epsilon-function
"Returns a function that does not generate any conditions for the given AST node of a code snippet."
[snippet-val]
(fn [snippet]
'()))
(declare ast-primitive-as-expression)
;; Grounding Functions
;; -------------------
(defn
gf-minimalistic
"Only generates grounding conditions for the root node of the snippet."
[snippet-ast]
(let [snippet-ast-keyw (astnode/ekeko-keyword-for-class-of snippet-ast)]
(fn [snippet]
(if
(= snippet-ast (:ast snippet))
(let [var-match (representation/snippet-var-for-node snippet snippet-ast)]
`((reification/ast ~snippet-ast-keyw ~var-match)))
'()))))
(defn
gf-node-exact
"Returns a function that will generate grounding conditions for the given AST node of a code snippet:
For AST node is the value of a property: ((has :property ?var-for-owner-match ?var-for-node-match))"
[snippet-ast]
(fn [snippet]
(let [snippet-owner (astnode/owner snippet-ast)
var-match (representation/snippet-var-for-node snippet snippet-ast)
var-match-owner (representation/snippet-var-for-node snippet snippet-owner)
owner-property (astnode/owner-property snippet-ast)
owner-property-keyw (astnode/ekeko-keyword-for-property-descriptor owner-property)]
`((has ~owner-property-keyw ~var-match-owner ~var-match)))))
(defn
gf-node-deep
"Returns a function that will generate grounding conditions for the given AST node of a code snippet:
For AST node is the value of a property: ((child+ ?var-for-owner-match ?var-for-node-match))"
[snippet-ast]
(fn [snippet]
(let [snippet-owner (astnode/owner snippet-ast)
var-match (representation/snippet-var-for-node snippet snippet-ast)
var-match-owner (representation/snippet-var-for-node snippet snippet-owner)]
`((child+ ~var-match-owner ~var-match)))))
;still not sure whether gf-node-exact & gf-node-deep is necessarry, because it maybe belongs to cf
(defn
make-grounding-function
[type]
(cond
(= type :minimalistic)
gf-minimalistic
(= type :node-exact)
gf-node-exact
(= type :node-deep)
gf-node-deep
(= type :epsilon)
make-epsilon-function
:default
(throw (Exception. (str "Unknown grounding function type: " type)))))
;; Constraining Functions
;; ----------------------
(defn
cf-node-exact
"Returns a function that will generate constraining conditions for the given property value of a code snippet:
For ASTNode instances: ((ast :kind-of-node ?var-for-node-match)
(has :property1 ?var-for-node-match ?var-for-child1-match)
(has :property2 ?var-for-node-match ''primitive-valued-child-as-string''))
...."
[snippet-ast]
(fn [snippet]
(let [snippet-keyw (astnode/ekeko-keyword-for-class-of snippet-ast)
snippet-properties (astnode/node-ekeko-properties snippet-ast)
var-match (representation/snippet-var-for-node snippet snippet-ast)
child-conditions
(for [[property-keyw retrievalf]
(seq snippet-properties)
:let [value (retrievalf)
var-value (representation/snippet-var-for-node snippet value)]]
`(reification/has ~property-keyw ~var-match ~var-value))]
(if
(= snippet-ast (:ast snippet))
`(~@child-conditions) ;because snippet root is already ground to an ast of the right kind
`((reification/ast ~snippet-keyw ~var-match)
~@child-conditions)))))
(defn
internal-cf-list
"Returns constraining-conditions for the given property value of a code snippet.
For Ekeko wrappers of ASTNode$NodeList instances:
(listvalue ?var-match)
(fresh [?newly-generated-var]
(value-raw ?var-match ?newly-generated-var)
(equals snippet-list-size (.size ?newly-generated-var)) {If type = :samesize}
(element-conditions)"
[snippet snippet-val type function-element-condition]
(let [lst (representation/snippet-value-for-node snippet snippet-val)
snippet-list-size (.size lst)
var-match (representation/snippet-var-for-node snippet snippet-val)
var-match-raw (representation/snippet-var-for-node snippet lst)
size-condition
(if (= type :samesize)
`((el/equals ~snippet-list-size (.size ~var-match-raw)))
`())
element-conditions
(for [element lst
:let [idx-el (.indexOf lst element)
var-el (representation/snippet-var-for-node snippet element)]]
(function-element-condition var-match-raw var-el idx-el))]
`((reification/listvalue ~var-match)
(reification/value-raw ~var-match ~var-match-raw)
~@size-condition
~@element-conditions)))
(defn
cf-list-exact
"Returns a function that will generate constraining conditions for the given property value of a code snippet.
Conditions : - size of list need to be the same
- for each element (el/equals ?var-el (.get ?var-list ?idx-el))"
[snippet-val]
(fn [snippet]
(internal-cf-list snippet snippet-val :samesize
(fn [var-list var-el idx-el]
`(el/equals ~var-el (.get ~var-list ~idx-el))))))
(defn
cf-primitive-exact
"Returns a function that will generate constraining conditions for the given primitive property value of a code snippet.
For Ekeko wrappers of primitive values (int/string/...):
(primitivevalue ?var-match)
(value-raw ?var-match <clojure-exp-evaluating-to-raw-value>))"
[snippet-ast]
(let [exp
(ast-primitive-as-expression (:value snippet-ast))]
(fn [snippet]
(let [var-match
(representation/snippet-var-for-node snippet snippet-ast)]
`((reification/primitivevalue ~var-match)
(reification/value-raw ~var-match ~exp))))))
(defn
cf-nil-exact
"Returns a function that will generate constraining conditions for the given primitive property value of a code snippet.
For Ekeko wrappers of Java null:
(nullvalue ?var-match)"
[snippet-ast]
(fn [snippet]
(let [var-match
(representation/snippet-var-for-node snippet snippet-ast)]
`((reification/nullvalue ~var-match)))))
(defn
cf-exact
[snippet-val]
(cond
(astnode/ast? snippet-val)
(cf-node-exact snippet-val)
(astnode/lstvalue? snippet-val)
(cf-list-exact snippet-val)
(astnode/primitivevalue? snippet-val)
(cf-primitive-exact snippet-val)
(astnode/nilvalue? snippet-val)
(cf-nil-exact snippet-val)))
;; Constraining Functions
;; Generalized
;; ----------------------
(defn
cf-list-contains-with-same-size
"Returns a function that will generate constraining conditions for the given property value of a code snippet.
Conditions : - size of list need to be the same
- for each element (el/contains ?var-list ?var-el)"
[snippet-val]
(fn [snippet]
(internal-cf-list snippet snippet-val :samesize
(fn [var-list var-el idx-el]
`(el/contains ~var-list ~var-el)))))
(defn
cf-list-contains
"Returns a function that will generate constraining conditions for the given property value of a code snippet.
Conditions : - size of list no need to be the same
- for each element (el/contains ?var-list ?var-el)"
[snippet-val]
(fn [snippet]
(internal-cf-list snippet snippet-val :notsamesize
(fn [var-list var-el idx-el]
`(el/contains ~var-list ~var-el)))))
(defn
internal-cf-list-contains-with-relative-order
"Returns constraining conditions of relative elements order for the given nodelist of a code snippet.
(succeeds (< (.indexOf alist el1) (.indexOf alist el2)))
(succeeds (< (.indexOf alist el2) (.indexOf alist el3))) ... "
[snippet var-list alist]
(if (empty? (next alist))
'()
(let [var-el1 (representation/snippet-var-for-node snippet (first alist))
var-el2 (representation/snippet-var-for-node snippet (fnext alist))]
(cons
`(el/succeeds (< (.indexOf ~var-list ~var-el1) (.indexOf ~var-list ~var-el2)))
(internal-cf-list-contains-with-relative-order snippet var-list (next alist))))))
(defn
cf-list-contains-with-relative-order
[snippet-val]
(fn [snippet]
(let [lst (representation/snippet-value-for-node snippet snippet-val)
var-match-raw (representation/snippet-var-for-node snippet lst)]
(concat
((cf-list-contains snippet-val) snippet)
(internal-cf-list-contains-with-relative-order snippet var-match-raw lst)))))
(defn
internal-cf-list-contains-with-repetition
"Returns constraining conditions of list having repetition elements for the given nodelist of a code snippet.
(fails (equals el1 el2))
(fails (equals el1 el3)) ... "
[snippet alist]
(if (empty? (next alist))
'()
(let [var-first-el (representation/snippet-var-for-node snippet (first alist))
element-conditions
(for [element (next alist)
:let [var-el (representation/snippet-var-for-node snippet element)]]
`(el/fails (el/equals ~var-first-el ~var-el)))]
(concat
`(~@element-conditions)
(internal-cf-list-contains-with-repetition snippet (next alist))))))
(defn
cf-list-contains-with-repetition
[snippet-val]
(fn [snippet]
(let [lst (representation/snippet-value-for-node snippet snippet-val)]
(concat
((cf-list-contains snippet-val) snippet)
(internal-cf-list-contains-with-repetition snippet lst)))))
(defn
cf-variable
"Returns a function that will generate a condition that will unify the match for the
given code snippet AST node with a user-provided logic variable:
(== ?uservar ?var-match)"
[snippet-ast]
(fn [snippet]
(let [var-match
(representation/snippet-var-for-node snippet snippet-ast)
var-userprovided
(representation/snippet-uservar-for-var snippet var-match)]
`((cl/== ~var-userprovided ~var-match)))))
(defn
cf-exact-with-variable
[snippet-val]
(fn [snippet]
(concat
((cf-node-exact snippet-val) snippet)
((cf-variable snippet-val) snippet))))
(defn
cf-subtype
"Returns a function that will generate constraining conditions for the given property value of a code snippet:
For ASTNode instances: ((type-subtypematch-logictype :kind-of-node ?var-for-node ?var-for-node-match)
(ast :kind-of-node ?var-for-node)
(has :property1 ?var-for-node ''primitive-valued-child-as-string''))
....
?var-for-node is new logic variable."
[snippet-ast]
(fn [snippet]
(let [snippet-keyw (astnode/ekeko-keyword-for-class-of snippet-ast)
snippet-properties (astnode/node-ekeko-properties snippet-ast)
var-match (representation/snippet-var-for-node snippet snippet-ast)
var-node (util/gen-lvar)
child-conditions
(for [[property-keyw retrievalf]
(seq snippet-properties)
:let [value (retrievalf)
var-value (representation/snippet-var-for-node snippet value)]]
`(reification/has ~property-keyw ~var-node ~var-value))]
`((cl/fresh [~var-node]
(reification/ast ~snippet-keyw ~var-node)
~@child-conditions
(runtime/type-relaxmatch-subtype ~snippet-keyw ~var-node ~var-match))))))
(defn
cf-variable-declaration-with-initializer
"Returns a function that will generate constraining conditions for the given property value of a code snippet:
For ASTNode instances: (assignment-relaxmatch-variable-declaration ?var-for-node-match ?var-left ?var-right)"
[snippet-ast]
(fn [snippet]
(let [var-match (representation/snippet-var-for-node snippet snippet-ast)
var-left (representation/snippet-var-for-node snippet (.getLeftHandSide (.getExpression snippet-ast)))
var-right (representation/snippet-var-for-node snippet (.getRightHandSide (.getExpression snippet-ast)))]
`((runtime/assignment-relaxmatch-variable-declaration ~var-match ~var-left ~var-right)))))
(defn
ast-conditions
"Returns a list of logic conditions that will retrieve matches for the given snippet-ast in snippet."
[snippet ast]
(defn
conditions
[ast-or-list]
(concat ((gf-minimalistic ast-or-list) snippet)
((cf-exact ast-or-list) snippet)))
(let [query (atom '())]
(representation/walk-jdt-node-of-snippet
snippet
ast
(fn [astval] (swap! query concat (conditions astval)))
(fn [lstval] (swap! query concat (conditions lstval)))
(fn [primval] (swap! query concat (conditions primval)))
(fn [nilval] (swap! query concat (conditions nilval))))
@query))
(defn
cf-negated
"Returns a function that will generate constraining conditions for the given property value of a code snippet:
For ASTNode instances: (fails (all (node-conditions) (child-conditions)))."
[snippet-ast]
(fn [snippet]
(let [conditions-of-ast (ast-conditions snippet snippet-ast)]
`((el/fails (cl/all ~@conditions-of-ast))))))
(defn
make-constraining-function
[type]
(cond
(= type :exact)
cf-exact
(= type :list-exact)
cf-list-exact
(= type :list-contains-with-same-size)
cf-list-contains-with-same-size
(= type :list-contains)
cf-list-contains
(= type :list-contains-with-relative-order)
cf-list-contains-with-relative-order
(= type :list-contains-with-repetition)
cf-list-contains-with-repetition
(= type :variable)
cf-variable
(= type :exact-with-variable)
cf-exact-with-variable
(= type :subtype)
cf-subtype
(= type :variable-declaration-with-initializer)
cf-variable-declaration-with-initializer
(= type :negated)
cf-negated
(= type :epsilon)
make-epsilon-function
:default
(throw (Exception. (str "Unknown constraining function type: " type)))))
(defn
ast-primitive-as-expression
"Returns the string representation of a primitive-valued JDT node (e.g., instances of Modifier.ModifierKeyword)."
[primitive]
;could dispatch on this as well
(cond (or (true? primitive) (false? primitive))
primitive
(number? primitive)
primitive
(instance? org.eclipse.jdt.core.dom.Modifier$ModifierKeyword primitive)
`(org.eclipse.jdt.core.dom.Modifier$ModifierKeyword/toKeyword ~(.toString primitive))
(instance? org.eclipse.jdt.core.dom.PrimitiveType$Code primitive)
`(org.eclipse.jdt.core.dom.PrimitiveType/toCode ~(.toString primitive))
(instance? org.eclipse.jdt.core.dom.Assignment$Operator primitive)
`(org.eclipse.jdt.core.dom.Assignment$Operator/toOperator ~(.toString primitive))
(instance? org.eclipse.jdt.core.dom.InfixExpression$Operator primitive)
`(org.eclipse.jdt.core.dom.InfixExpression$Operator/toOperator ~(.toString primitive))
(instance? org.eclipse.jdt.core.dom.PrefixExpression$Operator primitive)
`(org.eclipse.jdt.core.dom.PrefixExpression$Operator/toOperator ~(.toString primitive))
(nil? primitive)
(throw (Exception. (str "Encountered a null-valued property value that should have been wrapped by Ekeko.")))
:else (.toString primitive)))