jena-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From rve...@apache.org
Subject svn commit: r1411482 - in /jena/trunk/jena-arq/src: main/java/com/hp/hpl/jena/sparql/function/user/ test/java/com/hp/hpl/jena/sparql/function/user/
Date Mon, 19 Nov 2012 23:58:33 GMT
Author: rvesse
Date: Mon Nov 19 23:58:32 2012
New Revision: 1411482

URL: http://svn.apache.org/viewvc?rev=1411482&view=rev
Log:
Expand test coverage for new user defined functions

Modified:
    jena/trunk/jena-arq/src/main/java/com/hp/hpl/jena/sparql/function/user/UserDefinedFunctionDefinition.java
    jena/trunk/jena-arq/src/test/java/com/hp/hpl/jena/sparql/function/user/TS_UserFunctions.java
    jena/trunk/jena-arq/src/test/java/com/hp/hpl/jena/sparql/function/user/TestFunctionExpansion.java

Modified: jena/trunk/jena-arq/src/main/java/com/hp/hpl/jena/sparql/function/user/UserDefinedFunctionDefinition.java
URL: http://svn.apache.org/viewvc/jena/trunk/jena-arq/src/main/java/com/hp/hpl/jena/sparql/function/user/UserDefinedFunctionDefinition.java?rev=1411482&r1=1411481&r2=1411482&view=diff
==============================================================================
--- jena/trunk/jena-arq/src/main/java/com/hp/hpl/jena/sparql/function/user/UserDefinedFunctionDefinition.java
(original)
+++ jena/trunk/jena-arq/src/main/java/com/hp/hpl/jena/sparql/function/user/UserDefinedFunctionDefinition.java
Mon Nov 19 23:58:32 2012
@@ -20,16 +20,23 @@ package com.hp.hpl.jena.sparql.function.
 
 import java.util.ArrayList;
 import java.util.List;
+import java.util.Set;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 import com.hp.hpl.jena.sparql.core.Var;
 import com.hp.hpl.jena.sparql.expr.Expr;
 import com.hp.hpl.jena.sparql.function.Function;
+import com.hp.hpl.jena.sparql.sse.builders.ExprBuildException;
 
 /**
  * Represents the definition of a user defined function
  *
  */
 public class UserDefinedFunctionDefinition {
+	
+	private static final Logger LOG = LoggerFactory.getLogger(UserDefinedFunctionDefinition.class);
 
     private String uri;
     private Expr expr;
@@ -45,6 +52,20 @@ public class UserDefinedFunctionDefiniti
         this.uri = uri;
         this.expr = e;
         this.argList = new ArrayList<Var>(argList);
+        
+        //Verify that all mentioned variables are in the arguments list
+        Set<Var> mentioned = this.expr.getVarsMentioned();
+        for (Var v : mentioned) {
+        	if (!argList.contains(v)) throw new ExprBuildException("Cannot use the variable
" + v.toString() + " in the expression since it is not included in the argList argument. 
All variables must be arguments to the function"); 
+        }        
+        //If used variables is greater than argument variables this is an error
+        if (mentioned.size() > this.argList.size()) throw new ExprBuildException("Mismatch
between variables used in expression and number of variables in argument list, expected "
+ this.argList.size() + " but found " + mentioned.size());
+        //May have more arguments than used, however this only gives warning(s)
+        if (mentioned.size() < this.argList.size()) {
+        	for (Var v : this.argList) {
+        		if (!mentioned.contains(v)) LOG.warn("Function <" + uri + "> has argument
" + v + " which is never used in the expression");
+        	}
+        }
     }
     
     /**

Modified: jena/trunk/jena-arq/src/test/java/com/hp/hpl/jena/sparql/function/user/TS_UserFunctions.java
URL: http://svn.apache.org/viewvc/jena/trunk/jena-arq/src/test/java/com/hp/hpl/jena/sparql/function/user/TS_UserFunctions.java?rev=1411482&r1=1411481&r2=1411482&view=diff
==============================================================================
--- jena/trunk/jena-arq/src/test/java/com/hp/hpl/jena/sparql/function/user/TS_UserFunctions.java
(original)
+++ jena/trunk/jena-arq/src/test/java/com/hp/hpl/jena/sparql/function/user/TS_UserFunctions.java
Mon Nov 19 23:58:32 2012
@@ -18,12 +18,13 @@
 
 package com.hp.hpl.jena.sparql.function.user;
 
-import junit.framework.JUnit4TestAdapter ;
-import org.junit.runner.RunWith ;
-import org.junit.runners.Suite ;
-import org.junit.runners.Suite.SuiteClasses ;
+import junit.framework.JUnit4TestAdapter;
 
-import com.hp.hpl.jena.sparql.expr.TS_Expr ;
+import org.junit.runner.RunWith;
+import org.junit.runners.Suite;
+import org.junit.runners.Suite.SuiteClasses;
+
+import com.hp.hpl.jena.sparql.expr.TS_Expr;
 
 @RunWith(Suite.class)
 @SuiteClasses( {

Modified: jena/trunk/jena-arq/src/test/java/com/hp/hpl/jena/sparql/function/user/TestFunctionExpansion.java
URL: http://svn.apache.org/viewvc/jena/trunk/jena-arq/src/test/java/com/hp/hpl/jena/sparql/function/user/TestFunctionExpansion.java?rev=1411482&r1=1411481&r2=1411482&view=diff
==============================================================================
--- jena/trunk/jena-arq/src/test/java/com/hp/hpl/jena/sparql/function/user/TestFunctionExpansion.java
(original)
+++ jena/trunk/jena-arq/src/test/java/com/hp/hpl/jena/sparql/function/user/TestFunctionExpansion.java
Mon Nov 19 23:58:32 2012
@@ -18,19 +18,26 @@
 
 package com.hp.hpl.jena.sparql.function.user;
 
-import java.util.ArrayList ;
-import java.util.List ;
+import java.util.ArrayList;
+import java.util.List;
 
-import org.junit.AfterClass ;
-import org.junit.Assert ;
-import org.junit.BeforeClass ;
-import org.junit.Test ;
-
-import com.hp.hpl.jena.sparql.core.Var ;
-import com.hp.hpl.jena.sparql.expr.* ;
-import com.hp.hpl.jena.sparql.expr.nodevalue.NodeValueBoolean ;
-import com.hp.hpl.jena.sparql.expr.nodevalue.NodeValueDouble ;
-import com.hp.hpl.jena.sparql.expr.nodevalue.NodeValueInteger ;
+import org.junit.AfterClass;
+import org.junit.Assert;
+import org.junit.BeforeClass;
+import org.junit.Test;
+
+import com.hp.hpl.jena.sparql.core.Var;
+import com.hp.hpl.jena.sparql.expr.E_Add;
+import com.hp.hpl.jena.sparql.expr.E_Function;
+import com.hp.hpl.jena.sparql.expr.E_Multiply;
+import com.hp.hpl.jena.sparql.expr.E_Subtract;
+import com.hp.hpl.jena.sparql.expr.Expr;
+import com.hp.hpl.jena.sparql.expr.ExprList;
+import com.hp.hpl.jena.sparql.expr.ExprVar;
+import com.hp.hpl.jena.sparql.expr.nodevalue.NodeValueBoolean;
+import com.hp.hpl.jena.sparql.expr.nodevalue.NodeValueDouble;
+import com.hp.hpl.jena.sparql.expr.nodevalue.NodeValueInteger;
+import com.hp.hpl.jena.sparql.sse.builders.ExprBuildException;
 
 /**
  * Test for checking that functions are appropriately expanded when supplied with actual
arguments
@@ -194,8 +201,11 @@ public class TestFunctionExpansion {
         ExprList altArgs = new ExprList();
         altArgs.add(new ExprVar("a"));
         altArgs.add(new ExprVar("b"));
+        ArrayList<Var> defArgs = new ArrayList<Var>();
+        defArgs.add(Var.alloc("a"));
+        defArgs.add(Var.alloc("b"));
         Expr test = new E_Function("http://example/takeaway", altArgs);
-        UserDefinedFunctionFactory.getFactory().add("http://example/test", test, new ArrayList<Var>());
+        UserDefinedFunctionFactory.getFactory().add("http://example/test", test, defArgs);
         
         UserDefinedFunctionDefinition def = UserDefinedFunctionFactory.getFactory().get("http://example/test");
         Expr base = def.getBaseExpr();
@@ -219,8 +229,11 @@ public class TestFunctionExpansion {
         ExprList altArgs = new ExprList();
         altArgs.add(new ExprVar("b"));
         altArgs.add(new ExprVar("a"));
+        ArrayList<Var> defArgs = new ArrayList<Var>();
+        defArgs.add(Var.alloc("a"));
+        defArgs.add(Var.alloc("b"));
         Expr test = new E_Function("http://example/takeaway", altArgs);
-        UserDefinedFunctionFactory.getFactory().add("http://example/test", test, new ArrayList<Var>());
+        UserDefinedFunctionFactory.getFactory().add("http://example/test", test, defArgs);
         
         UserDefinedFunctionDefinition def = UserDefinedFunctionFactory.getFactory().get("http://example/test");
         Expr base = def.getBaseExpr();
@@ -231,4 +244,99 @@ public class TestFunctionExpansion {
         Assert.assertEquals(subtract.getArg1().getVarName(), "b");
         Assert.assertEquals(subtract.getArg2().getVarName(), "a");
     }
+    
+    @Test
+    public void test_function_expansion_10() {
+        Expr single = new ExprVar("x");
+        UserDefinedFunctionFactory.getFactory().add("http://example/single", single, new
ArrayList<Var>(single.getVarsMentioned()));
+        
+        //Test that with allowDependencies set to false (the default) that the definition
is expanded appropriately
+        //when the outer function has differing numbers of arguments
+        List<Var> args = new ArrayList<Var>();
+        args.add(Var.alloc("x"));
+        args.add(Var.alloc("y"));
+        Expr add = new E_Add(new E_Function("http://example/single", new ExprList(new ExprVar("x"))),
new ExprVar("y"));
+        UserDefinedFunctionFactory.getFactory().add("http://example/add", add, args);
+        
+        UserDefinedFunctionDefinition def = UserDefinedFunctionFactory.getFactory().get("http://example/add");
+        Expr base = def.getBaseExpr();
+        Assert.assertTrue(base instanceof E_Add);
+        E_Add actual = (E_Add)base;
+        Assert.assertTrue(actual.getArg1() instanceof ExprVar);
+        Assert.assertTrue(actual.getArg2() instanceof ExprVar);
+        Assert.assertEquals("x", actual.getArg1().getVarName());
+        Assert.assertEquals("y", actual.getArg2().getVarName());
+    }
+    
+    @Test
+    public void test_function_expansion_11() {
+        Expr single = new ExprVar("x");
+        UserDefinedFunctionFactory.getFactory().add("http://example/single", single, new
ArrayList<Var>(single.getVarsMentioned()));
+        
+        //Test that with allowDependencies set to false (the default) that the definition
is expanded appropriately
+        //when the outer function has differing numbers of arguments
+        List<Var> args = new ArrayList<Var>();
+        args.add(Var.alloc("x"));
+        args.add(Var.alloc("y"));
+        Expr add = new E_Add(new E_Function("http://example/single", new ExprList(new ExprVar("y"))),
new ExprVar("y"));
+        UserDefinedFunctionFactory.getFactory().add("http://example/add", add, args);
+        
+        UserDefinedFunctionDefinition def = UserDefinedFunctionFactory.getFactory().get("http://example/add");
+        Expr base = def.getBaseExpr();
+        Assert.assertTrue(base instanceof E_Add);
+        E_Add actual = (E_Add)base;
+        Assert.assertTrue(actual.getArg1() instanceof ExprVar);
+        Assert.assertTrue(actual.getArg2() instanceof ExprVar);
+        Assert.assertEquals("y", actual.getArg1().getVarName());
+        Assert.assertEquals("y", actual.getArg2().getVarName());
+    }
+    
+    @Test
+    public void test_function_expansion_12() {
+        Expr takeaway = new E_Subtract(new ExprVar("x"), new ExprVar("y"));
+        List<Var> args = new ArrayList<Var>();
+        args.add(Var.alloc("x"));
+        args.add(Var.alloc("y"));
+        UserDefinedFunctionFactory.getFactory().add("http://example/takeaway", takeaway,
args);
+        
+        //Test that with allowDependencies set to false (the default) that the definition
is expanded appropriately
+        ExprList altArgs = new ExprList();
+        altArgs.add(new ExprVar("a"));
+        altArgs.add(new ExprVar("a"));
+        ArrayList<Var> defArgs = new ArrayList<Var>();
+        defArgs.add(Var.alloc("a"));
+        defArgs.add(Var.alloc("b"));
+        Expr test = new E_Function("http://example/takeaway", altArgs);
+        UserDefinedFunctionFactory.getFactory().add("http://example/test", test, defArgs);
+        
+        UserDefinedFunctionDefinition def = UserDefinedFunctionFactory.getFactory().get("http://example/test");
+        Expr base = def.getBaseExpr();
+        Assert.assertTrue(base instanceof E_Subtract);
+        E_Subtract subtract = (E_Subtract)base;
+        Assert.assertTrue(subtract.getArg1() instanceof ExprVar);
+        Assert.assertTrue(subtract.getArg2() instanceof ExprVar);
+        Assert.assertEquals(subtract.getArg1().getVarName(), "a");
+        Assert.assertEquals(subtract.getArg2().getVarName(), "a");
+    }
+    
+    @Test(expected=ExprBuildException.class)
+    public void test_function_expansion_bad_01() {
+        List<Var> args = new ArrayList<Var>();
+        args.add(Var.alloc("x"));
+        args.add(Var.alloc("y"));
+        Expr add = new E_Add(new ExprVar("x"), new ExprVar("y"));
+        
+        //It's an error to use a variable which is not mentioned in the argument list
+        UserDefinedFunctionFactory.getFactory().add("http://example/add", add, new ArrayList<Var>());
+    }
+    
+    @Test(expected=ExprBuildException.class)
+    public void test_function_expansion_bad_02() {
+        Expr single = new ExprVar("x");
+        UserDefinedFunctionFactory.getFactory().add("http://example/single", single, new
ArrayList<Var>(single.getVarsMentioned()));
+        
+        //It's an error to use a variable which is not mentioned in the argument list, even
in a call to a dependent function
+        Expr test = new E_Function("http://example/single", new ExprList(new ExprVar("x")));
+        UserDefinedFunctionFactory.getFactory().add("http://example/test", test, new ArrayList<Var>());
+    }
 }



Mime
View raw message