Added logical and conditional operators

This commit is contained in:
Luca Frosini 2021-10-20 12:05:34 +02:00
parent 0d5e55472c
commit e64e3b9dc4
13 changed files with 123 additions and 49 deletions

View File

@ -6,6 +6,7 @@ import java.util.Set;
import org.gcube.com.fasterxml.jackson.databind.JsonNode; import org.gcube.com.fasterxml.jackson.databind.JsonNode;
import org.gcube.com.fasterxml.jackson.databind.ObjectMapper; import org.gcube.com.fasterxml.jackson.databind.ObjectMapper;
import org.gcube.com.fasterxml.jackson.databind.node.ArrayNode;
import org.gcube.com.fasterxml.jackson.databind.node.ObjectNode; import org.gcube.com.fasterxml.jackson.databind.node.ObjectNode;
import org.gcube.informationsystem.base.reference.AccessType; import org.gcube.informationsystem.base.reference.AccessType;
import org.gcube.informationsystem.base.reference.Direction; import org.gcube.informationsystem.base.reference.Direction;
@ -74,88 +75,131 @@ public abstract class JsonQueryERElement {
public abstract StringBuffer analize(StringBuffer stringBuffer) throws SchemaNotFoundException, InvalidQueryException, SchemaException, ResourceRegistryException; public abstract StringBuffer analize(StringBuffer stringBuffer) throws SchemaNotFoundException, InvalidQueryException, SchemaException, ResourceRegistryException;
protected StringBuffer addConstraint(JsonNode jsonNode, QueryLogicalOperator queryLogicalOperator, String fieldNamePrefix) throws InvalidQueryException { protected StringBuffer addConstraints(JsonNode jsonNode, QueryLogicalOperator queryLogicalOperator, String fieldNamePrefix) throws InvalidQueryException {
StringBuffer stringBuffer = new StringBuffer(); StringBuffer stringBuffer = new StringBuffer();
ObjectNode objectNode = jsonNode.deepCopy();
objectNode.remove(fieldNamesToRemove);
Iterator<String> iterator = objectNode.fieldNames();
if(queryLogicalOperator==null) { if(queryLogicalOperator==null) {
queryLogicalOperator = QueryLogicalOperator.AND; queryLogicalOperator = QueryLogicalOperator.AND;
} }
JsonNode copiedJsonNode = jsonNode.deepCopy();
if(jsonNode.isObject()) {
ObjectNode objectNode = (ObjectNode) copiedJsonNode;
objectNode.remove(fieldNamesToRemove);
Iterator<String> iterator = objectNode.fieldNames();
boolean first = true; boolean first = true;
while(iterator.hasNext()) { while(iterator.hasNext()) {
String fieldName = iterator.next(); String fieldName = iterator.next();
if(first) { if(first) {
first = false; first = false;
}else { }else {
stringBuffer.append(queryLogicalOperator.getLogicalOperator()); stringBuffer.append(queryLogicalOperator.getLogicalOperator());
} }
JsonNode node = objectNode.get(fieldName); JsonNode node = objectNode.get(fieldName);
stringBuffer.append(evaluateNode(node, fieldName, fieldNamePrefix)); stringBuffer.append(evaluateNode(node, fieldName, fieldNamePrefix));
} }
}
if(jsonNode.isArray()) {
ArrayNode arrayNode = (ArrayNode) copiedJsonNode;
Iterator<JsonNode> iterator = arrayNode.iterator();
boolean first = true;
while(iterator.hasNext()) {
if(first) {
first = false;
}else {
stringBuffer.append(queryLogicalOperator.getLogicalOperator());
}
JsonNode node = iterator.next();
stringBuffer.append(evaluateNode(node, null, fieldNamePrefix));
}
}
return stringBuffer; return stringBuffer;
} }
protected StringBuffer evaluateNode(JsonNode jsonNode, String fieldName, String fieldNamePrefix) throws InvalidQueryException { protected StringBuffer evaluateNode(JsonNode jsonNode, String fieldName, String fieldNamePrefix) throws InvalidQueryException {
StringBuffer stringBuffer = new StringBuffer(); StringBuffer stringBuffer = new StringBuffer();
if(QueryLogicalOperator.getOperators().contains(fieldName)) { if(QueryLogicalOperator.getOperators().contains(fieldName)) {
QueryLogicalOperator queryLogicalOperator = QueryLogicalOperator.getQueryLogicalOperator(fieldName); QueryLogicalOperator queryLogicalOperator = QueryLogicalOperator.getQueryLogicalOperator(fieldName);
stringBuffer.append(addConstraint(jsonNode, queryLogicalOperator, fieldNamePrefix)); stringBuffer.append("(");
stringBuffer.append(addConstraints(jsonNode, queryLogicalOperator, fieldNamePrefix));
stringBuffer.append(")");
return stringBuffer; return stringBuffer;
} }
if(QueryConditionalOperator.getOperators().contains(fieldName)) { if(QueryConditionalOperator.getOperators().contains(fieldName)) {
QueryConditionalOperator queryConditionalOperator = QueryConditionalOperator.getQueryComparisonOperator(fieldName); QueryConditionalOperator queryConditionalOperator = QueryConditionalOperator.getQueryComparisonOperator(fieldName);
if(queryConditionalOperator == QueryConditionalOperator.IN) {
throw new UnsupportedOperationException();
} }
if(jsonNode.isArray()) { StringBuffer key = getKey(null, fieldNamePrefix);
StringBuffer value = getValue(jsonNode);
stringBuffer.append(addCondition(queryConditionalOperator, key, value));
return stringBuffer; return stringBuffer;
} }
if(jsonNode.isObject()) { if(jsonNode.isObject()) {
StringBuffer newPrefix = new StringBuffer(); StringBuffer newPrefix = new StringBuffer();
if(fieldNamePrefix!=null) { if(fieldNamePrefix!=null && fieldNamePrefix.compareTo("")!=0) {
newPrefix.append(fieldNamePrefix); newPrefix.append(fieldNamePrefix);
if(fieldName!=null && fieldName.compareTo("")!=0) {
newPrefix.append("."); newPrefix.append(".");
} }
newPrefix.append(fieldName); }
stringBuffer.append(addConstraint(jsonNode, null, newPrefix.toString())); if(fieldName!=null && fieldName.compareTo("")!=0) {
newPrefix.append(fieldName);
}
stringBuffer.append(addConstraints(jsonNode, null, newPrefix.toString()));
return stringBuffer; return stringBuffer;
} }
if(jsonNode.isTextual() || jsonNode.isNumber()) { if(jsonNode.isTextual() || jsonNode.isNumber()) {
addNameToCompare(stringBuffer, fieldName, fieldNamePrefix); StringBuffer key = getKey(fieldName, fieldNamePrefix);
String value = jsonNode.asText(); StringBuffer value = getValue(jsonNode);
addValueToMatch(stringBuffer, jsonNode, value); stringBuffer.append(addCondition(QueryConditionalOperator.EQ, key, value));
} }
return stringBuffer; return stringBuffer;
} }
protected StringBuffer addNameToCompare(StringBuffer stringBuffer, String fieldName, String fieldNamePrefix) { protected StringBuffer addCondition(QueryConditionalOperator queryConditionalOperator, StringBuffer key, StringBuffer value) {
StringBuffer stringBuffer = new StringBuffer();
stringBuffer.append(key);
stringBuffer.append(queryConditionalOperator.getConditionalOperator());
stringBuffer.append(value);
return stringBuffer;
}
protected StringBuffer getKey(String fieldName, String fieldNamePrefix) {
StringBuffer stringBuffer = new StringBuffer();
if(fieldNamePrefix!=null) { if(fieldNamePrefix!=null) {
stringBuffer.append(fieldNamePrefix); stringBuffer.append(fieldNamePrefix);
if(fieldName!=null && fieldName.compareTo("")!=0) {
stringBuffer.append("."); stringBuffer.append(".");
} }
}
if(fieldName!=null) {
stringBuffer.append(fieldName); stringBuffer.append(fieldName);
stringBuffer.append("="); }
return stringBuffer; return stringBuffer;
} }
protected StringBuffer addValueToMatch(StringBuffer stringBuffer, JsonNode gotJoJsonNode, String value) { protected StringBuffer getValue(JsonNode jsonNode) {
if(gotJoJsonNode.isNumber()) { StringBuffer stringBuffer = new StringBuffer();
String value = jsonNode.asText();
if(jsonNode.isNumber()) {
stringBuffer.append(value); stringBuffer.append(value);
} else { } else {
stringBuffer.append("\""); stringBuffer.append("\"");

View File

@ -33,6 +33,13 @@ public enum QueryConditionalOperator {
return operator; return operator;
} }
public String getConditionalOperator() {
return conditionalOperator;
}
public String getDescription() {
return description;
}
private static Set<String> operators; private static Set<String> operators;
private static Map<String,QueryConditionalOperator> operatorByKey; private static Map<String,QueryConditionalOperator> operatorByKey;

View File

@ -57,7 +57,7 @@ public class JsonQueryFacet extends JsonQueryEntity {
// Size 1 means that only '@class' property is present // Size 1 means that only '@class' property is present
if(size > 1) { if(size > 1) {
newBuffer.append(" WHERE "); newBuffer.append(" WHERE ");
newBuffer.append(addConstraint(jsonNode, null, null)); newBuffer.append(addConstraints(jsonNode, null, null));
} }
return newBuffer; return newBuffer;

View File

@ -78,7 +78,7 @@ public class JsonQueryResource extends JsonQueryEntity {
newBuffer.append("SELECT FROM ( "); newBuffer.append("SELECT FROM ( ");
newBuffer.append(stringBuffer); newBuffer.append(stringBuffer);
newBuffer.append(") WHERE "); newBuffer.append(") WHERE ");
newBuffer.append(addConstraint(jsonNode, null, null)); newBuffer.append(addConstraints(jsonNode, null, null));
stringBuffer = newBuffer; stringBuffer = newBuffer;
} }

View File

@ -82,7 +82,7 @@ public class JsonQueryConsistsOf extends JsonQueryRelation {
// Size 2 means that only '@class' and 'target' properties are present // Size 2 means that only '@class' and 'target' properties are present
if(size > 2) { if(size > 2) {
consistsOfBuffer.append(") WHERE "); // Close ) SELECT consistsOfBuffer.append(") WHERE "); // Close ) SELECT
consistsOfBuffer.append(addConstraint(jsonNode, null, null)); consistsOfBuffer.append(addConstraints(jsonNode, null, null));
} }
if(!jsonNode.has(ConsistsOf.SOURCE_PROPERTY)) { if(!jsonNode.has(ConsistsOf.SOURCE_PROPERTY)) {

View File

@ -66,7 +66,7 @@ public class JsonQueryIsRelatedTo extends JsonQueryRelation {
stringBuffer.append(" )"); // Close ) SELECT stringBuffer.append(" )"); // Close ) SELECT
} }
stringBuffer.append(" WHERE "); stringBuffer.append(" WHERE ");
stringBuffer.append(addConstraint(jsonNode, null, null)); stringBuffer.append(addConstraints(jsonNode, null, null));
} }
return stringBuffer; return stringBuffer;

View File

@ -34,7 +34,7 @@ public class JsonQueryTest extends ContextTest {
File queriesDirectory = getQueriesDirectory(); File queriesDirectory = getQueriesDirectory();
for(int i=1; i<5; i++) { for(int i=1; i<6; i++) {
File jsonQueryFile = new File(queriesDirectory, "query" + i + ".json"); File jsonQueryFile = new File(queriesDirectory, "query" + i + ".json");
ObjectMapper objectMapper = new ObjectMapper(); ObjectMapper objectMapper = new ObjectMapper();
JsonNode jsonNode = objectMapper.readTree(jsonQueryFile); JsonNode jsonNode = objectMapper.readTree(jsonQueryFile);
@ -63,10 +63,10 @@ public class JsonQueryTest extends ContextTest {
} }
} }
// @Test @Test
public void testSingleCreateQuery() throws Exception { public void testSingleCreateQuery() throws Exception {
File queriesDirectory = getQueriesDirectory(); File queriesDirectory = getQueriesDirectory();
File jsonQueryFile = new File(queriesDirectory, "query4.json"); File jsonQueryFile = new File(queriesDirectory, "query5.json");
ObjectMapper objectMapper = new ObjectMapper(); ObjectMapper objectMapper = new ObjectMapper();
JsonNode jsonNode = objectMapper.readTree(jsonQueryFile); JsonNode jsonNode = objectMapper.readTree(jsonQueryFile);
logger.info("Going to test the following JSON query {}", jsonNode.toString()); logger.info("Going to test the following JSON query {}", jsonNode.toString());

View File

@ -1 +1 @@
SELECT FROM ( TRAVERSE outV("EService") FROM ( TRAVERSE inE("ConsistsOf") FROM ( SELECT FROM ( TRAVERSE inV("AccessPointFacet") FROM ( TRAVERSE outE("ConsistsOf") FROM ( TRAVERSE outV("EService") FROM ( TRAVERSE inE("IsIdentifiedBy") FROM ( SELECT FROM ( TRAVERSE inV("SoftwareFacet") FROM ( TRAVERSE outE("IsIdentifiedBy") FROM ( TRAVERSE outV("EService") FROM ( SELECT FROM ( TRAVERSE inE("ConsistsOf") FROM ( SELECT FROM ( TRAVERSE inV("StateFacet") FROM ( TRAVERSE outE("ConsistsOf") FROM ( TRAVERSE inV("EService") FROM ( TRAVERSE outE("Activates") FROM ( SELECT FROM ( TRAVERSE outV("HostingNode") FROM ( TRAVERSE inE("ConsistsOf") FROM ( SELECT FROM ( TRAVERSE inV("CPUFacet") FROM ( TRAVERSE outE("ConsistsOf") FROM ( TRAVERSE outV("HostingNode") FROM ( SELECT FROM Activates WHERE header.uuid="d3f58e52-5346-47bc-b736-9d77a0b554ce")))) WHERE vendor="GenuineIntel"))) WHERE header.uuid="5fbc1a56-d450-4f0f-85c1-9b1684581717"))))) WHERE value="down")) WHERE propagationConstraint.add="propagate")))) WHERE name="data-transfer-service" AND group="DataTransfer"))))) WHERE endpoint="http://pc-frosini.isti.cnr.it:8080/data-transfer-service/gcube/service"))) WHERE header.uuid="0255b7ec-e3da-4071-b456-9a2907ece1db" SELECT FROM ( TRAVERSE outV("EService") FROM ( TRAVERSE inE("ConsistsOf") FROM ( SELECT FROM ( TRAVERSE inV("AccessPointFacet") FROM ( TRAVERSE outE("ConsistsOf") FROM ( TRAVERSE outV("EService") FROM ( TRAVERSE inE("IsIdentifiedBy") FROM ( SELECT FROM ( TRAVERSE inV("SoftwareFacet") FROM ( TRAVERSE outE("IsIdentifiedBy") FROM ( TRAVERSE outV("EService") FROM ( SELECT FROM ( TRAVERSE inE("ConsistsOf") FROM ( SELECT FROM ( TRAVERSE inV("StateFacet") FROM ( TRAVERSE outE("ConsistsOf") FROM ( TRAVERSE inV("EService") FROM ( TRAVERSE outE("Activates") FROM ( SELECT FROM ( TRAVERSE outV("HostingNode") FROM ( TRAVERSE inE("ConsistsOf") FROM ( SELECT FROM ( TRAVERSE inV("CPUFacet") FROM ( TRAVERSE outE("ConsistsOf") FROM ( TRAVERSE outV("HostingNode") FROM ( SELECT FROM Activates WHERE header.uuid = "d3f58e52-5346-47bc-b736-9d77a0b554ce")))) WHERE vendor = "GenuineIntel"))) WHERE header.uuid = "5fbc1a56-d450-4f0f-85c1-9b1684581717"))))) WHERE value = "down")) WHERE propagationConstraint.add = "propagate")))) WHERE name = "data-transfer-service" AND group = "DataTransfer"))))) WHERE endpoint = "http://pc-frosini.isti.cnr.it:8080/data-transfer-service/gcube/service"))) WHERE header.uuid = "0255b7ec-e3da-4071-b456-9a2907ece1db"

View File

@ -1 +1 @@
TRAVERSE outV("EService") FROM ( TRAVERSE inE("ConsistsOf") FROM ( SELECT FROM ( TRAVERSE inV("AccessPointFacet") FROM ( TRAVERSE outE("ConsistsOf") FROM ( TRAVERSE outV("EService") FROM ( TRAVERSE inE("IsIdentifiedBy") FROM ( SELECT FROM ( TRAVERSE inV("SoftwareFacet") FROM ( TRAVERSE outE("IsIdentifiedBy") FROM ( TRAVERSE outV("EService") FROM ( SELECT FROM ( TRAVERSE inE("ConsistsOf") FROM ( SELECT FROM ( TRAVERSE inV("StateFacet") FROM ( TRAVERSE outE("ConsistsOf") FROM ( TRAVERSE inV("EService") FROM ( TRAVERSE outE("Activates") FROM ( SELECT FROM ( TRAVERSE outV("HostingNode") FROM ( SELECT FROM Activates WHERE header.uuid="d3f58e52-5346-47bc-b736-9d77a0b554ce")) WHERE header.uuid="5fbc1a56-d450-4f0f-85c1-9b1684581717"))))) WHERE value="down")) WHERE propagationConstraint.add="propagate")))) WHERE name="data-transfer-service" AND group="DataTransfer"))))) WHERE endpoint="http://pc-frosini.isti.cnr.it:8080/data-transfer-service/gcube/service")) TRAVERSE outV("EService") FROM ( TRAVERSE inE("ConsistsOf") FROM ( SELECT FROM ( TRAVERSE inV("AccessPointFacet") FROM ( TRAVERSE outE("ConsistsOf") FROM ( TRAVERSE outV("EService") FROM ( TRAVERSE inE("IsIdentifiedBy") FROM ( SELECT FROM ( TRAVERSE inV("SoftwareFacet") FROM ( TRAVERSE outE("IsIdentifiedBy") FROM ( TRAVERSE outV("EService") FROM ( SELECT FROM ( TRAVERSE inE("ConsistsOf") FROM ( SELECT FROM ( TRAVERSE inV("StateFacet") FROM ( TRAVERSE outE("ConsistsOf") FROM ( TRAVERSE inV("EService") FROM ( TRAVERSE outE("Activates") FROM ( SELECT FROM ( TRAVERSE outV("HostingNode") FROM ( SELECT FROM Activates WHERE header.uuid = "d3f58e52-5346-47bc-b736-9d77a0b554ce")) WHERE header.uuid = "5fbc1a56-d450-4f0f-85c1-9b1684581717"))))) WHERE value = "down")) WHERE propagationConstraint.add = "propagate")))) WHERE name = "data-transfer-service" AND group = "DataTransfer"))))) WHERE endpoint = "http://pc-frosini.isti.cnr.it:8080/data-transfer-service/gcube/service"))

View File

@ -1 +1 @@
TRAVERSE outV("EService") FROM ( TRAVERSE inE("ConsistsOf") FROM ( SELECT FROM ( TRAVERSE inV("AccessPointFacet") FROM ( TRAVERSE outE("ConsistsOf") FROM ( TRAVERSE outV("EService") FROM ( TRAVERSE inE("IsIdentifiedBy") FROM ( SELECT FROM ( TRAVERSE inV("SoftwareFacet") FROM ( TRAVERSE outE("IsIdentifiedBy") FROM ( TRAVERSE outV("EService") FROM ( SELECT FROM ( TRAVERSE inE("ConsistsOf") FROM ( SELECT FROM ( TRAVERSE inV("StateFacet") FROM ( TRAVERSE outE("ConsistsOf") FROM ( TRAVERSE inV("EService") FROM ( TRAVERSE outE("Activates") FROM ( TRAVERSE outV("HostingNode") FROM ( TRAVERSE inE("ConsistsOf") FROM ( SELECT FROM ( TRAVERSE inV("CPUFacet") FROM ( TRAVERSE outE("ConsistsOf") FROM ( TRAVERSE outV("HostingNode") FROM ( SELECT FROM Activates)))) WHERE vendor="GenuineIntel"))))))) WHERE value="down")) WHERE propagationConstraint.add="propagate")))) WHERE name="data-transfer-service" AND group="DataTransfer"))))) WHERE endpoint="http://pc-frosini.isti.cnr.it:8080/data-transfer-service/gcube/service")) TRAVERSE outV("EService") FROM ( TRAVERSE inE("ConsistsOf") FROM ( SELECT FROM ( TRAVERSE inV("AccessPointFacet") FROM ( TRAVERSE outE("ConsistsOf") FROM ( TRAVERSE outV("EService") FROM ( TRAVERSE inE("IsIdentifiedBy") FROM ( SELECT FROM ( TRAVERSE inV("SoftwareFacet") FROM ( TRAVERSE outE("IsIdentifiedBy") FROM ( TRAVERSE outV("EService") FROM ( SELECT FROM ( TRAVERSE inE("ConsistsOf") FROM ( SELECT FROM ( TRAVERSE inV("StateFacet") FROM ( TRAVERSE outE("ConsistsOf") FROM ( TRAVERSE inV("EService") FROM ( TRAVERSE outE("Activates") FROM ( TRAVERSE outV("HostingNode") FROM ( TRAVERSE inE("ConsistsOf") FROM ( SELECT FROM ( TRAVERSE inV("CPUFacet") FROM ( TRAVERSE outE("ConsistsOf") FROM ( TRAVERSE outV("HostingNode") FROM ( SELECT FROM Activates)))) WHERE vendor = "GenuineIntel"))))))) WHERE value = "down")) WHERE propagationConstraint.add = "propagate")))) WHERE name = "data-transfer-service" AND group = "DataTransfer"))))) WHERE endpoint = "http://pc-frosini.isti.cnr.it:8080/data-transfer-service/gcube/service"))

View File

@ -1 +1 @@
SELECT FROM ( TRAVERSE inV("StateFacet") FROM ( TRAVERSE outE("ConsistsOf") FROM ( SELECT FROM ( SELECT FROM EService) WHERE header.uuid="0255b7ec-e3da-4071-b456-9a2907ece1db"))) WHERE value="down" SELECT FROM ( TRAVERSE inV("StateFacet") FROM ( TRAVERSE outE("ConsistsOf") FROM ( SELECT FROM ( SELECT FROM EService) WHERE header.uuid = "0255b7ec-e3da-4071-b456-9a2907ece1db"))) WHERE value = "down"

View File

@ -0,0 +1,22 @@
{
"@class": "StateFacet",
"value": "down",
"_in": {
"@class": "ConsistsOf",
"source" : {
"@class" : "EService",
"header": {
"$or": [
{"$and": {
"uuid" : "aec0ef31-c735-4a4c-b2f4-57dfbd2fe925",
"createdBy": {"$ne": "luca.frosini"}
}},
{"$and": {
"uuid" : "0255b7ec-e3da-4071-b456-9a2907ece1db",
"createdBy": "DataTransfer:data-transfer-service:pc-frosini.isti.cnr.it_8080"
}}
]
}
}
}
}

View File

@ -0,0 +1 @@
SELECT FROM ( TRAVERSE inV("StateFacet") FROM ( TRAVERSE outE("ConsistsOf") FROM ( SELECT FROM ( SELECT FROM EService) WHERE ((header.uuid = "aec0ef31-c735-4a4c-b2f4-57dfbd2fe925" AND header.createdBy <> "luca.frosini") OR (header.uuid = "0255b7ec-e3da-4071-b456-9a2907ece1db" AND header.createdBy = "DataTransfer:data-transfer-service:pc-frosini.isti.cnr.it_8080"))))) WHERE value = "down"