Search in sources :

Example 1 with SingletonSet

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

the class QueryModelBuilder method visit.

@Override
public TupleExpr visit(ASTSelectQuery node, Object data) throws VisitorException {
    TupleExpr tupleExpr;
    ASTQueryBody queryBodyNode = node.getQueryBody();
    if (queryBodyNode != null) {
        // Build tuple expression for query body
        tupleExpr = (TupleExpr) queryBodyNode.jjtAccept(this, null);
    } else {
        tupleExpr = new SingletonSet();
    }
    // Apply result ordering
    ASTOrderBy orderByNode = node.getOrderBy();
    if (orderByNode != null) {
        List<OrderElem> orderElemements = (List<OrderElem>) orderByNode.jjtAccept(this, null);
        tupleExpr = new Order(tupleExpr, orderElemements);
    }
    // Apply projection
    tupleExpr = (TupleExpr) node.getSelectClause().jjtAccept(this, tupleExpr);
    // process limit and offset clauses, if present.
    ASTLimit limitNode = node.getLimit();
    int limit = -1;
    if (limitNode != null) {
        limit = (Integer) limitNode.jjtAccept(this, null);
    }
    ASTOffset offsetNode = node.getOffset();
    int offset = -1;
    if (offsetNode != null) {
        offset = (Integer) offsetNode.jjtAccept(this, null);
    }
    if (offset >= 1 || limit >= 0) {
        tupleExpr = new Slice(tupleExpr, offset, limit);
    }
    return tupleExpr;
}
Also used : Order(org.eclipse.rdf4j.query.algebra.Order) ASTLimit(org.eclipse.rdf4j.query.parser.serql.ast.ASTLimit) SingletonSet(org.eclipse.rdf4j.query.algebra.SingletonSet) Slice(org.eclipse.rdf4j.query.algebra.Slice) ASTQueryBody(org.eclipse.rdf4j.query.parser.serql.ast.ASTQueryBody) OrderElem(org.eclipse.rdf4j.query.algebra.OrderElem) ASTInList(org.eclipse.rdf4j.query.parser.serql.ast.ASTInList) ArrayList(java.util.ArrayList) List(java.util.List) ProjectionElemList(org.eclipse.rdf4j.query.algebra.ProjectionElemList) ASTOffset(org.eclipse.rdf4j.query.parser.serql.ast.ASTOffset) TupleExpr(org.eclipse.rdf4j.query.algebra.TupleExpr) ASTOrderBy(org.eclipse.rdf4j.query.parser.serql.ast.ASTOrderBy)

Example 2 with SingletonSet

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

the class TupleExprBuilder method visit.

@Override
public Object visit(ASTServiceGraphPattern node, Object data) throws VisitorException {
    GraphPattern parentGP = graphPattern;
    ValueExpr serviceRef = (ValueExpr) node.jjtGetChild(0).jjtAccept(this, null);
    graphPattern = new GraphPattern(parentGP);
    node.jjtGetChild(1).jjtAccept(this, null);
    TupleExpr serviceExpr = graphPattern.buildTupleExpr();
    if (serviceExpr instanceof SingletonSet)
        // do not add an empty service block
        return null;
    String serviceExpressionString = node.getPatternString();
    parentGP.addRequiredTE(new Service(mapValueExprToVar(serviceRef), serviceExpr, serviceExpressionString, node.getPrefixDeclarations(), node.getBaseURI(), node.isSilent()));
    graphPattern = parentGP;
    return null;
}
Also used : ValueExpr(org.eclipse.rdf4j.query.algebra.ValueExpr) SingletonSet(org.eclipse.rdf4j.query.algebra.SingletonSet) Service(org.eclipse.rdf4j.query.algebra.Service) TupleExpr(org.eclipse.rdf4j.query.algebra.TupleExpr)

Example 3 with SingletonSet

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

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

the class QueryModelBuilder method visit.

@Override
public TupleExpr visit(ASTConstructQuery node, Object data) throws VisitorException {
    TupleExpr tupleExpr;
    if (node.hasQueryBody()) {
        // Build tuple expression for query body
        tupleExpr = (TupleExpr) node.getQueryBody().jjtAccept(this, null);
    } else {
        tupleExpr = new SingletonSet();
    }
    // Apply result ordering
    ASTOrderBy orderByNode = node.getOrderBy();
    if (orderByNode != null) {
        List<OrderElem> orderElemements = (List<OrderElem>) orderByNode.jjtAccept(this, null);
        tupleExpr = new Order(tupleExpr, orderElemements);
    }
    // Create constructor
    ConstructorBuilder cb = new ConstructorBuilder();
    ASTConstruct constructNode = node.getConstructClause();
    if (!constructNode.isWildcard()) {
        TupleExpr constructExpr = (TupleExpr) constructNode.jjtAccept(this, null);
        tupleExpr = cb.buildConstructor(tupleExpr, constructExpr, constructNode.isDistinct(), constructNode.isReduced());
    } else if (node.hasQueryBody()) {
        tupleExpr = cb.buildConstructor(tupleExpr, constructNode.isDistinct(), constructNode.isReduced());
    }
    // else: "construct *" without query body, just return the SingletonSet
    // process limit and offset clauses, if present.
    ASTLimit limitNode = node.getLimit();
    int limit = -1;
    if (limitNode != null) {
        limit = (Integer) limitNode.jjtAccept(this, null);
    }
    ASTOffset offsetNode = node.getOffset();
    int offset = -1;
    if (offsetNode != null) {
        offset = (Integer) offsetNode.jjtAccept(this, null);
    }
    if (offset >= 1 || limit >= 0) {
        tupleExpr = new Slice(tupleExpr, offset, limit);
    }
    return tupleExpr;
}
Also used : Order(org.eclipse.rdf4j.query.algebra.Order) SingletonSet(org.eclipse.rdf4j.query.algebra.SingletonSet) OrderElem(org.eclipse.rdf4j.query.algebra.OrderElem) TupleExpr(org.eclipse.rdf4j.query.algebra.TupleExpr) ASTLimit(org.eclipse.rdf4j.query.parser.serql.ast.ASTLimit) Slice(org.eclipse.rdf4j.query.algebra.Slice) ASTInList(org.eclipse.rdf4j.query.parser.serql.ast.ASTInList) ArrayList(java.util.ArrayList) List(java.util.List) ProjectionElemList(org.eclipse.rdf4j.query.algebra.ProjectionElemList) ASTOffset(org.eclipse.rdf4j.query.parser.serql.ast.ASTOffset) ASTConstruct(org.eclipse.rdf4j.query.parser.serql.ast.ASTConstruct) ASTOrderBy(org.eclipse.rdf4j.query.parser.serql.ast.ASTOrderBy)

Example 5 with SingletonSet

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

the class TupleExprBuilderTest method testServiceGraphPatternChopping.

@Test
public void testServiceGraphPatternChopping() throws Exception {
    // just for construction
    Service service = new Service(null, new SingletonSet(), "", null, null, false);
    service.setExpressionString("SERVICE <a> { ?s ?p ?o }");
    assertEquals("?s ?p ?o", service.getServiceExpressionString());
    service.setExpressionString("SERVICE <a> {?s ?p ?o}");
    assertEquals("?s ?p ?o", service.getServiceExpressionString());
}
Also used : SingletonSet(org.eclipse.rdf4j.query.algebra.SingletonSet) Service(org.eclipse.rdf4j.query.algebra.Service) Test(org.junit.Test)

Aggregations

SingletonSet (org.eclipse.rdf4j.query.algebra.SingletonSet)6 TupleExpr (org.eclipse.rdf4j.query.algebra.TupleExpr)5 ArrayList (java.util.ArrayList)3 List (java.util.List)3 ProjectionElemList (org.eclipse.rdf4j.query.algebra.ProjectionElemList)3 ValueExpr (org.eclipse.rdf4j.query.algebra.ValueExpr)3 Order (org.eclipse.rdf4j.query.algebra.Order)2 OrderElem (org.eclipse.rdf4j.query.algebra.OrderElem)2 Service (org.eclipse.rdf4j.query.algebra.Service)2 Slice (org.eclipse.rdf4j.query.algebra.Slice)2 ASTInList (org.eclipse.rdf4j.query.parser.serql.ast.ASTInList)2 ASTLimit (org.eclipse.rdf4j.query.parser.serql.ast.ASTLimit)2 ASTOffset (org.eclipse.rdf4j.query.parser.serql.ast.ASTOffset)2 ASTOrderBy (org.eclipse.rdf4j.query.parser.serql.ast.ASTOrderBy)2 AbstractMap (java.util.AbstractMap)1 Map (java.util.Map)1 And (org.eclipse.rdf4j.query.algebra.And)1 DescribeOperator (org.eclipse.rdf4j.query.algebra.DescribeOperator)1 Extension (org.eclipse.rdf4j.query.algebra.Extension)1 ExtensionElem (org.eclipse.rdf4j.query.algebra.ExtensionElem)1