Java Code Examples for org.apache.jena.sparql.engine.QueryIterator#hasNext()

The following examples show how to use org.apache.jena.sparql.engine.QueryIterator#hasNext() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AlgebraEx.java    From xcurator with Apache License 2.0 6 votes vote down vote up
public static void main(String []args)
{
    String s = "SELECT DISTINCT ?s { ?s ?p ?o }";
    
    // Parse
    Query query = QueryFactory.create(s) ;
    System.out.println(query) ;
    
    // Generate algebra
    Op op = Algebra.compile(query) ;
    op = Algebra.optimize(op) ;
    System.out.println(op) ;
    
    // Execute it.
    QueryIterator qIter = Algebra.exec(op, ExQuerySelect1.createModel()) ;
    
    // Results
    for ( ; qIter.hasNext() ; )
    {
        Binding b = qIter.nextBinding() ;
        System.out.println(b) ;
    }
    qIter.close() ;
}
 
Example 2
Source File: StageGeneratorSPARQLStar.java    From RDFstarTools with Apache License 2.0 5 votes vote down vote up
@Override
protected QueryIterator execute(BasicPattern pattern,
                                ReorderTransformation reorder,
                                StageGenerator execution,
                                QueryIterator input,
                                ExecutionContext execCxt)
{
	// The implementation of this method is a copy of the
	// superclass method that is modified to use SPARQL*-aware
	// versions of Substitute and QueryIterBlockTriples.

       Explain.explain(pattern, execCxt.getContext());

       if ( ! input.hasNext() )
           return input;

       if ( reorder != null && pattern.size() >= 2 ) {
           // If pattern size is 0 or 1, nothing to do.
           BasicPattern bgp2 = pattern;

           // Try to ground the pattern
           if ( ! input.isJoinIdentity() ) {
               QueryIterPeek peek = QueryIterPeek.create(input, execCxt);
               // And now use this one
               input = peek;
               Binding b = peek.peek();
               bgp2 = ExtendedSubstitute.substitute(pattern, b);
           }
           ReorderProc reorderProc = reorder.reorderIndexes(bgp2);
           pattern = reorderProc.reorder(pattern);
       }
       Explain.explain("Reorder/generic", pattern, execCxt.getContext());

       QueryIterator chain = input;
       for ( final Triple triple : pattern )
           chain = QueryIterTripleStarPattern.create(chain, triple, execCxt);
       return chain;
}
 
Example 3
Source File: AlgebraExec.java    From xcurator with Apache License 2.0 4 votes vote down vote up
public static void main (String[] argv)
{
    String BASE = "http://example/" ; 
    BasicPattern bp = new BasicPattern() ;
    Var var_x = Var.alloc("x") ;
    Var var_z = Var.alloc("z") ;
    
    // ---- Build expression
    bp.add(new Triple(var_x, NodeFactory.createURI(BASE+"p"), var_z)) ;
    Op op = new OpBGP(bp) ;
    //Expr expr = ExprUtils.parse("?z < 2 ") ;
    Expr expr = new E_LessThan(new ExprVar(var_z), NodeValue.makeNodeInteger(2)) ;
    op = OpFilter.filter(expr, op) ;

    // ---- Example setup
    Model m = makeModel() ;
    m.write(System.out, "TTL") ;
    System.out.println("--------------") ;
    System.out.print(op) ;
    System.out.println("--------------") ;

    // ---- Execute expression
    QueryIterator qIter = Algebra.exec(op, m.getGraph()) ;
    
    // -------- Either read the query iterator directly ...
    if ( false )
    {
        for ( ; qIter.hasNext() ; )
        {
            Binding b = qIter.nextBinding() ;
            Node n = b.get(var_x) ;
            System.out.println(NodeFmtLib.displayStr(n)) ;
            System.out.println(b) ; 
        }
        qIter.close() ;
    }
    else
    {
        // -------- Or make ResultSet from it (but not both - reading an
        //          iterator consumes the current solution)
        List<String> varNames = new ArrayList<String>() ;
        varNames.add("x") ;
        varNames.add("z") ;
        ResultSet rs = new ResultSetStream(varNames, m, qIter);
        ResultSetFormatter.out(rs) ;
        qIter.close() ;
    }
    System.exit(0) ;
}