use of org.apache.jena.sparql.function.user.UserDefinedFunctionDefinition in project jena by apache.
the class TestFunctionNonExpansion method test_function_non_expansion_01.
@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<>(square.getVarsMentioned()));
//Test that with preserveDependencies 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<>(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());
}
use of org.apache.jena.sparql.function.user.UserDefinedFunctionDefinition in project jena by apache.
the class TestFunctionExpansion method test_function_expansion_04.
@Test
public void test_function_expansion_04() {
Expr square = new E_Multiply(new ExprVar("x"), new ExprVar("x"));
UserDefinedFunctionFactory.getFactory().add("http://example/square", square, new ArrayList<>(square.getVarsMentioned()));
//Test that with preserveDependencies set to false (the default) that the definition of cube is actually
//expanded to include the definition of square
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<>(cube.getVarsMentioned()));
UserDefinedFunctionDefinition def = UserDefinedFunctionFactory.getFactory().get("http://example/cube");
Expr base = def.getBaseExpr();
Assert.assertTrue(base instanceof E_Multiply);
E_Multiply m = (E_Multiply) base;
Assert.assertTrue(m.getArg1() instanceof E_Multiply);
Assert.assertTrue(m.getArg2() instanceof ExprVar);
Assert.assertEquals(1, base.getVarsMentioned().size());
}
use of org.apache.jena.sparql.function.user.UserDefinedFunctionDefinition in project jena by apache.
the class TestFunctionExpansion method test_function_expansion_09.
@Test
public void test_function_expansion_09() {
Expr takeaway = new E_Subtract(new ExprVar("x"), new ExprVar("y"));
List<Var> args = new ArrayList<>();
args.add(Var.alloc("x"));
args.add(Var.alloc("y"));
UserDefinedFunctionFactory.getFactory().add("http://example/takeaway", takeaway, args);
//Test that with preserveDependencies set to false (the default) that the definition is expanded appropriately
ExprList altArgs = new ExprList();
altArgs.add(new ExprVar("b"));
altArgs.add(new ExprVar("a"));
ArrayList<Var> defArgs = new ArrayList<>();
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(), "b");
Assert.assertEquals(subtract.getArg2().getVarName(), "a");
}
use of org.apache.jena.sparql.function.user.UserDefinedFunctionDefinition in project jena by apache.
the class TestFunctionExpansion method test_function_expansion_07.
@Test
public void test_function_expansion_07() {
Expr takeaway = new E_Subtract(new ExprVar("x"), new ExprVar("y"));
List<Var> args = new ArrayList<>();
args.add(Var.alloc("x"));
args.add(Var.alloc("y"));
UserDefinedFunctionFactory.getFactory().add("http://example/takeaway", takeaway, args);
//Test that with preserveDependencies set to false (the default) that the definition is expanded appropriately
ExprList numArgs = new ExprList();
numArgs.add(new NodeValueDouble(2.3));
numArgs.add(new NodeValueInteger(1));
Expr test = new E_Function("http://example/takeaway", numArgs);
UserDefinedFunctionFactory.getFactory().add("http://example/test", test, new ArrayList<Var>());
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 NodeValueDouble);
Assert.assertTrue(subtract.getArg2() instanceof NodeValueInteger);
}
use of org.apache.jena.sparql.function.user.UserDefinedFunctionDefinition in project jena by apache.
the class TestFunctionExpansion method test_function_expansion_12.
@Test
public void test_function_expansion_12() {
Expr takeaway = new E_Subtract(new ExprVar("x"), new ExprVar("y"));
List<Var> args = new ArrayList<>();
args.add(Var.alloc("x"));
args.add(Var.alloc("y"));
UserDefinedFunctionFactory.getFactory().add("http://example/takeaway", takeaway, args);
//Test that with preserveDependencies 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<>();
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");
}
Aggregations