use of org.vertexium.cypher.exceptions.VertexiumCypherTypeErrorException in project vertexium by visallo.
the class ExpressionExecutor method executeArrayAccess.
private Object executeArrayAccess(VertexiumCypherQueryContext ctx, CypherArrayAccess arrayAccess, ExpressionScope scope) {
Object array = ctx.getExpressionExecutor().executeExpression(ctx, arrayAccess.getArrayExpression(), scope);
if (array == null) {
return null;
}
Object indexObj = ctx.getExpressionExecutor().executeExpression(ctx, arrayAccess.getIndexExpression(), scope);
if (array instanceof Element) {
Element element = (Element) array;
if (indexObj instanceof String) {
String propertyName = (String) indexObj;
return element.getPropertyValue(propertyName);
}
throw new VertexiumCypherTypeErrorException("expected string property name, found " + indexObj.getClass().getName());
}
if (array instanceof Map) {
Map map = (Map) array;
if (indexObj instanceof String) {
String propertyName = (String) indexObj;
return map.get(propertyName);
}
throw new VertexiumCypherTypeErrorException("MapElementAccessByNonString: expected string, found " + indexObj.getClass().getName());
}
if (array instanceof List || array instanceof CypherListLiteral) {
if (indexObj instanceof Long) {
indexObj = ((Long) indexObj).intValue();
}
if (indexObj instanceof Integer) {
int index = (int) indexObj;
if (array instanceof CypherListLiteral) {
return ((CypherListLiteral) array).get(index);
} else if (array instanceof List) {
return ((List) array).get(index);
}
}
throw new VertexiumCypherTypeErrorException("ListElementAccessByNonInteger: expected integer, found " + indexObj.getClass().getName());
}
if (array instanceof Stream) {
if (indexObj instanceof Long) {
indexObj = ((Long) indexObj).intValue();
}
if (indexObj instanceof Integer) {
int index = (int) indexObj;
return ((Stream<?>) array).skip(index).findFirst().get();
}
throw new VertexiumCypherTypeErrorException("ListElementAccessByNonInteger: expected integer, found " + indexObj.getClass().getName());
}
throw new VertexiumCypherTypeErrorException("InvalidElementAccess: unexpected object access, found object " + array.getClass().getName() + ": " + array + ", index " + indexObj.getClass().getName() + ": " + indexObj);
}
use of org.vertexium.cypher.exceptions.VertexiumCypherTypeErrorException in project vertexium by visallo.
the class MatchConstraintBuilder method patternPartToConstraints.
public PatternPartMatchConstraint patternPartToConstraints(CypherPatternPart patternPart, boolean optional) {
String pathName = patternPart.getName();
CypherListLiteral<CypherElementPattern> elementPatterns = patternPart.getElementPatterns();
LinkedHashSet<MatchConstraint> allConstraints = new LinkedHashSet<>();
NodeMatchConstraint firstMatchConstraint = null;
MatchConstraint previousConstraint = null;
for (CypherElementPattern elementPattern : elementPatterns) {
if (elementPattern instanceof CypherNodePattern) {
Optional<NodeMatchConstraint> existingNodeMatchConstraint = allConstraints.stream().filter(c -> c.getName() != null && c.getName().equals(elementPattern.getName())).map(c -> (NodeMatchConstraint) c).findFirst();
NodeMatchConstraint nodeMatchConstraint = existingNodeMatchConstraint.orElseGet(() -> new NodeMatchConstraint(elementPattern.getName(), Lists.newArrayList(), optional));
nodeMatchConstraint.getPatterns().add((CypherNodePattern) elementPattern);
allConstraints.add(nodeMatchConstraint);
if (firstMatchConstraint == null) {
firstMatchConstraint = nodeMatchConstraint;
}
if (previousConstraint != null) {
// noinspection RedundantCast
nodeMatchConstraint.addConnectedConstraint((RelationshipMatchConstraint) previousConstraint);
// noinspection RedundantCast
((RelationshipMatchConstraint) previousConstraint).addConnectedConstraint(nodeMatchConstraint);
}
previousConstraint = nodeMatchConstraint;
} else if (elementPattern instanceof CypherRelationshipPattern) {
RelationshipMatchConstraint relationshipMatchConstraint = new RelationshipMatchConstraint(elementPattern.getName(), Lists.newArrayList((CypherRelationshipPattern) elementPattern), optional);
allConstraints.add(relationshipMatchConstraint);
if (previousConstraint != null) {
// noinspection RedundantCast
relationshipMatchConstraint.addConnectedConstraint((NodeMatchConstraint) previousConstraint);
// noinspection RedundantCast
((NodeMatchConstraint) previousConstraint).addConnectedConstraint(relationshipMatchConstraint);
}
previousConstraint = relationshipMatchConstraint;
} else {
throw new VertexiumCypherTypeErrorException(elementPattern, CypherNodePattern.class, CypherRelationshipPattern.class);
}
}
return new PatternPartMatchConstraint(pathName, allConstraints);
}
use of org.vertexium.cypher.exceptions.VertexiumCypherTypeErrorException in project vertexium by visallo.
the class CypherUnaryDateFunction method invoke.
@Override
public Object invoke(VertexiumCypherQueryContext ctx, CypherAstBase[] arguments, ExpressionScope scope) {
assertArgumentCount(arguments, 1);
Object arg0 = ctx.getExpressionExecutor().executeExpression(ctx, arguments[0], scope);
if (arg0 == null) {
return null;
}
if (arg0 instanceof Date) {
Date d = (Date) arg0;
return invokeZonedDateTime(ctx, ZonedDateTime.ofInstant(d.toInstant(), ZoneOffset.UTC), scope);
}
throw new VertexiumCypherTypeErrorException(arg0, Date.class);
}
use of org.vertexium.cypher.exceptions.VertexiumCypherTypeErrorException in project vertexium by visallo.
the class TailFunction method invoke.
@Override
public Object invoke(VertexiumCypherQueryContext ctx, CypherAstBase[] arguments, ExpressionScope scope) {
assertArgumentCount(arguments, 1);
Object arg0 = ctx.getExpressionExecutor().executeExpression(ctx, arguments[0], scope);
if (arg0 instanceof Stream) {
return ((Stream) arg0).skip(1);
}
if (arg0 instanceof List) {
List list = (List) arg0;
return list.subList(1, list.size());
}
throw new VertexiumCypherTypeErrorException(arg0, Collection.class, Stream.class);
}
use of org.vertexium.cypher.exceptions.VertexiumCypherTypeErrorException in project vertexium by visallo.
the class HeadFunction method invoke.
@Override
public Object invoke(VertexiumCypherQueryContext ctx, CypherAstBase[] arguments, ExpressionScope scope) {
assertArgumentCount(arguments, 1);
Object arg0 = ctx.getExpressionExecutor().executeExpression(ctx, arguments[0], scope);
if (arg0 instanceof Stream) {
Stream<?> stream = (Stream<?>) arg0;
return stream.findFirst().orElse(null);
}
if (arg0 instanceof List) {
List list = (List) arg0;
if (list.size() == 0) {
return null;
}
return list.get(0);
}
if (arg0 instanceof Collection) {
Collection collection = (Collection) arg0;
if (collection.size() == 0) {
return null;
}
return collection.iterator().next();
}
throw new VertexiumCypherTypeErrorException(arg0, Collection.class, Stream.class);
}
Aggregations