use of org.apache.jena.sparql.engine.ResultSetStream in project jena by apache.
the class QueryTest method unique.
private static ResultSetRewindable unique(ResultSetRewindable results) {
// VERY crude. Utilises the fact that bindings have value equality.
List<Binding> x = new ArrayList<>();
Set<Binding> seen = new HashSet<>();
for (; results.hasNext(); ) {
Binding b = results.nextBinding();
if (seen.contains(b))
continue;
seen.add(b);
x.add(b);
}
QueryIterator qIter = new QueryIterPlainWrapper(x.iterator());
ResultSet rs = new ResultSetStream(results.getResultVars(), ModelFactory.createDefaultModel(), qIter);
return ResultSetFactory.makeRewindable(rs);
}
use of org.apache.jena.sparql.engine.ResultSetStream in project jena by apache.
the class QueryTest method convertToStrings.
private ResultSetRewindable convertToStrings(ResultSetRewindable resultsActual) {
List<Binding> bindings = new ArrayList<>();
while (resultsActual.hasNext()) {
Binding b = resultsActual.nextBinding();
BindingMap b2 = BindingFactory.create();
for (String vn : resultsActual.getResultVars()) {
Var v = Var.alloc(vn);
Node n = b.get(v);
String s;
if (n == null)
s = "";
else if (n.isBlank())
s = "_:" + n.getBlankNodeLabel();
else
s = NodeFunctions.str(n);
b2.add(v, NodeFactory.createLiteral(s));
}
bindings.add(b2);
}
ResultSet rs = new ResultSetStream(resultsActual.getResultVars(), null, new QueryIterPlainWrapper(bindings.iterator()));
return ResultSetFactory.makeRewindable(rs);
}
use of org.apache.jena.sparql.engine.ResultSetStream in project jena by apache.
the class BuilderResultSet method build.
public static ResultSet build(Item item) {
BuilderLib.checkTagged(item, Tags.tagResultSet, "Not a (resultset ...)");
ItemList list = item.getList();
List<Var> vars = BuilderNode.buildVarList(list.get(1));
// skip tag, skip vars.
int start = 2;
List<Binding> bindings = new ArrayList<>();
for (int i = start; i < list.size(); i++) {
Item itemRow = list.get(i);
Binding b = BuilderBinding.build(itemRow);
bindings.add(b);
}
QueryIterator qIter = new QueryIterPlainWrapper(bindings.listIterator());
return new ResultSetStream(Var.varNames(vars), null, qIter);
}
use of org.apache.jena.sparql.engine.ResultSetStream in project jena by apache.
the class TSVInput method fromTSV.
/**
* Reads SPARQL Results from TSV format into a {@link ResultSet} instance
* @param in Input Stream
*/
public static ResultSet fromTSV(InputStream in) {
BufferedReader reader = IO.asBufferedUTF8(in);
List<Var> vars = new ArrayList<>();
List<String> varNames = new ArrayList<>();
String str = null;
try {
// Here we try to parse only the Header Row
str = reader.readLine();
if (str == null)
throw new ARQException("TSV Results malformed, input is empty (no header row)");
if (!str.isEmpty()) {
String[] tokens = pattern.split(str, -1);
for (String token : tokens) {
Node v;
try {
v = NodeFactoryExtra.parseNode(token);
if (v == null || !v.isVariable())
throw new ResultSetException("TSV Results malformed, not a variable: " + token);
} catch (RiotException ex) {
throw new ResultSetException("TSV Results malformed, variable names must begin with a ? in the header: " + token);
}
Var var = Var.alloc(v);
vars.add(var);
varNames.add(var.getName());
}
}
} catch (IOException ex) {
throw new ARQException(ex);
}
//This will parse actual result rows as needed thus minimising memory usage
return new ResultSetStream(varNames, null, new TSVInputIterator(reader, vars));
}
use of org.apache.jena.sparql.engine.ResultSetStream in project jena by apache.
the class XMLInputSAX method worker.
private void worker(InputSource in, Model model) {
if (model == null)
model = GraphFactory.makeJenaDefaultModel();
try {
XMLReader xr = XMLReaderFactory.createXMLReader();
xr.setFeature("http://xml.org/sax/features/namespace-prefixes", true);
// ResultSetXMLHandler1 handler = new ResultSetXMLHandler1() ;
ResultSetXMLHandler2 handler = new ResultSetXMLHandler2();
xr.setContentHandler(handler);
xr.parse(in);
if (handler.isBooleanResult) {
// Set superclass member
set(handler.askResult);
return;
}
ResultSetStream rss = new ResultSetStream(handler.variables, model, new QueryIterPlainWrapper(handler.results.iterator()));
// Set superclass member
set(rss);
} catch (SAXException ex) {
throw new ResultSetException("Problems parsing file (SAXException)", ex);
} catch (IOException ex) {
throw new ResultSetException("Problems parsing file (IOException)", ex);
}
}
Aggregations