Search in sources :

Example 1 with ParsedTupleQuery

use of org.eclipse.rdf4j.query.parser.ParsedTupleQuery in project rdf4j by eclipse.

the class SPARQLParserTest method testParsedTupleQueryRootNode.

@Test
public void testParsedTupleQueryRootNode() throws Exception {
    StringBuilder qb = new StringBuilder();
    qb.append("SELECT *  {?a <foo:bar> \"test\"}");
    ParsedTupleQuery q = (ParsedTupleQuery) parser.parseQuery(qb.toString(), null);
    TupleExpr te = q.getTupleExpr();
    assertNotNull(te);
    assertTrue(te instanceof Projection);
    assertNull(te.getParentNode());
}
Also used : Projection(org.eclipse.rdf4j.query.algebra.Projection) TupleExpr(org.eclipse.rdf4j.query.algebra.TupleExpr) ParsedTupleQuery(org.eclipse.rdf4j.query.parser.ParsedTupleQuery) Test(org.junit.Test)

Example 2 with ParsedTupleQuery

use of org.eclipse.rdf4j.query.parser.ParsedTupleQuery 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 ParsedTupleQuery

use of org.eclipse.rdf4j.query.parser.ParsedTupleQuery in project rdf4j by eclipse.

the class QueryBuilderFactory method select.

/**
 * Create a QueryBuilder for creating a select query
 *
 * @param theProjectionVars
 *        the list of elements in the projection of the query
 * @return a select query builder
 */
public static QueryBuilder<ParsedTupleQuery> select(String... theProjectionVars) {
    QueryBuilder<ParsedTupleQuery> aBuilder = new AbstractQueryBuilder<ParsedTupleQuery>(new ParsedTupleQuery());
    aBuilder.addProjectionVar(theProjectionVars);
    return aBuilder;
}
Also used : ParsedTupleQuery(org.eclipse.rdf4j.query.parser.ParsedTupleQuery)

Example 4 with ParsedTupleQuery

use of org.eclipse.rdf4j.query.parser.ParsedTupleQuery in project rdf4j by eclipse.

the class SeRQLParser method parseQuery.

public ParsedQuery parseQuery(String queryStr, String baseURI) throws MalformedQueryException {
    try {
        ASTQueryContainer qc = SyntaxTreeBuilder.parseQuery(queryStr);
        // Replace deprecated NULL nodes with semantically equivalent
        // alternatives
        NullProcessor.process(qc);
        StringEscapesProcessor.process(qc);
        Map<String, String> namespaces = NamespaceDeclProcessor.process(qc);
        ProjectionProcessor.process(qc);
        qc.jjtAccept(new ProjectionAliasProcessor(), null);
        qc.jjtAccept(new AnonymousVarGenerator(), null);
        // TODO: check use of unbound variables?
        TupleExpr tupleExpr = QueryModelBuilder.buildQueryModel(qc, SimpleValueFactory.getInstance());
        ASTQuery queryNode = qc.getQuery();
        ParsedQuery query;
        if (queryNode instanceof ASTTupleQuery) {
            query = new ParsedTupleQuery(tupleExpr);
        } else if (queryNode instanceof ASTGraphQuery) {
            query = new ParsedGraphQuery(tupleExpr, namespaces);
        } else {
            throw new RuntimeException("Unexpected query type: " + queryNode.getClass());
        }
        return query;
    } 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 : ParsedQuery(org.eclipse.rdf4j.query.parser.ParsedQuery) ParsedGraphQuery(org.eclipse.rdf4j.query.parser.ParsedGraphQuery) ASTQueryContainer(org.eclipse.rdf4j.query.parser.serql.ast.ASTQueryContainer) TokenMgrError(org.eclipse.rdf4j.query.parser.serql.ast.TokenMgrError) TupleExpr(org.eclipse.rdf4j.query.algebra.TupleExpr) ASTTupleQuery(org.eclipse.rdf4j.query.parser.serql.ast.ASTTupleQuery) ASTQuery(org.eclipse.rdf4j.query.parser.serql.ast.ASTQuery) ASTGraphQuery(org.eclipse.rdf4j.query.parser.serql.ast.ASTGraphQuery) MalformedQueryException(org.eclipse.rdf4j.query.MalformedQueryException) ParseException(org.eclipse.rdf4j.query.parser.serql.ast.ParseException) VisitorException(org.eclipse.rdf4j.query.parser.serql.ast.VisitorException) ParsedTupleQuery(org.eclipse.rdf4j.query.parser.ParsedTupleQuery)

Example 5 with ParsedTupleQuery

use of org.eclipse.rdf4j.query.parser.ParsedTupleQuery in project rdf4j by eclipse.

the class SPARQLParserTest method testParseIntegerObjectValue.

@Test
public void testParseIntegerObjectValue() throws Exception {
    // test that the parser correctly parses the object value as an integer, instead of as a decimal.
    String query = "select ?Concept where { ?Concept a 1. ?Concept2 a 1. } ";
    ParsedTupleQuery q = (ParsedTupleQuery) parser.parseQuery(query, null);
    // all we're verifying is that the query is parsed without error. If it doesn't parse as integer but as a decimal, the
    // parser will fail, because the statement pattern doesn't end with a full-stop.
    assertNotNull(q);
}
Also used : ParsedTupleQuery(org.eclipse.rdf4j.query.parser.ParsedTupleQuery) Test(org.junit.Test)

Aggregations

ParsedTupleQuery (org.eclipse.rdf4j.query.parser.ParsedTupleQuery)7 TupleExpr (org.eclipse.rdf4j.query.algebra.TupleExpr)4 ParsedGraphQuery (org.eclipse.rdf4j.query.parser.ParsedGraphQuery)4 IRI (org.eclipse.rdf4j.model.IRI)2 MalformedQueryException (org.eclipse.rdf4j.query.MalformedQueryException)2 ParsedBooleanQuery (org.eclipse.rdf4j.query.parser.ParsedBooleanQuery)2 ParsedQuery (org.eclipse.rdf4j.query.parser.ParsedQuery)2 Test (org.junit.Test)2 Dataset (org.eclipse.rdf4j.query.Dataset)1 IncompatibleOperationException (org.eclipse.rdf4j.query.IncompatibleOperationException)1 Distinct (org.eclipse.rdf4j.query.algebra.Distinct)1 Order (org.eclipse.rdf4j.query.algebra.Order)1 OrderElem (org.eclipse.rdf4j.query.algebra.OrderElem)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 Reduced (org.eclipse.rdf4j.query.algebra.Reduced)1 Slice (org.eclipse.rdf4j.query.algebra.Slice)1 UnaryTupleOperator (org.eclipse.rdf4j.query.algebra.UnaryTupleOperator)1 StatementPatternCollector (org.eclipse.rdf4j.query.algebra.helpers.StatementPatternCollector)1