Skip to content
Snippets Groups Projects
Commit de2a4004 authored by Geurtjens,D. (Douwe Geurtjens)'s avatar Geurtjens,D. (Douwe Geurtjens)
Browse files

Merge branch 'Different-JSON-Format' of...

Merge branch 'Different-JSON-Format' of https://git.science.uu.nl/GravenvanPolaris/query-conversion into Different-JSON-Format

# Conflicts:
#	aql/convertQuery.go
#	realtest.json
parents 0e17ac0a 41cf4778
No related branches found
No related tags found
1 merge request!1Big merge
...@@ -79,39 +79,42 @@ createQuery generates a query based on the json file provided ...@@ -79,39 +79,42 @@ createQuery generates a query based on the json file provided
Return: *string, a string containing the corresponding AQL query and an error Return: *string, a string containing the corresponding AQL query and an error
*/ */
func createQuery(JSONQuery *entity.IncomingQueryJSON, tree []entity.Tree, topNode entity.QueryEntityStruct) *string { func createQuery(JSONQuery *entity.IncomingQueryJSON, tree []entity.Tree, topNode entity.QueryEntityStruct) *string {
output := createLetFor("result", fmt.Sprintf("e_%v", topNode.ID), topNode.Name) output := createLetFor("result", fmt.Sprintf("e_%v", topNode.ID), topNode.Name, 0)
for constraint := range topNode.Constraints { for constraint := range topNode.Constraints {
output += createFilter(topNode.Constraints[constraint], fmt.Sprintf("e_%v", topNode.ID)) output += createFilter(topNode.Constraints[constraint], fmt.Sprintf("e_%v", topNode.ID))
} }
subQuery, subName := createQueryRecurse(JSONQuery, tree, 0, topNode) subQuery, subName := createQueryRecurse(JSONQuery, tree, 0, topNode, 1)
subNames := []string{subName} subNames := []string{subName}
output += subQuery output += subQuery
output += createZeroFilter(append(subNames, fmt.Sprintf("e_%v", topNode.ID))) output += createZeroFilter(append(subNames, fmt.Sprintf("e_%v", topNode.ID)))
output += createReturn(fmt.Sprintf("e_%v", topNode.ID), "", subNames) output += createReturn(fmt.Sprintf("e_%v", topNode.ID), "", subNames)
output += ")\n"
output += "let nodes = union_distinct(flatten(result[**].nodes),[])\nlet edges = union_distinct(flatten(result[**].rel),[])\nreturn {\"vertices\":nodes,\"edges\":edges}" output += "let nodes = union_distinct(flatten(result[**].nodes),[])\nlet edges = union_distinct(flatten(result[**].rel),[])\nreturn {\"vertices\":nodes,\"edges\":edges}"
return &output return &output
} }
func createQueryRecurse(JSONQuery *entity.IncomingQueryJSON, tree []entity.Tree, currentindex int, topNode entity.QueryEntityStruct) (string, string) { func createQueryRecurse(JSONQuery *entity.IncomingQueryJSON, tree []entity.Tree, currentindex int, topNode entity.QueryEntityStruct, indentindex int) (string, string) {
currentTree := tree[currentindex] currentTree := tree[currentindex]
newNode := getTreeNewNode(currentTree, tree, topNode) newNode := getTreeNewNode(currentTree, tree, topNode)
output := createLetFor(fmt.Sprintf("e%v", newNode.ID), fmt.Sprintf("e_%v", newNode.ID), newNode.Name) output := ""
output += fmt.Sprintf("FOR r%v IN %v", currentTree.Self.Rel.ID, currentTree.Self.Rel.Name) output += fixIndent(createLetFor(fmt.Sprintf("e%v", newNode.ID), fmt.Sprintf("e_%v", newNode.ID), newNode.Name, indentindex), indentindex)
output += fixIndent(fmt.Sprintf("\tFOR r%v IN %v\n", currentTree.Self.Rel.ID, currentTree.Self.Rel.Name), indentindex)
for constraint := range newNode.Constraints { for constraint := range newNode.Constraints {
output += createFilter(newNode.Constraints[constraint], fmt.Sprintf("e_%v", newNode.ID)) output += fixIndent(createFilter(newNode.Constraints[constraint], fmt.Sprintf("e_%v", newNode.ID)), indentindex)
} }
for constraint := range currentTree.Self.Rel.Constraints { for constraint := range currentTree.Self.Rel.Constraints {
output += createFilter(currentTree.Self.Rel.Constraints[constraint], fmt.Sprintf("r%v", currentTree.Self.Rel.ID)) output += fixIndent(createFilter(currentTree.Self.Rel.Constraints[constraint], fmt.Sprintf("r%v", currentTree.Self.Rel.ID)), indentindex)
} }
output += fmt.Sprintf("FILTER r%v._from == e_%v._id AND r%v._to == e_%v._id", currentTree.Self.Rel.ID, currentTree.Self.FromNode.ID, currentTree.Self.Rel.ID, currentTree.Self.ToNode.ID) output += fixIndent(fmt.Sprintf("\tFILTER r%v._from == e_%v._id AND r%v._to == e_%v._id\n", currentTree.Self.Rel.ID, currentTree.Self.FromNode.ID, currentTree.Self.Rel.ID, currentTree.Self.ToNode.ID), indentindex)
var subNames []string var subNames []string
for i := range currentTree.Children { for i := range currentTree.Children {
subQuery, subName := createQueryRecurse(JSONQuery, tree, currentTree.Children[i], topNode) subQuery, subName := createQueryRecurse(JSONQuery, tree, currentTree.Children[i], topNode, indentindex+1)
output += subQuery output += subQuery
subNames = append(subNames, subName) subNames = append(subNames, subName)
} }
output += createZeroFilter(append(subNames, fmt.Sprintf("e_%v", newNode.ID), fmt.Sprintf("r%v", currentTree.Self.Rel.ID))) output += fixIndent(createZeroFilter(append(subNames, fmt.Sprintf("e_%v", newNode.ID), fmt.Sprintf("r%v", currentTree.Self.Rel.ID))), indentindex)
output += createReturn(fmt.Sprintf("e_%v", newNode.ID), fmt.Sprintf("r%v", currentTree.Self.Rel.ID), subNames) output += fixIndent(createReturn(fmt.Sprintf("e_%v", newNode.ID), fmt.Sprintf("r%v", currentTree.Self.Rel.ID), subNames), indentindex)
output += fixIndent(")\n", indentindex)
return output, fmt.Sprintf("e%v", newNode.ID) return output, fmt.Sprintf("e%v", newNode.ID)
} }
...@@ -129,27 +132,52 @@ func getTreeNewNode(currentTree entity.Tree, tree []entity.Tree, topNode entity. ...@@ -129,27 +132,52 @@ func getTreeNewNode(currentTree entity.Tree, tree []entity.Tree, topNode entity.
} }
} }
func createLetFor(variableName string, forName string, enumerableName string) string { func createLetFor(variableName string, forName string, enumerableName string, indentindex int) string {
return "LET " + variableName + " = (\n\tFOR " + forName + " IN " + enumerableName + "\n" output := "LET " + variableName + " = (\n"
for i := 0; i < indentindex; i++ {
output += "\t"
}
output += "\tFOR " + forName + " IN " + enumerableName + "\n"
return output
} }
func createFilter(constraint entity.QueryConstraintStruct, filtered string) string { func createFilter(constraint entity.QueryConstraintStruct, filtered string) string {
return "\tFILTER + " + filtered + constraint.Attribute + " " + wordsToLogicalSign(constraint) + " " + constraint.Value + " \n" output := "\tFILTER " + filtered + "." + constraint.Attribute + " " + wordsToLogicalSign(constraint)
if constraint.DataType == "string" {
output += " \""
} else {
output += " "
}
if constraint.MatchType == "contains" {
output += "%"
}
output += constraint.Value
if constraint.MatchType == "contains" {
output += "%"
}
if constraint.DataType == "string" {
output += "\" "
} else {
output += " "
}
output += " \n"
return output
} }
func createZeroFilter(subNames []string) string { func createZeroFilter(subNames []string) string {
output := "FILTER" output := "\tFILTER"
for i := range subNames { for i := range subNames {
output += fmt.Sprintf(" length(%v) != 0", subNames[i]) output += fmt.Sprintf(" length(%v) != 0", subNames[i])
if i < len(subNames)-1 { if i < len(subNames)-1 {
output += " AND" output += " AND"
} }
} }
output += "\n"
return output return output
} }
func createReturn(nodeName string, relName string, subNames []string) string { func createReturn(nodeName string, relName string, subNames []string) string {
output := "RETURN {\"nodes\":union_distinct(" output := "\tRETURN {\"nodes\":union_distinct("
for i := range subNames { for i := range subNames {
output += fmt.Sprintf("flatten(%v[**].nodes), ", subNames[i]) output += fmt.Sprintf("flatten(%v[**].nodes), ", subNames[i])
} }
...@@ -165,7 +193,7 @@ func createReturn(nodeName string, relName string, subNames []string) string { ...@@ -165,7 +193,7 @@ func createReturn(nodeName string, relName string, subNames []string) string {
if len(subNames) == 0 { if len(subNames) == 0 {
output += ", []" output += ", []"
} }
output += ")}\n)\n" output += ")}\n"
return output return output
} }
...@@ -208,3 +236,12 @@ func wordsToLogicalSign(element entity.QueryConstraintStruct) string { ...@@ -208,3 +236,12 @@ func wordsToLogicalSign(element entity.QueryConstraintStruct) string {
} }
return match return match
} }
func fixIndent(input string, indentCount int) string {
output := ""
for i := 0; i < indentCount; i++ {
output += "\t"
}
output += input
return output
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment