Search in sources :

Example 1 with Modify

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

the class UpdateExprBuilder method visit.

@Override
public Modify visit(ASTDeleteWhere node, Object data) throws VisitorException {
    // Collect delete 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 whereExpr = graphPattern.buildTupleExpr();
    graphPattern = parentGP;
    TupleExpr deleteExpr = whereExpr.clone();
    // FIXME we should adapt the grammar so we can avoid doing this
    // post-processing.
    VarCollector collector = new VarCollector();
    deleteExpr.visit(collector);
    for (Var var : collector.getCollectedVars()) {
        if (var.isAnonymous() && !var.hasValue()) {
            throw new VisitorException("DELETE WHERE may not contain blank nodes");
        }
    }
    Modify modify = new Modify(deleteExpr, null, whereExpr);
    return modify;
}
Also used : Var(org.eclipse.rdf4j.query.algebra.Var) Modify(org.eclipse.rdf4j.query.algebra.Modify) ASTModify(org.eclipse.rdf4j.query.parser.sparql.ast.ASTModify) VisitorException(org.eclipse.rdf4j.query.parser.sparql.ast.VisitorException) TupleExpr(org.eclipse.rdf4j.query.algebra.TupleExpr)

Example 2 with Modify

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

the class SPARQLParserTest method testParseWildcardSubselectInUpdate.

/**
 * Verify that an INSERT with a subselect using a wildcard correctly adds vars to projection
 * @see <a href="https://github.com/eclipse/rdf4j/issues/686">#686</a>
 */
@Test
public void testParseWildcardSubselectInUpdate() throws Exception {
    StringBuilder update = new StringBuilder();
    update.append("INSERT { <urn:a> <urn:b> <urn:c> . } WHERE { SELECT * {?s ?p ?o } }");
    ParsedUpdate parsedUpdate = parser.parseUpdate(update.toString(), null);
    List<UpdateExpr> exprs = parsedUpdate.getUpdateExprs();
    assertEquals(1, exprs.size());
    UpdateExpr expr = exprs.get(0);
    assertTrue(expr instanceof Modify);
    Modify m = (Modify) expr;
    TupleExpr whereClause = m.getWhereExpr();
    assertTrue(whereClause instanceof Projection);
    ProjectionElemList projectionElemList = ((Projection) whereClause).getProjectionElemList();
    assertNotNull(projectionElemList);
    List<ProjectionElem> elements = projectionElemList.getElements();
    assertNotNull(elements);
    assertEquals("projection should contain all three variables", 3, elements.size());
}
Also used : ProjectionElemList(org.eclipse.rdf4j.query.algebra.ProjectionElemList) ParsedUpdate(org.eclipse.rdf4j.query.parser.ParsedUpdate) UpdateExpr(org.eclipse.rdf4j.query.algebra.UpdateExpr) Projection(org.eclipse.rdf4j.query.algebra.Projection) Modify(org.eclipse.rdf4j.query.algebra.Modify) TupleExpr(org.eclipse.rdf4j.query.algebra.TupleExpr) ProjectionElem(org.eclipse.rdf4j.query.algebra.ProjectionElem) Test(org.junit.Test)

Example 3 with Modify

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

Modify (org.eclipse.rdf4j.query.algebra.Modify)3 TupleExpr (org.eclipse.rdf4j.query.algebra.TupleExpr)3 ASTModify (org.eclipse.rdf4j.query.parser.sparql.ast.ASTModify)2 Projection (org.eclipse.rdf4j.query.algebra.Projection)1 ProjectionElem (org.eclipse.rdf4j.query.algebra.ProjectionElem)1 ProjectionElemList (org.eclipse.rdf4j.query.algebra.ProjectionElemList)1 UpdateExpr (org.eclipse.rdf4j.query.algebra.UpdateExpr)1 Var (org.eclipse.rdf4j.query.algebra.Var)1 ParsedUpdate (org.eclipse.rdf4j.query.parser.ParsedUpdate)1 ASTDeleteClause (org.eclipse.rdf4j.query.parser.sparql.ast.ASTDeleteClause)1 ASTInsertClause (org.eclipse.rdf4j.query.parser.sparql.ast.ASTInsertClause)1 ASTWhereClause (org.eclipse.rdf4j.query.parser.sparql.ast.ASTWhereClause)1 VisitorException (org.eclipse.rdf4j.query.parser.sparql.ast.VisitorException)1 Test (org.junit.Test)1