Search in sources :

Example 1 with UserDefinedFunctionDefinition

use of org.apache.jena.sparql.function.user.UserDefinedFunctionDefinition in project jena by apache.

the class TestFunctionExpansion method test_function_expansion_05.

@Test
public void test_function_expansion_05() {
    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("y"))), new ExprVar("y"));
    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());
}
Also used : UserDefinedFunctionDefinition(org.apache.jena.sparql.function.user.UserDefinedFunctionDefinition) Test(org.junit.Test)

Example 2 with UserDefinedFunctionDefinition

use of org.apache.jena.sparql.function.user.UserDefinedFunctionDefinition in project jena by apache.

the class TestFunctionExpansion method test_function_expansion_11.

@Test
public void test_function_expansion_11() {
    Expr single = new ExprVar("x");
    UserDefinedFunctionFactory.getFactory().add("http://example/single", single, new ArrayList<>(single.getVarsMentioned()));
    //Test that with preserveDependencies 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<>();
    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());
}
Also used : Var(org.apache.jena.sparql.core.Var) ArrayList(java.util.ArrayList) UserDefinedFunctionDefinition(org.apache.jena.sparql.function.user.UserDefinedFunctionDefinition) Test(org.junit.Test)

Example 3 with UserDefinedFunctionDefinition

use of org.apache.jena.sparql.function.user.UserDefinedFunctionDefinition in project jena by apache.

the class TestFunctionExpansion method test_function_expansion_10.

@Test
public void test_function_expansion_10() {
    Expr single = new ExprVar("x");
    UserDefinedFunctionFactory.getFactory().add("http://example/single", single, new ArrayList<>(single.getVarsMentioned()));
    //Test that with preserveDependencies 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<>();
    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());
}
Also used : Var(org.apache.jena.sparql.core.Var) ArrayList(java.util.ArrayList) UserDefinedFunctionDefinition(org.apache.jena.sparql.function.user.UserDefinedFunctionDefinition) Test(org.junit.Test)

Example 4 with UserDefinedFunctionDefinition

use of org.apache.jena.sparql.function.user.UserDefinedFunctionDefinition in project jena by apache.

the class TestFunctionExpansion method test_function_expansion_08.

@Test
public void test_function_expansion_08() {
    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("b"));
    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(), "b");
}
Also used : Var(org.apache.jena.sparql.core.Var) ArrayList(java.util.ArrayList) UserDefinedFunctionDefinition(org.apache.jena.sparql.function.user.UserDefinedFunctionDefinition) Test(org.junit.Test)

Example 5 with UserDefinedFunctionDefinition

use of org.apache.jena.sparql.function.user.UserDefinedFunctionDefinition in project jena by apache.

the class TestFunctionExpansion method test_function_expansion_06.

@Test
public void test_function_expansion_06() {
    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 NodeValueInteger(1));
    numArgs.add(new NodeValueDouble(2.3));
    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 NodeValueInteger);
    Assert.assertTrue(subtract.getArg2() instanceof NodeValueDouble);
}
Also used : Var(org.apache.jena.sparql.core.Var) ArrayList(java.util.ArrayList) UserDefinedFunctionDefinition(org.apache.jena.sparql.function.user.UserDefinedFunctionDefinition) NodeValueInteger(org.apache.jena.sparql.expr.nodevalue.NodeValueInteger) NodeValueDouble(org.apache.jena.sparql.expr.nodevalue.NodeValueDouble) Test(org.junit.Test)

Aggregations

UserDefinedFunctionDefinition (org.apache.jena.sparql.function.user.UserDefinedFunctionDefinition)10 Test (org.junit.Test)10 ArrayList (java.util.ArrayList)7 Var (org.apache.jena.sparql.core.Var)7 NodeValueDouble (org.apache.jena.sparql.expr.nodevalue.NodeValueDouble)2 NodeValueInteger (org.apache.jena.sparql.expr.nodevalue.NodeValueInteger)2