You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@jena.apache.org by an...@apache.org on 2015/07/14 11:57:11 UTC

jena git commit: JENA-991: Combine adjacent ElementPathBlocks

Repository: jena
Updated Branches:
  refs/heads/master 20ac8e6f7 -> 84ad58d4a


JENA-991: Combine adjacent ElementPathBlocks

Switch tests to stronger conditions for patterns now
made same as original syntax.

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

Branch: refs/heads/master
Commit: 84ad58d4a6900a3df8f456ab9cd9e5707a0369b5
Parents: 20ac8e6
Author: Andy Seaborne <an...@apache.org>
Authored: Tue Jul 14 10:56:54 2015 +0100
Committer: Andy Seaborne <an...@apache.org>
Committed: Tue Jul 14 10:56:54 2015 +0100

----------------------------------------------------------------------
 .../apache/jena/sparql/algebra/OpAsQuery.java   | 22 +++++++++++++++++-
 .../jena/sparql/algebra/TestOpAsQuery.java      | 24 +++++++++-----------
 2 files changed, 32 insertions(+), 14 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/jena/blob/84ad58d4/jena-arq/src/main/java/org/apache/jena/sparql/algebra/OpAsQuery.java
----------------------------------------------------------------------
diff --git a/jena-arq/src/main/java/org/apache/jena/sparql/algebra/OpAsQuery.java b/jena-arq/src/main/java/org/apache/jena/sparql/algebra/OpAsQuery.java
index b23b39c..19e56da 100644
--- a/jena-arq/src/main/java/org/apache/jena/sparql/algebra/OpAsQuery.java
+++ b/jena-arq/src/main/java/org/apache/jena/sparql/algebra/OpAsQuery.java
@@ -439,7 +439,7 @@ public class OpAsQuery {
         // There is one special case to consider:
         // A path expression was expanded into a OpSequence during Algenra
         // generation. The simple path expressions become an OpSequence that could be
-        // recombined into an ElementPathBlock
+        // recombined into an ElementPathBlock.
 
         @Override
         public void visit(OpSequence opSequence) {
@@ -450,11 +450,31 @@ public class OpAsQuery {
                 g = currentGroup() ;
             }
 
+            ElementPathBlock currentPathBlock = null ;
             for ( Op op : opSequence.getElements() ) {
                 Element e = asElement(op) ;
+                // -- Combining code.
+                if ( e instanceof ElementPathBlock ) {
+                    if ( currentPathBlock != null ) {
+                        // Use last ElementPathBlock
+                        ElementPathBlock newPathBlock = (ElementPathBlock)e ;
+                        currentPathBlock.getPattern().addAll(newPathBlock.getPattern());
+                        // Don't add to the ElementGroup.
+                        continue ;
+                    } else { 
+                        // Start an ElementPathBlock 
+                        currentPathBlock = (ElementPathBlock)e ;
+                        // fall through to add the element.
+                    }
+                } else {
+                    // Current element not a ElementPathBlock
+                    currentPathBlock = null ;
+                }
+                // -- End combining code
                 g.addElement(e) ;
             }
 
+            
             if ( nestGroup )
                 endSubGroup() ;
             return ;

http://git-wip-us.apache.org/repos/asf/jena/blob/84ad58d4/jena-arq/src/test/java/org/apache/jena/sparql/algebra/TestOpAsQuery.java
----------------------------------------------------------------------
diff --git a/jena-arq/src/test/java/org/apache/jena/sparql/algebra/TestOpAsQuery.java b/jena-arq/src/test/java/org/apache/jena/sparql/algebra/TestOpAsQuery.java
index 443a242..6e37c2d 100644
--- a/jena-arq/src/test/java/org/apache/jena/sparql/algebra/TestOpAsQuery.java
+++ b/jena-arq/src/test/java/org/apache/jena/sparql/algebra/TestOpAsQuery.java
@@ -128,7 +128,7 @@ public class TestOpAsQuery {
     { test_roundTripQuery("SELECT ?s { ?s ?p ?o { SELECT (count(*) as ?cp) { ?s ?p ?o } }}") ; }
     
     @Test public void testSubQuery_03()
-    //{ testRoundTripQuery("SELECT ?s { { SELECT (count(*) as ?cp) { ?s ?p ?o } } ?s ?p ?o }") ; }
+    //{ test_roundTripQuery("SELECT ?s { { SELECT (count(*) as ?cp) { ?s ?p ?o } } ?s ?p ?o }") ; }
     // The trailing ?s ?p ?o gets a {} round it.
     { test_equivalentQuery("SELECT ?s { { SELECT (count(*) as ?cp) { ?s ?p ?o } } ?s ?p ?o }",
                            "SELECT ?s { { SELECT (count(*) as ?cp) { ?s ?p ?o } } { ?s ?p ?o } }") ; }
@@ -365,16 +365,14 @@ public class TestOpAsQuery {
     
     @Test
     public void testPathExpressions1() {
-        // test that the query after serialization is legal (as much a test of the serializer as way OpAsQuery works)
         String query = "PREFIX : <http://example/> SELECT * { ?s :p* ?o . ?x :r 123 . }" ;
-        test_roundTripAlegbra(query);
+        test_roundTripQuery(query);
     }
         
     @Test
     public void testPathExpressions2() {
-        // test that the query  
         String query = "PREFIX : <http://example/> SELECT * { ?s :p*/:q ?o . ?x :r 123 . }" ;
-        test_roundTripAlegbra(query);
+        test_roundTripQuery(query);
     }
 
     @Test
@@ -384,16 +382,17 @@ public class TestOpAsQuery {
     
     @Test
     public void testMinus2() {
-        // query gains a level of {} but the meaning is the same. 
-        String query = "PREFIX : <http://example/> SELECT * { ?s :p ?o OPTIONAL { ?s :x ?2 } MINUS { ?s :q ?v .FILTER(?v<5) } }" ; 
-       test_roundTripAlegbra(query);
+        // query gains a level of {} but the meaning is the same.
+        String query = "PREFIX : <http://example/> SELECT * { ?s :p ?o OPTIONAL { ?s :x ?2 } MINUS { ?s :q ?v .FILTER(?v<5) } }" ;
+        test_roundTripAlegbra(query) ;
     }
     
-    
     // There 3 classes of transformations: there are 3 main test operations.
-    //   The same query is recovered from OpAsQuery
-    //   Different queries with the same alegra forms
-    //   Different equivalent queries - same answers, different algebra.
+    //   test_roundTripQuery: The same query is recovered from OpAsQuery
+    //   test_roundTripAlegbra: Different queries with the same alegra forms
+    //   test_equivalentQuery: Different equivalent queries - same answers, different algebra.
+    // 
+    // test_roundTripQuery is test_equivalentQuery with same input and expected.
     // + quad variants.
     
     public static void test_equivalentQuery(String input, String expected) {
@@ -426,7 +425,6 @@ public class TestOpAsQuery {
     // Sometimes Q1 and Q2 are equivalent but not .equals.  
     public void test_roundTripAlegbra(String query) {
         Query[] r = roundTripQuery(query);
-        
         // Even if the strings come out as non-equal because of the translation from algebra to query
         // the algebras should be equal
         // i.e. the queries should remain semantically equivalent