use of org.apache.jena.sparql.expr.ExprVar in project jena by apache.
the class TestSortedDataBag method testTemporaryFilesAreCleanedUpAfterCompletion.
@Test
public void testTemporaryFilesAreCleanedUpAfterCompletion() {
List<Binding> unsorted = randomBindings(500);
List<SortCondition> conditions = new ArrayList<>();
conditions.add(new SortCondition(new ExprVar("8"), Query.ORDER_ASCENDING));
BindingComparator comparator = new BindingComparator(conditions);
SortedDataBag<Binding> db = new SortedDataBag<>(new ThresholdPolicyCount<Binding>(10), SerializationFactoryFinder.bindingSerializationFactory(), comparator);
List<File> spillFiles = new ArrayList<>();
try {
db.addAll(unsorted);
spillFiles.addAll(db.getSpillFiles());
int count = 0;
for (File file : spillFiles) {
if (file.exists()) {
count++;
}
}
// 500 bindings divided into 50 chunks (49 in files, and 1 in memory)
assertEquals(49, count);
Iterator<Binding> iter = db.iterator();
while (iter.hasNext()) {
iter.next();
}
Iter.close(iter);
} finally {
db.close();
}
int count = 0;
for (File file : spillFiles) {
if (file.exists()) {
count++;
}
}
assertEquals(0, count);
}
use of org.apache.jena.sparql.expr.ExprVar 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.ExprVar 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.ExprVar in project jena by apache.
the class AlgebraExec method main.
public static void main(String[] argv) {
String BASE = "http://example/";
BasicPattern bp = new BasicPattern();
Var var_x = Var.alloc("x");
Var var_z = Var.alloc("z");
// ---- Build expression
bp.add(new Triple(var_x, NodeFactory.createURI(BASE + "p"), var_z));
Op op = new OpBGP(bp);
//Expr expr = ExprUtils.parse("?z < 2 ") ;
Expr expr = new E_LessThan(new ExprVar(var_z), NodeValue.makeNodeInteger(2));
op = OpFilter.filter(expr, op);
// ---- Example setup
Model m = makeModel();
m.write(System.out, "TTL");
System.out.println("--------------");
System.out.print(op);
System.out.println("--------------");
// ---- Execute expression
QueryIterator qIter = Algebra.exec(op, m.getGraph());
// -------- Either read the query iterator directly ...
if (false) {
for (; qIter.hasNext(); ) {
Binding b = qIter.nextBinding();
Node n = b.get(var_x);
System.out.println(NodeFmtLib.displayStr(n));
System.out.println(b);
}
qIter.close();
} else {
// -------- Or make ResultSet from it (but not both - reading an
// iterator consumes the current solution)
List<String> varNames = new ArrayList<>();
varNames.add("x");
varNames.add("z");
ResultSet rs = new ResultSetStream(varNames, m, qIter);
ResultSetFormatter.out(rs);
qIter.close();
}
System.exit(0);
}
use of org.apache.jena.sparql.expr.ExprVar 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