public static NodeValue makeInteger(BigInteger i) { return new NodeValueInteger(i) ; }
@Override public void visit(NodeValueInteger nv) { push(new NodeValueInteger(nv.getInteger(), changeNode(nv.asNode()))); }
@Override public String toString() { // Preserve lexical form if ( getNode() != null ) return super.asString() ; // str() return integer.toString() ; }
@Override public String asString() { return toString() ; }
public static NodeValue makeInteger(long i) { return new NodeValueInteger(BigInteger.valueOf(i)) ; }
@Override public void visit(NodeValueInteger nv) { push(new NodeValueInteger(nv.getInteger(), changeNode(nv.asNode()))); }
public static NodeValue makeInteger(String lexicalForm) { return new NodeValueInteger(new BigInteger(lexicalForm)) ; }
@Test public void promote_table_empty_assignment_03() { // Force algebra to have separate extends by using extendDirect() Op input = OpTable.empty(); input = OpExtend.create(input, new VarExprList(Var.alloc("x"), new NodeValueInteger(1))); input = OpExtend.create(input, new VarExprList(Var.alloc("y"), new NodeValueInteger(2))); test(input, t_promote, "(table empty)"); }
@Test public void combine_extend_01() { Op extend = OpExtend.create(OpTable.unit(), new VarExprList(Var.alloc("x"), new NodeValueInteger(1))); extend = OpExtend.create(extend, new VarExprList(Var.alloc("y"), new NodeValueInteger(2))); String opExpectedString = StrUtils.strjoinNL( "(extend ((?x 1) (?y 2))", " (table unit))"); check(extend, new TransformExtendCombine(), opExpectedString); }
@Test public void combine_assign_01() { Op assign = OpAssign.create(OpTable.unit(), new VarExprList(Var.alloc("x"), new NodeValueInteger(1))); assign = OpAssign.create(assign, new VarExprList(Var.alloc("y"), new NodeValueInteger(2))); String opExpectedString = StrUtils.strjoinNL( "(assign ((?x 1) (?y 2))", " (table unit))"); check(assign, new TransformExtendCombine(), opExpectedString); }
@Test public void combine_assign_03() { Op assign = OpAssign.create(OpTable.unit(), new VarExprList(Var.alloc("x"), new NodeValueInteger(1))); assign = OpAssign.create(assign, new VarExprList(Var.alloc("x"), new NodeValueInteger(2))); String opExpectedString = StrUtils.strjoinNL( "(assign ((?x 2))", " (assign ((?x 1))", " (table unit)))"); check(assign, new TransformExtendCombine(), opExpectedString); }
@Test public void combine_extend_03() { // Technically illegal SPARQL here but useful to validate that the optimizer doesn't do the wrong thing Op extend = OpExtend.create(OpTable.unit(), new VarExprList(Var.alloc("x"), new NodeValueInteger(1))); extend = OpExtend.create(extend, new VarExprList(Var.alloc("x"), new NodeValueInteger(2))); String opExpectedString = StrUtils.strjoinNL( "(extend ((?x 2))", " (extend ((?x 1))", " (table unit)))"); check(extend, new TransformExtendCombine(), opExpectedString); }
@Test public void test_function_expansion_02() { Expr e = new E_Multiply(new ExprVar("x"), new ExprVar("x")); UserDefinedFunctionFactory.getFactory().add("http://example/square", e, new ArrayList<>(e.getVarsMentioned())); UserDefinedFunction f = (UserDefinedFunction) UserDefinedFunctionFactory.getFactory().create("http://example/square"); f.build("http://example/square", new ExprList(new NodeValueInteger(3))); 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(3)), actual); }
@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); }
return new NodeValueInteger(integer, node) ; return new NodeValueInteger(((Integer)obj).longValue()) ; if ( obj instanceof RomanNumeral ) return new NodeValueInteger( ((RomanNumeral)obj).intValue() ) ; throw new ARQInternalErrorException("DatatypeFormatException: Roman numeral is unknown class") ;
@Test public void combine_extend_02() { Op extend = OpExtend.create(OpTable.unit(), new VarExprList(Var.alloc("x"), new NodeValueInteger(1))); extend = OpExtend.create(extend, new VarExprList(Var.alloc("y"), new ExprVar("x"))); String opExpectedString = StrUtils.strjoinNL( "(extend ((?x 1) (?y ?x))", " (table unit))"); check(extend, new TransformExtendCombine(), opExpectedString); }
@Test public void combine_assign_02() { Op assign = OpAssign.create(OpTable.unit(), new VarExprList(Var.alloc("x"), new NodeValueInteger(1))); assign = OpAssign.create(assign, new VarExprList(Var.alloc("y"), new ExprVar("x"))); String opExpectedString = StrUtils.strjoinNL( "(assign ((?x 1) (?y ?x))", " (table unit))"); check(assign, new TransformExtendCombine(), opExpectedString); }
@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); }
@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); }
@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(), LibTestExpr.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(), LibTestExpr.createTest()); Assert.assertEquals(4, NodeFactoryExtra.nodeToInt(result.asNode())); } }