Search in sources :

Example 1 with ASTQueryContainer

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

the class TupleExprBuilderTest method testAskQuerySolutionModifiers.

@Test
public void testAskQuerySolutionModifiers() {
    String query = "ASK WHERE { ?foo ?bar ?baz . } ORDER BY ?foo LIMIT 1";
    try {
        TupleExprBuilder builder = new TupleExprBuilder(ValueFactoryImpl.getInstance());
        ASTQueryContainer qc = SyntaxTreeBuilder.parseQuery(query);
        TupleExpr result = builder.visit(qc, null);
        assertTrue(result instanceof Order);
    } catch (Exception e) {
        e.printStackTrace();
        fail("should parse ask query with solution modifiers");
    }
}
Also used : Order(org.eclipse.rdf4j.query.algebra.Order) ASTQueryContainer(org.eclipse.rdf4j.query.parser.sparql.ast.ASTQueryContainer) TupleExpr(org.eclipse.rdf4j.query.algebra.TupleExpr) ParseException(org.eclipse.rdf4j.query.parser.sparql.ast.ParseException) VisitorException(org.eclipse.rdf4j.query.parser.sparql.ast.VisitorException) Test(org.junit.Test)

Example 2 with ASTQueryContainer

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

the class TupleExprBuilderTest method testServiceGraphPatternStringDetection3.

@Test
public void testServiceGraphPatternStringDetection3() throws TokenMgrError, ParseException, VisitorException {
    String servicePattern1 = "SERVICE <foo:bar> \n { ?x <foo:baz> ?y. }";
    String servicePattern2 = "SERVICE <foo:bar2> \n { ?x <foo:baz> ?y. }";
    StringBuilder qb = new StringBuilder();
    qb.append("SELECT * \n");
    qb.append("WHERE { \n");
    qb.append(servicePattern1);
    qb.append(" OPTIONAL { \n");
    qb.append(servicePattern2);
    qb.append("    } \n");
    qb.append(" } ");
    ASTQueryContainer qc = SyntaxTreeBuilder.parseQuery(qb.toString());
    ServiceNodeFinder f = new ServiceNodeFinder();
    f.visit(qc, null);
    assertTrue(f.getGraphPatterns().size() == 2);
    assertTrue(servicePattern1.equals(f.getGraphPatterns().get(0)));
    assertTrue(servicePattern2.equals(f.getGraphPatterns().get(1)));
}
Also used : ASTQueryContainer(org.eclipse.rdf4j.query.parser.sparql.ast.ASTQueryContainer) Test(org.junit.Test)

Example 3 with ASTQueryContainer

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

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

the class TupleExprBuilderTest method testServiceGraphPatternStringDetection1.

@Test
public void testServiceGraphPatternStringDetection1() throws TokenMgrError, ParseException, VisitorException {
    String servicePattern = "SERVICE <foo:bar> { ?x <foo:baz> ?y }";
    StringBuilder qb = new StringBuilder();
    qb.append("SELECT * \n");
    qb.append("WHERE { \n");
    qb.append(" { ?s ?p ?o } \n");
    qb.append(" UNION \n");
    qb.append(" { ?p ?q ?r } \n");
    qb.append(servicePattern);
    qb.append("\n");
    qb.append(" FILTER (?s = <foo:bar>) ");
    qb.append(" } ");
    ASTQueryContainer qc = SyntaxTreeBuilder.parseQuery(qb.toString());
    ServiceNodeFinder f = new ServiceNodeFinder();
    f.visit(qc, null);
    assertTrue(f.getGraphPatterns().size() == 1);
    assertTrue(servicePattern.equals(f.getGraphPatterns().get(0)));
}
Also used : ASTQueryContainer(org.eclipse.rdf4j.query.parser.sparql.ast.ASTQueryContainer) Test(org.junit.Test)

Example 5 with ASTQueryContainer

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

the class TupleExprBuilderTest method testServiceGraphPatternStringDetection2.

@Test
public void testServiceGraphPatternStringDetection2() throws TokenMgrError, ParseException, VisitorException {
    String servicePattern = "SERVICE <foo:bar> \r\n { ?x <foo:baz> ?y. \r\n \r\n }";
    StringBuilder qb = new StringBuilder();
    qb.append("SELECT * \n");
    qb.append("WHERE { \n");
    qb.append(" { ?s ?p ?o } \n");
    qb.append(" UNION \n");
    qb.append(" { ?p ?q ?r } \n");
    qb.append(servicePattern);
    qb.append("\n");
    qb.append(" FILTER (?s = <foo:bar>) ");
    qb.append(" } ");
    ASTQueryContainer qc = SyntaxTreeBuilder.parseQuery(qb.toString());
    ServiceNodeFinder f = new ServiceNodeFinder();
    f.visit(qc, null);
    assertTrue(f.getGraphPatterns().size() == 1);
    assertTrue(servicePattern.equals(f.getGraphPatterns().get(0)));
}
Also used : ASTQueryContainer(org.eclipse.rdf4j.query.parser.sparql.ast.ASTQueryContainer) Test(org.junit.Test)

Aggregations

ASTQueryContainer (org.eclipse.rdf4j.query.parser.sparql.ast.ASTQueryContainer)6 Test (org.junit.Test)5 TupleExpr (org.eclipse.rdf4j.query.algebra.TupleExpr)2 ParseException (org.eclipse.rdf4j.query.parser.sparql.ast.ParseException)2 Dataset (org.eclipse.rdf4j.query.Dataset)1 IncompatibleOperationException (org.eclipse.rdf4j.query.IncompatibleOperationException)1 MalformedQueryException (org.eclipse.rdf4j.query.MalformedQueryException)1 Order (org.eclipse.rdf4j.query.algebra.Order)1 ParsedBooleanQuery (org.eclipse.rdf4j.query.parser.ParsedBooleanQuery)1 ParsedDescribeQuery (org.eclipse.rdf4j.query.parser.ParsedDescribeQuery)1 ParsedGraphQuery (org.eclipse.rdf4j.query.parser.ParsedGraphQuery)1 ParsedQuery (org.eclipse.rdf4j.query.parser.ParsedQuery)1 ParsedTupleQuery (org.eclipse.rdf4j.query.parser.ParsedTupleQuery)1 ASTAskQuery (org.eclipse.rdf4j.query.parser.sparql.ast.ASTAskQuery)1 ASTConstructQuery (org.eclipse.rdf4j.query.parser.sparql.ast.ASTConstructQuery)1 ASTDescribeQuery (org.eclipse.rdf4j.query.parser.sparql.ast.ASTDescribeQuery)1 ASTQuery (org.eclipse.rdf4j.query.parser.sparql.ast.ASTQuery)1 ASTSelectQuery (org.eclipse.rdf4j.query.parser.sparql.ast.ASTSelectQuery)1 TokenMgrError (org.eclipse.rdf4j.query.parser.sparql.ast.TokenMgrError)1 VisitorException (org.eclipse.rdf4j.query.parser.sparql.ast.VisitorException)1