Search in sources :

Example 46 with MalformedQueryException

use of org.openrdf.query.MalformedQueryException in project QueryAnalysis by Wikidata.

the class OpenRDFQueryHandler method computeSparqlStatistics.

@Override
protected void computeSparqlStatistics() {
    if (getValidityStatus() != QueryHandler.Validity.VALID) {
        this.sparqlStatistics = new HashMap<>();
        return;
    }
    try {
        ASTQueryContainer queryContainer = new StandardizingSPARQLParser().getDebuggedASTQueryContainer(getQueryString(), BASE_URI);
        QueryContainerSparqlStatisticsCollector queryContainerSparqlStatisticsCollector = new QueryContainerSparqlStatisticsCollector();
        queryContainer.jjtAccept(queryContainerSparqlStatisticsCollector, null);
        this.sparqlStatistics = queryContainerSparqlStatisticsCollector.getStatistics();
        TupleExprSparqlStatisticsCollector tupleExprSparqlStatisticsCollector = new TupleExprSparqlStatisticsCollector();
        this.query.getTupleExpr().visitChildren(tupleExprSparqlStatisticsCollector);
        this.query.getTupleExpr().visit(tupleExprSparqlStatisticsCollector);
        this.sparqlStatistics.putAll(tupleExprSparqlStatisticsCollector.getStatistics());
        this.primaryLanguage = tupleExprSparqlStatisticsCollector.getPrimaryLanguage();
    } catch (TokenMgrError | MalformedQueryException e) {
        logger.error("Failed to parse the query although it was found valid - this is a serious bug.", e);
    } catch (VisitorException e) {
        logger.error("Failed to calculate the SPARQL Keyword Statistics. Error occured while visiting the query.", e);
    } catch (Exception e) {
        logger.error("An unknown error occured while computing the sparql statistics: ", e);
    }
}
Also used : TupleExprSparqlStatisticsCollector(query.statistics.TupleExprSparqlStatisticsCollector) StandardizingSPARQLParser(openrdffork.StandardizingSPARQLParser) QueryContainerSparqlStatisticsCollector(query.statistics.QueryContainerSparqlStatisticsCollector) MalformedQueryException(org.openrdf.query.MalformedQueryException) MalformedQueryException(org.openrdf.query.MalformedQueryException) NoURIException(utility.NoURIException)

Example 47 with MalformedQueryException

use of org.openrdf.query.MalformedQueryException in project QueryAnalysis by Wikidata.

the class StandardizingPrefixDeclProcessor method process.

/**
 * Processes prefix declarations in queries. This method collects all
 * prefixes that are declared in the supplied query, verifies that prefixes
 * are not redefined and replaces any {@link ASTQName} nodes in the query
 * with equivalent {@link ASTIRI} nodes.
 *
 * @param qc The query that needs to be processed.
 * @return A map containing the prefixes that are declared in the query (key)
 * and the namespace they map to (value).
 * @throws MalformedQueryException If the query contains redefined prefixes or qnames that use
 *                                 undefined prefixes.
 */
public static Map<String, String> process(ASTOperationContainer qc) throws MalformedQueryException {
    List<ASTPrefixDecl> prefixDeclList = qc.getPrefixDeclList();
    // Build a prefix --> IRI map
    Map<String, String> prefixMap = new LinkedHashMap<String, String>();
    for (ASTPrefixDecl prefixDecl : prefixDeclList) {
        String prefix = prefixDecl.getPrefix();
        String iri = prefixDecl.getIRI().getValue();
        if (prefixMap.containsKey(prefix)) {
            throw new MalformedQueryException("Multiple prefix declarations for prefix '" + prefix + "'");
        }
        prefixMap.put(prefix, iri);
    }
    // insert some default prefixes (if not explicitly defined in the query)
    for (Map.Entry<String, String> entry : Main.prefixes.entrySet()) {
        insertDefaultPrefix(prefixMap, entry.getKey(), entry.getValue());
    }
    ASTUnparsedQuadDataBlock dataBlock = null;
    if (qc.getOperation() instanceof ASTInsertData) {
        ASTInsertData insertData = (ASTInsertData) qc.getOperation();
        dataBlock = insertData.jjtGetChild(ASTUnparsedQuadDataBlock.class);
    } else if (qc.getOperation() instanceof ASTDeleteData) {
        ASTDeleteData deleteData = (ASTDeleteData) qc.getOperation();
        dataBlock = deleteData.jjtGetChild(ASTUnparsedQuadDataBlock.class);
    }
    if (dataBlock != null) {
        String prefixes = createPrefixesInSPARQLFormat(prefixMap);
        dataBlock.setDataBlock(prefixes + dataBlock.getDataBlock());
    } else {
        QNameProcessor visitor = new QNameProcessor(prefixMap);
        try {
            qc.jjtAccept(visitor, null);
        } catch (VisitorException e) {
            throw new MalformedQueryException(e);
        }
    }
    return prefixMap;
}
Also used : LinkedHashMap(java.util.LinkedHashMap) MalformedQueryException(org.openrdf.query.MalformedQueryException) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 48 with MalformedQueryException

use of org.openrdf.query.MalformedQueryException in project QueryAnalysis by Wikidata.

the class StandardizingSPARQLParser method getDebuggedASTQueryContainer.

/**
 * @param queryString The query to parsed.
 * @param baseURI The base URI to resolve any possible relative URIs against.
 * @return The ASTQueryContainer representing this query after debugging.
 * @throws MalformedQueryException If the query was in any way malformed.
 */
public final ASTQueryContainer getDebuggedASTQueryContainer(String queryString, String baseURI) throws MalformedQueryException {
    try {
        ASTQueryContainer qc = SyntaxTreeBuilder.parseQuery(queryString);
        debug(qc);
        return qc;
    } catch (TokenMgrError | ParseException e) {
        throw new MalformedQueryException(e.getMessage(), e);
    }
}
Also used : MalformedQueryException(org.openrdf.query.MalformedQueryException)

Example 49 with MalformedQueryException

use of org.openrdf.query.MalformedQueryException in project stanbol by apache.

the class SesameYard method executeSparqlFieldQuery.

/**
 * Returns the SPARQL result set for a given {@link SparqlFieldQuery} that
 * was executed on this yard
 * @param con the repository connection to use
 * @param fieldQuery the SparqlFieldQuery instance
 * @param limit the maximum number of results
 * @return the results of the SPARQL query in the {@link #contexts} of the
 * Sesame Repository
 * @throws RepositoryException on any error while using the parsed connection
 * @throws QueryEvaluationException  on any error while executing the query
 * @throws YardException if the SPARQL query created for the parsed FieldQuery
 * was illegal formatted or if the {@link #repository} does not support
 * SPARQL.
 */
private TupleQueryResult executeSparqlFieldQuery(RepositoryConnection con, final SparqlFieldQuery fieldQuery, int limit, boolean select) throws RepositoryException, YardException, QueryEvaluationException {
    log.debug("> execute FieldQuery: {}", fieldQuery);
    String sparqlQueryString = SparqlQueryUtils.createSparqlSelectQuery(fieldQuery, select, limit, SparqlEndpointTypeEnum.Sesame);
    log.debug(" - SPARQL Query: {}", sparqlQueryString);
    TupleQuery sparqlOuery;
    try {
        sparqlOuery = con.prepareTupleQuery(QueryLanguage.SPARQL, sparqlQueryString);
    } catch (MalformedQueryException e) {
        log.error("Unable to pparse SPARQL Query generated for a FieldQuery");
        log.error("FieldQuery: {}", fieldQuery);
        log.error("SPARQL Query: {}", sparqlQueryString);
        log.error("Exception ", e);
        throw new YardException("Unable to parse SPARQL query generated for the parse FieldQuery", e);
    } catch (UnsupportedQueryTypeException e) {
        String message = "The Sesame Repository '" + repository + "'(class: " + repository.getClass().getName() + ") does not support SPARQL!";
        log.error(message, e);
        throw new YardException(message, e);
    }
    if (dataset != null) {
        // respect the configured contexts
        sparqlOuery.setDataset(dataset);
    }
    return sparqlOuery.evaluate();
}
Also used : YardException(org.apache.stanbol.entityhub.servicesapi.yard.YardException) MalformedQueryException(org.openrdf.query.MalformedQueryException) UnsupportedQueryTypeException(org.apache.stanbol.entityhub.servicesapi.query.UnsupportedQueryTypeException) TupleQuery(org.openrdf.query.TupleQuery)

Aggregations

MalformedQueryException (org.openrdf.query.MalformedQueryException)49 QueryEvaluationException (org.openrdf.query.QueryEvaluationException)15 RepositoryException (org.openrdf.repository.RepositoryException)14 ParsedQuery (org.openrdf.query.parser.ParsedQuery)12 SPARQLParser (org.openrdf.query.parser.sparql.SPARQLParser)11 TupleQuery (org.openrdf.query.TupleQuery)10 SailException (org.openrdf.sail.SailException)9 IOException (java.io.IOException)8 PCJStorageException (org.apache.rya.indexing.pcj.storage.PrecomputedJoinStorage.PCJStorageException)8 ArrayList (java.util.ArrayList)7 RyaClientException (org.apache.rya.api.client.RyaClientException)7 PrecomputedJoinStorage (org.apache.rya.indexing.pcj.storage.PrecomputedJoinStorage)7 VisibilityBindingSet (org.apache.rya.api.model.VisibilityBindingSet)6 RyaDAOException (org.apache.rya.api.persist.RyaDAOException)6 UnsupportedQueryException (org.apache.rya.indexing.pcj.fluo.app.query.UnsupportedQueryException)6 PcjException (org.apache.rya.indexing.pcj.storage.PcjException)6 PcjMetadata (org.apache.rya.indexing.pcj.storage.PcjMetadata)6 TupleQueryResultHandlerException (org.openrdf.query.TupleQueryResultHandlerException)6 TupleExpr (org.openrdf.query.algebra.TupleExpr)6 List (java.util.List)5