Helpers to transform BGP to QuadBlock

Project: http://git-wip-us.apache.org/repos/asf/jena/repo
Commit: http://git-wip-us.apache.org/repos/asf/jena/commit/8aef2f1d
Tree: http://git-wip-us.apache.org/repos/asf/jena/tree/8aef2f1d
Diff: http://git-wip-us.apache.org/repos/asf/jena/diff/8aef2f1d

Branch: refs/heads/master
Commit: 8aef2f1da4e44dcc8bf1d55356f32b284f57d2e2
Parents: 537ffba
Author: Jeremy Coulon <jeremy.cou...@3ds.com>
Authored: Mon Apr 9 15:23:58 2018 +0200
Committer: Jeremy Coulon <jeremy.cou...@3ds.com>
Committed: Mon Apr 9 15:23:58 2018 +0200

----------------------------------------------------------------------
 .../org/apache/jena/sparql/algebra/Algebra.java |   8 +-
 .../apache/jena/sparql/algebra/AlgebraQuad.java |  13 ++
 .../sparql/algebra/TransformQuadBlockGraph.java | 128 +++++++++++++++++++
 3 files changed, 148 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/jena/blob/8aef2f1d/jena-arq/src/main/java/org/apache/jena/sparql/algebra/Algebra.java
----------------------------------------------------------------------
diff --git a/jena-arq/src/main/java/org/apache/jena/sparql/algebra/Algebra.java 
b/jena-arq/src/main/java/org/apache/jena/sparql/algebra/Algebra.java
index 7657c6d..32728ac 100644
--- a/jena-arq/src/main/java/org/apache/jena/sparql/algebra/Algebra.java
+++ b/jena-arq/src/main/java/org/apache/jena/sparql/algebra/Algebra.java
@@ -83,12 +83,18 @@ public class Algebra
         return new AlgebraGenerator().compile(elt) ;
     }
 
-    /** Turn an algebra expression into quad form */
+    /** Turn an algebra expression into quadpattern form */
     public static Op toQuadForm(Op op)
     {
         return AlgebraQuad.quadize(op) ;
     }
     
+    /** Turn an algebra expression into quadblock form */
+    public static Op toQuadBlockForm(Op op)
+    {
+        return AlgebraQuad.quadizeBlock(op) ;
+    }
+    
     /** Transform an algebra expression so that default graph is union of the 
named graphs. */
     public static Op unionDefaultGraph(Op op)
     {

http://git-wip-us.apache.org/repos/asf/jena/blob/8aef2f1d/jena-arq/src/main/java/org/apache/jena/sparql/algebra/AlgebraQuad.java
----------------------------------------------------------------------
diff --git 
a/jena-arq/src/main/java/org/apache/jena/sparql/algebra/AlgebraQuad.java 
b/jena-arq/src/main/java/org/apache/jena/sparql/algebra/AlgebraQuad.java
index c00d887..cb0e454 100644
--- a/jena-arq/src/main/java/org/apache/jena/sparql/algebra/AlgebraQuad.java
+++ b/jena-arq/src/main/java/org/apache/jena/sparql/algebra/AlgebraQuad.java
@@ -57,6 +57,19 @@ public class AlgebraQuad
         return Transformer.transformSkipService(qg, null, op, before, after) ;
     }
     
+    public static Op quadizeBlock(Op op)
+    {
+        final Deque<QuadSlot> stack = new ArrayDeque<>() ;
+        QuadSlot qSlot = new QuadSlot(Quad.defaultGraphNodeGenerated, 
Quad.defaultGraphNodeGenerated) ;  
+        stack.push(qSlot) ;             // Starting condition
+        
+        OpVisitor before = new Pusher(stack) ;
+        OpVisitor after = new Popper(stack) ;
+        
+        TransformQuadBlockGraph qg = new TransformQuadBlockGraph(stack, 
before, after) ;
+        return Transformer.transformSkipService(qg, null, op, before, after) ;
+    }
+    
     /** This is the record of the transformation.
      *  The rewriteGraphName is the node to put in the graph slot of the quad.
      *  The actualGraphName is the node used in SPARQL.

http://git-wip-us.apache.org/repos/asf/jena/blob/8aef2f1d/jena-arq/src/main/java/org/apache/jena/sparql/algebra/TransformQuadBlockGraph.java
----------------------------------------------------------------------
diff --git 
a/jena-arq/src/main/java/org/apache/jena/sparql/algebra/TransformQuadBlockGraph.java
 
b/jena-arq/src/main/java/org/apache/jena/sparql/algebra/TransformQuadBlockGraph.java
new file mode 100644
index 0000000..5ad1cce
--- /dev/null
+++ 
b/jena-arq/src/main/java/org/apache/jena/sparql/algebra/TransformQuadBlockGraph.java
@@ -0,0 +1,128 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.jena.sparql.algebra;
+
+import java.util.Deque ;
+
+import org.apache.jena.graph.Node ;
+import org.apache.jena.sparql.algebra.AlgebraQuad.QuadSlot ;
+import org.apache.jena.sparql.algebra.op.* ;
+import org.apache.jena.sparql.core.Var ;
+import org.apache.jena.sparql.expr.ExprVar ;
+
+/**
+ * Transform that rewrites an algebra into quadblock form
+ *
+ */
+public class TransformQuadBlockGraph extends TransformCopy
+{
+    private Deque<QuadSlot> tracker ;
+    private OpVisitor beforeVisitor ;
+    private OpVisitor afterVisitor ;
+
+    public TransformQuadBlockGraph(Deque<QuadSlot> tracker, OpVisitor before, 
OpVisitor after) {
+        this.tracker = tracker ;
+        this.beforeVisitor = before ;
+        this.afterVisitor = after ;
+    }
+    
+    private Node getNode() { return tracker.peek().rewriteGraphName ; }
+
+    @Override
+    public Op transform(OpGraph opGraph, Op op) {
+        
+        //System.err.println("transform(OpGraph)\n"+opGraph+op) ;
+        
+        // ?? Could just leave the (graph) in place always - just rewrite 
BGPs. 
+        boolean noPattern = false ;
+        
+        /* One case to consider is when the pattern for the GRAPH
+         * statement includes uses the variable inside the GRAPH clause. 
+         * In this case, we must rename away the inner variable
+         * to allow stream execution via index joins, 
+         * and then put back the value via an assign.
+         * (This is what QueryIterGraph does using a streaming join
+         * for triples)
+         */
+
+        // Note: op is already quads by this point.
+        // Must test scoping by the subOp of GRAPH
+        
+        QuadSlot qSlot = tracker.peek() ;
+        Node actualName= qSlot.actualGraphName ;
+        Node rewriteName= qSlot.rewriteGraphName ; 
+        
+        if ( OpBGP.isBGP(op) )
+        {
+            // Empty BGP
+            if ( ((OpBGP)op).getPattern().isEmpty() )
+                noPattern = true ;
+        }
+        else if ( op instanceof OpTable )
+        {
+            // Empty BGP compiled to a unit table
+            if ( ((OpTable)op).isJoinIdentity() )
+                noPattern = true ;
+        }
+        
+        if ( noPattern )
+        {
+            // The case of something like:
+            // GRAPH ?g {} or GRAPH <v> {}
+            // which are ways of accessing the names in the dataset.
+            return new OpDatasetNames(opGraph.getNode()) ;
+        }
+        
+        if ( actualName != rewriteName )
+            op = OpAssign.assign(op, Var.alloc(actualName), new 
ExprVar(rewriteName)) ;
+
+        // Drop (graph...) because inside nodes
+        // have been converted to quads.
+        return op ;
+    }
+    
+    @Override
+    public Op transform(OpPropFunc opPropFunc, Op subOp) {
+        if ( opPropFunc.getSubOp() != subOp )
+            opPropFunc = new OpPropFunc(opPropFunc.getProperty(), 
opPropFunc.getSubjectArgs(), opPropFunc.getObjectArgs(), subOp) ;
+        // Put the (graph) back round it so the property function works on the 
named graph.
+        return new OpGraph(getNode() , opPropFunc) ;
+    }
+    
+    @Override
+    public Op transform(OpPath opPath) {
+        // Put the (graph) back round it
+        // ?? inc default graph node.
+        return new OpGraph(getNode() , opPath) ;
+        // Does not get removed by transform above because this is
+        // not the OpGraph that gets walked by the transform.  
+    }
+    
+    @Override
+    public Op transform(OpBGP opBGP) { 
+        //System.out.print("transform(OpBGP) : "+getNode()+"\n"+opBGP) ;
+        return OpQuadBlock.create(getNode(), opBGP.getPattern()) ;
+    }
+    
+    @Override
+    public Op transform(OpExt opExt) {
+        return opExt.apply(this, beforeVisitor, afterVisitor) ;
+    }
+
+}
\ No newline at end of file

Reply via email to