Search in sources :

Example 11 with ElementUnion

use of org.apache.jena.sparql.syntax.ElementUnion 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)

Aggregations

ElementUnion (org.apache.jena.sparql.syntax.ElementUnion)11 SelectBuilder (org.apache.jena.arq.querybuilder.SelectBuilder)8 WhereValidator (org.apache.jena.arq.querybuilder.WhereValidator)8 Triple (org.apache.jena.graph.Triple)8 ElementPathBlock (org.apache.jena.sparql.syntax.ElementPathBlock)8 Query (org.apache.jena.query.Query)5 ElementSubQuery (org.apache.jena.sparql.syntax.ElementSubQuery)5 Test (org.junit.Test)5 FrontsTriple (org.apache.jena.graph.FrontsTriple)3 Node (org.apache.jena.graph.Node)3 TriplePath (org.apache.jena.sparql.core.TriplePath)3 Element (org.apache.jena.sparql.syntax.Element)3 ContractTest (org.xenei.junit.contract.ContractTest)3 SelectClause (org.apache.jena.arq.querybuilder.clauses.SelectClause)2 Var (org.apache.jena.sparql.core.Var)2 ElementGroup (org.apache.jena.sparql.syntax.ElementGroup)2 HashMap (java.util.HashMap)1 LinkedHashMap (java.util.LinkedHashMap)1 ExprVar (org.apache.jena.sparql.expr.ExprVar)1