use of org.apache.jena.sparql.expr.E_Regex in project jena by apache.
the class TestRegex method regexTest.
public void regexTest(String value, String pattern, String flags, boolean expected) {
Expr s = NodeValue.makeString(value);
E_Regex r = new E_Regex(s, pattern, flags);
NodeValue nv = r.eval(BindingFactory.binding(), null);
boolean b = nv.getBoolean();
if (b != expected)
fail(fmtTest(value, pattern, flags) + " ==> " + b + " expected " + expected);
}
use of org.apache.jena.sparql.expr.E_Regex in project jena by apache.
the class labelSearch method exec.
/* This be called once, with unevaluated arguments.
* To do a rewrite of part of a query, we must use the fundamental PropertyFunction
* interface to be called once with the input iterator.
* Must not return null nor throw an exception. Instead, return a QueryIterNullIterator
* indicating no matches.
*/
@Override
public QueryIterator exec(QueryIterator input, PropFuncArg argSubject, Node predicate, PropFuncArg argObject, ExecutionContext execCxt) {
// No real need to check the pattern arguments because
// the replacement triple pattern and regex will cope
// but we illustrate testing here.
Node nodeVar = argSubject.getArg();
String pattern = NodeUtils.stringLiteral(argObject.getArg());
if (pattern == null) {
Log.warn(this, "Pattern must be a plain literal or xsd:string: " + argObject.getArg());
return QueryIterNullIterator.create(execCxt);
}
if (false)
// Old (ARQ 1) way - not recommended.
return buildSyntax(input, nodeVar, pattern, execCxt);
// Better
// Build a SPARQL algebra expression
// Hidden variable
Var var2 = createNewVar();
BasicPattern bp = new BasicPattern();
Triple t = new Triple(nodeVar, RDFS.label.asNode(), var2);
bp.add(t);
OpBGP op = new OpBGP(bp);
Expr regex = new E_Regex(new ExprVar(var2.getName()), pattern, "i");
Op filter = OpFilter.filter(regex, op);
// ---- Evaluation
if (true) {
// Use the reference query engine
// Create a table for the input stream (so it uses working memory at this point,
// which is why this is not the preferred way).
// Then join to expression for this stage.
Table table = TableFactory.create(input);
Op op2 = OpJoin.create(OpTable.create(table), filter);
return Algebra.exec(op2, execCxt.getDataset());
}
// Use the default, optimizing query engine.
return QC.execute(filter, input, execCxt);
}
use of org.apache.jena.sparql.expr.E_Regex 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.expr.E_Regex 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