Search in sources :

Example 16 with Var

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

the class TestOpVars method check.

private static void check(String[] varsExpected, Collection<Var> varsFound) {
    Var[] vars = new Var[varsExpected.length];
    for (int i = 0; i < varsExpected.length; i++) {
        Var v = Var.alloc(varsExpected[i]);
        vars[i] = v;
    }
    List<Var> varList = Arrays.asList(vars);
    HashSet<Var> varSet = new HashSet<>();
    varSet.addAll(varList);
    assertEquals(varSet, varsFound);
}
Also used : Var(org.apache.jena.sparql.core.Var) HashSet(java.util.HashSet)

Example 17 with Var

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

the class TestSyntaxTransform method testUpdate.

private void testUpdate(String input, String output, String varStr, String valStr) {
    UpdateRequest req1 = UpdateFactory.create(PREFIX + input);
    UpdateRequest reqExpected = UpdateFactory.create(PREFIX + output);
    Map<Var, Node> map = new HashMap<>();
    map.put(Var.alloc(varStr), SSE.parseNode(valStr));
    UpdateRequest reqTrans = UpdateTransformOps.transform(req1, map);
    // Crude.
    String x1 = reqExpected.toString().replaceAll("[ \n\t]", "");
    String x2 = reqTrans.toString().replaceAll("[ \n\t]", "");
    //assertEquals(reqExpected, reqTrans) ;
    assertEquals(x1, x2);
}
Also used : UpdateRequest(org.apache.jena.update.UpdateRequest) HashMap(java.util.HashMap) Var(org.apache.jena.sparql.core.Var) RDFNode(org.apache.jena.rdf.model.RDFNode) Node(org.apache.jena.graph.Node)

Example 18 with Var

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

the class TestSyntaxTransform method testQuery.

private void testQuery(String input, String output, String varStr, String valStr) {
    Query q1 = QueryFactory.create(PREFIX + input);
    Query qExpected = QueryFactory.create(PREFIX + output);
    Map<Var, Node> map = new HashMap<>();
    map.put(Var.alloc(varStr), SSE.parseNode(valStr));
    Query qTrans = QueryTransformOps.transform(q1, map);
    assertEquals(qExpected, qTrans);
}
Also used : Query(org.apache.jena.query.Query) HashMap(java.util.HashMap) Var(org.apache.jena.sparql.core.Var) RDFNode(org.apache.jena.rdf.model.RDFNode) Node(org.apache.jena.graph.Node)

Example 19 with Var

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

the class TestQuery method query_recursive_01.

@Test
public void query_recursive_01() {
    String query = "SELECT * WHERE { SERVICE <" + serviceQuery() + "> { ?s ?p ?o . BIND(?o AS ?x) } }";
    try (QueryExecution qExec = QueryExecutionFactory.sparqlService(serviceQuery(), query)) {
        ResultSet rs = qExec.execSelect();
        Var x = Var.alloc("x");
        while (rs.hasNext()) {
            Binding b = rs.nextBinding();
            Assert.assertNotNull(b.get(x));
        }
    }
}
Also used : Binding(org.apache.jena.sparql.engine.binding.Binding) Var(org.apache.jena.sparql.core.Var) ServerTest(org.apache.jena.fuseki.ServerTest) Test(org.junit.Test)

Example 20 with Var

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

the class TransformFilterEquality method safeToTransform.

private static boolean safeToTransform(Collection<Var> varsEquality, Op op) {
    // Structure as a visitor?
    if (op instanceof OpBGP || op instanceof OpQuadPattern)
        return true;
    if (op instanceof OpPath)
        return true;
    if (op instanceof OpFilter) {
        OpFilter opf = (OpFilter) op;
        // Expressions are always safe transform by substitution.
        return safeToTransform(varsEquality, opf.getSubOp());
    }
    // are very rarely so deep that it matters.
    if (op instanceof OpSequence) {
        OpN opN = (OpN) op;
        for (Op subOp : opN.getElements()) {
            if (!safeToTransform(varsEquality, subOp))
                return false;
        }
        return true;
    }
    if (op instanceof OpJoin || op instanceof OpUnion) {
        Op2 op2 = (Op2) op;
        return safeToTransform(varsEquality, op2.getLeft()) && safeToTransform(varsEquality, op2.getRight());
    }
    // Not safe unless filter variables are mentioned on the LHS.
    if (op instanceof OpConditional || op instanceof OpLeftJoin) {
        Op2 opleftjoin = (Op2) op;
        if (!safeToTransform(varsEquality, opleftjoin.getLeft()) || !safeToTransform(varsEquality, opleftjoin.getRight()))
            return false;
        // Not only must the left and right be safe to transform,
        // but the equality variable must be known to be always set.
        // If the varsLeft are disjoint from assigned vars,
        // we may be able to push assign down right
        // (this generalises the unit table case specialcase1)
        // Needs more investigation.
        Op opLeft = opleftjoin.getLeft();
        Set<Var> varsLeft = OpVars.visibleVars(opLeft);
        if (varsLeft.containsAll(varsEquality))
            return true;
        return false;
    }
    if (op instanceof OpGraph) {
        OpGraph opg = (OpGraph) op;
        return safeToTransform(varsEquality, opg.getSubOp());
    }
    // Subquery - assume scope rewriting has already been applied.
    if (op instanceof OpModifier) {
        // ORDER BY?
        OpModifier opMod = (OpModifier) op;
        if (opMod instanceof OpProject) {
            OpProject opProject = (OpProject) op;
            // AS-ification.
            for (Var v : opProject.getVars()) {
                if (varsEquality.contains(v))
                    return false;
            }
        }
        return safeToTransform(varsEquality, opMod.getSubOp());
    }
    if (op instanceof OpGroup) {
        OpGroup opGroup = (OpGroup) op;
        VarExprList varExprList = opGroup.getGroupVars();
        return safeToTransform(varsEquality, varExprList) && safeToTransform(varsEquality, opGroup.getSubOp());
    }
    if (op instanceof OpTable) {
        OpTable opTable = (OpTable) op;
        if (opTable.isJoinIdentity())
            return true;
    }
    return false;
}
Also used : Op(org.apache.jena.sparql.algebra.Op) Var(org.apache.jena.sparql.core.Var) VarExprList(org.apache.jena.sparql.core.VarExprList)

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