Search in sources :

Example 16 with ValueConstant

use of org.openrdf.query.algebra.ValueConstant in project incubator-rya by apache.

the class GeoTemporalMongoDBStorageStrategyTest method extractArguments.

private Value[] extractArguments(final String matchName, final FunctionCall call) {
    final Value[] args = new Value[call.getArgs().size() - 1];
    int argI = 0;
    for (int i = 0; i != call.getArgs().size(); ++i) {
        final ValueExpr arg = call.getArgs().get(i);
        if (argI == i && arg instanceof Var && matchName.equals(((Var) arg).getName())) {
            continue;
        }
        if (arg instanceof ValueConstant) {
            args[argI] = ((ValueConstant) arg).getValue();
        } else if (arg instanceof Var && ((Var) arg).hasValue()) {
            args[argI] = ((Var) arg).getValue();
        } else {
            throw new IllegalArgumentException("Query error: Found " + arg + ", expected a Literal, BNode or URI");
        }
        ++argI;
    }
    return args;
}
Also used : ValueExpr(org.openrdf.query.algebra.ValueExpr) Var(org.openrdf.query.algebra.Var) ValueConstant(org.openrdf.query.algebra.ValueConstant) Value(org.openrdf.model.Value)

Example 17 with ValueConstant

use of org.openrdf.query.algebra.ValueConstant in project incubator-rya by apache.

the class AggregationPipelineQueryNode method extend.

/**
 * Add a SPARQL extension to the pipeline, if possible. An extension adds
 * some number of variables to the result. Adds a "$project" step to the
 * pipeline, but differs from the SPARQL project operation in that
 * 1) pre-existing variables are always kept, and 2) values of new variables
 * are defined by expressions, which may be more complex than simply
 * variable names. Not all expressions are supported. If unsupported
 * expression types are used in the extension, the pipeline will remain
 * unchanged and this method will return false.
 * @param extensionElements A list of new variables and their expressions
 * @return True if the extension was successfully converted into a pipeline
 *  step, false otherwise.
 */
public boolean extend(Iterable<ExtensionElem> extensionElements) {
    List<Bson> valueFields = new LinkedList<>();
    List<Bson> hashFields = new LinkedList<>();
    List<Bson> typeFields = new LinkedList<>();
    for (String varName : bindingNames) {
        valueFields.add(Projections.include(varName));
        hashFields.add(Projections.include(varName));
        typeFields.add(Projections.include(varName));
    }
    Set<String> newVarNames = new HashSet<>();
    for (ExtensionElem elem : extensionElements) {
        String name = elem.getName();
        if (!isValidFieldName(name)) {
            // If the field name is invalid, replace it internally
            name = replace(name);
        }
        // We can only handle certain kinds of value expressions; return
        // failure for any others.
        ValueExpr expr = elem.getExpr();
        final Object valueField;
        final Object hashField;
        final Object typeField;
        if (expr instanceof Var) {
            String varName = ((Var) expr).getName();
            valueField = "$" + varName;
            hashField = "$" + varName;
            typeField = "$" + varName;
        } else if (expr instanceof ValueConstant) {
            Value val = ((ValueConstant) expr).getValue();
            valueField = new Document("$literal", val.stringValue());
            hashField = new Document("$literal", SimpleMongoDBStorageStrategy.hash(val.stringValue()));
            if (val instanceof Literal) {
                typeField = new Document("$literal", ((Literal) val).getDatatype().stringValue());
            } else {
                typeField = null;
            }
        } else {
            // if not understood, return failure
            return false;
        }
        valueFields.add(Projections.computed(name, valueField));
        hashFields.add(Projections.computed(name, hashField));
        if (typeField != null) {
            typeFields.add(Projections.computed(name, typeField));
        }
        newVarNames.add(name);
    }
    assuredBindingNames.addAll(newVarNames);
    bindingNames.addAll(newVarNames);
    Bson projectOpts = Projections.fields(Projections.computed(VALUES, Projections.fields(valueFields)), Projections.computed(HASHES, Projections.fields(hashFields)), Projections.computed(TYPES, Projections.fields(typeFields)), Projections.include(LEVEL), Projections.include(TIMESTAMP));
    pipeline.add(Aggregates.project(projectOpts));
    return true;
}
Also used : ValueExpr(org.openrdf.query.algebra.ValueExpr) Var(org.openrdf.query.algebra.Var) ExtensionElem(org.openrdf.query.algebra.ExtensionElem) Document(org.bson.Document) LinkedList(java.util.LinkedList) Bson(org.bson.conversions.Bson) ValueConstant(org.openrdf.query.algebra.ValueConstant) Literal(org.openrdf.model.Literal) Value(org.openrdf.model.Value) DBObject(com.mongodb.DBObject) BasicDBObject(com.mongodb.BasicDBObject) HashSet(java.util.HashSet)

Aggregations

ValueConstant (org.openrdf.query.algebra.ValueConstant)17 Var (org.openrdf.query.algebra.Var)14 Test (org.junit.Test)9 ExtensionElem (org.openrdf.query.algebra.ExtensionElem)8 ValueExpr (org.openrdf.query.algebra.ValueExpr)8 StatementPattern (org.openrdf.query.algebra.StatementPattern)6 Value (org.openrdf.model.Value)5 Extension (org.openrdf.query.algebra.Extension)5 FunctionCall (org.openrdf.query.algebra.FunctionCall)4 ProjectionElem (org.openrdf.query.algebra.ProjectionElem)4 ProjectionElemList (org.openrdf.query.algebra.ProjectionElemList)4 Join (org.openrdf.query.algebra.Join)3 MultiProjection (org.openrdf.query.algebra.MultiProjection)3 SingletonSet (org.openrdf.query.algebra.SingletonSet)3 HashSet (java.util.HashSet)2 Literal (org.openrdf.model.Literal)2 Not (org.openrdf.query.algebra.Not)2 Projection (org.openrdf.query.algebra.Projection)2 QueryRoot (org.openrdf.query.algebra.QueryRoot)2 BasicDBObject (com.mongodb.BasicDBObject)1