use of org.apache.jena.sparql.core.BasicPattern 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.core.BasicPattern in project jena by apache.
the class QueryIterPropertyTable method sort.
private Collection<BasicPattern> sort(BasicPattern pattern) {
HashMap<Node, BasicPattern> map = new HashMap<>();
for (Triple triple : pattern.getList()) {
Node subject = triple.getSubject();
if (!map.containsKey(subject)) {
List<Triple> triples = new ArrayList<>();
BasicPattern p = BasicPattern.wrap(triples);
map.put(subject, p);
p.add(triple);
} else {
map.get(subject).add(triple);
}
}
return map.values();
}
use of org.apache.jena.sparql.core.BasicPattern in project jena by apache.
the class TransformMergeBGPs method merge.
private static BasicPattern merge(BasicPattern p1, BasicPattern p2) {
if (p1 == null || p2 == null)
return null;
BasicPattern p = new BasicPattern();
p.addAll(p1);
p.addAll(p2);
return p;
}
use of org.apache.jena.sparql.core.BasicPattern in project jena by apache.
the class TransformReorder method transform.
/**
* Transforms Quad Patterns with the reordering
*/
@Override
public Op transform(OpQuadPattern opQuadPattern) {
BasicPattern pattern = opQuadPattern.getBasicPattern();
if (pattern.size() < 2)
return opQuadPattern;
BasicPattern pattern2 = reorder.reorder(pattern);
return new OpQuadPattern(opQuadPattern.getGraphNode(), pattern2);
}
use of org.apache.jena.sparql.core.BasicPattern in project jena by apache.
the class TransformFilterPlacementConservative method transformFilterQuadPattern.
private static Op transformFilterQuadPattern(ExprList exprs, Set<Var> patternVarsScope, Node graphNode, BasicPattern pattern) {
// Any filters that depend on no variables.
Op op = insertAnyFilter(exprs, patternVarsScope, null);
if (Var.isVar(graphNode)) {
// Add in the graph node of the quad block.
// It's picked up after the first triple is processed.
VarUtils.addVar(patternVarsScope, Var.alloc(graphNode));
}
for (Triple triple : pattern) {
OpQuadPattern opQuad = getQuads(op);
if (opQuad == null) {
opQuad = new OpQuadPattern(graphNode, new BasicPattern());
op = OpSequence.create(op, opQuad);
}
opQuad.getBasicPattern().add(triple);
// Update variables in scope.
VarUtils.addVarsFromTriple(patternVarsScope, triple);
// Attempt to place any filters
op = insertAnyFilter(exprs, patternVarsScope, op);
}
return op;
}
Aggregations