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;
}
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);
}
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;
}
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);
}
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;
}
Aggregations