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 2012/11/20 01:33:38 UTC
svn commit: r1411488 - in
/jena/trunk/jena-arq/src/test/java/com/hp/hpl/jena/sparql/function/user:
TS_UserFunctions.java TestFunctionExpansion.java
TestFunctionNonExpansion.java
Author: rvesse
Date: Tue Nov 20 00:33:38 2012
New Revision: 1411488
URL: http://svn.apache.org/viewvc?rev=1411488&view=rev
Log:
Adds tests which demonstrate the differences in behaviour when explicit dependencies are and aren't allowed
Added:
jena/trunk/jena-arq/src/test/java/com/hp/hpl/jena/sparql/function/user/TestFunctionNonExpansion.java
Modified:
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/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=1411488&r1=1411487&r2=1411488&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 Tue Nov 20 00:33:38 2012
@@ -30,6 +30,7 @@ import com.hp.hpl.jena.sparql.expr.TS_Ex
@SuiteClasses( {
TestUserDefinedFunctionFactory.class,
TestFunctionExpansion.class,
+ TestFunctionNonExpansion.class,
TestUserFunctionsInSparql.class
})
public class TS_UserFunctions {
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=1411488&r1=1411487&r2=1411488&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 Tue Nov 20 00:33:38 2012
@@ -27,6 +27,7 @@ import org.junit.BeforeClass;
import org.junit.Test;
import com.hp.hpl.jena.sparql.core.Var;
+import com.hp.hpl.jena.sparql.engine.binding.BindingFactory;
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;
@@ -34,10 +35,13 @@ import com.hp.hpl.jena.sparql.expr.E_Sub
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;
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.function.FunctionEnvBase;
import com.hp.hpl.jena.sparql.sse.builders.ExprBuildException;
+import com.hp.hpl.jena.sparql.util.NodeFactory;
/**
* Test for checking that functions are appropriately expanded when supplied with actual arguments
@@ -319,6 +323,36 @@ public class TestFunctionExpansion {
Assert.assertEquals(subtract.getArg2().getVarName(), "a");
}
+ @Test
+ public void test_function_expansion_13() {
+ Expr square = new E_Multiply(new ExprVar("x"), new ExprVar("x"));
+ UserDefinedFunctionFactory.getFactory().add("http://example/square", square, new ArrayList<Var>(square.getVarsMentioned()));
+
+ //This test illustrates that if we change the definition of square and call our function again we always
+ //get the same result with dependencies disallowed (false) because even though the definition of the dependent function
+ //can change the definition of our function is fully expanded when first defined
+ Expr cube = new E_Multiply(new E_Function("http://example/square", new ExprList(new ExprVar("x"))), new ExprVar("x"));
+ UserDefinedFunctionFactory.getFactory().add("http://example/cube", cube, new ArrayList<Var>(cube.getVarsMentioned()));
+
+ UserDefinedFunction f = (UserDefinedFunction) UserDefinedFunctionFactory.getFactory().create("http://example/cube");
+ f.build("http://example/cube", new ExprList(new NodeValueInteger(2)));
+
+ Expr actual = f.getActualExpr();
+ NodeValue result = actual.eval(BindingFactory.create(), FunctionEnvBase.createTest());
+ Assert.assertEquals(8, NodeFactory.nodeToInt(result.asNode()));
+
+ //Change the definition of the function we depend on
+ //This has no effect with allowDependencies set to false (the default) since we fully expanded the call to the dependent
+ //function when our outer function was defined
+ square = new ExprVar("x");
+ UserDefinedFunctionFactory.getFactory().add("http://example/square", square, new ArrayList<Var>(square.getVarsMentioned()));
+ f.build("http://example/cube", new ExprList(new NodeValueInteger(2)));
+
+ actual = f.getActualExpr();
+ result = actual.eval(BindingFactory.create(), FunctionEnvBase.createTest());
+ Assert.assertEquals(8, NodeFactory.nodeToInt(result.asNode()));
+ }
+
@Test(expected=ExprBuildException.class)
public void test_function_expansion_bad_01() {
List<Var> args = new ArrayList<Var>();
Added: jena/trunk/jena-arq/src/test/java/com/hp/hpl/jena/sparql/function/user/TestFunctionNonExpansion.java
URL: http://svn.apache.org/viewvc/jena/trunk/jena-arq/src/test/java/com/hp/hpl/jena/sparql/function/user/TestFunctionNonExpansion.java?rev=1411488&view=auto
==============================================================================
--- jena/trunk/jena-arq/src/test/java/com/hp/hpl/jena/sparql/function/user/TestFunctionNonExpansion.java (added)
+++ jena/trunk/jena-arq/src/test/java/com/hp/hpl/jena/sparql/function/user/TestFunctionNonExpansion.java Tue Nov 20 00:33:38 2012
@@ -0,0 +1,104 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.hp.hpl.jena.sparql.function.user;
+
+import java.util.ArrayList;
+
+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.engine.binding.BindingFactory;
+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.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;
+import com.hp.hpl.jena.sparql.expr.nodevalue.NodeValueInteger;
+import com.hp.hpl.jena.sparql.function.FunctionEnvBase;
+import com.hp.hpl.jena.sparql.util.NodeFactory;
+
+/**
+ * Tests which check that functions are not expanded when {@link UserDefinedFunctionFactory#setAllowDependencies(boolean)} is set to true
+ *
+ */
+public class TestFunctionNonExpansion {
+
+ @BeforeClass
+ public static void setup() {
+ UserDefinedFunctionFactory.getFactory().clear();
+ UserDefinedFunctionFactory.getFactory().setAllowDependencies(true);
+ }
+
+ @AfterClass
+ public static void teardown() {
+ UserDefinedFunctionFactory.getFactory().clear();
+ UserDefinedFunctionFactory.getFactory().setAllowDependencies(false);
+ }
+
+ @Test
+ public void test_function_non_expansion_01() {
+ Expr square = new E_Multiply(new ExprVar("x"), new ExprVar("x"));
+ UserDefinedFunctionFactory.getFactory().add("http://example/square", square, new ArrayList<Var>(square.getVarsMentioned()));
+
+ //Test that with allowDependencies set to true that the definition of cube is not expanded
+ Expr cube = new E_Multiply(new E_Function("http://example/square", new ExprList(new ExprVar("x"))), new ExprVar("x"));
+ UserDefinedFunctionFactory.getFactory().add("http://example/cube", cube, new ArrayList<Var>(cube.getVarsMentioned()));
+
+ UserDefinedFunctionDefinition def = UserDefinedFunctionFactory.getFactory().get("http://example/cube");
+ Expr base = def.getBaseExpr();
+ Assert.assertTrue(base instanceof E_Multiply);
+ E_Multiply multiply = (E_Multiply)base;
+ Assert.assertTrue(multiply.getArg1() instanceof E_Function);
+ Assert.assertTrue(multiply.getArg2() instanceof ExprVar);
+ E_Function lhs = (E_Function)multiply.getArg1();
+ Assert.assertEquals("http://example/square", lhs.getFunctionIRI());
+ Assert.assertEquals(1, base.getVarsMentioned().size());
+ }
+
+ @Test
+ public void test_function_non_expansion_02() {
+ Expr square = new E_Multiply(new ExprVar("x"), new ExprVar("x"));
+ UserDefinedFunctionFactory.getFactory().add("http://example/square", square, new ArrayList<Var>(square.getVarsMentioned()));
+
+ //This test illustrates that if we change the definition of square and call our function again we can
+ //get a different result with dependencies allowed because the definition of the dependent function can change
+ Expr cube = new E_Multiply(new E_Function("http://example/square", new ExprList(new ExprVar("x"))), new ExprVar("x"));
+ UserDefinedFunctionFactory.getFactory().add("http://example/cube", cube, new ArrayList<Var>(cube.getVarsMentioned()));
+
+ UserDefinedFunction f = (UserDefinedFunction) UserDefinedFunctionFactory.getFactory().create("http://example/cube");
+ f.build("http://example/cube", new ExprList(new NodeValueInteger(2)));
+
+ Expr actual = f.getActualExpr();
+ NodeValue result = actual.eval(BindingFactory.create(), FunctionEnvBase.createTest());
+ Assert.assertEquals(8, NodeFactory.nodeToInt(result.asNode()));
+
+ //Change the definition of the function we depend on
+ square = new ExprVar("x");
+ UserDefinedFunctionFactory.getFactory().add("http://example/square", square, new ArrayList<Var>(square.getVarsMentioned()));
+ f.build("http://example/cube", new ExprList(new NodeValueInteger(2)));
+
+ actual = f.getActualExpr();
+ result = actual.eval(BindingFactory.create(), FunctionEnvBase.createTest());
+ Assert.assertEquals(4, NodeFactory.nodeToInt(result.asNode()));
+ }
+}