You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@jena.apache.org by rv...@apache.org on 2014/09/12 14:17:33 UTC

svn commit: r1624515 - in /jena/trunk/jena-arq/src: main/java/com/hp/hpl/jena/sparql/util/NodeUtils.java test/java/com/hp/hpl/jena/sparql/expr/TestOrdering.java

Author: rvesse
Date: Fri Sep 12 12:17:33 2014
New Revision: 1624515

URL: http://svn.apache.org/r1624515
Log:
Improve NodeUtils.compareRDFTerms() to cope with variable nodes (JENA-784)

Modified:
    jena/trunk/jena-arq/src/main/java/com/hp/hpl/jena/sparql/util/NodeUtils.java
    jena/trunk/jena-arq/src/test/java/com/hp/hpl/jena/sparql/expr/TestOrdering.java

Modified: jena/trunk/jena-arq/src/main/java/com/hp/hpl/jena/sparql/util/NodeUtils.java
URL: http://svn.apache.org/viewvc/jena/trunk/jena-arq/src/main/java/com/hp/hpl/jena/sparql/util/NodeUtils.java?rev=1624515&r1=1624514&r2=1624515&view=diff
==============================================================================
--- jena/trunk/jena-arq/src/main/java/com/hp/hpl/jena/sparql/util/NodeUtils.java (original)
+++ jena/trunk/jena-arq/src/main/java/com/hp/hpl/jena/sparql/util/NodeUtils.java Fri Sep 12 12:17:33 2014
@@ -127,7 +127,22 @@ public class NodeUtils
             return compareLiteralsBySyntax(node1, node2) ;
         
         // One or both not literals
-        // Blank nodes < URIs < Literals
+        // Variables < Blank nodes < URIs < Literals
+        
+        if ( node1.isVariable() )
+        {
+            if ( node2.isVariable() )
+            {
+                return StrUtils.strCompare(node1.getName(), node2.getName()) ;
+            }
+            // Variables before anything else
+            return Expr.CMP_LESS;
+        }
+        
+        if ( node2.isVariable() ) {
+            // node1 not variable
+            return Expr.CMP_GREATER ;
+        }
         
         if ( node1.isBlank() )
         {
@@ -137,7 +152,7 @@ public class NodeUtils
                 String s2 = node2.getBlankNodeId().getLabelString() ;
                 return StrUtils.strCompare(s1, s2) ;
             }
-            // bNodes before anything else.
+            // bNodes before anything but variables
             return Expr.CMP_LESS ;
         }
             

Modified: jena/trunk/jena-arq/src/test/java/com/hp/hpl/jena/sparql/expr/TestOrdering.java
URL: http://svn.apache.org/viewvc/jena/trunk/jena-arq/src/test/java/com/hp/hpl/jena/sparql/expr/TestOrdering.java?rev=1624515&r1=1624514&r2=1624515&view=diff
==============================================================================
--- jena/trunk/jena-arq/src/test/java/com/hp/hpl/jena/sparql/expr/TestOrdering.java (original)
+++ jena/trunk/jena-arq/src/test/java/com/hp/hpl/jena/sparql/expr/TestOrdering.java Fri Sep 12 12:17:33 2014
@@ -247,4 +247,52 @@ public class TestOrdering extends BaseTe
         int y = NodeUtils.compareRDFTerms(nv1.asNode() , nv2.asNode()) ;
         assertTrue(Expr.CMP_GREATER == x ) ;
     }
+    
+    @Test public void test_variable1() {
+        Node x = NodeFactory.createVariable("x");
+        Node y = NodeFactory.createVariable("x");
+        
+        int res = NodeUtils.compareRDFTerms(x, y);
+        assertTrue("Variable nodes should sort by variable names", Expr.CMP_EQUAL == res);
+    }
+    
+    @Test public void test_variable2() {
+        Node x = NodeFactory.createVariable("x");
+        Node y = NodeFactory.createVariable("y");
+        
+        int res = NodeUtils.compareRDFTerms(x, y);
+        assertTrue("Variable nodes should sort by variable names", Expr.CMP_LESS == res);
+        res = NodeUtils.compareRDFTerms(y, x);
+        assertTrue("Variable nodes should sort by variable names", Expr.CMP_GREATER == res);
+    }
+    
+    @Test public void test_variable3() {
+        Node x = NodeFactory.createVariable("x");
+        Node y = NodeFactory.createAnon();
+        
+        int res = NodeUtils.compareRDFTerms(x, y);
+        assertTrue("Variable nodes should be less than blank nodes", Expr.CMP_LESS == res);
+        res = NodeUtils.compareRDFTerms(y, x);
+        assertTrue("Variable nodes should be less than blank nodes", Expr.CMP_GREATER == res);
+    }
+    
+    @Test public void test_variable4() {
+        Node x = NodeFactory.createVariable("x");
+        Node y = NodeFactory.createURI("http://uri");
+        
+        int res = NodeUtils.compareRDFTerms(x, y);
+        assertTrue("Variable nodes should be less than URI nodes", Expr.CMP_LESS == res);
+        res = NodeUtils.compareRDFTerms(y, x);
+        assertTrue("Variable nodes should be less than URI nodes", Expr.CMP_GREATER == res);
+    }
+    
+    @Test public void test_variable5() {
+        Node x = NodeFactory.createVariable("x");
+        Node y = NodeFactory.createLiteral("test");
+        
+        int res = NodeUtils.compareRDFTerms(x, y);
+        assertTrue("Variable nodes should be less than literal nodes", Expr.CMP_LESS == res);
+        res = NodeUtils.compareRDFTerms(y, x);
+        assertTrue("Variable nodes should be less than literal nodes", Expr.CMP_GREATER == res);
+    }
 }