Search in sources :

Example 1 with ElementData

use of org.apache.jena.sparql.syntax.ElementData in project jena by apache.

the class ValuesHandler method asElement.

public ElementData asElement() {
    ElementData ed = new ElementData();
    // remove all the vars that have been set
    List<Var> vars = new ArrayList<Var>(valuesTable.keySet());
    vars.removeAll(valueMap.keySet());
    if (vars.isEmpty()) {
        return ed;
    }
    vars.forEach(var -> ed.add(var));
    // List<Binding> bindings = new ArrayList<Binding>();
    int count = valuesTable.get(vars.get(0)).size();
    BindingBuilder builder = Binding.builder();
    for (int i = 0; i < count; i++) {
        builder.reset();
        for (Var var : vars) {
            List<Node> lst = valuesTable.get(var);
            // must be square
            if (lst.size() != count) {
                throw new QueryBuildException(String.format("The number of data items for %s (%s) is not the same as for %s (%s)", var, lst.size(), vars.get(0), count));
            }
            Node n = lst.get(i);
            if (n != null) {
                if (valueMap.containsKey(n)) {
                    n = valueMap.get(n);
                }
                builder.add(var, n);
            }
        }
        if (builder.isEmpty())
            continue;
        ed.add(builder.build());
    }
    return ed;
}
Also used : BindingBuilder(org.apache.jena.sparql.engine.binding.BindingBuilder) Var(org.apache.jena.sparql.core.Var) Node(org.apache.jena.graph.Node) QueryBuildException(org.apache.jena.query.QueryBuildException) ArrayList(java.util.ArrayList) ElementData(org.apache.jena.sparql.syntax.ElementData)

Example 2 with ElementData

use of org.apache.jena.sparql.syntax.ElementData in project jena by apache.

the class ElementRewriter method visit.

@Override
public void visit(ElementData el) {
    ElementData retval = new ElementData();
    for (Var v : el.getVars()) {
        retval.add(v);
    }
    for (Binding binding : el.getRows()) {
        retval.add(binding);
    }
    push(retval);
}
Also used : Binding(org.apache.jena.sparql.engine.binding.Binding) Var(org.apache.jena.sparql.core.Var) ElementData(org.apache.jena.sparql.syntax.ElementData)

Example 3 with ElementData

use of org.apache.jena.sparql.syntax.ElementData in project jena by apache.

the class WhereHandlerTest method testSetVarsInWhereValues.

@Test
public void testSetVarsInWhereValues() {
    Var v = Var.alloc("v");
    Node value = NodeFactory.createLiteral(LiteralLabelFactory.createTypedLiteral(10));
    Map<Var, Node> values = new HashMap<>();
    values.put(v, value);
    handler.addValueVar(query.getPrefixMapping(), "?x", "<one>", "?v");
    handler.setVars(values);
    handler.build();
    ElementData edat = new ElementData();
    Var x = Var.alloc("x");
    edat.add(x);
    Binding binding1 = BindingFactory.binding(x, NodeFactory.createURI("one"));
    edat.add(binding1);
    Binding binding2 = BindingFactory.binding(x, value);
    edat.add(binding2);
    WhereValidator visitor = new WhereValidator(edat);
    query.getQueryPattern().visit(visitor);
    assertTrue(visitor.matching);
}
Also used : Binding(org.apache.jena.sparql.engine.binding.Binding) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Var(org.apache.jena.sparql.core.Var) WhereValidator(org.apache.jena.arq.querybuilder.WhereValidator) Node(org.apache.jena.graph.Node) ElementData(org.apache.jena.sparql.syntax.ElementData) Test(org.junit.Test)

Example 4 with ElementData

use of org.apache.jena.sparql.syntax.ElementData in project jena by apache.

the class WhereHandlerTest method testAddValueVar_pfx_obj.

@Test
public void testAddValueVar_pfx_obj() {
    handler.addValueVar(query.getPrefixMapping(), "?v");
    handler.build();
    Var v = Var.alloc("v");
    ElementData edat = new ElementData();
    edat.add(v);
    WhereValidator visitor = new WhereValidator(edat);
    query.getQueryPattern().visit(visitor);
    assertTrue(visitor.matching);
}
Also used : Var(org.apache.jena.sparql.core.Var) WhereValidator(org.apache.jena.arq.querybuilder.WhereValidator) ElementData(org.apache.jena.sparql.syntax.ElementData) Test(org.junit.Test)

Example 5 with ElementData

use of org.apache.jena.sparql.syntax.ElementData in project jena by apache.

the class WhereClauseTest method testAddWhereValueVars.

@ContractTest
public void testAddWhereValueVars() {
    final Var v = Var.alloc("v");
    Map<Object, List<?>> map = new HashMap<Object, List<?>>();
    map.put(Var.alloc("v"), Arrays.asList("<one>", "<two>"));
    map.put("?x", Arrays.asList("three", "four"));
    WhereClause<?> whereClause = getProducer().newInstance();
    AbstractQueryBuilder<?> builder = whereClause.addWhereValueVars(map);
    Query query = builder.build();
    Var x = Var.alloc("x");
    ElementData edat = new ElementData();
    edat.add(x);
    edat.add(v);
    setupBindings(edat, x, v);
    WhereValidator visitor = new WhereValidator(edat);
    query.getQueryPattern().visit(visitor);
    assertTrue(visitor.matching);
}
Also used : ElementSubQuery(org.apache.jena.sparql.syntax.ElementSubQuery) Query(org.apache.jena.query.Query) HashMap(java.util.HashMap) ExprVar(org.apache.jena.sparql.expr.ExprVar) Var(org.apache.jena.sparql.core.Var) WhereValidator(org.apache.jena.arq.querybuilder.WhereValidator) List(java.util.List) ElementData(org.apache.jena.sparql.syntax.ElementData) ContractTest(org.xenei.junit.contract.ContractTest)

Aggregations

ElementData (org.apache.jena.sparql.syntax.ElementData)20 Var (org.apache.jena.sparql.core.Var)19 WhereValidator (org.apache.jena.arq.querybuilder.WhereValidator)18 Query (org.apache.jena.query.Query)11 ElementSubQuery (org.apache.jena.sparql.syntax.ElementSubQuery)11 ContractTest (org.xenei.junit.contract.ContractTest)11 ExprVar (org.apache.jena.sparql.expr.ExprVar)10 Binding (org.apache.jena.sparql.engine.binding.Binding)7 Test (org.junit.Test)7 Node (org.apache.jena.graph.Node)6 HashMap (java.util.HashMap)5 List (java.util.List)4 LinkedHashMap (java.util.LinkedHashMap)2 ArrayList (java.util.ArrayList)1 QueryBuildException (org.apache.jena.query.QueryBuildException)1 BindingBuilder (org.apache.jena.sparql.engine.binding.BindingBuilder)1