Search in sources :

Example 1 with Dataset

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

the class StandardizingSPARQLParser method parseQuery.

/**
 * @param qc      The query container to be parsed
 * @param baseURI The base URI to resolve any possible relative URIs against
 * @return The parsed query
 * @throws MalformedQueryException If the query was in any way malformed
 */
@SuppressWarnings("WeakerAccess")
public final ParsedQuery parseQuery(ASTQueryContainer qc, String baseURI) throws MalformedQueryException {
    StringEscapesProcessor.process(qc);
    BaseDeclProcessor.process(qc, baseURI);
    Map<String, String> prefixes = StandardizingPrefixDeclProcessor.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(qc.getSourceString(), tupleExpr);
        } else if (queryNode instanceof ASTConstructQuery) {
            query = new ParsedGraphQuery(qc.getSourceString(), tupleExpr, prefixes);
        } else if (queryNode instanceof ASTAskQuery) {
            query = new ParsedBooleanQuery(qc.getSourceString(), tupleExpr);
        } else if (queryNode instanceof ASTDescribeQuery) {
            query = new ParsedGraphQuery(qc.getSourceString(), 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");
    }
}
Also used : IncompatibleOperationException(org.openrdf.query.IncompatibleOperationException) ParsedQuery(org.openrdf.query.parser.ParsedQuery) Dataset(org.openrdf.query.Dataset) ParsedGraphQuery(org.openrdf.query.parser.ParsedGraphQuery) TupleExpr(org.openrdf.query.algebra.TupleExpr) ParsedBooleanQuery(org.openrdf.query.parser.ParsedBooleanQuery) ParsedTupleQuery(org.openrdf.query.parser.ParsedTupleQuery)

Aggregations

Dataset (org.openrdf.query.Dataset)1 IncompatibleOperationException (org.openrdf.query.IncompatibleOperationException)1 TupleExpr (org.openrdf.query.algebra.TupleExpr)1 ParsedBooleanQuery (org.openrdf.query.parser.ParsedBooleanQuery)1 ParsedGraphQuery (org.openrdf.query.parser.ParsedGraphQuery)1 ParsedQuery (org.openrdf.query.parser.ParsedQuery)1 ParsedTupleQuery (org.openrdf.query.parser.ParsedTupleQuery)1