Search in sources :

Example 1 with OpGraph

use of org.apache.jena.sparql.algebra.op.OpGraph in project jena by apache.

the class Eval method evalGraph.

static Table evalGraph(OpGraph opGraph, Evaluator evaluator) {
    ExecutionContext execCxt = evaluator.getExecContext();
    if (!Var.isVar(opGraph.getNode())) {
        DatasetGraph dsg = execCxt.getDataset();
        Node graphNode = opGraph.getNode();
        if (!dsg.containsGraph(graphNode))
            return new TableEmpty();
        Graph graph = execCxt.getDataset().getGraph(opGraph.getNode());
        if (// But contains was true?!!
        graph == null)
            throw new InternalErrorException("Graph was present, now it's not");
        ExecutionContext execCxt2 = new ExecutionContext(execCxt, graph);
        Evaluator e2 = EvaluatorFactory.create(execCxt2);
        return eval(e2, opGraph.getSubOp());
    }
    // Graph node is a variable.
    Var gVar = Var.alloc(opGraph.getNode());
    Table current = null;
    for (Iterator<Node> iter = execCxt.getDataset().listGraphNodes(); iter.hasNext(); ) {
        Node gn = iter.next();
        Graph graph = execCxt.getDataset().getGraph(gn);
        ExecutionContext execCxt2 = new ExecutionContext(execCxt, graph);
        Evaluator e2 = EvaluatorFactory.create(execCxt2);
        Table tableVarURI = TableFactory.create(gVar, gn);
        // Evaluate the pattern, join with this graph node possibility.
        // XXX If Var.ANON then no-opt.
        Table patternTable = eval(e2, opGraph.getSubOp());
        Table stepResult = evaluator.join(patternTable, tableVarURI);
        if (current == null)
            current = stepResult;
        else
            current = evaluator.union(current, stepResult);
    }
    if (current == null)
        // Nothing to loop over
        return new TableEmpty();
    return current;
}
Also used : 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) Table(org.apache.jena.sparql.algebra.Table) Var(org.apache.jena.sparql.core.Var) Node(org.apache.jena.graph.Node) TableEmpty(org.apache.jena.sparql.algebra.table.TableEmpty) ARQInternalErrorException(org.apache.jena.sparql.ARQInternalErrorException) InternalErrorException(org.apache.jena.atlas.lib.InternalErrorException) DatasetGraph(org.apache.jena.sparql.core.DatasetGraph)

Example 2 with OpGraph

use of org.apache.jena.sparql.algebra.op.OpGraph in project jena by apache.

the class TestTransformOpExt method textOpExtQuads.

@Test
public void textOpExtQuads() {
    String x = StrUtils.strjoinNL("(graph <g>", "    (join", "     (bgp (?s ?p ?o))", "      (graph <g2> (bgp (?s ?p ?o)))", "))");
    String y = StrUtils.strjoinNL("(join", "   (quadpattern (quad <g> ?s ?p ?o))", "   (quadpattern (quad <g2> ?s ?p ?o)))");
    // Build
    Op op = SSE.parseOp(x);
    OpGraph opg = (OpGraph) op;
    // Insert OpExtTest
    Op op1 = opg.getSubOp();
    op1 = new OpExtTest(op1);
    op = new OpGraph(opg.getNode(), op1);
    Op op2 = AlgebraQuad.quadize(op);
    assertTrue(op2 instanceof OpExt);
    Op opSub = ((OpExt) op2).effectiveOp();
    Op expectedSub = SSE.parseOp(y);
    assertEquals(expectedSub, opSub);
}
Also used : WriterOp(org.apache.jena.sparql.sse.writers.WriterOp) OpGraph(org.apache.jena.sparql.algebra.op.OpGraph) OpExt(org.apache.jena.sparql.algebra.op.OpExt) Test(org.junit.Test)

Example 3 with OpGraph

use of org.apache.jena.sparql.algebra.op.OpGraph in project jena by apache.

the class OpRewriter method visit.

@Override
public void visit(OpGraph opGraph) {
    opGraph.getSubOp().visit(this);
    push(new OpGraph(changeNode(opGraph.getNode()), pop()));
}
Also used : OpGraph(org.apache.jena.sparql.algebra.op.OpGraph)

Example 4 with OpGraph

use of org.apache.jena.sparql.algebra.op.OpGraph in project jena by apache.

the class TestSpecialGraphNames method union_dft_1.

private void union_dft_1(Mode mode) {
    Op opPattern = SSE.parseOp("(bgp (?s ?p ?o))");
    Op op = op("(bgp (?s ?p ?o))", mode);
    List<Binding> results = exec(op);
    assertEquals(5, results.size());
    // Now execute as the union graph.
    Op op2 = new OpGraph(Quad.unionGraph, opPattern);
    List<Binding> results2 = exec(op2);
    assertEquals(4, results2.size());
}
Also used : Binding(org.apache.jena.sparql.engine.binding.Binding) Op(org.apache.jena.sparql.algebra.Op) OpGraph(org.apache.jena.sparql.algebra.op.OpGraph)

Example 5 with OpGraph

use of org.apache.jena.sparql.algebra.op.OpGraph 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)

Aggregations

OpGraph (org.apache.jena.sparql.algebra.op.OpGraph)8 Var (org.apache.jena.sparql.core.Var)5 Graph (org.apache.jena.graph.Graph)4 Node (org.apache.jena.graph.Node)4 ARQInternalErrorException (org.apache.jena.sparql.ARQInternalErrorException)4 Table (org.apache.jena.sparql.algebra.Table)4 TableEmpty (org.apache.jena.sparql.algebra.table.TableEmpty)4 DatasetGraph (org.apache.jena.sparql.core.DatasetGraph)4 ExecutionContext (org.apache.jena.sparql.engine.ExecutionContext)4 Binding (org.apache.jena.sparql.engine.binding.Binding)3 InternalErrorException (org.apache.jena.atlas.lib.InternalErrorException)2 BasicPattern (org.apache.jena.sparql.core.BasicPattern)2 QueryIterator (org.apache.jena.sparql.engine.QueryIterator)2 QueryIterConcat (org.apache.jena.sparql.engine.iterator.QueryIterConcat)2 Op (org.apache.jena.sparql.algebra.Op)1 OpExt (org.apache.jena.sparql.algebra.op.OpExt)1 WriterOp (org.apache.jena.sparql.sse.writers.WriterOp)1 Test (org.junit.Test)1