Search in sources :

Example 31 with ElementGroup

use of org.apache.jena.sparql.syntax.ElementGroup in project jena by apache.

the class WhereHandler method addUnion.

/**
 * Add a union to the where clause.
 *
 * @param subQuery The subquery to add as the union.
 */
public void addUnion(AbstractQueryBuilder<?> subQuery) {
    ElementUnion union = null;
    ElementGroup clause = getClause();
    // if the last element is a union make sure we add to it.
    if (!clause.isEmpty()) {
        Element lastElement = clause.getElements().get(clause.getElements().size() - 1);
        if (lastElement instanceof ElementUnion) {
            union = (ElementUnion) lastElement;
        } else {
            // clauses is not empty and is not a union so it is the left
            // side of the union.
            union = new ElementUnion();
            union.addElement(clause);
            query.setQueryPattern(union);
        }
    } else {
        // add the union as the first element in the clause.
        union = new ElementUnion();
        clause.addElement(union);
    }
    // otherwise just add the clause.
    if (subQuery instanceof SelectClause && ((SelectClause<?>) subQuery).getVars().size() > 0) {
        union.addElement(makeSubQuery(subQuery));
    } else {
        PrologHandler ph = new PrologHandler(query);
        ph.addPrefixes(subQuery.getPrologHandler().getPrefixes());
        union.addElement(subQuery.getWhereHandler().getClause());
    }
}
Also used : SelectClause(org.apache.jena.arq.querybuilder.clauses.SelectClause) ElementUnion(org.apache.jena.sparql.syntax.ElementUnion) Element(org.apache.jena.sparql.syntax.Element) ElementGroup(org.apache.jena.sparql.syntax.ElementGroup)

Example 32 with ElementGroup

use of org.apache.jena.sparql.syntax.ElementGroup in project jena by apache.

the class WhereHandler method build.

@Override
public void build() {
    /*
         * cleanup union-of-one and other similar issues.
         */
    BuildElementVisitor visitor = new BuildElementVisitor();
    getElement().visit(visitor);
    if (!valuesHandler.isEmpty()) {
        if (visitor.getResult() instanceof ElementGroup) {
            ((ElementGroup) visitor.getResult()).addElement(valuesHandler.asElement());
            ;
        } else {
            ElementGroup eg = new ElementGroup();
            eg.addElement(visitor.getResult());
            eg.addElement(valuesHandler.asElement());
            visitor.setResult(eg);
        }
    }
    query.setQueryPattern(visitor.getResult());
}
Also used : BuildElementVisitor(org.apache.jena.arq.querybuilder.rewriters.BuildElementVisitor) ElementGroup(org.apache.jena.sparql.syntax.ElementGroup)

Example 33 with ElementGroup

use of org.apache.jena.sparql.syntax.ElementGroup in project jena by apache.

the class WhereHandler method addWhere.

/**
 * Add the triple path to the where clause
 *
 * @param t The triple path to add.
 * @throws IllegalArgumentException If the triple path is not a valid triple
 * path for a where clause.
 */
public void addWhere(TriplePath t) throws IllegalArgumentException {
    testTriple(t);
    ElementGroup eg = getClause();
    List<Element> lst = eg.getElements();
    if (lst.isEmpty()) {
        ElementPathBlock epb = new ElementPathBlock();
        epb.addTriple(t);
        eg.addElement(epb);
    } else {
        Element e = lst.get(lst.size() - 1);
        if (e instanceof ElementTriplesBlock && t.isTriple()) {
            ElementTriplesBlock etb = (ElementTriplesBlock) e;
            etb.addTriple(t.asTriple());
        } else if (e instanceof ElementPathBlock) {
            ElementPathBlock epb = (ElementPathBlock) e;
            epb.addTriple(t);
        } else {
            ElementPathBlock etb = new ElementPathBlock();
            etb.addTriple(t);
            eg.addElement(etb);
        }
    }
}
Also used : Element(org.apache.jena.sparql.syntax.Element) ElementTriplesBlock(org.apache.jena.sparql.syntax.ElementTriplesBlock) ElementGroup(org.apache.jena.sparql.syntax.ElementGroup) ElementPathBlock(org.apache.jena.sparql.syntax.ElementPathBlock)

Example 34 with ElementGroup

use of org.apache.jena.sparql.syntax.ElementGroup in project jena by apache.

the class WhereQuadHolder method addUnion.

/**
 * Add a union to the where clause.
 *
 * @param subQuery The subquery to add as the union.
 */
public void addUnion(AbstractQueryBuilder<?> subQuery) {
    ElementUnion union = null;
    ElementGroup clause = getClause();
    // if the last element is a union make sure we add to it.
    if (!clause.isEmpty()) {
        Element lastElement = clause.getElements().get(clause.getElements().size() - 1);
        if (lastElement instanceof ElementUnion) {
            union = (ElementUnion) lastElement;
        } else {
            // clauses is not empty and is not a union so it is the left
            // side of the union.
            union = new ElementUnion();
            union.addElement(clause);
            whereClause = union;
        }
    } else {
        // add the union as the first element in the clause.
        union = new ElementUnion();
        clause.addElement(union);
    }
    // otherwise just add the clause.
    if (subQuery instanceof SelectClause && ((SelectClause<?>) subQuery).getVars().size() > 0) {
        union.addElement(subQuery.asSubQuery());
    } else {
        prefixHandler.addPrefixes(subQuery.getPrologHandler().getPrefixes());
        union.addElement(subQuery.getWhereHandler().getClause());
    }
}
Also used : SelectClause(org.apache.jena.arq.querybuilder.clauses.SelectClause) ElementUnion(org.apache.jena.sparql.syntax.ElementUnion) Element(org.apache.jena.sparql.syntax.Element) ElementGroup(org.apache.jena.sparql.syntax.ElementGroup)

Example 35 with ElementGroup

use of org.apache.jena.sparql.syntax.ElementGroup in project jena by apache.

the class labelSearch method buildSyntax.

// Build SPARQL syntax and compile it.
// Not recommended.
private QueryIterator buildSyntax(QueryIterator input, Node nodeVar, String pattern, ExecutionContext execCxt) {
    Var var2 = createNewVar();
    // Triple patterns for   ?x rdfs:label ?hiddenVar
    ElementTriplesBlock elementBGP = new ElementTriplesBlock();
    Triple t = new Triple(nodeVar, RDFS.label.asNode(), var2);
    elementBGP.addTriple(t);
    // Regular expression for  regex(?hiddenVar, "pattern", "i")
    Expr regex = new E_Regex(new ExprVar(var2.getName()), pattern, "i");
    ElementGroup elementGroup = new ElementGroup();
    elementGroup.addElement(elementBGP);
    elementGroup.addElement(new ElementFilter(regex));
    // Compile it.
    // The better design is to build the Op structure programmatically,
    Op op = Algebra.compile(elementGroup);
    op = Algebra.optimize(op, execCxt.getContext());
    return QC.execute(op, input, execCxt);
}
Also used : Triple(org.apache.jena.graph.Triple) E_Regex(org.apache.jena.sparql.expr.E_Regex) ExprVar(org.apache.jena.sparql.expr.ExprVar) Op(org.apache.jena.sparql.algebra.Op) Expr(org.apache.jena.sparql.expr.Expr) ExprVar(org.apache.jena.sparql.expr.ExprVar) Var(org.apache.jena.sparql.core.Var) ElementFilter(org.apache.jena.sparql.syntax.ElementFilter) ElementTriplesBlock(org.apache.jena.sparql.syntax.ElementTriplesBlock) ElementGroup(org.apache.jena.sparql.syntax.ElementGroup)

Aggregations

ElementGroup (org.apache.jena.sparql.syntax.ElementGroup)37 Element (org.apache.jena.sparql.syntax.Element)24 ElementPathBlock (org.apache.jena.sparql.syntax.ElementPathBlock)11 Test (org.junit.Test)11 Triple (org.apache.jena.graph.Triple)9 ElementTriplesBlock (org.apache.jena.sparql.syntax.ElementTriplesBlock)9 Node (org.apache.jena.graph.Node)8 Query (org.apache.jena.query.Query)6 Var (org.apache.jena.sparql.core.Var)6 Quad (org.apache.jena.sparql.core.Quad)5 UpdateModify (org.apache.jena.sparql.modify.request.UpdateModify)5 Update (org.apache.jena.update.Update)5 Op (org.apache.jena.sparql.algebra.Op)3 TriplePath (org.apache.jena.sparql.core.TriplePath)3 ElementNamedGraph (org.apache.jena.sparql.syntax.ElementNamedGraph)3 ElementSubQuery (org.apache.jena.sparql.syntax.ElementSubQuery)3 HashSet (java.util.HashSet)2 SelectClause (org.apache.jena.arq.querybuilder.clauses.SelectClause)2 ElementRewriter (org.apache.jena.arq.querybuilder.rewriters.ElementRewriter)2 IndentedWriter (org.apache.jena.atlas.io.IndentedWriter)2