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());
}
}
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());
}
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);
}
}
}
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());
}
}
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);
}
Aggregations