Search in sources :

Example 1 with ResultSetStream

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);
}
Also used : Binding(org.apache.jena.sparql.engine.binding.Binding) QueryIterPlainWrapper(org.apache.jena.sparql.engine.iterator.QueryIterPlainWrapper) QueryIterator(org.apache.jena.sparql.engine.QueryIterator) ArrayList(java.util.ArrayList) ResultSet(org.apache.jena.query.ResultSet) ResultSetStream(org.apache.jena.sparql.engine.ResultSetStream) HashSet(java.util.HashSet)

Example 2 with ResultSetStream

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);
}
Also used : Binding(org.apache.jena.sparql.engine.binding.Binding) QueryIterPlainWrapper(org.apache.jena.sparql.engine.iterator.QueryIterPlainWrapper) Var(org.apache.jena.sparql.core.Var) RDFNode(org.apache.jena.rdf.model.RDFNode) Node(org.apache.jena.graph.Node) ArrayList(java.util.ArrayList) ResultSet(org.apache.jena.query.ResultSet) BindingMap(org.apache.jena.sparql.engine.binding.BindingMap) ResultSetStream(org.apache.jena.sparql.engine.ResultSetStream)

Example 3 with ResultSetStream

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);
}
Also used : Binding(org.apache.jena.sparql.engine.binding.Binding) Item(org.apache.jena.sparql.sse.Item) QueryIterPlainWrapper(org.apache.jena.sparql.engine.iterator.QueryIterPlainWrapper) QueryIterator(org.apache.jena.sparql.engine.QueryIterator) Var(org.apache.jena.sparql.core.Var) ItemList(org.apache.jena.sparql.sse.ItemList) ArrayList(java.util.ArrayList) ResultSetStream(org.apache.jena.sparql.engine.ResultSetStream)

Example 4 with ResultSetStream

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));
}
Also used : Var(org.apache.jena.sparql.core.Var) Node(org.apache.jena.graph.Node) ArrayList(java.util.ArrayList) IOException(java.io.IOException) ResultSetStream(org.apache.jena.sparql.engine.ResultSetStream) RiotException(org.apache.jena.riot.RiotException) ARQException(org.apache.jena.sparql.ARQException) BufferedReader(java.io.BufferedReader)

Example 5 with ResultSetStream

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);
    }
}
Also used : QueryIterPlainWrapper(org.apache.jena.sparql.engine.iterator.QueryIterPlainWrapper) IOException(java.io.IOException) ResultSetStream(org.apache.jena.sparql.engine.ResultSetStream)

Aggregations

ResultSetStream (org.apache.jena.sparql.engine.ResultSetStream)10 ArrayList (java.util.ArrayList)7 Var (org.apache.jena.sparql.core.Var)6 QueryIterator (org.apache.jena.sparql.engine.QueryIterator)6 Binding (org.apache.jena.sparql.engine.binding.Binding)6 QueryIterPlainWrapper (org.apache.jena.sparql.engine.iterator.QueryIterPlainWrapper)6 ResultSet (org.apache.jena.query.ResultSet)5 Node (org.apache.jena.graph.Node)4 IOException (java.io.IOException)2 BindingMap (org.apache.jena.sparql.engine.binding.BindingMap)2 BufferedReader (java.io.BufferedReader)1 HashSet (java.util.HashSet)1 List (java.util.List)1 Function (java.util.function.Function)1 CSVParser (org.apache.jena.atlas.csv.CSVParser)1 Triple (org.apache.jena.graph.Triple)1 RDFNode (org.apache.jena.rdf.model.RDFNode)1 RiotException (org.apache.jena.riot.RiotException)1 ARQException (org.apache.jena.sparql.ARQException)1 Op (org.apache.jena.sparql.algebra.Op)1