jena-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From a...@apache.org
Subject [11/23] jena git commit: Intermediate towards new walker/transformer.
Date Sat, 21 May 2016 18:54:31 GMT
Intermediate towards new walker/transformer.

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

Branch: refs/heads/master
Commit: 4a9ead549eae83aeba8aff522f32c891c11991f9
Parents: bbd9d59
Author: Andy Seaborne <andy@apache.org>
Authored: Sat Apr 23 00:21:14 2016 +0100
Committer: Andy Seaborne <andy@apache.org>
Committed: Sat May 21 18:29:13 2016 +0100

----------------------------------------------------------------------
 .../algebra/walker/ApplyTransformVisitor.java   | 122 +++++++++++++------
 .../sparql/algebra/walker/WalkerVisitor.java    |  50 +++++++-
 2 files changed, 133 insertions(+), 39 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/jena/blob/4a9ead54/jena-arq/src/main/java/org/apache/jena/sparql/algebra/walker/ApplyTransformVisitor.java
----------------------------------------------------------------------
diff --git a/jena-arq/src/main/java/org/apache/jena/sparql/algebra/walker/ApplyTransformVisitor.java
b/jena-arq/src/main/java/org/apache/jena/sparql/algebra/walker/ApplyTransformVisitor.java
index 750f893..2d545dd 100644
--- a/jena-arq/src/main/java/org/apache/jena/sparql/algebra/walker/ApplyTransformVisitor.java
+++ b/jena-arq/src/main/java/org/apache/jena/sparql/algebra/walker/ApplyTransformVisitor.java
@@ -57,15 +57,48 @@ public class ApplyTransformVisitor implements OpVisitorByTypeAndExpr,
ExprVisito
     /*package*/ final Expr exprResult() {
         return pop(exprStack) ;
     }
+    
+    private static boolean ISOLATE = false ;
 
     protected Op transform(Op op) {
-        // reuse this ApplyTransformVisitor? with stack checking?
-        return Walker.transform(op, this, beforeVisitor, afterVisitor) ;
+        if ( ISOLATE ) {
+            ApplyTransformVisitor atv = new ApplyTransformVisitor(opTransform, exprTransform,
beforeVisitor, afterVisitor) ;
+            return Walker.transform(op, atv, beforeVisitor, afterVisitor) ;
+        }
+        
+        int x1 = opStack.size() ;
+        int x2 = exprStack.size() ;
+        try {
+            // reuse this ApplyTransformVisitor? with stack checking?
+            return Walker.transform(op, this, beforeVisitor, afterVisitor) ;
+        } finally {
+            int y1 = opStack.size() ;
+            int y2 = exprStack.size() ;
+            if ( x1 != y1 )
+                System.err.println("Misaligned opStack") ;
+            if ( x2 != y2 )
+                System.err.println("Misaligned exprStack") ;
+        }
     }
     
     protected Expr transform(Expr expr) {
+        if ( ISOLATE ) {
+            ApplyTransformVisitor atv = new ApplyTransformVisitor(opTransform, exprTransform,
beforeVisitor, afterVisitor) ;
+            return Walker.transform(expr, atv, beforeVisitor, afterVisitor) ;
+        }
+        int x1 = opStack.size() ;
+        int x2 = exprStack.size() ;
         // reuse this ApplyTransformVisitor? with stack checking?
-        return Walker.transform(expr, this, beforeVisitor, afterVisitor) ;
+        try {
+            return Walker.transform(expr, this, beforeVisitor, afterVisitor) ;
+        } finally {
+            int y1 = opStack.size() ;
+            int y2 = exprStack.size() ;
+            if ( x1 != y1 )
+                System.err.println("Misaligned opStack") ;
+            if ( x2 != y2 )
+                System.err.println("Misaligned exprStack") ;
+        }
     }
     
     protected ExprList transform(ExprList exprList) {
@@ -115,6 +148,8 @@ public class ApplyTransformVisitor implements OpVisitorByTypeAndExpr,
ExprVisito
         visit1(opExtend2) ;
     }
 
+    // VarExprLists are not visited by OpVisitorByTypeAndExpr 
+    // XXX Maybe they should.
     private VarExprList process(VarExprList varExprList) {
         if ( varExprList == null )
             return varExprList ;
@@ -138,32 +173,39 @@ public class ApplyTransformVisitor implements OpVisitorByTypeAndExpr,
ExprVisito
         return varExpr2 ;
     }
 
-    private ExprList process(ExprList exprList) {
+    private ExprList collect(ExprList exprList) {
         if ( exprList == null )
             return null ;
-        ExprList exprList2 = new ExprList() ;
-        boolean changed = false ;
-        for ( Expr e : exprList ) {
-            Expr e2 = process(e) ;
-            exprList2.add(e2) ;
-            if ( e != e2 )
-                changed = true ;
-        }
-        if ( !changed )
-            return exprList ;
-        return exprList2 ;
-    }
-
-    private Expr process(Expr expr) {
-        Expr e = expr ;
-        Expr e2 = e ;
-        if ( e != null )
-            e2 = transform(e) ;
-        if ( e == e2 )
-            return expr ;
-        return e2 ;
-    }
-
+        ExprList ex2 = new ExprList() ;
+        exprList.forEach((e)->ex2.add(pop(exprStack)));
+        return ex2 ; 
+    }
+     
+//    private ExprList process(ExprList exprList) {
+//        if ( exprList == null )
+//            return null ;
+//        ExprList exprList2 = new ExprList() ;
+//        boolean changed = false ;
+//        for ( Expr e : exprList ) {
+//            Expr e2 = process(e) ;
+//            exprList2.add(e2) ;
+//            if ( e != e2 )
+//                changed = true ;
+//        }
+//        if ( !changed )
+//            return exprList ;
+//        return exprList2 ;
+//
+//    private Expr process(Expr expr) {
+//        Expr e = expr ;
+//        Expr e2 = e ;
+//        if ( e != null )
+//            e2 = transform(e) ;
+//        if ( e == e2 )
+//            return expr ;
+//        return e2 ;
+//    }
+//
     @Override
     public void visit(OpGroup opGroup) {
         boolean changed = false ;
@@ -249,6 +291,14 @@ public class ApplyTransformVisitor implements OpVisitorByTypeAndExpr,
ExprVisito
         push(opStack, opX) ;
     }
 
+    private void dump(String label) {
+        System.out.println(label) ;
+        String x = opStack.toString().replace('\n', ' ').replaceAll("  +", " ") ;
+        String y = exprStack.toString().replace('\n', ' ').replaceAll("  +", " ") ;
+        System.out.println("    O:"+x);
+        System.out.println("    E:"+y);
+    }
+    
     @Override
     public void visit(OpFilter opFilter) {
         Op subOp = null ;
@@ -257,7 +307,8 @@ public class ApplyTransformVisitor implements OpVisitorByTypeAndExpr,
ExprVisito
         boolean changed = (opFilter.getSubOp() != subOp) ;
 
         ExprList ex = opFilter.getExprs() ;
-        ExprList ex2 = process(ex) ;
+        ExprList ex2 = collect(ex) ;
+        
         OpFilter f = opFilter ;
         if ( ex != ex2 )
             f = (OpFilter)OpFilter.filter(ex2, subOp) ;
@@ -276,7 +327,7 @@ public class ApplyTransformVisitor implements OpVisitorByTypeAndExpr,
ExprVisito
             left = pop(opStack) ;
 
         ExprList exprs = op.getExprs() ;
-        ExprList exprs2 = process(exprs) ;
+        ExprList exprs2 = collect(exprs) ;
         OpLeftJoin x = op ;
         if ( exprs != exprs2 )
             x = OpLeftJoin.createLeftJoin(left, right, exprs2) ;
@@ -302,6 +353,7 @@ public class ApplyTransformVisitor implements OpVisitorByTypeAndExpr,
ExprVisito
     
     @Override
     public void visitExpr(ExprList exprs) { 
+        // XXX
         System.err.println("visitExpr(ExprList)") ;
         if ( exprs != null && exprTransform != null ) {
             
@@ -309,7 +361,7 @@ public class ApplyTransformVisitor implements OpVisitorByTypeAndExpr,
ExprVisito
     }
     
     @Override
-    public void visitExpr(VarExprList exprVarExprList)  {
+    public void visitVarExpr(VarExprList exprVarExprList)  {
         System.err.println("visitExpr(ExprList)") ;
         if ( exprVarExprList != null && exprTransform != null ) {
             
@@ -348,12 +400,12 @@ public class ApplyTransformVisitor implements OpVisitorByTypeAndExpr,
ExprVisito
 
     @Override
     public void visit(ExprFunctionN func) {
-        ExprList x = process(func.getArgs()) ;
+        ExprList x = collect(func.getArgs()) ;
         Expr e = func.apply(exprTransform, x) ;
         push(exprStack, e) ;
     }
 
-    private ExprList process(List<Expr> exprList) {
+    private ExprList collect(List<Expr> exprList) {
         if ( exprList == null )
             return null ;
         int N = exprList.size() ;
@@ -369,10 +421,10 @@ public class ApplyTransformVisitor implements OpVisitorByTypeAndExpr,
ExprVisito
     @Override
     public void visit(ExprFunctionOp funcOp) {
         ExprList x = null ;
-//        Op op = transform(funcOp.getGraphPattern()) ;
         if ( funcOp.getArgs() != null )
-            x = process(funcOp.getArgs()) ;
-        Expr e = funcOp.apply(exprTransform, x, funcOp.getGraphPattern()) ;
+            x = collect(funcOp.getArgs()) ;
+        Op op = pop(opStack) ;
+        Expr e = funcOp.apply(exprTransform, x, op) ;
         push(exprStack, e) ;
     }
 

http://git-wip-us.apache.org/repos/asf/jena/blob/4a9ead54/jena-arq/src/main/java/org/apache/jena/sparql/algebra/walker/WalkerVisitor.java
----------------------------------------------------------------------
diff --git a/jena-arq/src/main/java/org/apache/jena/sparql/algebra/walker/WalkerVisitor.java
b/jena-arq/src/main/java/org/apache/jena/sparql/algebra/walker/WalkerVisitor.java
index 59fa8a0..a54572a 100644
--- a/jena-arq/src/main/java/org/apache/jena/sparql/algebra/walker/WalkerVisitor.java
+++ b/jena-arq/src/main/java/org/apache/jena/sparql/algebra/walker/WalkerVisitor.java
@@ -114,7 +114,7 @@ public class WalkerVisitor implements OpVisitorByTypeAndExpr, ExprVisitorFunctio
     }
 
     @Override
-    public void visitExpr(VarExprList varExprList) {
+    public void visitVarExpr(VarExprList varExprList) {
         if ( exprVisitor != null )
             walk(varExprList);
     }
@@ -137,13 +137,17 @@ public class WalkerVisitor implements OpVisitorByTypeAndExpr, ExprVisitorFunctio
     @Override
     public void visit1(Op1 op) {
         before(op) ;
+        visit1$(op) ;
+        after(op) ;
+    }
+    
+    private void visit1$(Op1 op) {
         if ( op.getSubOp() != null )
             op.getSubOp().visit(this) ;
         if ( opVisitor != null )
             op.visit(opVisitor) ;
-        after(op) ;
     }
-    
+
     @Override
     public void visit2(Op2 op) {
         before(op) ;
@@ -186,6 +190,45 @@ public class WalkerVisitor implements OpVisitorByTypeAndExpr, ExprVisitorFunctio
         OpVisitorByTypeAndExpr.super.visit(op) ;
     }
     
+    @Override
+    public void visit(OpOrder opOrder) {
+        // XXX
+//        List<SortCondition> conditions = opOrder.getConditions() ;
+//        List<SortCondition> conditions2 = new ArrayList<>() ;
+//        boolean changed = false ;
+//
+//        for ( SortCondition sc : conditions ) {
+//            Expr e = sc.getExpression() ;
+//            Expr e2 = transform(e) ;
+//            conditions2.add(new SortCondition(e2, sc.getDirection())) ;
+//            if ( e != e2 )
+//                changed = true ;
+//        }
+//        OpOrder x = opOrder ;
+//        if ( changed )
+//            x = new OpOrder(opOrder.getSubOp(), conditions2) ;
+        visit1(opOrder) ;
+    }
+
+    @Override
+    public void visit(OpAssign opAssign) {
+        before(opAssign) ;
+        VarExprList varExpr = opAssign.getVarExprList() ;
+        visitVarExpr(varExpr); 
+        visit1$(opAssign) ;
+        after(opAssign) ;
+    }
+
+    @Override
+    public void visit(OpExtend opExtend) {
+        before(opExtend) ;
+        VarExprList varExpr = opExtend.getVarExprList() ;
+        visitVarExpr(varExpr); 
+        visit1$(opExtend) ;
+        after(opExtend) ;
+    }
+
+    
     // Transforming to quads needs the graph node handled before doing the sub-algebra ops
     // so it has to be done as before/after by the Walker. By the time visit(OpGraph) is
called,
     // the sub-tree has already been visited. 
@@ -210,7 +253,6 @@ public class WalkerVisitor implements OpVisitorByTypeAndExpr, ExprVisitorFunctio
 //        stack.pop() ;
 //    }
 
-    // Shared with ElementWalker - mixin
     @Override
     public void visit(ExprFunction0 func) { visitExprFunction(func) ; }
     @Override


Mime
View raw message