Search in sources :

Example 26 with TupleExpr

use of org.eclipse.rdf4j.query.algebra.TupleExpr 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 27 with TupleExpr

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

the class TupleExprBuilder method visit.

@Override
public Exists visit(ASTExistsFunc node, Object data) throws VisitorException {
    GraphPattern parentGP = graphPattern;
    graphPattern = new GraphPattern(parentGP);
    Exists e = new Exists();
    node.jjtGetChild(0).jjtAccept(this, e);
    TupleExpr te = graphPattern.buildTupleExpr();
    e.setSubQuery(te);
    graphPattern = parentGP;
    return e;
}
Also used : Exists(org.eclipse.rdf4j.query.algebra.Exists) TupleExpr(org.eclipse.rdf4j.query.algebra.TupleExpr)

Example 28 with TupleExpr

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

the class TupleExprBuilder method handlePathModifiers.

private TupleExpr handlePathModifiers(Scope scope, Var subjVar, TupleExpr te, Var endVar, Var contextVar, long lowerBound, long upperBound) throws VisitorException {
    TupleExpr result = te;
    if (lowerBound >= 0L) {
        if (lowerBound < upperBound) {
            if (upperBound < Long.MAX_VALUE) {
                // upperbound is fixed-length
                // create set of unions for all path lengths between lower
                // and upper bound.
                Union union = new Union();
                Union currentUnion = union;
                for (long length = lowerBound; length < upperBound; length++) {
                    TupleExpr path = createPath(scope, subjVar, te, endVar, contextVar, length);
                    currentUnion.setLeftArg(path);
                    if (length == upperBound - 1) {
                        path = createPath(scope, subjVar, te, endVar, contextVar, length + 1);
                        currentUnion.setRightArg(path);
                    } else {
                        Union nextUnion = new Union();
                        currentUnion.setRightArg(nextUnion);
                        currentUnion = nextUnion;
                    }
                }
                ProjectionElemList pelist = new ProjectionElemList();
                for (String name : union.getAssuredBindingNames()) {
                    ProjectionElem pe = new ProjectionElem(name);
                    pelist.addElement(pe);
                }
                result = new Distinct(new Projection(union, pelist, false));
            } else {
                // upperbound is abitrary-length
                result = new ArbitraryLengthPath(scope, subjVar, te, endVar, contextVar, lowerBound);
            }
        } else {
            // create single path of fixed length.
            TupleExpr path = createPath(scope, subjVar, te, endVar, contextVar, lowerBound);
            result = path;
        }
    }
    return result;
}
Also used : ProjectionElemList(org.eclipse.rdf4j.query.algebra.ProjectionElemList) Distinct(org.eclipse.rdf4j.query.algebra.Distinct) Projection(org.eclipse.rdf4j.query.algebra.Projection) MultiProjection(org.eclipse.rdf4j.query.algebra.MultiProjection) ArbitraryLengthPath(org.eclipse.rdf4j.query.algebra.ArbitraryLengthPath) TupleExpr(org.eclipse.rdf4j.query.algebra.TupleExpr) Union(org.eclipse.rdf4j.query.algebra.Union) ProjectionElem(org.eclipse.rdf4j.query.algebra.ProjectionElem)

Example 29 with TupleExpr

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

the class TupleExprBuilder method visit.

@Override
public Object visit(ASTUnionGraphPattern node, Object data) throws VisitorException {
    GraphPattern parentGP = graphPattern;
    graphPattern = new GraphPattern(parentGP);
    node.jjtGetChild(0).jjtAccept(this, null);
    TupleExpr leftArg = graphPattern.buildTupleExpr();
    graphPattern = new GraphPattern(parentGP);
    node.jjtGetChild(1).jjtAccept(this, null);
    TupleExpr rightArg = graphPattern.buildTupleExpr();
    parentGP.addRequiredTE(new Union(leftArg, rightArg));
    graphPattern = parentGP;
    return null;
}
Also used : TupleExpr(org.eclipse.rdf4j.query.algebra.TupleExpr) Union(org.eclipse.rdf4j.query.algebra.Union)

Example 30 with TupleExpr

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

the class TupleExprs method containsProjection.

/**
 * Verifies if the supplied {@link TupleExpr} contains a {@link Projection}. If the supplied TupleExpr is
 * a {@link Join} or contains a {@link Join}, projections inside that Join's arguments will not be taken
 * into account.
 *
 * @param t
 *        a tuple expression.
 * @return <code>true</code> if the TupleExpr contains a projection (outside of a Join),
 *         <code>false</code> otherwise.
 * @deprecated Since 2.3. Use {@link TupleExprs#containsSubQuery(TupleExpr)} instead.
 */
@Deprecated
public static boolean containsProjection(TupleExpr t) {
    Deque<TupleExpr> queue = new ArrayDeque<>();
    queue.add(t);
    while (!queue.isEmpty()) {
        TupleExpr n = queue.removeFirst();
        if (n instanceof Projection) {
            return true;
        } else if (n instanceof Join) {
            // taken into account
            return false;
        } else {
            queue.addAll(getChildren(n));
        }
    }
    return false;
}
Also used : Projection(org.eclipse.rdf4j.query.algebra.Projection) Join(org.eclipse.rdf4j.query.algebra.Join) TupleExpr(org.eclipse.rdf4j.query.algebra.TupleExpr) ArrayDeque(java.util.ArrayDeque)

Aggregations

TupleExpr (org.eclipse.rdf4j.query.algebra.TupleExpr)61 ValueExpr (org.eclipse.rdf4j.query.algebra.ValueExpr)19 Var (org.eclipse.rdf4j.query.algebra.Var)14 Projection (org.eclipse.rdf4j.query.algebra.Projection)13 Join (org.eclipse.rdf4j.query.algebra.Join)12 ProjectionElemList (org.eclipse.rdf4j.query.algebra.ProjectionElemList)11 Slice (org.eclipse.rdf4j.query.algebra.Slice)11 ArrayList (java.util.ArrayList)9 Extension (org.eclipse.rdf4j.query.algebra.Extension)9 StatementPattern (org.eclipse.rdf4j.query.algebra.StatementPattern)9 Test (org.junit.Test)9 Distinct (org.eclipse.rdf4j.query.algebra.Distinct)8 ExtensionElem (org.eclipse.rdf4j.query.algebra.ExtensionElem)8 ProjectionElem (org.eclipse.rdf4j.query.algebra.ProjectionElem)8 Filter (org.eclipse.rdf4j.query.algebra.Filter)7 Union (org.eclipse.rdf4j.query.algebra.Union)7 Group (org.eclipse.rdf4j.query.algebra.Group)6 MultiProjection (org.eclipse.rdf4j.query.algebra.MultiProjection)6 Order (org.eclipse.rdf4j.query.algebra.Order)6 Reduced (org.eclipse.rdf4j.query.algebra.Reduced)6