Search in sources :

Example 1 with And

use of org.eclipse.rdf4j.query.algebra.And in project rdf4j by eclipse.

the class GraphPattern method addOptionalTE.

public void addOptionalTE(GraphPattern gp) {
    List<ValueExpr> constraints = gp.removeAllConstraints();
    TupleExpr tupleExpr = gp.buildTupleExpr();
    OptionalTupleExpr optTE;
    if (constraints.isEmpty()) {
        optTE = new OptionalTupleExpr(tupleExpr);
    } else {
        ValueExpr constraint = constraints.get(0);
        for (int i = 1; i < constraints.size(); i++) {
            constraint = new And(constraint, constraints.get(i));
        }
        optTE = new OptionalTupleExpr(tupleExpr, constraint);
    }
    optionalTEs.add(optTE);
}
Also used : ValueExpr(org.eclipse.rdf4j.query.algebra.ValueExpr) And(org.eclipse.rdf4j.query.algebra.And) TupleExpr(org.eclipse.rdf4j.query.algebra.TupleExpr)

Example 2 with And

use of org.eclipse.rdf4j.query.algebra.And in project rdf4j by eclipse.

the class TupleExprBuilder method visit.

@Override
public ValueExpr visit(ASTNotIn node, Object data) throws VisitorException {
    ValueExpr result = null;
    ValueExpr leftArg = (ValueExpr) data;
    int listItemCount = node.jjtGetNumChildren();
    if (listItemCount == 0) {
        result = new ValueConstant(BooleanLiteral.TRUE);
    } else if (listItemCount == 1) {
        ValueExpr arg = (ValueExpr) node.jjtGetChild(0).jjtAccept(this, null);
        result = new Compare(leftArg, arg, CompareOp.NE);
    } else {
        // create a set of conjunctive comparisons to represent the NOT IN
        // operator: X NOT IN (a, b, c) -> X != a && X != b && X != c.
        And and = new And();
        And currentAnd = and;
        for (int i = 0; i < listItemCount - 1; i++) {
            ValueExpr arg = (ValueExpr) node.jjtGetChild(i).jjtAccept(this, null);
            currentAnd.setLeftArg(new Compare(leftArg, arg, CompareOp.NE));
            if (i == listItemCount - 2) {
                // second-to-last item
                arg = (ValueExpr) node.jjtGetChild(i + 1).jjtAccept(this, null);
                currentAnd.setRightArg(new Compare(leftArg, arg, CompareOp.NE));
            } else {
                And newAnd = new And();
                currentAnd.setRightArg(newAnd);
                currentAnd = newAnd;
            }
        }
        result = and;
    }
    return result;
}
Also used : ValueExpr(org.eclipse.rdf4j.query.algebra.ValueExpr) And(org.eclipse.rdf4j.query.algebra.And) ValueConstant(org.eclipse.rdf4j.query.algebra.ValueConstant) Compare(org.eclipse.rdf4j.query.algebra.Compare)

Example 3 with And

use of org.eclipse.rdf4j.query.algebra.And in project rdf4j by eclipse.

the class GraphPattern method buildTupleExpr.

/**
 * Builds a combined tuple expression from the tuple expressions and constraints in this graph pattern.
 *
 * @return A tuple expression for this graph pattern.
 */
public TupleExpr buildTupleExpr() {
    TupleExpr result;
    if (requiredTEs.isEmpty()) {
        result = new SingletonSet();
    } else {
        result = requiredTEs.get(0);
        for (int i = 1; i < requiredTEs.size(); i++) {
            TupleExpr te = requiredTEs.get(i);
            // if (containsProjection(te) || containsProjection(result))
            // {
            // result = new BottomUpJoin(result, te);
            // }
            // else {
            result = new Join(result, te);
        // }
        }
    }
    for (Map.Entry<TupleExpr, List<ValueExpr>> entry : optionalTEs) {
        List<ValueExpr> constraints = entry.getValue();
        if (constraints != null && !constraints.isEmpty()) {
            ValueExpr condition = constraints.get(0);
            for (int i = 1; i < constraints.size(); i++) {
                condition = new And(condition, constraints.get(i));
            }
            result = new LeftJoin(result, entry.getKey(), condition);
        } else {
            result = new LeftJoin(result, entry.getKey());
        }
    }
    for (ValueExpr constraint : constraints) {
        result = new Filter(result, constraint);
    }
    return result;
}
Also used : ValueExpr(org.eclipse.rdf4j.query.algebra.ValueExpr) LeftJoin(org.eclipse.rdf4j.query.algebra.LeftJoin) SingletonSet(org.eclipse.rdf4j.query.algebra.SingletonSet) Filter(org.eclipse.rdf4j.query.algebra.Filter) And(org.eclipse.rdf4j.query.algebra.And) LeftJoin(org.eclipse.rdf4j.query.algebra.LeftJoin) Join(org.eclipse.rdf4j.query.algebra.Join) ArrayList(java.util.ArrayList) List(java.util.List) AbstractMap(java.util.AbstractMap) Map(java.util.Map) TupleExpr(org.eclipse.rdf4j.query.algebra.TupleExpr)

Example 4 with And

use of org.eclipse.rdf4j.query.algebra.And in project rdf4j by eclipse.

the class TupleExprBuilder method createTupleExprForNegatedPropertySet.

private TupleExpr createTupleExprForNegatedPropertySet(NegatedPropertySet nps, int index) {
    Var subjVar = nps.getSubjectVar();
    Var predVar = createAnonVar();
    ValueExpr filterCondition = null;
    ValueExpr filterConditionInverse = null;
    // build (inverted) filter conditions for each negated path element.
    for (PropertySetElem elem : nps.getPropertySetElems()) {
        ValueConstant predicate = elem.getPredicate();
        if (elem.isInverse()) {
            Compare compare = new Compare(predVar, predicate, CompareOp.NE);
            if (filterConditionInverse == null) {
                filterConditionInverse = compare;
            } else {
                filterConditionInverse = new And(compare, filterConditionInverse);
            }
        } else {
            Compare compare = new Compare(predVar, predicate, CompareOp.NE);
            if (filterCondition == null) {
                filterCondition = compare;
            } else {
                filterCondition = new And(compare, filterCondition);
            }
        }
    }
    TupleExpr patternMatch = null;
    // one item)
    if (filterCondition != null) {
        for (ValueExpr objVar : nps.getObjectList()) {
            if (patternMatch == null) {
                patternMatch = new StatementPattern(nps.getScope(), subjVar, predVar, (Var) objVar, nps.getContextVar());
            } else {
                patternMatch = new Join(new StatementPattern(nps.getScope(), subjVar, predVar, (Var) objVar, nps.getContextVar()), patternMatch);
            }
        }
    }
    TupleExpr patternMatchInverse = null;
    // one item):
    if (filterConditionInverse != null) {
        for (ValueExpr objVar : nps.getObjectList()) {
            if (patternMatchInverse == null) {
                patternMatchInverse = new StatementPattern(nps.getScope(), (Var) objVar, predVar, subjVar, nps.getContextVar());
            } else {
                patternMatchInverse = new Join(new StatementPattern(nps.getScope(), (Var) objVar, predVar, subjVar, nps.getContextVar()), patternMatchInverse);
            }
        }
    }
    TupleExpr completeMatch = null;
    if (patternMatch != null) {
        completeMatch = new Filter(patternMatch, filterCondition);
    }
    if (patternMatchInverse != null) {
        if (completeMatch == null) {
            completeMatch = new Filter(patternMatchInverse, filterConditionInverse);
        } else {
            completeMatch = new Union(new Filter(patternMatchInverse, filterConditionInverse), completeMatch);
        }
    }
    return completeMatch;
}
Also used : ValueExpr(org.eclipse.rdf4j.query.algebra.ValueExpr) StatementPattern(org.eclipse.rdf4j.query.algebra.StatementPattern) Filter(org.eclipse.rdf4j.query.algebra.Filter) Var(org.eclipse.rdf4j.query.algebra.Var) And(org.eclipse.rdf4j.query.algebra.And) ValueConstant(org.eclipse.rdf4j.query.algebra.ValueConstant) Join(org.eclipse.rdf4j.query.algebra.Join) Compare(org.eclipse.rdf4j.query.algebra.Compare) TupleExpr(org.eclipse.rdf4j.query.algebra.TupleExpr) Union(org.eclipse.rdf4j.query.algebra.Union)

Example 5 with And

use of org.eclipse.rdf4j.query.algebra.And in project rdf4j by eclipse.

the class TupleExprBuilder method visit.

@Override
public Object visit(ASTAnd node, Object data) throws VisitorException {
    ValueExpr leftArg = (ValueExpr) node.jjtGetChild(0).jjtAccept(this, null);
    ValueExpr rightArg = (ValueExpr) node.jjtGetChild(1).jjtAccept(this, null);
    return new And(leftArg, rightArg);
}
Also used : ValueExpr(org.eclipse.rdf4j.query.algebra.ValueExpr) And(org.eclipse.rdf4j.query.algebra.And)

Aggregations

And (org.eclipse.rdf4j.query.algebra.And)8 ValueExpr (org.eclipse.rdf4j.query.algebra.ValueExpr)7 TupleExpr (org.eclipse.rdf4j.query.algebra.TupleExpr)4 Filter (org.eclipse.rdf4j.query.algebra.Filter)3 Join (org.eclipse.rdf4j.query.algebra.Join)3 Compare (org.eclipse.rdf4j.query.algebra.Compare)2 LeftJoin (org.eclipse.rdf4j.query.algebra.LeftJoin)2 ValueConstant (org.eclipse.rdf4j.query.algebra.ValueConstant)2 AbstractMap (java.util.AbstractMap)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Map (java.util.Map)1 BinaryTupleOperator (org.eclipse.rdf4j.query.algebra.BinaryTupleOperator)1 EmptySet (org.eclipse.rdf4j.query.algebra.EmptySet)1 SingletonSet (org.eclipse.rdf4j.query.algebra.SingletonSet)1 StatementPattern (org.eclipse.rdf4j.query.algebra.StatementPattern)1 Union (org.eclipse.rdf4j.query.algebra.Union)1 Var (org.eclipse.rdf4j.query.algebra.Var)1 ASTAnd (org.eclipse.rdf4j.query.parser.serql.ast.ASTAnd)1 ASTBooleanExpr (org.eclipse.rdf4j.query.parser.serql.ast.ASTBooleanExpr)1