use of org.apache.jena.sparql.syntax.ElementFilter in project jena by apache.
the class WhereHandlerTest method testAddFilterWithNamespace.
@Test
public void testAddFilterWithNamespace() throws ParseException {
query.setPrefix("afn", "http://jena.apache.org/ARQ/function#");
handler.addFilter("afn:namespace(?one) = 'foo'");
handler.build();
ExprFactory fact = new ExprFactory();
E_Function func = new E_Function("http://jena.apache.org/ARQ/function#namespace", fact.asList("?one"));
E_Equals expr = fact.eq(func, "foo");
WhereValidator visitor = new WhereValidator(new ElementFilter(expr));
handler.getQueryPattern().visit(visitor);
assertTrue(visitor.matching);
}
use of org.apache.jena.sparql.syntax.ElementFilter in project jena by apache.
the class WhereClauseTest method testAddFilter.
@ContractTest
public void testAddFilter() throws ParseException {
WhereClause<?> whereClause = getProducer().newInstance();
AbstractQueryBuilder<?> builder = whereClause.addFilter("?one<10");
E_LessThan lt = new E_LessThan(new ExprVar(Var.alloc("one")), new NodeValueInteger(10));
ElementFilter ef = new ElementFilter(lt);
WhereValidator visitor = new WhereValidator(ef);
builder.build().getQueryPattern().visit(visitor);
assertTrue(visitor.matching);
}
use of org.apache.jena.sparql.syntax.ElementFilter 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);
}
use of org.apache.jena.sparql.syntax.ElementFilter in project jena by apache.
the class ExProg2 method main.
public static void main(String[] args) {
Model model = createModel();
Query query = QueryFactory.make();
query.setQuerySelectType();
// See also ExProg1
ElementGroup elg = new ElementGroup();
Var varTitle = Var.alloc("title");
Var varX = Var.alloc("x");
Triple t1 = new Triple(varX, DC.title.asNode(), varTitle);
elg.addTriplePattern(t1);
// Adds a filter. Need to wrap variable in a NodeVar.
Expr expr = new E_Regex(new ExprVar(varTitle), "sparql", "i");
ElementFilter filter = new ElementFilter(expr);
elg.addElementFilter(filter);
// Attach the group to query.
query.setQueryPattern(elg);
// Choose what we want - SELECT ?title
query.addResultVar(varTitle);
// Print query with line numbers
// Prefix mapping just helps serialization
query.getPrefixMapping().setNsPrefix("dc", DC.getURI());
query.serialize(new IndentedWriter(System.out, true));
System.out.println();
try (QueryExecution qexec = QueryExecutionFactory.create(query, model)) {
// Assumption: it's a SELECT query.
ResultSet rs = qexec.execSelect();
// The order of results is undefined.
System.out.println("Titles: ");
for (; rs.hasNext(); ) {
QuerySolution rb = rs.nextSolution();
// Get title - variable names do not include the '?' (or '$')
RDFNode x = rb.get("title");
// Check the type of the result value
if (x instanceof Literal) {
Literal titleStr = (Literal) x;
System.out.println(" " + titleStr);
} else
System.out.println("Strange - not a literal: " + x);
}
}
}
Aggregations