Search in sources :

Example 1 with Union

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

the class QueryModelBuilder method visit.

@Override
public Object visit(ASTPathExprUnion node, Object data) throws VisitorException {
    Iterator<ASTPathExpr> args = node.getPathExprList().iterator();
    // Create new sub-graph pattern for optional path expressions
    TupleExpr unionExpr = parseGraphPattern(args.next()).buildTupleExpr();
    while (args.hasNext()) {
        TupleExpr argExpr = parseGraphPattern(args.next()).buildTupleExpr();
        unionExpr = new Union(unionExpr, argExpr);
    }
    graphPattern.addRequiredTE(unionExpr);
    return null;
}
Also used : ASTPathExpr(org.eclipse.rdf4j.query.parser.serql.ast.ASTPathExpr) TupleExpr(org.eclipse.rdf4j.query.algebra.TupleExpr) ASTGraphUnion(org.eclipse.rdf4j.query.parser.serql.ast.ASTGraphUnion) Union(org.eclipse.rdf4j.query.algebra.Union) ASTPathExprUnion(org.eclipse.rdf4j.query.parser.serql.ast.ASTPathExprUnion) ASTTupleUnion(org.eclipse.rdf4j.query.parser.serql.ast.ASTTupleUnion)

Example 2 with Union

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

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

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

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

the class QueryModelBuilder method visit.

@Override
public TupleExpr visit(ASTGraphUnion node, Object data) throws VisitorException {
    TupleExpr leftArg = (TupleExpr) node.getLeftArg().jjtAccept(this, null);
    TupleExpr rightArg = (TupleExpr) node.getRightArg().jjtAccept(this, null);
    TupleExpr result = new Union(leftArg, rightArg);
    if (node.isDistinct()) {
        result = new Distinct(result);
    }
    return result;
}
Also used : Distinct(org.eclipse.rdf4j.query.algebra.Distinct) TupleExpr(org.eclipse.rdf4j.query.algebra.TupleExpr) ASTGraphUnion(org.eclipse.rdf4j.query.parser.serql.ast.ASTGraphUnion) Union(org.eclipse.rdf4j.query.algebra.Union) ASTPathExprUnion(org.eclipse.rdf4j.query.parser.serql.ast.ASTPathExprUnion) ASTTupleUnion(org.eclipse.rdf4j.query.parser.serql.ast.ASTTupleUnion)

Aggregations

TupleExpr (org.eclipse.rdf4j.query.algebra.TupleExpr)7 Union (org.eclipse.rdf4j.query.algebra.Union)7 Distinct (org.eclipse.rdf4j.query.algebra.Distinct)3 ASTGraphUnion (org.eclipse.rdf4j.query.parser.serql.ast.ASTGraphUnion)3 ASTPathExprUnion (org.eclipse.rdf4j.query.parser.serql.ast.ASTPathExprUnion)3 ASTTupleUnion (org.eclipse.rdf4j.query.parser.serql.ast.ASTTupleUnion)3 And (org.eclipse.rdf4j.query.algebra.And)1 ArbitraryLengthPath (org.eclipse.rdf4j.query.algebra.ArbitraryLengthPath)1 Compare (org.eclipse.rdf4j.query.algebra.Compare)1 Filter (org.eclipse.rdf4j.query.algebra.Filter)1 Join (org.eclipse.rdf4j.query.algebra.Join)1 MultiProjection (org.eclipse.rdf4j.query.algebra.MultiProjection)1 Projection (org.eclipse.rdf4j.query.algebra.Projection)1 ProjectionElem (org.eclipse.rdf4j.query.algebra.ProjectionElem)1 ProjectionElemList (org.eclipse.rdf4j.query.algebra.ProjectionElemList)1 StatementPattern (org.eclipse.rdf4j.query.algebra.StatementPattern)1 ValueConstant (org.eclipse.rdf4j.query.algebra.ValueConstant)1 ValueExpr (org.eclipse.rdf4j.query.algebra.ValueExpr)1 Var (org.eclipse.rdf4j.query.algebra.Var)1 ASTPathExpr (org.eclipse.rdf4j.query.parser.serql.ast.ASTPathExpr)1