Search in sources :

Example 71 with Var

use of org.apache.jena.sparql.core.Var in project jena by apache.

the class TestDistinctDataBag method testTemporaryFilesAreCleanedUpAfterCompletion.

@Test
public void testTemporaryFilesAreCleanedUpAfterCompletion() {
    List<Binding> undistinct = new ArrayList<>();
    random = new Random();
    Var[] vars = new Var[] { Var.alloc("1"), Var.alloc("2"), Var.alloc("3"), Var.alloc("4"), Var.alloc("5"), Var.alloc("6"), Var.alloc("7"), Var.alloc("8"), Var.alloc("9"), Var.alloc("0") };
    for (int i = 0; i < 500; i++) {
        undistinct.add(randomBinding(vars));
    }
    DistinctDataBag<Binding> db = new DistinctDataBag<>(new ThresholdPolicyCount<Binding>(10), SerializationFactoryFinder.bindingSerializationFactory(), new BindingComparator(new ArrayList<SortCondition>()));
    List<File> spillFiles = new ArrayList<>();
    try {
        db.addAll(undistinct);
        spillFiles.addAll(db.getSpillFiles());
        int count = 0;
        for (File file : spillFiles) {
            if (file.exists()) {
                count++;
            }
        }
        // 500 bindings divided into 50 chunks (49 in files, and 1 in memory)
        assertEquals(49, count);
        Iterator<Binding> iter = db.iterator();
        while (iter.hasNext()) {
            iter.next();
        }
        Iter.close(iter);
    } finally {
        db.close();
    }
    int count = 0;
    for (File file : spillFiles) {
        if (file.exists()) {
            count++;
        }
    }
    assertEquals(0, count);
}
Also used : Binding(org.apache.jena.sparql.engine.binding.Binding) BuilderBinding(org.apache.jena.sparql.sse.builders.BuilderBinding) BindingComparator(org.apache.jena.sparql.engine.binding.BindingComparator) Var(org.apache.jena.sparql.core.Var) ArrayList(java.util.ArrayList) DistinctDataBag(org.apache.jena.atlas.data.DistinctDataBag) Random(java.util.Random) File(java.io.File) Test(org.junit.Test)

Example 72 with Var

use of org.apache.jena.sparql.core.Var 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 73 with Var

use of org.apache.jena.sparql.core.Var 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 74 with Var

use of org.apache.jena.sparql.core.Var 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 75 with Var

use of org.apache.jena.sparql.core.Var 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

Var (org.apache.jena.sparql.core.Var)264 Node (org.apache.jena.graph.Node)83 ArrayList (java.util.ArrayList)53 Test (org.junit.Test)47 Binding (org.apache.jena.sparql.engine.binding.Binding)33 VarExprList (org.apache.jena.sparql.core.VarExprList)30 Op (org.apache.jena.sparql.algebra.Op)29 Expr (org.apache.jena.sparql.expr.Expr)28 Triple (org.apache.jena.graph.Triple)17 HashMap (java.util.HashMap)15 ContractTest (org.xenei.junit.contract.ContractTest)13 BindingMap (org.apache.jena.sparql.engine.binding.BindingMap)12 Query (org.apache.jena.query.Query)11 ExprList (org.apache.jena.sparql.expr.ExprList)11 SortCondition (org.apache.jena.query.SortCondition)10 ExprVar (org.apache.jena.sparql.expr.ExprVar)10 HashSet (java.util.HashSet)9 Pair (org.apache.jena.atlas.lib.Pair)9 SqlColumn (org.apache.jena.sdb.core.sqlexpr.SqlColumn)9 QueryIterator (org.apache.jena.sparql.engine.QueryIterator)9