Search in sources :

Example 11 with ProjectionElem

use of org.eclipse.rdf4j.query.algebra.ProjectionElem in project rdf4j by eclipse.

the class TupleExprBuilder method visit.

@Override
public TupleExpr visit(ASTDescribe node, Object data) throws VisitorException {
    TupleExpr tupleExpr = (TupleExpr) data;
    if (tupleExpr == null) {
        tupleExpr = new SingletonSet();
    }
    Extension e = new Extension();
    ProjectionElemList projectionElements = new ProjectionElemList();
    for (int i = 0; i < node.jjtGetNumChildren(); i++) {
        ValueExpr resource = (ValueExpr) node.jjtGetChild(i).jjtAccept(this, null);
        if (resource instanceof Var) {
            projectionElements.addElement(new ProjectionElem(((Var) resource).getName()));
        } else {
            String alias = "_describe_" + UUID.randomUUID().toString().replaceAll("-", "_");
            ExtensionElem elem = new ExtensionElem(resource, alias);
            e.addElement(elem);
            projectionElements.addElement(new ProjectionElem(alias));
        }
    }
    if (!e.getElements().isEmpty()) {
        e.setArg(tupleExpr);
        tupleExpr = e;
    }
    Projection p = new Projection(tupleExpr, projectionElements);
    return new DescribeOperator(p);
}
Also used : Extension(org.eclipse.rdf4j.query.algebra.Extension) ProjectionElemList(org.eclipse.rdf4j.query.algebra.ProjectionElemList) ValueExpr(org.eclipse.rdf4j.query.algebra.ValueExpr) SingletonSet(org.eclipse.rdf4j.query.algebra.SingletonSet) Var(org.eclipse.rdf4j.query.algebra.Var) DescribeOperator(org.eclipse.rdf4j.query.algebra.DescribeOperator) ExtensionElem(org.eclipse.rdf4j.query.algebra.ExtensionElem) Projection(org.eclipse.rdf4j.query.algebra.Projection) MultiProjection(org.eclipse.rdf4j.query.algebra.MultiProjection) TupleExpr(org.eclipse.rdf4j.query.algebra.TupleExpr) ProjectionElem(org.eclipse.rdf4j.query.algebra.ProjectionElem)

Example 12 with ProjectionElem

use of org.eclipse.rdf4j.query.algebra.ProjectionElem in project rdf4j by eclipse.

the class AbstractQueryBuilder method projection.

private UnaryTupleOperator projection() {
    if (!mProjectionPatterns.isEmpty()) {
        return multiProjection();
    } else {
        Extension aExt = null;
        ProjectionElemList aList = new ProjectionElemList();
        for (String aVar : mProjectionVars) {
            aList.addElement(new ProjectionElem(aVar));
        }
        Projection aProjection = new Projection();
        aProjection.setProjectionElemList(aList);
        if (aExt != null) {
            aProjection.setArg(aExt);
        }
        return aProjection;
    }
}
Also used : Extension(org.eclipse.rdf4j.query.algebra.Extension) ProjectionElemList(org.eclipse.rdf4j.query.algebra.ProjectionElemList) MultiProjection(org.eclipse.rdf4j.query.algebra.MultiProjection) Projection(org.eclipse.rdf4j.query.algebra.Projection) ProjectionElem(org.eclipse.rdf4j.query.algebra.ProjectionElem)

Example 13 with ProjectionElem

use of org.eclipse.rdf4j.query.algebra.ProjectionElem 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

ProjectionElem (org.eclipse.rdf4j.query.algebra.ProjectionElem)13 ProjectionElemList (org.eclipse.rdf4j.query.algebra.ProjectionElemList)12 Projection (org.eclipse.rdf4j.query.algebra.Projection)9 Extension (org.eclipse.rdf4j.query.algebra.Extension)8 MultiProjection (org.eclipse.rdf4j.query.algebra.MultiProjection)8 TupleExpr (org.eclipse.rdf4j.query.algebra.TupleExpr)8 ExtensionElem (org.eclipse.rdf4j.query.algebra.ExtensionElem)7 Var (org.eclipse.rdf4j.query.algebra.Var)7 ValueExpr (org.eclipse.rdf4j.query.algebra.ValueExpr)6 Distinct (org.eclipse.rdf4j.query.algebra.Distinct)5 Reduced (org.eclipse.rdf4j.query.algebra.Reduced)5 StatementPattern (org.eclipse.rdf4j.query.algebra.StatementPattern)5 ArrayList (java.util.ArrayList)4 ValueConstant (org.eclipse.rdf4j.query.algebra.ValueConstant)4 BNodeGenerator (org.eclipse.rdf4j.query.algebra.BNodeGenerator)3 EmptySet (org.eclipse.rdf4j.query.algebra.EmptySet)3 HashMap (java.util.HashMap)2 OrderElem (org.eclipse.rdf4j.query.algebra.OrderElem)2 LinkedHashMap (java.util.LinkedHashMap)1 IRI (org.eclipse.rdf4j.model.IRI)1