Search in sources :

Example 36 with BasicPattern

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);
}
Also used : ExprVar(org.apache.jena.sparql.expr.ExprVar) Binding(org.apache.jena.sparql.engine.binding.Binding) Op(org.apache.jena.sparql.algebra.Op) ExprVar(org.apache.jena.sparql.expr.ExprVar) Var(org.apache.jena.sparql.core.Var) OpBGP(org.apache.jena.sparql.algebra.op.OpBGP) Node(org.apache.jena.graph.Node) ArrayList(java.util.ArrayList) BasicPattern(org.apache.jena.sparql.core.BasicPattern) E_LessThan(org.apache.jena.sparql.expr.E_LessThan) ResultSetStream(org.apache.jena.sparql.engine.ResultSetStream) Triple(org.apache.jena.graph.Triple) Expr(org.apache.jena.sparql.expr.Expr) QueryIterator(org.apache.jena.sparql.engine.QueryIterator) ResultSet(org.apache.jena.query.ResultSet)

Example 37 with BasicPattern

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();
}
Also used : Triple(org.apache.jena.graph.Triple) HashMap(java.util.HashMap) Node(org.apache.jena.graph.Node) ArrayList(java.util.ArrayList) BasicPattern(org.apache.jena.sparql.core.BasicPattern)

Example 38 with BasicPattern

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;
}
Also used : BasicPattern(org.apache.jena.sparql.core.BasicPattern)

Example 39 with BasicPattern

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);
}
Also used : OpQuadPattern(org.apache.jena.sparql.algebra.op.OpQuadPattern) BasicPattern(org.apache.jena.sparql.core.BasicPattern)

Example 40 with BasicPattern

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;
}
Also used : Triple(org.apache.jena.graph.Triple) Op(org.apache.jena.sparql.algebra.Op) BasicPattern(org.apache.jena.sparql.core.BasicPattern)

Aggregations

BasicPattern (org.apache.jena.sparql.core.BasicPattern)44 Node (org.apache.jena.graph.Node)15 Triple (org.apache.jena.graph.Triple)13 OpBGP (org.apache.jena.sparql.algebra.op.OpBGP)13 Op (org.apache.jena.sparql.algebra.Op)11 Test (org.junit.Test)11 Var (org.apache.jena.sparql.core.Var)5 QueryIterator (org.apache.jena.sparql.engine.QueryIterator)5 Binding (org.apache.jena.sparql.engine.binding.Binding)5 ArrayList (java.util.ArrayList)4 BaseTest (org.apache.jena.atlas.junit.BaseTest)4 OpQuadPattern (org.apache.jena.sparql.algebra.op.OpQuadPattern)4 Quad (org.apache.jena.sparql.core.Quad)4 SocketException (java.net.SocketException)3 HashMap (java.util.HashMap)3 ConnectTimeoutException (org.apache.http.conn.ConnectTimeoutException)3 OpService (org.apache.jena.sparql.algebra.op.OpService)3 QueryExceptionHTTP (org.apache.jena.sparql.engine.http.QueryExceptionHTTP)3 Context (org.apache.jena.sparql.util.Context)3 IndentedLineBuffer (org.apache.jena.atlas.io.IndentedLineBuffer)2