Search in sources :

Example 1 with UpdateExpr

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

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

the class ParsedUpdate method toString.

@Override
public String toString() {
    StringBuilder stringBuilder = new StringBuilder();
    // TODO visualize dataset in toString()?
    for (UpdateExpr updateExpr : updateExprs) {
        stringBuilder.append(updateExpr.toString());
        stringBuilder.append("; ");
    }
    return stringBuilder.toString();
}
Also used : UpdateExpr(org.eclipse.rdf4j.query.algebra.UpdateExpr)

Example 3 with UpdateExpr

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

the class SPARQLParser method parseUpdate.

@Override
public ParsedUpdate parseUpdate(String updateStr, String baseURI) throws MalformedQueryException {
    try {
        ParsedUpdate update = new ParsedUpdate(updateStr);
        ASTUpdateSequence updateSequence = SyntaxTreeBuilder.parseUpdateSequence(updateStr);
        List<ASTUpdateContainer> updateOperations = updateSequence.getUpdateContainers();
        List<ASTPrefixDecl> sharedPrefixDeclarations = null;
        Node node = updateSequence.jjtGetChild(0);
        Set<String> globalUsedBNodeIds = new HashSet<String>();
        for (int i = 0; i < updateOperations.size(); i++) {
            ASTUpdateContainer uc = updateOperations.get(i);
            if (uc.jjtGetNumChildren() == 0 && i > 0 && i < updateOperations.size() - 1) {
                // empty update in the middle of the sequence
                throw new MalformedQueryException("empty update in sequence not allowed");
            }
            StringEscapesProcessor.process(uc);
            BaseDeclProcessor.process(uc, baseURI);
            WildcardProjectionProcessor.process(uc);
            if (uc.getBaseDecl() != null) {
                baseURI = uc.getBaseDecl().getIRI();
            }
            // do a special dance to handle prefix declarations in sequences: if
            // the current
            // operation has its own prefix declarations, use those. Otherwise,
            // try and use
            // prefix declarations from a previous operation in this sequence.
            List<ASTPrefixDecl> prefixDeclList = uc.getPrefixDeclList();
            if (prefixDeclList == null || prefixDeclList.size() == 0) {
                if (sharedPrefixDeclarations != null) {
                    for (ASTPrefixDecl prefixDecl : sharedPrefixDeclarations) {
                        uc.jjtAppendChild(prefixDecl);
                    }
                }
            } else {
                sharedPrefixDeclarations = prefixDeclList;
            }
            PrefixDeclProcessor.process(uc);
            Set<String> usedBNodeIds = BlankNodeVarProcessor.process(uc);
            if (uc.getUpdate() instanceof ASTInsertData || uc.getUpdate() instanceof ASTInsertData) {
                if (Collections.disjoint(usedBNodeIds, globalUsedBNodeIds)) {
                    globalUsedBNodeIds.addAll(usedBNodeIds);
                } else {
                    throw new MalformedQueryException("blank node identifier may not be shared across INSERT/DELETE DATA operations");
                }
            }
            UpdateExprBuilder updateExprBuilder = new UpdateExprBuilder(SimpleValueFactory.getInstance());
            ASTUpdate updateNode = uc.getUpdate();
            if (updateNode != null) {
                UpdateExpr updateExpr = (UpdateExpr) updateNode.jjtAccept(updateExprBuilder, null);
                // add individual update expression to ParsedUpdate sequence
                // container
                update.addUpdateExpr(updateExpr);
                // associate updateExpr with the correct dataset (if any)
                Dataset dataset = DatasetDeclProcessor.process(uc);
                update.map(updateExpr, dataset);
            }
        }
        return update;
    } 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 : Dataset(org.eclipse.rdf4j.query.Dataset) Node(org.eclipse.rdf4j.query.parser.sparql.ast.Node) TokenMgrError(org.eclipse.rdf4j.query.parser.sparql.ast.TokenMgrError) ASTUpdateSequence(org.eclipse.rdf4j.query.parser.sparql.ast.ASTUpdateSequence) ASTInsertData(org.eclipse.rdf4j.query.parser.sparql.ast.ASTInsertData) ASTPrefixDecl(org.eclipse.rdf4j.query.parser.sparql.ast.ASTPrefixDecl) ParsedUpdate(org.eclipse.rdf4j.query.parser.ParsedUpdate) MalformedQueryException(org.eclipse.rdf4j.query.MalformedQueryException) ASTUpdate(org.eclipse.rdf4j.query.parser.sparql.ast.ASTUpdate) UpdateExpr(org.eclipse.rdf4j.query.algebra.UpdateExpr) ParseException(org.eclipse.rdf4j.query.parser.sparql.ast.ParseException) VisitorException(org.eclipse.rdf4j.query.parser.sparql.ast.VisitorException) ASTUpdateContainer(org.eclipse.rdf4j.query.parser.sparql.ast.ASTUpdateContainer) HashSet(java.util.HashSet)

Aggregations

UpdateExpr (org.eclipse.rdf4j.query.algebra.UpdateExpr)3 ParsedUpdate (org.eclipse.rdf4j.query.parser.ParsedUpdate)2 HashSet (java.util.HashSet)1 Dataset (org.eclipse.rdf4j.query.Dataset)1 MalformedQueryException (org.eclipse.rdf4j.query.MalformedQueryException)1 Modify (org.eclipse.rdf4j.query.algebra.Modify)1 Projection (org.eclipse.rdf4j.query.algebra.Projection)1 ProjectionElem (org.eclipse.rdf4j.query.algebra.ProjectionElem)1 ProjectionElemList (org.eclipse.rdf4j.query.algebra.ProjectionElemList)1 TupleExpr (org.eclipse.rdf4j.query.algebra.TupleExpr)1 ASTInsertData (org.eclipse.rdf4j.query.parser.sparql.ast.ASTInsertData)1 ASTPrefixDecl (org.eclipse.rdf4j.query.parser.sparql.ast.ASTPrefixDecl)1 ASTUpdate (org.eclipse.rdf4j.query.parser.sparql.ast.ASTUpdate)1 ASTUpdateContainer (org.eclipse.rdf4j.query.parser.sparql.ast.ASTUpdateContainer)1 ASTUpdateSequence (org.eclipse.rdf4j.query.parser.sparql.ast.ASTUpdateSequence)1 Node (org.eclipse.rdf4j.query.parser.sparql.ast.Node)1 ParseException (org.eclipse.rdf4j.query.parser.sparql.ast.ParseException)1 TokenMgrError (org.eclipse.rdf4j.query.parser.sparql.ast.TokenMgrError)1 VisitorException (org.eclipse.rdf4j.query.parser.sparql.ast.VisitorException)1 Test (org.junit.Test)1