@@ -175,14 +175,14 @@ object CustomDgraphQueries {
175
175
expr match {
176
176
case Ast .Or (x) =>
177
177
" ("
178
- + (for (k <- x.zipWithIndex)
178
+ + (for (k <- x.filter(v => ! v. isInstanceOf [ Ast . Null ]). zipWithIndex)
179
179
yield
180
180
if (k._2 == 0 ) expression(k._1)
181
181
else " && " + expression(k._1)).mkString
182
182
+ " )"
183
183
case Ast .And (x) =>
184
184
" ("
185
- + (for (k <- x.zipWithIndex)
185
+ + (for (k <- x.filter(v => ! v. isInstanceOf [ Ast . Null ]). zipWithIndex)
186
186
yield
187
187
if (k._2 == 0 ) expression(k._1)
188
188
else " || " + expression(k._1)).mkString
@@ -193,6 +193,8 @@ object CustomDgraphQueries {
193
193
Utils .snakeCaseToCamelCase(variable.name) + " IsBlank"
194
194
case Ast .Eq (variable) =>
195
195
Utils .snakeCaseToCamelCase(variable.name) + " IsBlank"
196
+ case Ast .Null (variable) =>
197
+ " false"
196
198
case _ =>
197
199
" ERROR"
198
200
}
@@ -256,13 +258,58 @@ object CustomDgraphQueries {
256
258
257
259
def emitRuleTemplate (name : String , rule : Rule ): Unit = {
258
260
261
+ case class VarMeta (astName : String ,
262
+ func: String ,
263
+ funcParam : Option [Function1 [String , String ]],
264
+ funcName : String ,
265
+ distance: Option [Integer ])
266
+
259
267
val vars = for (v <- rule.vars) yield v
268
+ var varsMeta = Map [String , List [VarMeta ]]()
269
+
270
+ vars.foreach(v => {
271
+ if (! varsMeta.contains(v)){
272
+ varsMeta += (v -> List [VarMeta ]())
273
+ }
274
+ })
275
+
276
+
277
+ var currentFuncIndex : Int = - 1
260
278
val text = rule.text
261
279
val expression : Ast .Expression = ParseRule .parse(text)
262
- val varsMap = vars.zipWithIndex
263
- .toMap[String , Int ]
264
- .map((k, i) => (k, (" A" .head + i).toChar.toString))
265
- var meta = Map [String , (String , Option [Integer ])]()
280
+
281
+
282
+ def addMeta (v : String ,
283
+ astName : String ,
284
+ func: String ,
285
+ funcParam : Option [Function1 [String , String ]],
286
+ distance: Option [Integer ]): String = {
287
+
288
+ if (! varsMeta.contains(v)){
289
+ varsMeta += (v -> List [VarMeta ]())
290
+ }
291
+
292
+ val existingMeta = varsMeta(v).find(m => {
293
+ m.astName == astName &&
294
+ m.func == func &&
295
+ m.funcParam.toString == funcParam.toString &&
296
+ m.distance.toString == distance.toString
297
+ })
298
+
299
+ if (existingMeta.isDefined){
300
+ return existingMeta.get.funcName
301
+ }
302
+ else {
303
+ val newMeta = VarMeta (astName, func, funcParam, getFuncIndex(), distance)
304
+ varsMeta = varsMeta + (v -> (varsMeta(v) :+ newMeta ))
305
+ return newMeta.funcName
306
+ }
307
+ }
308
+
309
+ def getFuncIndex (): String = {
310
+ currentFuncIndex = currentFuncIndex + 1
311
+ return (" A" .head + currentFuncIndex).toChar.toString
312
+ }
266
313
267
314
def main_func (expression : Ast .Expression ): String = {
268
315
expression match {
@@ -283,25 +330,41 @@ object CustomDgraphQueries {
283
330
case Ast .Not (x) =>
284
331
" NOT (" + main_func(x) + " )"
285
332
case Ast .Match (variable, distance) =>
286
- meta += (variable.name -> ( " match" , Option (distance) ))
287
- " uid(" + variable.name + " )"
333
+ val uidName = addMeta (variable.name, " match " , " match" , None , Option (distance))
334
+ " uid(" + uidName + " )"
288
335
case Ast .Eq (variable) =>
289
- meta += (variable.name -> (" eq" , None ))
290
- " uid(" + variable.name + " )"
336
+ val uidName = addMeta(variable.name, " eq" , " eq" , None , None )
337
+ " uid(" + uidName + " )"
338
+ case Ast .Null (variable) =>
339
+ val uidName = addMeta(variable.name, " null" , " eq" , Option (new Function1 [String , String ] {
340
+ def apply (x : String ): String = " \"\" "
341
+ }), None )
342
+ " uid(" + uidName + " )"
291
343
case _ =>
292
344
" ERROR"
293
345
}
294
346
}
295
347
296
- def createScalerFunc (): Unit = {
297
- vars.foreach(v => {
298
- val fn = meta(v)._1
299
- writer.println(
300
- s """ ${" " * 12 }all(func:type(GoldenRecord)) @filter( $fn(GoldenRecord. $v, $$ $v${
301
- if (meta(v)._2.isDefined) " , " + meta(v)._2.get
348
+ def getFilterParams (v : String , metaInfo : VarMeta ): String = {
349
+ if (metaInfo.funcParam.isDefined) {
350
+ return metaInfo.funcParam.get.apply(v)
351
+ }
352
+
353
+ return s """
354
+ $$ $v${
355
+ if (metaInfo.distance.isDefined) " , " + metaInfo.distance.get
302
356
else
303
357
" "
304
- })) {
358
+ }
359
+ """
360
+ }
361
+
362
+ def createScalerFunc (): Unit = {
363
+ varsMeta.foreach((v, mL) => {
364
+ val m = mL(0 )
365
+ val fn = m.func
366
+ writer.println(
367
+ s """ ${" " * 12 }all(func:type(GoldenRecord)) @filter( $fn(GoldenRecord. $v, ${getFilterParams(v, m)})) {
305
368
| ${" " * 15 }uid
306
369
| ${" " * 15 }GoldenRecord.source_id {
307
370
| ${" " * 18 }uid
@@ -320,16 +383,17 @@ object CustomDgraphQueries {
320
383
}
321
384
322
385
def createFilterFunc (all_func_str : String ): Unit = {
323
- vars .foreach(v => {
324
- val fn = meta(v)._1
325
- writer.println(
326
- s """ ${ " " * 12 } var(func:type(GoldenRecord)) @filter( $fn (GoldenRecord. $v , $$ $v${
327
- if (meta(v)._2.isDefined) " , " + meta(v)._2.get else " "
328
- } )) {
329
- | ${" " * 15 }${varsMap(v)} as uid
330
- | ${ " " * 12 } } """ .stripMargin
331
- )
386
+ varsMeta .foreach((v, mL) => {
387
+ mL.foreach(m => {
388
+ val fn = m.func
389
+ writer.println(
390
+ s """ ${ " " * 12 } var(func:type(GoldenRecord)) @filter( $fn (GoldenRecord. $v , ${getFilterParams(v, m)} )) {
391
+ | ${ " " * 15 }${m.funcName} as uid
392
+ | ${" " * 12 } } """ .stripMargin
393
+ )
394
+ } )
332
395
})
396
+
333
397
writer.println(s """ ${" " * 12 }all(func:type(GoldenRecord)) @filter ${
334
398
if (all_func_str.startsWith(" (" )) " " else " ("
335
399
}$all_func_str${
@@ -351,9 +415,6 @@ object CustomDgraphQueries {
351
415
}
352
416
353
417
var all_func_str = main_func(expression)
354
- varsMap.foreach((k, v) => {
355
- all_func_str = all_func_str.replace(" uid(" + k + " )" , " uid(" + v + " )" )
356
- })
357
418
358
419
writer.print(
359
420
s """ ${" " * 3 }private static final String ${name.toUpperCase} =
@@ -369,7 +430,7 @@ object CustomDgraphQueries {
369
430
})
370
431
writer.println(" ) {" )
371
432
372
- if (vars.length == 1 )
433
+ if (varsMeta.size == 1 && varsMeta.values.headOption.get.size < 2 )
373
434
createScalerFunc()
374
435
else
375
436
createFilterFunc(all_func_str)
0 commit comments