Search in sources :

Example 26 with BasicPattern

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

Example 27 with BasicPattern

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);
    }
}
Also used : Binding(org.apache.jena.sparql.engine.binding.Binding) Table(org.apache.jena.sparql.algebra.Table) Var(org.apache.jena.sparql.core.Var) ARQInternalErrorException(org.apache.jena.sparql.ARQInternalErrorException) Node(org.apache.jena.graph.Node) BasicPattern(org.apache.jena.sparql.core.BasicPattern) TableEmpty(org.apache.jena.sparql.algebra.table.TableEmpty) DatasetGraph(org.apache.jena.sparql.core.DatasetGraph) QueryIterConcat(org.apache.jena.sparql.engine.iterator.QueryIterConcat) ExecutionContext(org.apache.jena.sparql.engine.ExecutionContext) DatasetGraph(org.apache.jena.sparql.core.DatasetGraph) OpGraph(org.apache.jena.sparql.algebra.op.OpGraph) Graph(org.apache.jena.graph.Graph) QueryIterator(org.apache.jena.sparql.engine.QueryIterator)

Example 28 with BasicPattern

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

Example 29 with BasicPattern

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

Example 30 with BasicPattern

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));
}
Also used : Triple(org.apache.jena.graph.Triple) Binding(org.apache.jena.sparql.engine.binding.Binding) Op(org.apache.jena.sparql.algebra.Op) OpQuadPattern(org.apache.jena.sparql.algebra.op.OpQuadPattern) QueryIterator(org.apache.jena.sparql.engine.QueryIterator) Node(org.apache.jena.graph.Node) OpBGP(org.apache.jena.sparql.algebra.op.OpBGP) InternalErrorException(org.apache.jena.atlas.lib.InternalErrorException) BasicPattern(org.apache.jena.sparql.core.BasicPattern) Plan(org.apache.jena.sparql.engine.Plan)

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