Search in sources :

Example 1 with MalformedQueryException

use of org.eclipse.rdf4j.query.MalformedQueryException in project rdf4j by eclipse.

the class TupleExprBuilder method visit.

@Override
public TupleExpr visit(ASTConstructQuery node, Object data) throws VisitorException {
    // Start with building the graph pattern
    graphPattern = new GraphPattern();
    node.getWhereClause().jjtAccept(this, null);
    TupleExpr tupleExpr = graphPattern.buildTupleExpr();
    // Apply grouping
    ASTGroupClause groupNode = node.getGroupClause();
    if (groupNode != null) {
        tupleExpr = (TupleExpr) groupNode.jjtAccept(this, tupleExpr);
    }
    Group group = null;
    if (tupleExpr instanceof Group) {
        group = (Group) tupleExpr;
    } else {
        // create a new implicit group. Note that this group will only
        // actually
        // be used in the query model if the query has HAVING or ORDER BY
        // clause
        group = new Group(tupleExpr);
    }
    // Apply HAVING group filter condition
    tupleExpr = processHavingClause(node.getHavingClause(), tupleExpr, group);
    // process bindings clause
    ASTBindingsClause bindingsClause = node.getBindingsClause();
    if (bindingsClause != null) {
        tupleExpr = new Join((BindingSetAssignment) bindingsClause.jjtAccept(this, null), tupleExpr);
    }
    // Apply result ordering
    tupleExpr = processOrderClause(node.getOrderClause(), tupleExpr, null);
    // Process construct clause
    ASTConstruct constructNode = node.getConstruct();
    if (!constructNode.isWildcard()) {
        tupleExpr = (TupleExpr) constructNode.jjtAccept(this, tupleExpr);
    } else {
        // create construct clause from graph pattern.
        ConstructorBuilder cb = new ConstructorBuilder();
        // possible future use.
        try {
            tupleExpr = cb.buildConstructor(tupleExpr, false, false);
        } catch (MalformedQueryException e) {
            throw new VisitorException(e.getMessage());
        }
    }
    // process limit and offset clauses
    ASTLimit limitNode = node.getLimit();
    long limit = -1L;
    if (limitNode != null) {
        limit = (Long) limitNode.jjtAccept(this, null);
    }
    ASTOffset offsetNode = node.getOffset();
    long offset = -1;
    if (offsetNode != null) {
        offset = (Long) offsetNode.jjtAccept(this, null);
    }
    if (offset >= 1 || limit >= 0) {
        tupleExpr = new Slice(tupleExpr, offset, limit);
    }
    return tupleExpr;
}
Also used : Group(org.eclipse.rdf4j.query.algebra.Group) BindingSetAssignment(org.eclipse.rdf4j.query.algebra.BindingSetAssignment) Join(org.eclipse.rdf4j.query.algebra.Join) TupleExpr(org.eclipse.rdf4j.query.algebra.TupleExpr) Slice(org.eclipse.rdf4j.query.algebra.Slice) MalformedQueryException(org.eclipse.rdf4j.query.MalformedQueryException)

Example 2 with MalformedQueryException

use of org.eclipse.rdf4j.query.MalformedQueryException in project rdf4j by eclipse.

the class SPARQLParser method parseQuery.

@Override
public ParsedQuery parseQuery(String queryStr, String baseURI) throws MalformedQueryException {
    try {
        ASTQueryContainer qc = SyntaxTreeBuilder.parseQuery(queryStr);
        StringEscapesProcessor.process(qc);
        BaseDeclProcessor.process(qc, baseURI);
        Map<String, String> prefixes = PrefixDeclProcessor.process(qc);
        WildcardProjectionProcessor.process(qc);
        BlankNodeVarProcessor.process(qc);
        if (qc.containsQuery()) {
            // handle query operation
            TupleExpr tupleExpr = buildQueryModel(qc);
            ParsedQuery query;
            ASTQuery queryNode = qc.getQuery();
            if (queryNode instanceof ASTSelectQuery) {
                query = new ParsedTupleQuery(queryStr, tupleExpr);
            } else if (queryNode instanceof ASTConstructQuery) {
                query = new ParsedGraphQuery(queryStr, tupleExpr, prefixes);
            } else if (queryNode instanceof ASTAskQuery) {
                query = new ParsedBooleanQuery(queryStr, tupleExpr);
            } else if (queryNode instanceof ASTDescribeQuery) {
                query = new ParsedDescribeQuery(queryStr, tupleExpr, prefixes);
            } else {
                throw new RuntimeException("Unexpected query type: " + queryNode.getClass());
            }
            // Handle dataset declaration
            Dataset dataset = DatasetDeclProcessor.process(qc);
            if (dataset != null) {
                query.setDataset(dataset);
            }
            return query;
        } else {
            throw new IncompatibleOperationException("supplied string is not a query operation");
        }
    } catch (ParseException e) {
        throw new MalformedQueryException(e.getMessage(), e);
    } catch (TokenMgrError e) {
        throw new MalformedQueryException(e.getMessage(), e);
    }
}
Also used : ASTAskQuery(org.eclipse.rdf4j.query.parser.sparql.ast.ASTAskQuery) ParsedDescribeQuery(org.eclipse.rdf4j.query.parser.ParsedDescribeQuery) IncompatibleOperationException(org.eclipse.rdf4j.query.IncompatibleOperationException) ParsedQuery(org.eclipse.rdf4j.query.parser.ParsedQuery) Dataset(org.eclipse.rdf4j.query.Dataset) ParsedGraphQuery(org.eclipse.rdf4j.query.parser.ParsedGraphQuery) ASTQueryContainer(org.eclipse.rdf4j.query.parser.sparql.ast.ASTQueryContainer) TokenMgrError(org.eclipse.rdf4j.query.parser.sparql.ast.TokenMgrError) ASTDescribeQuery(org.eclipse.rdf4j.query.parser.sparql.ast.ASTDescribeQuery) TupleExpr(org.eclipse.rdf4j.query.algebra.TupleExpr) ASTQuery(org.eclipse.rdf4j.query.parser.sparql.ast.ASTQuery) ParsedBooleanQuery(org.eclipse.rdf4j.query.parser.ParsedBooleanQuery) MalformedQueryException(org.eclipse.rdf4j.query.MalformedQueryException) ASTConstructQuery(org.eclipse.rdf4j.query.parser.sparql.ast.ASTConstructQuery) ASTSelectQuery(org.eclipse.rdf4j.query.parser.sparql.ast.ASTSelectQuery) ParseException(org.eclipse.rdf4j.query.parser.sparql.ast.ParseException) ParsedTupleQuery(org.eclipse.rdf4j.query.parser.ParsedTupleQuery)

Example 3 with MalformedQueryException

use of org.eclipse.rdf4j.query.MalformedQueryException in project rdf4j by eclipse.

the class DatasetDeclProcessor method process.

/**
 * Extracts a SPARQL {@link Dataset} from an ASTQueryContainer, if one is contained. Returns null
 * otherwise.
 *
 * @param qc
 *        The query model to resolve relative URIs in.
 * @throws MalformedQueryException
 *         If DatasetClause does not contain a valid URI.
 */
public static Dataset process(ASTOperationContainer qc) throws MalformedQueryException {
    SimpleDataset dataset = null;
    ASTOperation op = qc.getOperation();
    if (op != null) {
        List<ASTDatasetClause> datasetClauses = op.getDatasetClauseList();
        if (!datasetClauses.isEmpty()) {
            dataset = new SimpleDataset();
            for (ASTDatasetClause dc : datasetClauses) {
                ASTIRI astIri = dc.jjtGetChild(ASTIRI.class);
                try {
                    IRI uri = SESAME.NIL;
                    if (astIri != null) {
                        uri = SimpleValueFactory.getInstance().createIRI(astIri.getValue());
                    }
                    boolean withClause = false;
                    if (op instanceof ASTModify) {
                        if (dc.equals(((ASTModify) op).getWithClause())) {
                            withClause = true;
                            dataset.setDefaultInsertGraph(uri);
                            dataset.addDefaultRemoveGraph(uri);
                        }
                    }
                    // clauses.
                    if (!withClause || datasetClauses.size() == 1) {
                        if (dc.isNamed()) {
                            dataset.addNamedGraph(uri);
                        } else {
                            dataset.addDefaultGraph(uri);
                        }
                    }
                } catch (IllegalArgumentException e) {
                    throw new MalformedQueryException(e.getMessage(), e);
                }
            }
        }
    }
    return dataset;
}
Also used : ASTIRI(org.eclipse.rdf4j.query.parser.sparql.ast.ASTIRI) IRI(org.eclipse.rdf4j.model.IRI) ASTModify(org.eclipse.rdf4j.query.parser.sparql.ast.ASTModify) ASTOperation(org.eclipse.rdf4j.query.parser.sparql.ast.ASTOperation) ASTDatasetClause(org.eclipse.rdf4j.query.parser.sparql.ast.ASTDatasetClause) MalformedQueryException(org.eclipse.rdf4j.query.MalformedQueryException) SimpleDataset(org.eclipse.rdf4j.query.impl.SimpleDataset) ASTIRI(org.eclipse.rdf4j.query.parser.sparql.ast.ASTIRI)

Example 4 with MalformedQueryException

use of org.eclipse.rdf4j.query.MalformedQueryException in project rdf4j by eclipse.

the class WildcardProjectionProcessor method addQueryVars.

private static void addQueryVars(ASTWhereClause queryBody, Node wildcardNode) throws MalformedQueryException {
    QueryVariableCollector visitor = new QueryVariableCollector();
    try {
        // Collect variable names from query
        queryBody.jjtAccept(visitor, null);
        // Adds ASTVar nodes to the ASTProjectionElem nodes and to the parent
        for (String varName : visitor.getVariableNames()) {
            ASTVar varNode = new ASTVar(SyntaxTreeBuilderTreeConstants.JJTVAR);
            ASTProjectionElem projectionElemNode = new ASTProjectionElem(SyntaxTreeBuilderTreeConstants.JJTPROJECTIONELEM);
            varNode.setName(varName);
            projectionElemNode.jjtAppendChild(varNode);
            varNode.jjtSetParent(projectionElemNode);
            wildcardNode.jjtAppendChild(projectionElemNode);
            projectionElemNode.jjtSetParent(wildcardNode);
        }
    } catch (VisitorException e) {
        throw new MalformedQueryException(e);
    }
}
Also used : ASTVar(org.eclipse.rdf4j.query.parser.sparql.ast.ASTVar) MalformedQueryException(org.eclipse.rdf4j.query.MalformedQueryException) ASTProjectionElem(org.eclipse.rdf4j.query.parser.sparql.ast.ASTProjectionElem) VisitorException(org.eclipse.rdf4j.query.parser.sparql.ast.VisitorException)

Example 5 with MalformedQueryException

use of org.eclipse.rdf4j.query.MalformedQueryException in project rdf4j by eclipse.

the class RDF4JProtocolSession method getStatements.

/*---------------------------*
	 * Get/add/remove statements *
	 *---------------------------*/
public void getStatements(Resource subj, IRI pred, Value obj, boolean includeInferred, RDFHandler handler, Resource... contexts) throws IOException, RDFHandlerException, RepositoryException, UnauthorizedException, QueryInterruptedException {
    checkRepositoryURL();
    try {
        String transactionURL = getTransactionURL();
        final boolean useTransaction = transactionURL != null;
        String baseLocation = useTransaction ? transactionURL : Protocol.getStatementsLocation(getQueryURL());
        URIBuilder url = new URIBuilder(baseLocation);
        if (subj != null) {
            url.setParameter(Protocol.SUBJECT_PARAM_NAME, Protocol.encodeValue(subj));
        }
        if (pred != null) {
            url.setParameter(Protocol.PREDICATE_PARAM_NAME, Protocol.encodeValue(pred));
        }
        if (obj != null) {
            url.setParameter(Protocol.OBJECT_PARAM_NAME, Protocol.encodeValue(obj));
        }
        for (String encodedContext : Protocol.encodeContexts(contexts)) {
            url.addParameter(Protocol.CONTEXT_PARAM_NAME, encodedContext);
        }
        url.setParameter(Protocol.INCLUDE_INFERRED_PARAM_NAME, Boolean.toString(includeInferred));
        if (useTransaction) {
            url.setParameter(Protocol.ACTION_PARAM_NAME, Action.GET.toString());
        }
        HttpRequestBase method = useTransaction ? new HttpPut(url.build()) : new HttpGet(url.build());
        try {
            getRDF(method, handler, true);
        } catch (MalformedQueryException e) {
            logger.warn("Server reported unexpected malfored query error", e);
            throw new RepositoryException(e.getMessage(), e);
        } finally {
            method.reset();
        }
    } catch (URISyntaxException e) {
        throw new AssertionError(e);
    }
    pingTransaction();
}
Also used : HttpRequestBase(org.apache.http.client.methods.HttpRequestBase) HttpGet(org.apache.http.client.methods.HttpGet) MalformedQueryException(org.eclipse.rdf4j.query.MalformedQueryException) RepositoryException(org.eclipse.rdf4j.repository.RepositoryException) URISyntaxException(java.net.URISyntaxException) HttpPut(org.apache.http.client.methods.HttpPut) URIBuilder(org.apache.http.client.utils.URIBuilder)

Aggregations

MalformedQueryException (org.eclipse.rdf4j.query.MalformedQueryException)33 RepositoryException (org.eclipse.rdf4j.repository.RepositoryException)20 QueryEvaluationException (org.eclipse.rdf4j.query.QueryEvaluationException)9 IOException (java.io.IOException)6 TupleQuery (org.eclipse.rdf4j.query.TupleQuery)5 TupleQueryResult (org.eclipse.rdf4j.query.TupleQueryResult)5 VisitorException (org.eclipse.rdf4j.query.parser.sparql.ast.VisitorException)5 HttpGet (org.apache.http.client.methods.HttpGet)4 QueryInterruptedException (org.eclipse.rdf4j.query.QueryInterruptedException)4 UnsupportedQueryLanguageException (org.eclipse.rdf4j.query.UnsupportedQueryLanguageException)4 UpdateExecutionException (org.eclipse.rdf4j.query.UpdateExecutionException)4 ExceptionConvertingIteration (org.eclipse.rdf4j.common.iteration.ExceptionConvertingIteration)3 SPARQLProtocolSession (org.eclipse.rdf4j.http.client.SPARQLProtocolSession)3 Binding (org.eclipse.rdf4j.query.Binding)3 TupleExpr (org.eclipse.rdf4j.query.algebra.TupleExpr)3 ParsedUpdate (org.eclipse.rdf4j.query.parser.ParsedUpdate)3 ASTInsertData (org.eclipse.rdf4j.query.parser.sparql.ast.ASTInsertData)3 UnknownTransactionStateException (org.eclipse.rdf4j.repository.UnknownTransactionStateException)3 RDFHandlerException (org.eclipse.rdf4j.rio.RDFHandlerException)3 RDFParseException (org.eclipse.rdf4j.rio.RDFParseException)3