Search in sources :

Example 31 with TupleExpr

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

the class ConstructorBuilder method buildConstructor.

private TupleExpr buildConstructor(TupleExpr bodyExpr, TupleExpr constructExpr, boolean explicitConstructor, boolean distinct, boolean reduced) {
    TupleExpr result = bodyExpr;
    // Retrieve all StatementPattern's from the construct expression
    List<StatementPattern> statementPatterns = StatementPatternCollector.process(constructExpr);
    Set<Var> constructVars = getConstructVars(statementPatterns);
    // Finally, the spo-bindings are again filtered for duplicates.
    if (distinct || reduced) {
        // Create projection that removes all bindings that are not used in the
        // constructor
        ProjectionElemList projElemList = new ProjectionElemList();
        for (Var var : constructVars) {
            // the distinct
            if (!var.isAnonymous() && !var.hasValue()) {
                projElemList.addElement(new ProjectionElem(var.getName()));
            }
        }
        result = new Projection(result, projElemList);
        // Filter the duplicates from these projected bindings
        if (distinct) {
            result = new Distinct(result);
        } else {
            result = new Reduced(result);
        }
    }
    // Create BNodeGenerator's for all anonymous variables
    Map<Var, ExtensionElem> extElemMap = new HashMap<Var, ExtensionElem>();
    for (Var var : constructVars) {
        if (var.isAnonymous() && !extElemMap.containsKey(var)) {
            ValueExpr valueExpr = null;
            if (var.hasValue()) {
                valueExpr = new ValueConstant(var.getValue());
            } else if (explicitConstructor) {
                // only generate bnodes in case of an explicit constructor
                valueExpr = new BNodeGenerator();
            }
            if (valueExpr != null) {
                extElemMap.put(var, new ExtensionElem(valueExpr, var.getName()));
            }
        }
    }
    if (!extElemMap.isEmpty()) {
        result = new Extension(result, extElemMap.values());
    }
    // Create a Projection for each StatementPattern in the constructor
    List<ProjectionElemList> projections = new ArrayList<ProjectionElemList>();
    for (StatementPattern sp : statementPatterns) {
        ProjectionElemList projElemList = new ProjectionElemList();
        projElemList.addElement(new ProjectionElem(sp.getSubjectVar().getName(), "subject"));
        projElemList.addElement(new ProjectionElem(sp.getPredicateVar().getName(), "predicate"));
        projElemList.addElement(new ProjectionElem(sp.getObjectVar().getName(), "object"));
        projections.add(projElemList);
    }
    if (projections.size() == 1) {
        result = new Projection(result, projections.get(0));
    // Note: no need to apply the second duplicate elimination step if
    // there's just one projection
    } else if (projections.size() > 1) {
        result = new MultiProjection(result, projections);
        if (distinct) {
            // Add another distinct to filter duplicate statements
            result = new Distinct(result);
        } else if (reduced) {
            result = new Reduced(result);
        }
    } else {
        // Empty constructor
        result = new EmptySet();
    }
    return result;
}
Also used : ProjectionElemList(org.eclipse.rdf4j.query.algebra.ProjectionElemList) ValueExpr(org.eclipse.rdf4j.query.algebra.ValueExpr) HashMap(java.util.HashMap) Var(org.eclipse.rdf4j.query.algebra.Var) EmptySet(org.eclipse.rdf4j.query.algebra.EmptySet) ArrayList(java.util.ArrayList) Projection(org.eclipse.rdf4j.query.algebra.Projection) MultiProjection(org.eclipse.rdf4j.query.algebra.MultiProjection) ExtensionElem(org.eclipse.rdf4j.query.algebra.ExtensionElem) TupleExpr(org.eclipse.rdf4j.query.algebra.TupleExpr) Reduced(org.eclipse.rdf4j.query.algebra.Reduced) Extension(org.eclipse.rdf4j.query.algebra.Extension) StatementPattern(org.eclipse.rdf4j.query.algebra.StatementPattern) Distinct(org.eclipse.rdf4j.query.algebra.Distinct) BNodeGenerator(org.eclipse.rdf4j.query.algebra.BNodeGenerator) ValueConstant(org.eclipse.rdf4j.query.algebra.ValueConstant) MultiProjection(org.eclipse.rdf4j.query.algebra.MultiProjection) ProjectionElem(org.eclipse.rdf4j.query.algebra.ProjectionElem)

Example 32 with TupleExpr

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

the class SeRQLParser method parseQuery.

public ParsedQuery parseQuery(String queryStr, String baseURI) throws MalformedQueryException {
    try {
        ASTQueryContainer qc = SyntaxTreeBuilder.parseQuery(queryStr);
        // Replace deprecated NULL nodes with semantically equivalent
        // alternatives
        NullProcessor.process(qc);
        StringEscapesProcessor.process(qc);
        Map<String, String> namespaces = NamespaceDeclProcessor.process(qc);
        ProjectionProcessor.process(qc);
        qc.jjtAccept(new ProjectionAliasProcessor(), null);
        qc.jjtAccept(new AnonymousVarGenerator(), null);
        // TODO: check use of unbound variables?
        TupleExpr tupleExpr = QueryModelBuilder.buildQueryModel(qc, SimpleValueFactory.getInstance());
        ASTQuery queryNode = qc.getQuery();
        ParsedQuery query;
        if (queryNode instanceof ASTTupleQuery) {
            query = new ParsedTupleQuery(tupleExpr);
        } else if (queryNode instanceof ASTGraphQuery) {
            query = new ParsedGraphQuery(tupleExpr, namespaces);
        } else {
            throw new RuntimeException("Unexpected query type: " + queryNode.getClass());
        }
        return query;
    } catch (ParseException e) {
        throw new MalformedQueryException(e.getMessage(), e);
    } catch (TokenMgrError e) {
        throw new MalformedQueryException(e.getMessage(), e);
    } catch (VisitorException e) {
        throw new MalformedQueryException(e.getMessage(), e);
    }
}
Also used : ParsedQuery(org.eclipse.rdf4j.query.parser.ParsedQuery) ParsedGraphQuery(org.eclipse.rdf4j.query.parser.ParsedGraphQuery) ASTQueryContainer(org.eclipse.rdf4j.query.parser.serql.ast.ASTQueryContainer) TokenMgrError(org.eclipse.rdf4j.query.parser.serql.ast.TokenMgrError) TupleExpr(org.eclipse.rdf4j.query.algebra.TupleExpr) ASTTupleQuery(org.eclipse.rdf4j.query.parser.serql.ast.ASTTupleQuery) ASTQuery(org.eclipse.rdf4j.query.parser.serql.ast.ASTQuery) ASTGraphQuery(org.eclipse.rdf4j.query.parser.serql.ast.ASTGraphQuery) MalformedQueryException(org.eclipse.rdf4j.query.MalformedQueryException) ParseException(org.eclipse.rdf4j.query.parser.serql.ast.ParseException) VisitorException(org.eclipse.rdf4j.query.parser.serql.ast.VisitorException) ParsedTupleQuery(org.eclipse.rdf4j.query.parser.ParsedTupleQuery)

Example 33 with TupleExpr

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

Example 34 with TupleExpr

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

the class QueryModelBuilder method visit.

@Override
public TupleExpr visit(ASTGraphIntersect node, Object data) throws VisitorException {
    TupleExpr leftArg = (TupleExpr) node.getLeftArg().jjtAccept(this, null);
    TupleExpr rightArg = (TupleExpr) node.getRightArg().jjtAccept(this, null);
    return new Intersection(leftArg, rightArg);
}
Also used : Intersection(org.eclipse.rdf4j.query.algebra.Intersection) TupleExpr(org.eclipse.rdf4j.query.algebra.TupleExpr)

Example 35 with TupleExpr

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

the class QueryModelBuilder method visit.

@Override
public CompareAll visit(ASTCompareAll node, Object data) throws VisitorException {
    ValueExpr valueExpr = (ValueExpr) node.getLeftOperand().jjtAccept(this, null);
    TupleExpr tupleExpr = (TupleExpr) node.getRightOperand().jjtAccept(this, null);
    CompareOp op = node.getOperator().getValue();
    return new CompareAll(valueExpr, tupleExpr, op);
}
Also used : ValueExpr(org.eclipse.rdf4j.query.algebra.ValueExpr) ASTValueExpr(org.eclipse.rdf4j.query.parser.serql.ast.ASTValueExpr) CompareAll(org.eclipse.rdf4j.query.algebra.CompareAll) ASTCompareAll(org.eclipse.rdf4j.query.parser.serql.ast.ASTCompareAll) CompareOp(org.eclipse.rdf4j.query.algebra.Compare.CompareOp) 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