Search in sources :

Example 1 with VertexiumCypherTypeErrorException

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);
}
Also used : VertexiumCypherTypeErrorException(org.vertexium.cypher.exceptions.VertexiumCypherTypeErrorException) Element(org.vertexium.Element) Stream(java.util.stream.Stream)

Example 2 with VertexiumCypherTypeErrorException

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);
}
Also used : org.vertexium.cypher.ast.model(org.vertexium.cypher.ast.model) java.util(java.util) Lists(com.google.common.collect.Lists) org.vertexium.cypher.executor.models.match(org.vertexium.cypher.executor.models.match) VertexiumCypherTypeErrorException(org.vertexium.cypher.exceptions.VertexiumCypherTypeErrorException) Collectors(java.util.stream.Collectors) VertexiumCypherTypeErrorException(org.vertexium.cypher.exceptions.VertexiumCypherTypeErrorException)

Example 3 with VertexiumCypherTypeErrorException

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);
}
Also used : VertexiumCypherTypeErrorException(org.vertexium.cypher.exceptions.VertexiumCypherTypeErrorException) Date(java.util.Date)

Example 4 with VertexiumCypherTypeErrorException

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);
}
Also used : VertexiumCypherTypeErrorException(org.vertexium.cypher.exceptions.VertexiumCypherTypeErrorException) Stream(java.util.stream.Stream) List(java.util.List)

Example 5 with VertexiumCypherTypeErrorException

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);
}
Also used : VertexiumCypherTypeErrorException(org.vertexium.cypher.exceptions.VertexiumCypherTypeErrorException) Collection(java.util.Collection) Stream(java.util.stream.Stream) List(java.util.List)

Aggregations

VertexiumCypherTypeErrorException (org.vertexium.cypher.exceptions.VertexiumCypherTypeErrorException)8 Stream (java.util.stream.Stream)3 List (java.util.List)2 Element (org.vertexium.Element)2 Lists (com.google.common.collect.Lists)1 java.util (java.util)1 Collection (java.util.Collection)1 Date (java.util.Date)1 Collectors (java.util.stream.Collectors)1 Edge (org.vertexium.Edge)1 Vertex (org.vertexium.Vertex)1 VertexiumException (org.vertexium.VertexiumException)1 org.vertexium.cypher.ast.model (org.vertexium.cypher.ast.model)1 CypherAstBase (org.vertexium.cypher.ast.model.CypherAstBase)1 org.vertexium.cypher.executor.models.match (org.vertexium.cypher.executor.models.match)1