You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@jena.apache.org by ca...@apache.org on 2011/09/01 16:49:28 UTC

svn commit: r1164094 - in /incubator/jena/Jena2/ARQ/trunk: src-test/com/hp/hpl/jena/sparql/resultset/TestResultSet.java src/com/hp/hpl/jena/sparql/resultset/ResultSetMem.java src/com/hp/hpl/jena/sparql/util/ResultSetUtils.java

Author: castagna
Date: Thu Sep  1 14:49:27 2011
New Revision: 1164094

URL: http://svn.apache.org/viewvc?rev=1164094&view=rev
Log:
JENA-66 - Patch applied (with two small additional tests). Thank you Laurent (and sorry for the delay).

Modified:
    incubator/jena/Jena2/ARQ/trunk/src-test/com/hp/hpl/jena/sparql/resultset/TestResultSet.java
    incubator/jena/Jena2/ARQ/trunk/src/com/hp/hpl/jena/sparql/resultset/ResultSetMem.java
    incubator/jena/Jena2/ARQ/trunk/src/com/hp/hpl/jena/sparql/util/ResultSetUtils.java

Modified: incubator/jena/Jena2/ARQ/trunk/src-test/com/hp/hpl/jena/sparql/resultset/TestResultSet.java
URL: http://svn.apache.org/viewvc/incubator/jena/Jena2/ARQ/trunk/src-test/com/hp/hpl/jena/sparql/resultset/TestResultSet.java?rev=1164094&r1=1164093&r2=1164094&view=diff
==============================================================================
--- incubator/jena/Jena2/ARQ/trunk/src-test/com/hp/hpl/jena/sparql/resultset/TestResultSet.java (original)
+++ incubator/jena/Jena2/ARQ/trunk/src-test/com/hp/hpl/jena/sparql/resultset/TestResultSet.java Thu Sep  1 14:49:27 2011
@@ -12,8 +12,8 @@ import java.io.ByteArrayOutputStream ;
 import java.util.ArrayList ;
 import java.util.List ;
 
-import junit.framework.TestCase ;
 import org.junit.Test ;
+import org.openjena.atlas.junit.BaseTest ;
 import org.openjena.atlas.lib.StrUtils ;
 
 import com.hp.hpl.jena.graph.Node ;
@@ -32,8 +32,9 @@ import com.hp.hpl.jena.sparql.engine.ite
 import com.hp.hpl.jena.sparql.sse.SSE ;
 import com.hp.hpl.jena.sparql.sse.builders.BuilderResultSet ;
 import com.hp.hpl.jena.sparql.util.NodeFactory ;
+import com.hp.hpl.jena.sparql.util.ResultSetUtils ;
 
-public class TestResultSet extends TestCase
+public class TestResultSet extends BaseTest
 {
     // Test reading, writing and comparison
     @Test public void test_RS_1()
@@ -149,6 +150,22 @@ public class TestResultSet extends TestC
         test_RS_fmt(rs, ResultsFormat.FMT_RDF_XML, false) ;
     }
 
+    @Test public void test_RS_union_1() 
+    {
+    	ResultSet rs1 = make("x", Node.createURI("tag:local")) ;
+    	ResultSet rs2 = make("x", Node.createURI("tag:local")) ;
+    	ResultSet rs3 = make2("x", Node.createURI("tag:local")) ;
+    	assertTrue(ResultSetCompare.equalsByTerm(rs3, ResultSetUtils.union(rs1, rs2))) ;
+    }
+
+    @Test(expected = ResultSetException.class) 
+    public void test_RS_union_2() 
+    {
+    	ResultSet rs1 = make("x", Node.createURI("tag:local")) ;
+    	ResultSet rs2 = make("y", Node.createURI("tag:local")) ;
+    	ResultSetUtils.union(rs1, rs2) ;
+    }
+
     private void test_RS_fmt(ResultSet rs, ResultsFormat fmt, boolean ordered)
     {
         ResultSetRewindable rs1 = ResultSetFactory.makeRewindable(rs) ;

Modified: incubator/jena/Jena2/ARQ/trunk/src/com/hp/hpl/jena/sparql/resultset/ResultSetMem.java
URL: http://svn.apache.org/viewvc/incubator/jena/Jena2/ARQ/trunk/src/com/hp/hpl/jena/sparql/resultset/ResultSetMem.java?rev=1164094&r1=1164093&r2=1164094&view=diff
==============================================================================
--- incubator/jena/Jena2/ARQ/trunk/src/com/hp/hpl/jena/sparql/resultset/ResultSetMem.java (original)
+++ incubator/jena/Jena2/ARQ/trunk/src/com/hp/hpl/jena/sparql/resultset/ResultSetMem.java Thu Sep  1 14:49:27 2011
@@ -87,6 +87,29 @@ public class ResultSetMem implements com
         }
         reset();
     }
+    
+    /** Create an in-memory result set from an array of 
+     * ResulSets. It is assumed that all the ResultSets 
+     * from the array have the same variables.
+     * 
+     * @param sets the ResultSet objects to concatenate.
+     */
+    
+    public ResultSetMem(ResultSet... sets) 
+    {
+        varNames = sets[0].getResultVars();
+        
+        for (ResultSet rs : sets) 
+        {
+        	if ( !varNames.equals(rs.getResultVars()) )
+        		throw new ResultSetException("ResultSet must have the same variables.") ;
+            if (rs instanceof ResultSetMem)
+                rows.addAll(((ResultSetMem) rs).rows);
+            else 
+                while (rs.hasNext()) rows.add(rs.nextBinding());
+        }
+        reset();
+    }
 
     public ResultSetMem()
     {

Modified: incubator/jena/Jena2/ARQ/trunk/src/com/hp/hpl/jena/sparql/util/ResultSetUtils.java
URL: http://svn.apache.org/viewvc/incubator/jena/Jena2/ARQ/trunk/src/com/hp/hpl/jena/sparql/util/ResultSetUtils.java?rev=1164094&r1=1164093&r2=1164094&view=diff
==============================================================================
--- incubator/jena/Jena2/ARQ/trunk/src/com/hp/hpl/jena/sparql/util/ResultSetUtils.java (original)
+++ incubator/jena/Jena2/ARQ/trunk/src/com/hp/hpl/jena/sparql/util/ResultSetUtils.java Thu Sep  1 14:49:27 2011
@@ -15,6 +15,7 @@ import com.hp.hpl.jena.rdf.model.Literal
 import com.hp.hpl.jena.rdf.model.RDFNode ;
 import com.hp.hpl.jena.rdf.model.Resource ;
 import com.hp.hpl.jena.sparql.ARQException ;
+import com.hp.hpl.jena.sparql.resultset.ResultSetMem ;
 
 public class ResultSetUtils
 {
@@ -67,6 +68,18 @@ public class ResultSetUtils
         }
         return items ;
     }
+
+    /**
+     * Create an in-memory result set from an array of 
+     * ResulSets. It is assumed that all the ResultSets 
+     * from the array have the same variables.
+     * 
+     * @param sets the ResultSets to concatenate.
+     */
+    public static ResultSet union(ResultSet... sets) {
+        return new ResultSetMem(sets);
+    }
+    
 }
 
 /*