Search in sources :

Example 46 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) MultiProjection(org.eclipse.rdf4j.query.algebra.MultiProjection) Projection(org.eclipse.rdf4j.query.algebra.Projection) 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 47 with TupleExpr

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

the class UpdateExprBuilder method visit.

@Override
public TupleExpr visit(ASTQuadsNotTriples node, Object data) throws VisitorException {
    GraphPattern parentGP = graphPattern;
    graphPattern = new GraphPattern();
    ValueExpr contextNode = (ValueExpr) node.jjtGetChild(0).jjtAccept(this, data);
    Var contextVar = mapValueExprToVar(contextNode);
    graphPattern.setContextVar(contextVar);
    graphPattern.setStatementPatternScope(Scope.NAMED_CONTEXTS);
    for (int i = 1; i < node.jjtGetNumChildren(); i++) {
        node.jjtGetChild(i).jjtAccept(this, data);
    }
    TupleExpr result = graphPattern.buildTupleExpr();
    parentGP.addRequiredTE(result);
    graphPattern = parentGP;
    return result;
}
Also used : ValueExpr(org.eclipse.rdf4j.query.algebra.ValueExpr) Var(org.eclipse.rdf4j.query.algebra.Var) TupleExpr(org.eclipse.rdf4j.query.algebra.TupleExpr)

Example 48 with TupleExpr

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

the class UpdateExprBuilder method visit.

@Override
public TupleExpr visit(ASTInsertClause node, Object data) throws VisitorException {
    TupleExpr result = (TupleExpr) data;
    // Collect insert clause triples
    GraphPattern parentGP = graphPattern;
    graphPattern = new GraphPattern();
    // inherit scope & context
    graphPattern.setStatementPatternScope(parentGP.getStatementPatternScope());
    graphPattern.setContextVar(parentGP.getContextVar());
    for (int i = 0; i < node.jjtGetNumChildren(); i++) {
        node.jjtGetChild(i).jjtAccept(this, data);
    }
    TupleExpr insertExpr = graphPattern.buildTupleExpr();
    graphPattern = parentGP;
    return insertExpr;
}
Also used : TupleExpr(org.eclipse.rdf4j.query.algebra.TupleExpr)

Example 49 with TupleExpr

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

the class UpdateExprBuilder method visit.

@Override
public TupleExpr visit(ASTDeleteClause node, Object data) throws VisitorException {
    TupleExpr result = (TupleExpr) data;
    // Collect construct triples
    GraphPattern parentGP = graphPattern;
    graphPattern = new GraphPattern();
    // inherit scope & context
    graphPattern.setStatementPatternScope(parentGP.getStatementPatternScope());
    graphPattern.setContextVar(parentGP.getContextVar());
    for (int i = 0; i < node.jjtGetNumChildren(); i++) {
        node.jjtGetChild(i).jjtAccept(this, data);
    }
    TupleExpr deleteExpr = graphPattern.buildTupleExpr();
    // FIXME we should adapt the grammar so we can avoid doing this in
    // post-processing.
    VarCollector collector = new VarCollector();
    deleteExpr.visit(collector);
    for (Var var : collector.getCollectedVars()) {
        if (var.isAnonymous() && !var.hasValue()) {
            // blank node in delete pattern, not allowed by SPARQL spec.
            throw new VisitorException("DELETE clause may not contain blank nodes");
        }
    }
    graphPattern = parentGP;
    return deleteExpr;
}
Also used : Var(org.eclipse.rdf4j.query.algebra.Var) VisitorException(org.eclipse.rdf4j.query.parser.sparql.ast.VisitorException) TupleExpr(org.eclipse.rdf4j.query.algebra.TupleExpr)

Example 50 with TupleExpr

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

the class UpdateExprBuilder method visit.

@Override
public Modify visit(ASTModify node, Object data) throws VisitorException {
    ASTWhereClause whereClause = node.getWhereClause();
    TupleExpr where = null;
    if (whereClause != null) {
        where = (TupleExpr) whereClause.jjtAccept(this, data);
    }
    TupleExpr delete = null;
    ASTDeleteClause deleteNode = node.getDeleteClause();
    if (deleteNode != null) {
        delete = (TupleExpr) deleteNode.jjtAccept(this, data);
    }
    TupleExpr insert = null;
    ASTInsertClause insertNode = node.getInsertClause();
    if (insertNode != null) {
        insert = (TupleExpr) insertNode.jjtAccept(this, data);
    }
    Modify modifyExpr = new Modify(delete, insert, where);
    return modifyExpr;
}
Also used : ASTWhereClause(org.eclipse.rdf4j.query.parser.sparql.ast.ASTWhereClause) ASTDeleteClause(org.eclipse.rdf4j.query.parser.sparql.ast.ASTDeleteClause) Modify(org.eclipse.rdf4j.query.algebra.Modify) ASTModify(org.eclipse.rdf4j.query.parser.sparql.ast.ASTModify) TupleExpr(org.eclipse.rdf4j.query.algebra.TupleExpr) ASTInsertClause(org.eclipse.rdf4j.query.parser.sparql.ast.ASTInsertClause)

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