use of org.apache.jena.sparql.core.BasicPattern in project jena by apache.
the class FormatterElement method visit.
@Override
public void visit(ElementPathBlock el) {
// Write path block - don't put in a final trailing "."
if (el.isEmpty()) {
out.println("# Empty BGP");
return;
}
// Split into BGP-path-BGP-...
// where the BGPs may be empty.
PathBlock pBlk = el.getPattern();
BasicPattern bgp = new BasicPattern();
// Has anything been output?
boolean first = true;
for (TriplePath tp : pBlk) {
if (tp.isTriple()) {
bgp.add(tp.asTriple());
continue;
}
if (!bgp.isEmpty()) {
if (!first)
out.println(" .");
flush(bgp);
first = false;
}
if (!first)
out.println(" .");
// Path
printSubject(tp.getSubject());
out.print(" ");
PathWriter.write(out, tp.getPath(), context.getPrologue());
out.print(" ");
printObject(tp.getObject());
first = false;
}
// Flush any stored triple patterns.
if (!bgp.isEmpty()) {
if (!first)
out.println(" .");
flush(bgp);
first = false;
}
}
use of org.apache.jena.sparql.core.BasicPattern 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.core.BasicPattern in project jena by apache.
the class Template method getGraphPattern.
public Map<Node, BasicPattern> getGraphPattern() {
List<Quad> quads = getQuads();
HashMap<Node, BasicPattern> graphs = new HashMap<>();
for (Quad q : quads) {
BasicPattern bgp = graphs.get(q.getGraph());
if (bgp == null) {
bgp = new BasicPattern();
graphs.put(q.getGraph(), bgp);
}
bgp.add(q.asTriple());
}
return graphs;
}
use of org.apache.jena.sparql.core.BasicPattern in project jena by apache.
the class tdbreorder method main.
public static void main(String... args) {
if (args.length != 2) {
System.err.println("Usage: PATTERN STATS");
System.exit(1);
}
LogCtl.enable(StatsMatcher.class);
LogCtl.enable(ReorderTransformationSubstitution.class);
if (args.length != 2) {
System.err.println("Usage: op stats");
System.exit(1);
}
String pattern = args[0];
String statsFile = args[1];
Op op = SSE.readOp(pattern);
BasicPattern bgp;
if (op instanceof OpQuadPattern) {
bgp = ((OpQuadPattern) op).getBasicPattern();
} else if (op instanceof OpBGP) {
bgp = ((OpBGP) op).getPattern();
} else {
System.err.println("Not a quad or triple pattern");
System.exit(2);
bgp = null;
}
ReorderTransformation reorder = chooseReorder(statsFile);
// ReorderTransformation reorder = ReorderLib.fixed() ;
BasicPattern bgp2 = reorder.reorder(bgp);
System.out.println();
print(bgp);
System.out.println();
System.out.println(" ======== >>>>>>>>");
print(bgp2);
System.out.println();
}
use of org.apache.jena.sparql.core.BasicPattern in project jena by apache.
the class LibSDB method findTriples.
/** Find triples, in the default graph or a named graph. */
public static Iterator<Triple> findTriples(DatasetGraph dsg, Node g, Node s, Node p, Node o) {
if (Var.isVar(g))
throw new InternalErrorException("Graph node is a variable : " + g);
final Node vs = varOrConst(s, "s");
final Node vp = varOrConst(p, "p");
final Node vo = varOrConst(o, "o");
// Evaluate as an algebra expression
Triple triple = new Triple(vs, vp, vo);
BasicPattern pattern = new BasicPattern();
pattern.add(triple);
Op op = (g != null) ? new OpQuadPattern(g, pattern) : new OpBGP(pattern);
Plan plan = QueryEngineSDB.getFactory().create(op, dsg, BindingRoot.create(), null);
QueryIterator qIter = plan.iterator();
Iterator<Binding> iter;
if (SDB.getContext().isTrue(SDB.streamGraphAPI)) {
// Assumes iterator closed properly.
iter = qIter;
} else {
// ---- Safe version:
List<Binding> x = Iter.toList(qIter);
Iterator<Binding> qIter2 = x.iterator();
qIter.close();
iter = qIter2;
}
return Iter.map(iter, (b) -> bindingToTriple(vs, vp, vo, b));
}
Aggregations