Skip to content
Snippets Groups Projects
Commit 46a5bdb9 authored by sivan's avatar sivan
Browse files

fix aql conversion json format

parent 286ca322
No related branches found
No related tags found
No related merge requests found
......@@ -6,7 +6,6 @@ import (
"log"
"query-service/internal/aql"
"query-service/internal/errorhandler"
"query-service/internal/messagequeue"
"query-service/internal/redisclient"
"query-service/internal/request"
......@@ -18,17 +17,21 @@ import (
var producer alice.Producer
func main() {
exchangeID := "query-requests"
routingKey := "aql-user-request"
// exchangeID := "query-requests"
// routingKey := "aql-user-request"
broker := messagequeue.Create()
messagequeue.StartConsumer(broker, &exchangeID, &routingKey, onMessageReceived)
// broker := messagequeue.Create()
// messagequeue.StartConsumer(broker, &exchangeID, &routingKey, onMessageReceived)
producer = messagequeue.StartProducer(broker)
// producer = messagequeue.StartProducer(broker)
redisclient.Start()
// redisclient.Start()
msg := []byte(`{"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":[]}]}`)
aqlQuery, _ := aql.ConvertJSONToAQL(&msg)
select {}
fmt.Println(*aqlQuery)
// select {}
}
......
......
......@@ -6,6 +6,80 @@ import (
"strconv"
)
/*
// 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":[
]
}
]
}
&{{[0] [0]} [] [{Flight 0 -1 {1 3} []}]}
*/
// Constraint datatypes
// text MatchTypes: exact/contains/startswith/endswith
// number MatchTypes: GT/LT/EQ
......@@ -16,7 +90,7 @@ import (
// Struct used for JSON conversion
type parsedJSON struct {
Return returnStruct
Nodes []nodeStruct
Entities []entityStruct
Relations []relationStruct
}
......@@ -25,16 +99,16 @@ type returnStruct struct {
Relations []int
}
type nodeStruct struct {
NodeType string
Constraints map[string]constraintStruct
type entityStruct struct {
Type string
Constraints []constraintStruct
}
type relationStruct struct {
RelationType string
NodeFrom int
NodeTo int
Type string
EntityFrom int
EntityTo int
Depth searchDepthStruct
Constraints map[string]constraintStruct
Constraints []constraintStruct
}
type searchDepthStruct struct {
Min int
......@@ -42,6 +116,7 @@ type searchDepthStruct struct {
}
type constraintStruct struct {
Attribute string
Value string
DataType string
MatchType string
......@@ -61,16 +136,16 @@ func ConvertJSONToAQL(jsonMsg *[]byte) (*string, error) {
//relations koppelen ze samen
//return statement
result := createAllNodesQuery(jsonStruct.Return.Entities, jsonStruct.Nodes)
result := createAllNodesQuery(jsonStruct.Return.Entities, jsonStruct.Entities)
return result, nil
}
func createAllNodesQuery(returnEntitiesIndices []int, nodes []nodeStruct) *string {
func createAllNodesQuery(returnEntitiesIndices []int, entities []entityStruct) *string {
var result string
for nodeIndex := range returnEntitiesIndices {
nodeID := fmt.Sprintf("n%s", strconv.Itoa(nodeIndex))
for _, entityIndex := range returnEntitiesIndices {
nodeID := fmt.Sprintf("n%s", strconv.Itoa(entityIndex))
nodeQueryString := *createNodeQuery(&nodes[nodeIndex], nodeID)
nodeQueryString := *createNodeQuery(&entities[entityIndex], nodeID)
result += fmt.Sprintf(" \n%s", nodeQueryString)
}
......@@ -79,7 +154,7 @@ func createAllNodesQuery(returnEntitiesIndices []int, nodes []nodeStruct) *strin
}
// createNodeQuery converts the node part of the json to a subquery
func createNodeQuery(node *nodeStruct, name string) *string {
func createNodeQuery(node *entityStruct, name string) *string {
/*
LET alices = (
FOR x IN female
......@@ -99,13 +174,13 @@ func createNodeQuery(node *nodeStruct, name string) *string {
*/
letStatement := fmt.Sprintf("LET %s = (\n", name)
forStatement := fmt.Sprintf("\tFOR x IN %s \n", node.NodeType)
forStatement := fmt.Sprintf("\tFOR x IN %s \n", node.Type)
// Generate all constraints as FILTER statements
first := true
filter := "\tFILTER "
for key, constraint := range node.Constraints {
constraint := createQueryConstraint(&constraint, key)
for _, constraint := range node.Constraints {
constraint := createQueryConstraint(&constraint)
if first {
filter += fmt.Sprintf("\t%s ", *constraint)
......@@ -130,55 +205,55 @@ func createNodeQuery(node *nodeStruct, name string) *string {
// bool MatchTypes: EQ/NEQ
// createQueryConstraint creates a sinlge line of AQL filtering/constraint
func createQueryConstraint(con *constraintStruct, key string) *string {
func createQueryConstraint(constraint *constraintStruct) *string {
//FILTER x.{CONSTRAINT[0]} {{CONSTRAINT[0]}.MATCHTYPE} {CONSTRAINT[0].VALUE}
// name mtch val + dataType
// name match value + dataType
var (
mtch string
val string
match string
value string
line string
)
//Wicked switches letsgo
switch con.DataType {
switch constraint.DataType {
case "text":
val = fmt.Sprintf("\"%s\"", con.Value)
switch con.MatchType {
value = fmt.Sprintf("\"%s\"", constraint.Value)
switch constraint.MatchType {
case "contains":
mtch = "IN"
match = "IN"
case "startswith":
mtch = "LIKE"
val = fmt.Sprintf("\"%s%%\"", con.Value)
match = "LIKE"
value = fmt.Sprintf("\"%s%%\"", constraint.Value)
case "endswith":
mtch = "LIKE"
val = fmt.Sprintf("\"_%s\"", con.Value)
match = "LIKE"
value = fmt.Sprintf("\"_%s\"", constraint.Value)
default: //exact
mtch = "=="
match = "=="
}
case "number":
val = con.Value
switch con.MatchType {
value = constraint.Value
switch constraint.MatchType {
case "GT":
mtch = ">"
match = ">"
case "LT":
mtch = "<"
match = "<"
case "GET":
mtch = ">="
match = ">="
case "LET":
mtch = "<="
match = "<="
default: //EQ
mtch = "=="
match = "=="
}
default: /*bool*/
val = con.Value
switch con.MatchType {
value = constraint.Value
switch constraint.MatchType {
case "NEQ":
mtch = "!="
match = "!="
default: //EQ
mtch = "=="
match = "=="
}
}
line = fmt.Sprintf("x.%s %s %s", key, mtch, val)
line = fmt.Sprintf("x.%s %s %s", constraint.Attribute, match, value)
return &line
}
......
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment