Search in sources :

Example 6 with Join

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

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

the class TupleExprs method containsSubquery.

/**
 * Verifies if the supplied {@link TupleExpr} contains a {@link Projection} with the subquery flag set to
 * true (default). 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 subquery projection (outside of a Join),
 *         <code>false</code> otherwise.
 */
public static boolean containsSubquery(TupleExpr t) {
    Deque<TupleExpr> queue = new ArrayDeque<>();
    queue.add(t);
    while (!queue.isEmpty()) {
        TupleExpr n = queue.removeFirst();
        if (n instanceof Projection && ((Projection) n).isSubquery()) {
            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)

Example 8 with Join

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

the class SPARQLParserTest method testSES1922PathSequenceWithValueConstant.

@Test
public void testSES1922PathSequenceWithValueConstant() throws Exception {
    StringBuilder qb = new StringBuilder();
    qb.append("ASK {?A (<foo:bar>)/<foo:foo> <foo:objValue>} ");
    ParsedQuery q = parser.parseQuery(qb.toString(), null);
    TupleExpr te = q.getTupleExpr();
    assertNotNull(te);
    assertTrue(te instanceof Slice);
    Slice s = (Slice) te;
    assertTrue(s.getArg() instanceof Join);
    Join j = (Join) s.getArg();
    assertTrue(j.getLeftArg() instanceof StatementPattern);
    assertTrue(j.getRightArg() instanceof StatementPattern);
    StatementPattern leftArg = (StatementPattern) j.getLeftArg();
    StatementPattern rightArg = (StatementPattern) j.getRightArg();
    assertTrue(leftArg.getObjectVar().equals(rightArg.getSubjectVar()));
    assertEquals(leftArg.getObjectVar().getName(), rightArg.getSubjectVar().getName());
}
Also used : StatementPattern(org.eclipse.rdf4j.query.algebra.StatementPattern) ParsedQuery(org.eclipse.rdf4j.query.parser.ParsedQuery) Slice(org.eclipse.rdf4j.query.algebra.Slice) Join(org.eclipse.rdf4j.query.algebra.Join) TupleExpr(org.eclipse.rdf4j.query.algebra.TupleExpr) Test(org.junit.Test)

Example 9 with Join

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

the class SPARQLParserTest method testSES1927UnequalLiteralValueConstants1.

@Test
public void testSES1927UnequalLiteralValueConstants1() throws Exception {
    StringBuilder qb = new StringBuilder();
    qb.append("ASK {?a <foo:bar> \"test\". ?a <foo:foo> \"test\"@en .} ");
    ParsedQuery q = parser.parseQuery(qb.toString(), null);
    TupleExpr te = q.getTupleExpr();
    assertNotNull(te);
    assertTrue(te instanceof Slice);
    Slice s = (Slice) te;
    assertTrue(s.getArg() instanceof Join);
    Join j = (Join) s.getArg();
    assertTrue(j.getLeftArg() instanceof StatementPattern);
    assertTrue(j.getRightArg() instanceof StatementPattern);
    StatementPattern leftArg = (StatementPattern) j.getLeftArg();
    StatementPattern rightArg = (StatementPattern) j.getRightArg();
    assertFalse(leftArg.getObjectVar().equals(rightArg.getObjectVar()));
    assertNotEquals(leftArg.getObjectVar().getName(), rightArg.getObjectVar().getName());
}
Also used : StatementPattern(org.eclipse.rdf4j.query.algebra.StatementPattern) ParsedQuery(org.eclipse.rdf4j.query.parser.ParsedQuery) Slice(org.eclipse.rdf4j.query.algebra.Slice) Join(org.eclipse.rdf4j.query.algebra.Join) TupleExpr(org.eclipse.rdf4j.query.algebra.TupleExpr) Test(org.junit.Test)

Example 10 with Join

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

the class BasicGroup method expr.

private TupleExpr expr(boolean filterExpr) {
    TupleExpr aExpr = null;
    if (mExpressions.isEmpty() && mFilters.isEmpty()) {
        if (mChildren.isEmpty()) {
            return null;
        }
    } else if (mExpressions.isEmpty() && !mFilters.isEmpty()) {
        if (mChildren.isEmpty()) {
            aExpr = new Filter(new EmptySet(), filtersAsAnd());
        }
    } else {
        aExpr = asJoin(mExpressions);
        if (filterExpr) {
            aExpr = filteredTuple(aExpr);
        }
    }
    if (!mChildren.isEmpty()) {
        for (Group aGroup : mChildren) {
            if (aExpr == null) {
                if (mExpressions.isEmpty() && !mFilters.isEmpty()) {
                    aExpr = new Filter(aGroup.expr(), filtersAsAnd());
                } else {
                    aExpr = aGroup.expr();
                }
            } else {
                BinaryTupleOperator aJoin = aGroup.isOptional() ? new LeftJoin() : new Join();
                aJoin.setLeftArg(aExpr);
                if (aGroup.isOptional() && aJoin instanceof LeftJoin && aGroup instanceof BasicGroup && !((BasicGroup) aGroup).mFilters.isEmpty()) {
                    BasicGroup aBasicGroup = (BasicGroup) aGroup;
                    aJoin.setRightArg(aBasicGroup.expr(false));
                    ((LeftJoin) aJoin).setCondition(aBasicGroup.filtersAsAnd());
                } else {
                    aJoin.setRightArg(aGroup.expr());
                }
                aExpr = aJoin;
            }
        }
    }
    return aExpr;
}
Also used : LeftJoin(org.eclipse.rdf4j.query.algebra.LeftJoin) Filter(org.eclipse.rdf4j.query.algebra.Filter) EmptySet(org.eclipse.rdf4j.query.algebra.EmptySet) BinaryTupleOperator(org.eclipse.rdf4j.query.algebra.BinaryTupleOperator) LeftJoin(org.eclipse.rdf4j.query.algebra.LeftJoin) Join(org.eclipse.rdf4j.query.algebra.Join) TupleExpr(org.eclipse.rdf4j.query.algebra.TupleExpr)

Aggregations

Join (org.eclipse.rdf4j.query.algebra.Join)12 TupleExpr (org.eclipse.rdf4j.query.algebra.TupleExpr)12 Slice (org.eclipse.rdf4j.query.algebra.Slice)6 Filter (org.eclipse.rdf4j.query.algebra.Filter)4 StatementPattern (org.eclipse.rdf4j.query.algebra.StatementPattern)4 And (org.eclipse.rdf4j.query.algebra.And)3 BindingSetAssignment (org.eclipse.rdf4j.query.algebra.BindingSetAssignment)3 Group (org.eclipse.rdf4j.query.algebra.Group)3 LeftJoin (org.eclipse.rdf4j.query.algebra.LeftJoin)3 ParsedQuery (org.eclipse.rdf4j.query.parser.ParsedQuery)3 Test (org.junit.Test)3 ArrayDeque (java.util.ArrayDeque)2 BinaryTupleOperator (org.eclipse.rdf4j.query.algebra.BinaryTupleOperator)2 EmptySet (org.eclipse.rdf4j.query.algebra.EmptySet)2 Projection (org.eclipse.rdf4j.query.algebra.Projection)2 ValueExpr (org.eclipse.rdf4j.query.algebra.ValueExpr)2 AbstractMap (java.util.AbstractMap)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Map (java.util.Map)1