Search in sources :

Example 6 with VisitorException

use of org.eclipse.rdf4j.query.parser.sparql.ast.VisitorException 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)

Example 7 with VisitorException

use of org.eclipse.rdf4j.query.parser.sparql.ast.VisitorException 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 8 with VisitorException

use of org.eclipse.rdf4j.query.parser.sparql.ast.VisitorException in project rdf4j by eclipse.

the class WildcardProjectionProcessor method process.

public static void process(ASTOperationContainer container) throws MalformedQueryException {
    ASTOperation operation = container.getOperation();
    // scan for nested SELECT clauses in the operation's WHERE clause
    if (operation != null) {
        ASTWhereClause whereClause = operation.getWhereClause();
        // DESCRIBE queries and certain update operations can be without a WHERE clause
        if (whereClause != null) {
            SelectClauseCollector collector = new SelectClauseCollector();
            try {
                whereClause.jjtAccept(collector, null);
                Set<ASTSelect> selectClauses = collector.getSelectClauses();
                for (ASTSelect selectClause : selectClauses) {
                    if (selectClause.isWildcard()) {
                        ASTSelectQuery q = (ASTSelectQuery) selectClause.jjtGetParent();
                        addQueryVars(q.getWhereClause(), selectClause);
                        selectClause.setWildcard(false);
                    }
                }
            } catch (VisitorException e) {
                throw new MalformedQueryException(e);
            }
        }
    }
    if (operation instanceof ASTSelectQuery) {
        // check for wildcard in upper SELECT query
        ASTSelectQuery selectQuery = (ASTSelectQuery) operation;
        ASTSelect selectClause = selectQuery.getSelect();
        if (selectClause.isWildcard()) {
            addQueryVars(selectQuery.getWhereClause(), selectClause);
            selectClause.setWildcard(false);
        }
    } else if (operation instanceof ASTDescribeQuery) {
        // check for possible wildcard in DESCRIBE query
        ASTDescribeQuery describeQuery = (ASTDescribeQuery) operation;
        ASTDescribe describeClause = describeQuery.getDescribe();
        if (describeClause.isWildcard()) {
            addQueryVars(describeQuery.getWhereClause(), describeClause);
            describeClause.setWildcard(false);
        }
    }
}
Also used : ASTOperation(org.eclipse.rdf4j.query.parser.sparql.ast.ASTOperation) ASTWhereClause(org.eclipse.rdf4j.query.parser.sparql.ast.ASTWhereClause) MalformedQueryException(org.eclipse.rdf4j.query.MalformedQueryException) ASTSelectQuery(org.eclipse.rdf4j.query.parser.sparql.ast.ASTSelectQuery) VisitorException(org.eclipse.rdf4j.query.parser.sparql.ast.VisitorException) ASTDescribeQuery(org.eclipse.rdf4j.query.parser.sparql.ast.ASTDescribeQuery) ASTDescribe(org.eclipse.rdf4j.query.parser.sparql.ast.ASTDescribe) ASTSelect(org.eclipse.rdf4j.query.parser.sparql.ast.ASTSelect)

Aggregations

VisitorException (org.eclipse.rdf4j.query.parser.sparql.ast.VisitorException)8 MalformedQueryException (org.eclipse.rdf4j.query.MalformedQueryException)6 ASTInsertData (org.eclipse.rdf4j.query.parser.sparql.ast.ASTInsertData)3 TupleExpr (org.eclipse.rdf4j.query.algebra.TupleExpr)2 Var (org.eclipse.rdf4j.query.algebra.Var)2 ASTDeleteData (org.eclipse.rdf4j.query.parser.sparql.ast.ASTDeleteData)2 ASTPrefixDecl (org.eclipse.rdf4j.query.parser.sparql.ast.ASTPrefixDecl)2 ASTUnparsedQuadDataBlock (org.eclipse.rdf4j.query.parser.sparql.ast.ASTUnparsedQuadDataBlock)2 URISyntaxException (java.net.URISyntaxException)1 HashSet (java.util.HashSet)1 LinkedHashMap (java.util.LinkedHashMap)1 ParsedIRI (org.eclipse.rdf4j.common.net.ParsedIRI)1 Dataset (org.eclipse.rdf4j.query.Dataset)1 Modify (org.eclipse.rdf4j.query.algebra.Modify)1 UpdateExpr (org.eclipse.rdf4j.query.algebra.UpdateExpr)1 ParsedUpdate (org.eclipse.rdf4j.query.parser.ParsedUpdate)1 ASTBaseDecl (org.eclipse.rdf4j.query.parser.sparql.ast.ASTBaseDecl)1 ASTDescribe (org.eclipse.rdf4j.query.parser.sparql.ast.ASTDescribe)1 ASTDescribeQuery (org.eclipse.rdf4j.query.parser.sparql.ast.ASTDescribeQuery)1 ASTModify (org.eclipse.rdf4j.query.parser.sparql.ast.ASTModify)1