Search in sources :

Example 11 with NodeValueInteger

use of org.apache.jena.sparql.expr.nodevalue.NodeValueInteger in project jena by apache.

the class TestFunctionNonExpansion method test_function_non_expansion_02.

@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<>(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 preserved 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<>(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, NodeFactoryExtra.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<>(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, NodeFactoryExtra.nodeToInt(result.asNode()));
}
Also used : UserDefinedFunction(org.apache.jena.sparql.function.user.UserDefinedFunction) NodeValueInteger(org.apache.jena.sparql.expr.nodevalue.NodeValueInteger) Test(org.junit.Test)

Example 12 with NodeValueInteger

use of org.apache.jena.sparql.expr.nodevalue.NodeValueInteger in project jena by apache.

the class TestFunctionExpansion method test_function_expansion_13.

@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<>(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 not preserved 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<>(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, NodeFactoryExtra.nodeToInt(result.asNode()));
    //Change the definition of the function we depend on
    //This has no effect with preserveDependencies 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<>(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, NodeFactoryExtra.nodeToInt(result.asNode()));
}
Also used : UserDefinedFunction(org.apache.jena.sparql.function.user.UserDefinedFunction) NodeValueInteger(org.apache.jena.sparql.expr.nodevalue.NodeValueInteger) Test(org.junit.Test)

Example 13 with NodeValueInteger

use of org.apache.jena.sparql.expr.nodevalue.NodeValueInteger in project jena by apache.

the class TestFunctionExpansion method test_function_expansion_03.

@Test
public void test_function_expansion_03() {
    Expr e = new E_Multiply(new ExprVar("x"), new ExprVar("y"));
    List<Var> defArgs = new ArrayList<>();
    defArgs.add(Var.alloc("x"));
    defArgs.add(Var.alloc("y"));
    UserDefinedFunctionFactory.getFactory().add("http://example/square", e, defArgs);
    UserDefinedFunction f = (UserDefinedFunction) UserDefinedFunctionFactory.getFactory().create("http://example/square");
    ExprList args = new ExprList();
    args.add(new NodeValueInteger(3));
    args.add(new NodeValueInteger(4));
    f.build("http://example/square", args);
    Expr actual = f.getActualExpr();
    Assert.assertFalse(e.equals(actual));
    Assert.assertEquals(0, actual.getVarsMentioned().size());
    Assert.assertEquals(new E_Multiply(new NodeValueInteger(3), new NodeValueInteger(4)), actual);
}
Also used : UserDefinedFunction(org.apache.jena.sparql.function.user.UserDefinedFunction) Var(org.apache.jena.sparql.core.Var) ArrayList(java.util.ArrayList) NodeValueInteger(org.apache.jena.sparql.expr.nodevalue.NodeValueInteger) Test(org.junit.Test)

Example 14 with NodeValueInteger

use of org.apache.jena.sparql.expr.nodevalue.NodeValueInteger 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);
}
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

NodeValueInteger (org.apache.jena.sparql.expr.nodevalue.NodeValueInteger)14 Test (org.junit.Test)14 Op (org.apache.jena.sparql.algebra.Op)7 VarExprList (org.apache.jena.sparql.core.VarExprList)7 UserDefinedFunction (org.apache.jena.sparql.function.user.UserDefinedFunction)4 ArrayList (java.util.ArrayList)3 Var (org.apache.jena.sparql.core.Var)3 ExprVar (org.apache.jena.sparql.expr.ExprVar)2 NodeValueDouble (org.apache.jena.sparql.expr.nodevalue.NodeValueDouble)2 UserDefinedFunctionDefinition (org.apache.jena.sparql.function.user.UserDefinedFunctionDefinition)2