Search in sources :

Example 1 with Compare

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

the class AggregationPipelineQueryNodeTest method testFilter.

@Test
public void testFilter() {
    final AggregationPipelineQueryNode base = new AggregationPipelineQueryNode(collection, new LinkedList<>(), Sets.newHashSet("x", "y"), Sets.newHashSet("x", "y", "opt"), HashBiMap.create());
    // Extend with a supported filter
    AggregationPipelineQueryNode node = base.clone();
    boolean success = node.filter(new Compare(new Var("x"), new Var("y"), Compare.CompareOp.EQ));
    Assert.assertTrue(success);
    Assert.assertEquals(Sets.newHashSet("x", "y", "opt"), node.getBindingNames());
    Assert.assertEquals(Sets.newHashSet("x", "y"), node.getAssuredBindingNames());
    Assert.assertEquals(3, node.getPipeline().size());
    // Extend with an unsupported filter
    node = base.clone();
    success = node.filter(new IsLiteral(new Var("opt")));
    Assert.assertFalse(success);
    Assert.assertEquals(Sets.newHashSet("x", "y", "opt"), node.getBindingNames());
    Assert.assertEquals(Sets.newHashSet("x", "y"), node.getAssuredBindingNames());
    Assert.assertEquals(0, node.getPipeline().size());
}
Also used : Var(org.openrdf.query.algebra.Var) IsLiteral(org.openrdf.query.algebra.IsLiteral) Compare(org.openrdf.query.algebra.Compare) Test(org.junit.Test)

Example 2 with Compare

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

the class AggregationPipelineQueryNode method filter.

/**
 * Add a SPARQL filter to the pipeline, if possible. A filter eliminates
 * results that don't satisfy a given condition. Not all conditional
 * expressions are supported. If unsupported expressions are used in the
 * filter, the pipeline will remain unchanged and this method will return
 * false. Currently only supports binary {@link Compare} conditions among
 * variables and/or literals.
 * @param condition The filter condition
 * @return True if the filter was successfully converted into a pipeline
 *  step, false otherwise.
 */
public boolean filter(ValueExpr condition) {
    if (condition instanceof Compare) {
        Compare compare = (Compare) condition;
        Compare.CompareOp operator = compare.getOperator();
        Object leftArg = valueFieldExpr(compare.getLeftArg());
        Object rightArg = valueFieldExpr(compare.getRightArg());
        if (leftArg == null || rightArg == null) {
            // unsupported value expression, can't convert filter
            return false;
        }
        final String opFunc;
        switch(operator) {
            case EQ:
                opFunc = "$eq";
                break;
            case NE:
                opFunc = "$ne";
                break;
            case LT:
                opFunc = "$lt";
                break;
            case LE:
                opFunc = "$le";
                break;
            case GT:
                opFunc = "$gt";
                break;
            case GE:
                opFunc = "$ge";
                break;
            default:
                // unrecognized comparison operator, can't convert filter
                return false;
        }
        Document compareDoc = new Document(opFunc, Arrays.asList(leftArg, rightArg));
        pipeline.add(Aggregates.project(Projections.fields(Projections.computed("FILTER", compareDoc), Projections.include(VALUES, HASHES, TYPES, LEVEL, TIMESTAMP))));
        pipeline.add(Aggregates.match(new Document("FILTER", true)));
        pipeline.add(Aggregates.project(Projections.fields(Projections.include(VALUES, HASHES, TYPES, LEVEL, TIMESTAMP))));
        return true;
    }
    return false;
}
Also used : Compare(org.openrdf.query.algebra.Compare) DBObject(com.mongodb.DBObject) BasicDBObject(com.mongodb.BasicDBObject) Document(org.bson.Document)

Example 3 with Compare

use of org.openrdf.query.algebra.Compare in project backstage by zepheira.

the class ListFacet method createRestrictionClause.

protected ValueExpr createRestrictionClause(TupleQueryBuilder builder, String valueAsString, String valueType, ValueExpr input, ValueExpr previousClauses) {
    Value value = stringToValue(valueAsString);
    Compare compare = new Compare(input, builder.makeVar("v", value), CompareOp.EQ);
    return previousClauses == null ? compare : new Or(previousClauses, compare);
}
Also used : Or(org.openrdf.query.algebra.Or) Value(org.openrdf.model.Value) Compare(org.openrdf.query.algebra.Compare)

Aggregations

Compare (org.openrdf.query.algebra.Compare)3 BasicDBObject (com.mongodb.BasicDBObject)1 DBObject (com.mongodb.DBObject)1 Document (org.bson.Document)1 Test (org.junit.Test)1 Value (org.openrdf.model.Value)1 IsLiteral (org.openrdf.query.algebra.IsLiteral)1 Or (org.openrdf.query.algebra.Or)1 Var (org.openrdf.query.algebra.Var)1