Search in sources :

Example 1 with ParsedQuery

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

the class SPARQLParserTest method testSES1927UnequalLiteralValueConstants2.

@Test
public void testSES1927UnequalLiteralValueConstants2() throws Exception {
    StringBuilder qb = new StringBuilder();
    qb.append("ASK {?a <foo:bar> \"test\". ?a <foo:foo> \"test\"^^<foo:bar> .} ");
    ParsedQuery q = parser.parseQuery(qb.toString(), null);
    TupleExpr te = q.getTupleExpr();
    assertNotNull(te);
    assertTrue(te instanceof Slice);
    Slice s = (Slice) te;
    assertTrue(s.getArg() instanceof Join);
    Join j = (Join) s.getArg();
    assertTrue(j.getLeftArg() instanceof StatementPattern);
    assertTrue(j.getRightArg() instanceof StatementPattern);
    StatementPattern leftArg = (StatementPattern) j.getLeftArg();
    StatementPattern rightArg = (StatementPattern) j.getRightArg();
    assertFalse(leftArg.getObjectVar().equals(rightArg.getObjectVar()));
    assertNotEquals(leftArg.getObjectVar().getName(), rightArg.getObjectVar().getName());
}
Also used : StatementPattern(org.eclipse.rdf4j.query.algebra.StatementPattern) ParsedQuery(org.eclipse.rdf4j.query.parser.ParsedQuery) Slice(org.eclipse.rdf4j.query.algebra.Slice) Join(org.eclipse.rdf4j.query.algebra.Join) TupleExpr(org.eclipse.rdf4j.query.algebra.TupleExpr) Test(org.junit.Test)

Example 2 with ParsedQuery

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

the class SPARQLParserTest method testOrderByWithAliases1.

@Test
public void testOrderByWithAliases1() throws Exception {
    String queryString = " SELECT ?x (SUM(?v1)/COUNT(?v1) as ?r) WHERE { ?x <urn:foo> ?v1 } GROUP BY ?x ORDER BY ?r";
    ParsedQuery query = parser.parseQuery(queryString, null);
    assertNotNull(query);
    TupleExpr te = query.getTupleExpr();
    assertTrue(te instanceof Projection);
    te = ((Projection) te).getArg();
    assertTrue(te instanceof Order);
    te = ((Order) te).getArg();
    assertTrue(te instanceof Extension);
}
Also used : Order(org.eclipse.rdf4j.query.algebra.Order) Extension(org.eclipse.rdf4j.query.algebra.Extension) ParsedQuery(org.eclipse.rdf4j.query.parser.ParsedQuery) Projection(org.eclipse.rdf4j.query.algebra.Projection) TupleExpr(org.eclipse.rdf4j.query.algebra.TupleExpr) Test(org.junit.Test)

Example 3 with ParsedQuery

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

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

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

the class SPARQLParserTest method testSourceStringAssignment.

/**
 * Test method for
 * {@link org.eclipse.rdf4j.query.parser.sparql.SPARQLParser#parseQuery(java.lang.String, java.lang.String)}
 * .
 */
@Test
public void testSourceStringAssignment() throws Exception {
    String simpleSparqlQuery = "SELECT * WHERE {?X ?P ?Y }";
    ParsedQuery q = parser.parseQuery(simpleSparqlQuery, null);
    assertNotNull(q);
    assertEquals(simpleSparqlQuery, q.getSourceString());
}
Also used : ParsedQuery(org.eclipse.rdf4j.query.parser.ParsedQuery) Test(org.junit.Test)

Aggregations

ParsedQuery (org.eclipse.rdf4j.query.parser.ParsedQuery)7 TupleExpr (org.eclipse.rdf4j.query.algebra.TupleExpr)6 Test (org.junit.Test)5 Join (org.eclipse.rdf4j.query.algebra.Join)3 Slice (org.eclipse.rdf4j.query.algebra.Slice)3 StatementPattern (org.eclipse.rdf4j.query.algebra.StatementPattern)3 MalformedQueryException (org.eclipse.rdf4j.query.MalformedQueryException)2 ParsedGraphQuery (org.eclipse.rdf4j.query.parser.ParsedGraphQuery)2 ParsedTupleQuery (org.eclipse.rdf4j.query.parser.ParsedTupleQuery)2 Dataset (org.eclipse.rdf4j.query.Dataset)1 IncompatibleOperationException (org.eclipse.rdf4j.query.IncompatibleOperationException)1 Extension (org.eclipse.rdf4j.query.algebra.Extension)1 Order (org.eclipse.rdf4j.query.algebra.Order)1 Projection (org.eclipse.rdf4j.query.algebra.Projection)1 ParsedBooleanQuery (org.eclipse.rdf4j.query.parser.ParsedBooleanQuery)1 ParsedDescribeQuery (org.eclipse.rdf4j.query.parser.ParsedDescribeQuery)1 ASTGraphQuery (org.eclipse.rdf4j.query.parser.serql.ast.ASTGraphQuery)1 ASTQuery (org.eclipse.rdf4j.query.parser.serql.ast.ASTQuery)1 ASTQueryContainer (org.eclipse.rdf4j.query.parser.serql.ast.ASTQueryContainer)1 ASTTupleQuery (org.eclipse.rdf4j.query.parser.serql.ast.ASTTupleQuery)1