Search in sources :

Example 21 with BindingMap

use of org.apache.jena.sparql.engine.binding.BindingMap in project jena by apache.

the class TestDistinctDataNet method randomBinding.

private Binding randomBinding(Var[] vars) {
    BindingMap binding = BindingFactory.create();
    binding.add(vars[0], NodeFactory.createBlankNode());
    binding.add(vars[1], NodeFactory.createURI(randomURI()));
    binding.add(vars[2], NodeFactory.createURI(randomURI()));
    binding.add(vars[3], NodeFactory.createLiteral(randomString(20)));
    binding.add(vars[4], NodeFactory.createBlankNode());
    binding.add(vars[5], NodeFactory.createURI(randomURI()));
    binding.add(vars[6], NodeFactory.createURI(randomURI()));
    binding.add(vars[7], NodeFactory.createLiteral(randomString(5)));
    binding.add(vars[8], NodeFactory.createLiteral("" + random.nextInt(), XSDDatatype.XSDinteger));
    binding.add(vars[9], NodeFactory.createBlankNode());
    return binding;
}
Also used : BindingMap(org.apache.jena.sparql.engine.binding.BindingMap)

Example 22 with BindingMap

use of org.apache.jena.sparql.engine.binding.BindingMap in project jena by apache.

the class localname method slot.

private void slot(Collection<Binding> bindings, Binding input, Node node, Var subjVar, Node nodeLocalname) {
    if (!node.isURI())
        return;
    Node localname = NodeFactory.createLiteral(node.getLocalName());
    if (nodeLocalname.isVariable()) {
        // Object is an unbound variable.
        BindingMap b = BindingFactory.create(input);
        // Bind a pair for subject and object variables
        b.add(Var.alloc(subjVar), node);
        b.add(Var.alloc(nodeLocalname), localname);
        bindings.add(b);
        return;
    }
    // Object is a value / bound variable.
    if (!nodeLocalname.sameValueAs(localname))
        return;
    // Bind subject to this node.
    Binding b = BindingFactory.binding(input, subjVar, node);
    bindings.add(b);
}
Also used : Binding(org.apache.jena.sparql.engine.binding.Binding) Node(org.apache.jena.graph.Node) BindingMap(org.apache.jena.sparql.engine.binding.BindingMap)

Example 23 with BindingMap

use of org.apache.jena.sparql.engine.binding.BindingMap in project jena by apache.

the class TransformFilterInequality method buildTable.

private static Table buildTable(Map<Var, Set<NodeValue>> possibleValues) {
    if (possibleValues.size() == 0)
        return TableFactory.createEmpty();
    Table table = TableFactory.create();
    // combination a single row must be produced
    for (Var v : possibleValues.keySet()) {
        for (NodeValue value : possibleValues.get(v)) {
            BindingMap b = BindingFactory.create();
            b.add(v, value.asNode());
            table.addBinding(b);
        }
    }
    return table;
}
Also used : Var(org.apache.jena.sparql.core.Var) BindingMap(org.apache.jena.sparql.engine.binding.BindingMap)

Example 24 with BindingMap

use of org.apache.jena.sparql.engine.binding.BindingMap in project jena by apache.

the class CSVInput method fromCSV.

public static ResultSet fromCSV(InputStream in) {
    CSVParser parser = CSVParser.create(in);
    final List<Var> vars = vars(parser);
    List<String> varNames = Var.varNames(vars);
    Function<List<String>, Binding> transform = new Function<List<String>, Binding>() {

        private int count = 1;

        @Override
        public Binding apply(List<String> row) {
            if (row.size() != vars.size())
                FmtLog.warn(log, "Row %d: Length=%d: expected=%d", count, row.size(), vars.size());
            BindingMap binding = BindingFactory.create();
            // Check.
            for (int i = 0; i < vars.size(); i++) {
                Var v = vars.get(i);
                String field = (i < row.size()) ? row.get(i) : "";
                Node n = NodeFactory.createLiteral(field);
                binding.add(v, n);
            }
            count++;
            return binding;
        }
    };
    Iterator<Binding> bindings = Iter.map(parser.iterator(), transform);
    //This will parse actual result rows as needed thus minimising memory usage
    return new ResultSetStream(varNames, null, bindings);
}
Also used : Binding(org.apache.jena.sparql.engine.binding.Binding) Var(org.apache.jena.sparql.core.Var) Node(org.apache.jena.graph.Node) BindingMap(org.apache.jena.sparql.engine.binding.BindingMap) ResultSetStream(org.apache.jena.sparql.engine.ResultSetStream) Function(java.util.function.Function) CSVParser(org.apache.jena.atlas.csv.CSVParser) ArrayList(java.util.ArrayList) List(java.util.List)

Example 25 with BindingMap

use of org.apache.jena.sparql.engine.binding.BindingMap in project jena by apache.

the class CSVInputIterator method parseLine.

private BindingMap parseLine(List<Var> vars, String line) {
    BindingMap binding = BindingFactory.create();
    List<String> terms = new ArrayList<>();
    int idx = 0;
    while (idx < line.length()) {
        char ch = line.charAt(idx);
        StringBuilder s = new StringBuilder();
        if (ch == '\"' || ch == '\'') {
            char qCh = ch;
            idx++;
            while (idx < line.length()) {
                ch = line.charAt(idx);
                idx++;
                if (ch == qCh)
                    break;
                // escapes??
                s.append(ch);
            }
            if (ch != qCh)
                throw new QueryException(String.format("Error Parsing CSV results at Line %d  - Unterminated quoted string", this.lineNum));
            if (idx < line.length()) {
                ch = line.charAt(idx);
                if (ch != ',')
                    throw new QueryException(String.format("Error Parsing CSV results at Line %d - Expected comma after quote", this.lineNum));
            }
        } else {
            while (idx < line.length()) {
                ch = line.charAt(idx);
                if (ch == ',')
                    break;
                idx++;
                // escapes
                s.append(ch);
            }
        }
        terms.add(s.toString());
        // Looking at , or EOL.
        if (ch == ',' && idx == line.length() - 1) {
            //EOL
            terms.add("");
            break;
        }
        // Skip ","
        idx++;
    }
    if (terms.size() != vars.size())
        throw new QueryException(String.format("Error Parsing CSV results at Line %d - The result row '%s' has %d items when %d was expected", this.lineNum, line, terms.size(), vars.size()));
    for (int i = 0; i < vars.size(); i++) binding.add(vars.get(i), NodeFactory.createLiteral(terms.get(i)));
    return binding;
}
Also used : QueryException(org.apache.jena.query.QueryException) ArrayList(java.util.ArrayList) BindingMap(org.apache.jena.sparql.engine.binding.BindingMap)

Aggregations

BindingMap (org.apache.jena.sparql.engine.binding.BindingMap)29 Node (org.apache.jena.graph.Node)18 Var (org.apache.jena.sparql.core.Var)12 Binding (org.apache.jena.sparql.engine.binding.Binding)10 ArrayList (java.util.ArrayList)8 QueryIterPlainWrapper (org.apache.jena.sparql.engine.iterator.QueryIterPlainWrapper)5 ResultSetStream (org.apache.jena.sparql.engine.ResultSetStream)4 ResultSet (org.apache.jena.query.ResultSet)3 QueryIterator (org.apache.jena.sparql.engine.QueryIterator)3 SQLException (java.sql.SQLException)2 SqlNode (org.apache.jena.sdb.core.sqlnode.SqlNode)2 BuilderResultSet (org.apache.jena.sparql.sse.builders.BuilderResultSet)2 ResultSet (java.sql.ResultSet)1 List (java.util.List)1 Function (java.util.function.Function)1 CSVParser (org.apache.jena.atlas.csv.CSVParser)1 IteratorDelayedInitialization (org.apache.jena.atlas.iterator.IteratorDelayedInitialization)1 JsonArray (org.apache.jena.atlas.json.JsonArray)1 JsonObject (org.apache.jena.atlas.json.JsonObject)1 JsonValue (org.apache.jena.atlas.json.JsonValue)1