Search in sources :

Example 1 with ParsedBooleanQuery

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

the class SPARQLParserTest method testParsedBooleanQueryRootNode.

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

Example 2 with ParsedBooleanQuery

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

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

the class SPARQLQueryRenderer method render.

/**
 * @inheritDoc
 */
public String render(final ParsedQuery theQuery) throws Exception {
    mRenderer.reset();
    StringBuffer aBody = new StringBuffer(mRenderer.render(theQuery.getTupleExpr()));
    boolean aFirst = true;
    StringBuffer aQuery = new StringBuffer();
    if (theQuery instanceof ParsedTupleQuery) {
        aQuery.append("select ");
    } else if (theQuery instanceof ParsedBooleanQuery) {
        aQuery.append("ask\n");
    } else {
        aQuery.append("construct ");
    }
    if (mRenderer.isDistinct()) {
        aQuery.append("distinct ");
    }
    if (mRenderer.isReduced() && theQuery instanceof ParsedTupleQuery) {
        aQuery.append("reduced ");
    }
    if (!mRenderer.getProjection().isEmpty() && !(theQuery instanceof ParsedBooleanQuery)) {
        aFirst = true;
        if (!(theQuery instanceof ParsedTupleQuery)) {
            aQuery.append(" {\n");
        }
        for (ProjectionElemList aList : mRenderer.getProjection()) {
            if (SparqlTupleExprRenderer.isSPOElemList(aList)) {
                if (!aFirst) {
                    aQuery.append("\n");
                } else {
                    aFirst = false;
                }
                aQuery.append("  ").append(mRenderer.renderPattern(mRenderer.toStatementPattern(aList)));
            } else {
                for (ProjectionElem aElem : aList.getElements()) {
                    if (!aFirst) {
                        aQuery.append(" ");
                    } else {
                        aFirst = false;
                    }
                    aQuery.append("?" + aElem.getSourceName());
                // SPARQL does not support this, its an artifact of copy and
                // paste from the serql stuff
                // aQuery.append(mRenderer.getExtensions().containsKey(aElem.getSourceName())
                // ?
                // mRenderer.renderValueExpr(mRenderer.getExtensions().get(aElem.getSourceName()))
                // : "?"+aElem.getSourceName());
                // 
                // if (!aElem.getSourceName().equals(aElem.getTargetName()) ||
                // (mRenderer.getExtensions().containsKey(aElem.getTargetName())
                // &&
                // !mRenderer.getExtensions().containsKey(aElem.getSourceName())))
                // {
                // aQuery.append(" as ").append(mRenderer.getExtensions().containsKey(aElem.getTargetName())
                // ?
                // mRenderer.renderValueExpr(mRenderer.getExtensions().get(aElem.getTargetName()))
                // : aElem.getTargetName());
                // }
                }
            }
        }
        if (!(theQuery instanceof ParsedTupleQuery)) {
            aQuery.append("}");
        }
        aQuery.append("\n");
    } else if (mRenderer.getProjection().isEmpty()) {
        if (theQuery instanceof ParsedGraphQuery) {
            aQuery.append("{ }\n");
        } else if (theQuery instanceof ParsedTupleQuery) {
            aQuery.append("*\n");
        }
    }
    if (theQuery.getDataset() != null) {
        for (IRI aURI : theQuery.getDataset().getDefaultGraphs()) {
            aQuery.append("from <").append(aURI).append(">\n");
        }
        for (IRI aURI : theQuery.getDataset().getNamedGraphs()) {
            aQuery.append("from named <").append(aURI).append(">\n");
        }
    }
    if (aBody.length() > 0) {
        // test cases to prove these things work would be lovely.
        if (aBody.toString().trim().lastIndexOf(",") == aBody.length() - 1) {
            aBody.setCharAt(aBody.lastIndexOf(","), ' ');
        }
        if (!(theQuery instanceof ParsedBooleanQuery)) {
            aQuery.append("where ");
        }
        aQuery.append("{\n");
        aQuery.append(aBody);
        aQuery.append("}");
    }
    if (!mRenderer.getOrdering().isEmpty()) {
        aQuery.append("\norder by ");
        aFirst = true;
        for (OrderElem aOrder : mRenderer.getOrdering()) {
            if (!aFirst) {
                aQuery.append(" ");
            } else {
                aFirst = false;
            }
            if (aOrder.isAscending()) {
                aQuery.append(mRenderer.renderValueExpr(aOrder.getExpr()));
            } else {
                aQuery.append("desc(");
                aQuery.append(mRenderer.renderValueExpr(aOrder.getExpr()));
                aQuery.append(")");
            }
        }
    }
    if (mRenderer.getLimit() != -1 && !(theQuery instanceof ParsedBooleanQuery)) {
        aQuery.append("\nlimit ").append(mRenderer.getLimit());
    }
    if (mRenderer.getOffset() != -1) {
        aQuery.append("\noffset ").append(mRenderer.getOffset());
    }
    return aQuery.toString();
}
Also used : ProjectionElemList(org.eclipse.rdf4j.query.algebra.ProjectionElemList) IRI(org.eclipse.rdf4j.model.IRI) ParsedGraphQuery(org.eclipse.rdf4j.query.parser.ParsedGraphQuery) ParsedBooleanQuery(org.eclipse.rdf4j.query.parser.ParsedBooleanQuery) OrderElem(org.eclipse.rdf4j.query.algebra.OrderElem) ProjectionElem(org.eclipse.rdf4j.query.algebra.ProjectionElem) ParsedTupleQuery(org.eclipse.rdf4j.query.parser.ParsedTupleQuery)

Aggregations

ParsedBooleanQuery (org.eclipse.rdf4j.query.parser.ParsedBooleanQuery)3 TupleExpr (org.eclipse.rdf4j.query.algebra.TupleExpr)2 ParsedGraphQuery (org.eclipse.rdf4j.query.parser.ParsedGraphQuery)2 ParsedTupleQuery (org.eclipse.rdf4j.query.parser.ParsedTupleQuery)2 IRI (org.eclipse.rdf4j.model.IRI)1 Dataset (org.eclipse.rdf4j.query.Dataset)1 IncompatibleOperationException (org.eclipse.rdf4j.query.IncompatibleOperationException)1 MalformedQueryException (org.eclipse.rdf4j.query.MalformedQueryException)1 OrderElem (org.eclipse.rdf4j.query.algebra.OrderElem)1 ProjectionElem (org.eclipse.rdf4j.query.algebra.ProjectionElem)1 ProjectionElemList (org.eclipse.rdf4j.query.algebra.ProjectionElemList)1 Slice (org.eclipse.rdf4j.query.algebra.Slice)1 ParsedDescribeQuery (org.eclipse.rdf4j.query.parser.ParsedDescribeQuery)1 ParsedQuery (org.eclipse.rdf4j.query.parser.ParsedQuery)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 ASTQueryContainer (org.eclipse.rdf4j.query.parser.sparql.ast.ASTQueryContainer)1 ASTSelectQuery (org.eclipse.rdf4j.query.parser.sparql.ast.ASTSelectQuery)1