Search in sources :

Example 6 with ParsedTupleQuery

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

the class AbstractQueryBuilder method query.

/**
 * @inheritDoc
 */
public T query() {
    UnaryTupleOperator aRoot = null;
    UnaryTupleOperator aCurr = null;
    if (mLimit != -1 || mOffset != -1) {
        Slice aSlice = new Slice();
        if (mLimit != -1) {
            aSlice.setLimit(mLimit);
        }
        if (mOffset != -1) {
            aSlice.setOffset(mOffset);
        }
        aRoot = aCurr = aSlice;
    }
    if (mOrderByElems != null && !mOrderByElems.isEmpty()) {
        Order aOrder = new Order();
        aOrder.addElements(mOrderByElems);
        if (aRoot == null) {
            aRoot = aCurr = aOrder;
        } else {
            aCurr.setArg(aOrder);
            aCurr = aOrder;
        }
    }
    if (mDistinct) {
        Distinct aDistinct = new Distinct();
        if (aRoot == null) {
            aRoot = aCurr = aDistinct;
        } else {
            aCurr.setArg(aDistinct);
            aCurr = aDistinct;
        }
    }
    if (mReduced) {
        Reduced aReduced = new Reduced();
        if (aRoot == null) {
            aRoot = aCurr = aReduced;
        } else {
            aCurr.setArg(aReduced);
            aCurr = aReduced;
        }
    }
    TupleExpr aJoin = join();
    if (mQuery instanceof ParsedTupleQuery && mProjectionVars.isEmpty()) {
        VarNameCollector aCollector = new VarNameCollector();
        aJoin.visit(aCollector);
        mProjectionVars.addAll(aCollector.getVarNames());
    } else if (mQuery instanceof ParsedGraphQuery && mProjectionPatterns.isEmpty()) {
        StatementPatternCollector aCollector = new StatementPatternCollector();
        aJoin.visit(aCollector);
        mProjectionPatterns.addAll(aCollector.getStatementPatterns());
    }
    UnaryTupleOperator aProjection = projection();
    if (aRoot == null) {
        aRoot = aCurr = aProjection;
    } else {
        aCurr.setArg(aProjection);
    }
    if (aProjection.getArg() == null) {
        aCurr = aProjection;
    } else {
        // I think this is always a safe cast
        aCurr = (UnaryTupleOperator) aProjection.getArg();
    }
    if (aJoin != null) {
        aCurr.setArg(aJoin);
    }
    mQuery.setTupleExpr(aRoot);
    if (!mFrom.isEmpty() || !mFromNamed.isEmpty()) {
        SimpleDataset aDataset = new SimpleDataset();
        for (IRI aFrom : mFrom) {
            aDataset.addDefaultGraph(aFrom);
        }
        for (IRI aFrom : mFromNamed) {
            aDataset.addNamedGraph(aFrom);
        }
        mQuery.setDataset(aDataset);
    }
    return mQuery;
}
Also used : Order(org.eclipse.rdf4j.query.algebra.Order) StatementPatternCollector(org.eclipse.rdf4j.query.algebra.helpers.StatementPatternCollector) IRI(org.eclipse.rdf4j.model.IRI) Distinct(org.eclipse.rdf4j.query.algebra.Distinct) UnaryTupleOperator(org.eclipse.rdf4j.query.algebra.UnaryTupleOperator) Slice(org.eclipse.rdf4j.query.algebra.Slice) ParsedGraphQuery(org.eclipse.rdf4j.query.parser.ParsedGraphQuery) VarNameCollector(org.eclipse.rdf4j.query.algebra.helpers.VarNameCollector) SimpleDataset(org.eclipse.rdf4j.query.impl.SimpleDataset) Reduced(org.eclipse.rdf4j.query.algebra.Reduced) TupleExpr(org.eclipse.rdf4j.query.algebra.TupleExpr) ParsedTupleQuery(org.eclipse.rdf4j.query.parser.ParsedTupleQuery)

Example 7 with ParsedTupleQuery

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

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