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.ObjectMapper;
import org.gcube.com.fasterxml.jackson.databind.node.ArrayNode;
import org.gcube.com.fasterxml.jackson.databind.node.ObjectNode;
import org.gcube.informationsystem.base.reference.AccessType;
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;
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();
ObjectNode objectNode = jsonNode.deepCopy();
objectNode.remove(fieldNamesToRemove);
Iterator<String> iterator = objectNode.fieldNames();
if(queryLogicalOperator==null) {
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;
while(iterator.hasNext()) {
String fieldName = iterator.next();
if(first) {
first = false;
}else {
stringBuffer.append(queryLogicalOperator.getLogicalOperator());
}
JsonNode node = objectNode.get(fieldName);
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;
}
protected StringBuffer evaluateNode(JsonNode jsonNode, String fieldName, String fieldNamePrefix) throws InvalidQueryException {
StringBuffer stringBuffer = new StringBuffer();
if(QueryLogicalOperator.getOperators().contains(fieldName)) {
QueryLogicalOperator queryLogicalOperator = QueryLogicalOperator.getQueryLogicalOperator(fieldName);
stringBuffer.append(addConstraint(jsonNode, queryLogicalOperator, fieldNamePrefix));
stringBuffer.append("(");
stringBuffer.append(addConstraints(jsonNode, queryLogicalOperator, fieldNamePrefix));
stringBuffer.append(")");
return stringBuffer;
}
if(QueryConditionalOperator.getOperators().contains(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;
}
if(jsonNode.isObject()) {
StringBuffer newPrefix = new StringBuffer();
if(fieldNamePrefix!=null) {
if(fieldNamePrefix!=null && fieldNamePrefix.compareTo("")!=0) {
newPrefix.append(fieldNamePrefix);
if(fieldName!=null && fieldName.compareTo("")!=0) {
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;
}
if(jsonNode.isTextual() || jsonNode.isNumber()) {
addNameToCompare(stringBuffer, fieldName, fieldNamePrefix);
String value = jsonNode.asText();
addValueToMatch(stringBuffer, jsonNode, value);
StringBuffer key = getKey(fieldName, fieldNamePrefix);
StringBuffer value = getValue(jsonNode);
stringBuffer.append(addCondition(QueryConditionalOperator.EQ, key, value));
}
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) {
stringBuffer.append(fieldNamePrefix);
if(fieldName!=null && fieldName.compareTo("")!=0) {
stringBuffer.append(".");
}
}
if(fieldName!=null) {
stringBuffer.append(fieldName);
stringBuffer.append("=");
}
return stringBuffer;
}
protected StringBuffer addValueToMatch(StringBuffer stringBuffer, JsonNode gotJoJsonNode, String value) {
if(gotJoJsonNode.isNumber()) {
protected StringBuffer getValue(JsonNode jsonNode) {
StringBuffer stringBuffer = new StringBuffer();
String value = jsonNode.asText();
if(jsonNode.isNumber()) {
stringBuffer.append(value);
} else {
stringBuffer.append("\"");

View File

@ -33,6 +33,13 @@ public enum QueryConditionalOperator {
return operator;
}
public String getConditionalOperator() {
return conditionalOperator;
}
public String getDescription() {
return description;
}
private static Set<String> operators;
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
if(size > 1) {
newBuffer.append(" WHERE ");
newBuffer.append(addConstraint(jsonNode, null, null));
newBuffer.append(addConstraints(jsonNode, null, null));
}
return newBuffer;

View File

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

View File

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

View File

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

View File

@ -34,7 +34,7 @@ public class JsonQueryTest extends ContextTest {
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");
ObjectMapper objectMapper = new ObjectMapper();
JsonNode jsonNode = objectMapper.readTree(jsonQueryFile);
@ -63,10 +63,10 @@ public class JsonQueryTest extends ContextTest {
}
}
// @Test
@Test
public void testSingleCreateQuery() throws Exception {
File queriesDirectory = getQueriesDirectory();
File jsonQueryFile = new File(queriesDirectory, "query4.json");
File jsonQueryFile = new File(queriesDirectory, "query5.json");
ObjectMapper objectMapper = new ObjectMapper();
JsonNode jsonNode = objectMapper.readTree(jsonQueryFile);
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"