use of org.apache.jena.sparql.engine.ExecutionContext in project jena by apache.
the class Eval method evalQuadPattern.
static Table evalQuadPattern(OpQuadPattern opQuad, Evaluator evaluator) {
if (opQuad.isEmpty())
return TableFactory.createUnit();
ExecutionContext cxt = evaluator.getExecContext();
DatasetGraph ds = cxt.getDataset();
BasicPattern pattern = opQuad.getBasicPattern();
if (!opQuad.getGraphNode().isVariable()) {
if (!opQuad.getGraphNode().isURI()) {
throw new ARQInternalErrorException("Not a URI or variable: " + opQuad.getGraphNode());
}
Graph g = null;
if (opQuad.isDefaultGraph())
g = ds.getDefaultGraph();
else
g = ds.getGraph(opQuad.getGraphNode());
if (g == null)
return new TableEmpty();
ExecutionContext cxt2 = new ExecutionContext(cxt, g);
QueryIterator qIter = executeBGP(pattern, QueryIterRoot.create(cxt2), cxt2);
return TableFactory.create(qIter);
} else {
// Variable.
Var gVar = Var.alloc(opQuad.getGraphNode());
// Or just just devolve to OpGraph and get OpUnion chain of OpJoin
QueryIterConcat concat = new QueryIterConcat(cxt);
for (Iterator<Node> graphNodes = cxt.getDataset().listGraphNodes(); graphNodes.hasNext(); ) {
Node gn = graphNodes.next();
//Op tableVarURI = TableFactory.create(gn.getName(), Node.createURI(uri)) ;
Graph g = cxt.getDataset().getGraph(gn);
Binding b = BindingFactory.binding(BindingRoot.create(), gVar, gn);
ExecutionContext cxt2 = new ExecutionContext(cxt, g);
// Eval the pattern, eval the variable, join.
// Pattern may be non-linear in the variable - do a pure execution.
Table t1 = TableFactory.create(gVar, gn);
QueryIterator qIter = executeBGP(pattern, QueryIterRoot.create(cxt2), cxt2);
Table t2 = TableFactory.create(qIter);
Table t3 = evaluator.join(t1, t2);
concat.add(t3.iterator(cxt2));
}
return TableFactory.create(concat);
}
}
use of org.apache.jena.sparql.engine.ExecutionContext in project jena by apache.
the class SolverRX method matchQuadPattern.
// Entry point from SolverLib.
/*package*/
static Iterator<BindingNodeId> matchQuadPattern(Iterator<BindingNodeId> chain, Node graphNode, Triple tPattern, NodeTupleTable nodeTupleTable, Tuple<Node> patternTuple, boolean anyGraph, Predicate<Tuple<NodeId>> filter, ExecutionContext execCxt) {
if (DATAPATH) {
if (!tripleHasEmbTripleWithVars(tPattern))
// No RDF-star <<>> with variables.
return StageMatchTuple.access(nodeTupleTable, chain, patternTuple, filter, anyGraph, execCxt);
}
// RDF-star <<>> with variables.
// This path should work regardless.
boolean isTriple = (patternTuple.len() == 3);
NodeTable nodeTable = nodeTupleTable.getNodeTable();
Function<BindingNodeId, Iterator<BindingNodeId>> step = bnid -> find(bnid, nodeTupleTable, graphNode, tPattern, anyGraph, filter, execCxt);
return Iter.flatMap(chain, step);
}
use of org.apache.jena.sparql.engine.ExecutionContext in project jena by apache.
the class PathEngine method graphFindWorker.
private Iterator<Triple> graphFindWorker(Graph graph, Node s, PropertyFunctionFactory f, Node p, Node o, Context context) {
// Expensive?
PropertyFunction pf = f.create(p.getURI());
PropFuncArg sv = arg(s, "S");
PropFuncArg ov = arg(o, "O");
QueryIterator r = QueryIterRoot.create(new ExecutionContext(context, graph, null, null));
QueryIterator qIter = pf.exec(r, sv, p, ov, new ExecutionContext(ARQ.getContext(), graph, null, null));
if (!qIter.hasNext())
return Iter.nullIterator();
List<Triple> array = new ArrayList<>();
for (; qIter.hasNext(); ) {
Binding b = qIter.next();
Node st = value(sv, b);
Node ot = value(ov, b);
array.add(Triple.create(st, p, ot));
}
// Materialise so the inner QueryIterators are used up.
return array.iterator();
}
use of org.apache.jena.sparql.engine.ExecutionContext in project jena by apache.
the class qexpr method main2.
public static void main2(String... argv) {
CmdLineArgs cl = new CmdLineArgs(argv);
ArgDecl helpDecl = new ArgDecl(ArgDecl.NoValue, "h", "help");
cl.add(helpDecl);
ArgDecl verboseDecl = new ArgDecl(ArgDecl.NoValue, "v", "verbose");
cl.add(verboseDecl);
ArgDecl versionDecl = new ArgDecl(ArgDecl.NoValue, "ver", "version", "V");
cl.add(versionDecl);
ArgDecl quietDecl = new ArgDecl(ArgDecl.NoValue, "q", "quiet");
cl.add(quietDecl);
ArgDecl reduceDecl = new ArgDecl(ArgDecl.NoValue, "reduce", "fold", "simplify");
cl.add(reduceDecl);
ArgDecl strictDecl = new ArgDecl(ArgDecl.NoValue, "strict");
cl.add(strictDecl);
ArgDecl printDecl = new ArgDecl(ArgDecl.HasValue, "print");
cl.add(printDecl);
try {
cl.process();
} catch (IllegalArgumentException ex) {
System.err.println(ex.getMessage());
usage(System.err);
throw new CmdException();
}
if (cl.contains(helpDecl)) {
usage();
throw new TerminationException(0);
}
if (cl.contains(versionDecl)) {
System.out.println("ARQ Version: " + ARQ.VERSION + " (Jena: " + Jena.VERSION + ")");
throw new TerminationException(0);
}
// ==== General things
boolean verbose = cl.contains(verboseDecl);
boolean quiet = cl.contains(quietDecl);
if (cl.contains(strictDecl))
ARQ.setStrictMode();
boolean actionCopySubstitute = cl.contains(reduceDecl);
boolean actionPrintPrefix = false;
boolean actionPrintSPARQL = false;
boolean actionPrint = cl.contains(printDecl);
for (String v : cl.getValues(printDecl)) {
if (v.equalsIgnoreCase("prefix") || v.equalsIgnoreCase("op")) {
actionPrintPrefix = true;
} else if (v.equalsIgnoreCase("expr")) {
actionPrintSPARQL = true;
} else {
System.err.println("Unknown print form: " + v);
throw new TerminationException(0);
}
}
for (int i = 0; i < cl.getNumPositional(); i++) {
String exprStr = cl.getPositionalArg(i);
exprStr = cl.indirect(exprStr);
try {
PrefixMapping pmap = PrefixMapping.Factory.create();
pmap.setNsPrefixes(ARQConstants.getGlobalPrefixMap());
pmap.setNsPrefix("", "http://example/");
pmap.setNsPrefix("ex", "http://example/ns#");
Expr expr = ExprUtils.parse(exprStr, pmap);
if (actionPrint) {
IndentedWriter iOut = IndentedWriter.stdout;
if (actionPrintSPARQL) {
ExprUtils.fmtSPARQL(iOut, expr);
iOut.ensureStartOfLine();
}
if (actionPrintPrefix) {
WriterSSE.out(iOut, expr, new Prologue(pmap));
iOut.ensureStartOfLine();
}
iOut.flush();
continue;
}
if (verbose)
System.out.print(expr.toString() + " => ");
try {
if (actionCopySubstitute) {
Expr e = ExprLib.foldConstants(expr);
System.out.println(e);
} else {
// Default action
ARQ.getContext().set(ARQConstants.sysCurrentTime, NodeFactoryExtra.nowAsDateTime());
FunctionEnv env = new ExecutionContext(ARQ.getContext(), null, null, null);
NodeValue r = expr.eval(null, env);
// System.out.println(r.asQuotedString()) ;
Node n = r.asNode();
String s = NodeFmtLib.displayStr(n);
System.out.println(s);
}
} catch (ExprEvalException ex) {
System.out.println("Exception: " + ex.getMessage());
throw new TerminationException(2);
}
} catch (QueryParseException ex) {
System.err.println("Parse error: " + ex.getMessage());
throw new TerminationException(2);
}
}
}
use of org.apache.jena.sparql.engine.ExecutionContext in project jena by apache.
the class TestQueryIterSort method testCancelClosesSourceIterator.
@Test
public void testCancelClosesSourceIterator() {
Context context = new Context();
ExecutionContext ec = new ExecutionContext(context, (Graph) null, (DatasetGraph) null, (OpExecutorFactory) null);
QueryIterSort qis = new QueryIterSort(iterator, comparator, ec);
try {
while (qis.hasNext()) qis.next();
fail("query should have been cancelled by trigger");
} catch (QueryCancelledException q) {
assertTrue("source iterator should have been closed", iterator.isClosed());
}
}
Aggregations