Search in sources :

Example 41 with TupleExpr

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

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

the class SPARQLParserTest method testParsedGraphQueryRootNode.

@Test
public void testParsedGraphQueryRootNode() throws Exception {
    StringBuilder qb = new StringBuilder();
    qb.append("CONSTRUCT WHERE {?a <foo:bar> \"test\"}");
    ParsedGraphQuery q = (ParsedGraphQuery) parser.parseQuery(qb.toString(), null);
    TupleExpr te = q.getTupleExpr();
    assertNotNull(te);
    assertTrue(te instanceof Projection);
    assertNull(te.getParentNode());
}
Also used : ParsedGraphQuery(org.eclipse.rdf4j.query.parser.ParsedGraphQuery) Projection(org.eclipse.rdf4j.query.algebra.Projection) TupleExpr(org.eclipse.rdf4j.query.algebra.TupleExpr) Test(org.junit.Test)

Example 43 with TupleExpr

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

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

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

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