Newer
Older
// Service is a model for the convertquery use case
type Service struct {
}
// NewService creates a new convertquery service
func NewService() *Service {
return &Service{}
}
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
/*
// Query format for exporting to JSON
export type JSONFormat = {
Return: {
Entities: number[];
Relations: number[];
};
Entities: Entity[];
Relations: Relation[];
};
type Entity = {
Type: string;
Constraints: Constraint[];
};
type Relation = {
Type: string;
EntityFrom: number;
EntityTo: number;
Depth: { min: number; max: number };
Constraints: Constraint[];
};
type Constraint = {
Attribute: string;
Value: string;
// Constraint datatypes
// text MatchTypes: exact/contains/startswith/endswith
// number MatchTypes: GT/LT/EQ
// bool MatchTypes: EQ/NEQ
DataType: string;
MatchType: string;
};
{
"Return":{
"Entities":[
0
],
"Relations":[
0
]
},
"Entities":[
{
"Type":"Airport",
"Constraints":[
{
"Attribute":"month",
"Value":"8",
"DataType":"text",
"MatchType":"exact"
}
]
}
],
"Relations":[
{
"Type":"Flight",
"Depth":{
"min":1,
"max":3
},
"EntityFrom":0,
"EntityTo":-1,
"Constraints":[
]
}
]
}
*/
// Constraint datatypes
// text MatchTypes: exact/contains/startswith/endswith
// number MatchTypes: GT/LT/EQ
// bool MatchTypes: EQ/NEQ
// Ranges dus tussen 10 half 5 bijv.
// Struct used for JSON conversion
type parsedJSON struct {
Return returnStruct
Relations []relationStruct
}
type returnStruct struct {
Entities []int
Relations []int
}
type entityStruct struct {
Type string
Constraints []constraintStruct
Type string
EntityFrom int
EntityTo int
Depth searchDepthStruct
Constraints []constraintStruct
}
type searchDepthStruct struct {
Min int
Max int
}
type constraintStruct struct {
// ConvertQuery converts a json string to an AQL query
func (s *Service) ConvertQuery(jsonMsg *[]byte) (*string, error) {
jsonStruct, err := convertJSONToStruct(jsonMsg)
if err != nil {
fmt.Println(err)
return nil, err
}
//Per node query
// per constraint
//relations koppelen ze samen
//return statement
var result *string
if len(jsonStruct.Return.Relations) > 0 {
result = createEdgeQuery(jsonStruct, "e0")
} else {
result = createAllNodesQuery(jsonStruct.Return.Entities, jsonStruct.Entities)
}
// createAllNodesQuery creates node queries in AQL
func createAllNodesQuery(returnEntitiesIndices []int, entities []entityStruct) *string {
var (
result string
ret string
)
for _, entityIndex := range returnEntitiesIndices {
nodeID := fmt.Sprintf("n%s", strconv.Itoa(entityIndex))
nodeQueryString := *createNodeQuery(&entities[entityIndex], nodeID)
ret += "\t" + nodeID + ":" + nodeID + ",\n"
ret = strings.TrimSuffix(ret, ",\n")
ret += "\n}"
return &result
}
// createNodeQuery converts the node part of the json to a subquery
func createNodeQuery(node *entityStruct, name string) *string {
/*
LET alices = (
FOR x IN female
FILTER x.name == "Alice" AND x.birth_year > 1997
RETURN x
)
NAAR -->
LET {NAAM**} = (
FOR x IN {NODETYPE}
FILTER x.{CONSTRAINT[0]} {{CONSTRAINT[0]}.MATCHTYPE} {CONSTRAINT[0].VALUE}
AND x.{CONSTRAINT[1]} {{CONSTRAINT[1]}.MATCHTYPE} {CONSTRAINT[1].VALUE}
RETURN x
)
*/
letStatement := fmt.Sprintf("LET %s = (\n", name)
forStatement := fmt.Sprintf("\tFOR x IN %s \n", node.Type)
// Generate all constraints as FILTER statements
first := true
filter := "\tFILTER "
for _, constraint := range node.Constraints {
constraint := createQueryConstraint(&constraint)
if first {
filter += fmt.Sprintf("\t%s ", *constraint)
first = false
} else {
filter += fmt.Sprintf("AND\n\t\t%s", *constraint)
}
}
returnStatement := "\n\tRETURN x\n)"
// Concatenate all the statements
result := letStatement + forStatement + filter + returnStatement
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
/*
{
"Return": {
"Entities": [
0,
1
],
"Relations": [
0
]
},
"Entities": [
{
"Type": "airports",
"Constraints": [
{
"Attribute": "country",
"Value": "USA",
"DataType": "text",
"MatchType": "exact"
}
]
},
{
"Type": "airports",
"Constraints": [
{
"Attribute": "city",
"Value": "New York",
"DataType": "text",
"MatchType": "exact"
},
{
"Attribute": "vip",
"Value": "true",
"DataType": "bool",
"MatchType": "exact"
}
]
}
],
"Relations": [
{
"Type": "flights",
"Depth": {
"min": 1,
"max": 1
},
"EntityFrom": 0,
"EntityTo": 1,
"Constraints": [
{
"Attribute": "Month",
"Value": "1",
"DataType": "number",
"MatchType": "exact"
},
{
"Attribute": "Day",
"Value": "15",
"DataType": "number",
"MatchType": "exact"
}
]
}
]
}
LET n0 = (
FOR n IN airports
FILTER n.country == "USA"
RETURN n
)
LET n1 = (
FOR n IN airports
FILTER n.city == "New York" AND
n.vip == true
RETURN n
)
LET e0 = (
FOR a IN n0
FOR v, e, p IN 1..1 OUTBOUND a flights
FILTER v in n1
FILTER p.edges[0].Month == 1 AND
p.edges[0].Day == 15
RETURN { vertices: p.vertices[*], edges: p.edges[*] }
)
RETURN e0
*/
// createEdgeQuery creates an aql query for relations
func createEdgeQuery(q *parsedJSON, name string) *string {
//Creation of n0 n1 let statements
query := ""
entityList := q.Entities
for i, x := range entityList {
query += "\n"
n := fmt.Sprintf("n%v", i)
query += *createNodeQuery(&x, n)
}
query += *createLetStatementForRelation(&q.Relations[0], name) //DIT GAAN WE EVEN NEGEREN
//ZEER GEHARDCODED ivm demo
// finalReturn := "\nRETURN { "
// for x := range q.Return.Entities {
// finalReturn += fmt.Sprintf("n%v, ", x)
// }
// finalReturn += fmt.Sprintf("e%v }", q.Return.Relations[0])
// query += finalReturn
query += "\nRETURN {e0:e0}"
// createLetStatementForRelation creates the relation query
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
func createLetStatementForRelation(relation *relationStruct, name string) *string {
letStatement := fmt.Sprintf("\nLET %s = (\n", name)
upperForStatement := fmt.Sprintf("\tFOR x IN n%v \n", relation.EntityFrom)
edgeForStatement := fmt.Sprintf("\tFOR v, e, p IN %v..%v OUTBOUND x %s \n", relation.Depth.Min, relation.Depth.Max, relation.Type)
upperFilter := fmt.Sprintf("\tFILTER v IN n%v \n", relation.EntityTo)
// Generate all constraints as FILTER statements
first := true
nestedFilter := "\tFILTER "
for _, constraint := range relation.Constraints {
constraint := createEdgeQueryConstraint(&constraint)
if first {
nestedFilter += fmt.Sprintf("\t%s ", *constraint)
first = false
} else {
nestedFilter += fmt.Sprintf("AND\n\t\t%s", *constraint)
}
}
returnStatement := "\n\tLIMIT 100\n\tRETURN { vertices: p.vertices[*], edges: p.edges[*] }\n)"
// Concatenate all the statements
result := letStatement + upperForStatement + edgeForStatement + upperFilter + nestedFilter + returnStatement
return &result
}
// Constraint datatypes
// text MatchTypes: exact/contains/startswith/endswith
// number MatchTypes: GT/LT/EQ
// bool MatchTypes: EQ/NEQ
// createQueryConstraint creates a sinlge line of AQL filtering/constraint
func createQueryConstraint(constraint *constraintStruct) *string {
//FILTER x.{CONSTRAINT[0]} {{CONSTRAINT[0]}.MATCHTYPE} {CONSTRAINT[0].VALUE}
value = fmt.Sprintf("\"%s\"", constraint.Value)
switch constraint.MatchType {
match = "LIKE"
value = fmt.Sprintf("\"%s%%\"", constraint.Value)
match = "LIKE"
value = fmt.Sprintf("\"_%s\"", constraint.Value)
value = constraint.Value
switch constraint.MatchType {
value = constraint.Value
switch constraint.MatchType {
line = fmt.Sprintf("x.%s %s %s", constraint.Attribute, match, value)
// createEdgeQueryConstraint translates the constraints of an edge query to aql
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
func createEdgeQueryConstraint(constraint *constraintStruct) *string {
//FILTER x.{CONSTRAINT[0]} {{CONSTRAINT[0]}.MATCHTYPE} {CONSTRAINT[0].VALUE}
// name match value + dataType
var (
match string
value string
line string
)
//Wicked switches letsgo
switch constraint.DataType {
case "text":
value = fmt.Sprintf("\"%s\"", constraint.Value)
switch constraint.MatchType {
case "contains":
match = "IN"
case "startswith":
match = "LIKE"
value = fmt.Sprintf("\"%s%%\"", constraint.Value)
case "endswith":
match = "LIKE"
value = fmt.Sprintf("\"_%s\"", constraint.Value)
default: //exact
match = "=="
}
case "number":
value = constraint.Value
switch constraint.MatchType {
case "GT":
match = ">"
case "LT":
match = "<"
case "GET":
match = ">="
case "LET":
match = "<="
default: //EQ
match = "=="
}
default: /*bool*/
value = constraint.Value
switch constraint.MatchType {
case "NEQ":
match = "!="
default: //EQ
match = "=="
}
}
line = fmt.Sprintf("p.edges[*].%s ALL %s %s", constraint.Attribute, match, value)
return &line
}
// convertJSONToStruct takes the json as byte array and converts it to a struct
func convertJSONToStruct(jsonMsg *[]byte) (*parsedJSON, error) {
jsonStruct := parsedJSON{}
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
if err != nil {
return nil, err
}
return &jsonStruct, nil
}
/*
desired output
WITH male
FOR a1 IN female
FILTER a1.name == "Alice"
(FOR r1v,r1e,r1p IN 1..1 OUTBOUND a1 relation
FILTER r1v.name == "Bob")
OR
(FOR r2v,r2e,r2p IN 1..1 OUTBOUND a1 relation
FILTER r2v.name == "Martin" && r2v.hasdog == true)
// constraints for Bob
OR r1v.name == "Martin"
FILTER r1e.type == "married"
FOR r2v,r2e,r2p IN 1..1 OUTBOUND r1v relation
FILTER r2v.name == "Figo"
FILTER r2e.type == "has_dog"
RETURN {a1,r1v,r2v}
of
LET alices = (
FOR x IN female
FILTER x.name == "Alice"
RETURN x
)
LET bobs = (
FOR x IN male
FILTER x.name == "Bob"
RETURN x
)
LET alices = (
FOR alice IN alices
FOR r1v,r1e,r1p IN 1..1 OUTBOUND alice relation
FILTER r1v IN bobs AND r1e.type == "married"
RETURN {alice}
)
LET alices = (
FOR alice IN alices
FOR r1v,r1e,r1p IN 1..1 OUTBOUND alice relation
FILTER r1v IN marting AND r1e.type == "friend"
RETURN {alice}
)
FOR a2 IN male
FILTER a2.name == "Bob"
FOR r1v,r1e,r1p IN 1..1 OUTBOUND a1 relation
FILTER r1v._id == a2._id
FILTER r1e.type == "married"
RETURN {a1,a2,r1e}
*/
/*
{
"NodeType": "female",
"Constraints":
{
"name": { "Value": "Alice", "DataType": "text", "MatchType": "exact" },
"birth_year": { "Value": "1997", "DataType": "number", "MatchType": "GT" }
}
}
NAAR -->
LET alices = (
FOR x IN female
FILTER x.name == "Alice" AND x.birth_year > 1997
RETURN x
)
*/
//Nog een manier vinden om namen te syncen over de queries heen **
// func forGlory() *constraintStruct {
// var yeet constraintStruct
// yeet = constraintStruct{
// ConstraintName: "name",
// Value: "Alice",
// MatchType: "exact",
// DataType: "text",
// }
// return &yeet
// }
// func alsoForGlory() {
// con := forGlory()
// toPrint := createQueryConstraint(*con)
// fmt.Println(*toPrint)
// }