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

The following examples show how to use org.apache.jena.sparql.engine.QueryIterator#nextBinding() . 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: QueryIterTripleStarPatternTest.java    From RDFstarTools with Apache License 2.0 6 votes vote down vote up
@Test
public void matchSubjectInSubjectTriple()
{
	// << ?V1 ex:p ex:o >> ex:m1 ex:x1
	final Triple tp1 = new Triple( $V1(), $p(), $o() );
	final Node nTP1 = new Node_TripleStarPattern( tp1 );
	final Triple tp2 = new Triple( nTP1, $m1(), $x1() );

	final Binding inputBinding = BindingFactory.binding();
	final ExecutionContext execCxt = createTestExecCxt();
	final QueryIterator input = QueryIterSingleton.create(inputBinding, execCxt);

	final QueryIterator it = QueryIterTripleStarPattern.create( input, tp2, execCxt );
	assertTrue( it.hasNext() );

	final Binding outputBinding = it.nextBinding();
	assertEquals( 1, outputBinding.size() );
	assertEquals( $V1(), outputBinding.vars().next() );

	assertEquals( $s(), outputBinding.get($V1()) );

	assertFalse( it.hasNext() ); 
	it.close();
}
 
Example 2
Source File: QueryIterTripleStarPatternTest.java    From RDFstarTools with Apache License 2.0 6 votes vote down vote up
@Test
public void matchSubjectInSubjectTripleWithGivenObjectInMetaTriple()
{
	// << ?V1 ex:p ex:o >> ex:m1 ?V2
	final Triple tp1 = new Triple( $V1(), $p(), $o() );
	final Node nTP1 = new Node_TripleStarPattern( tp1 );
	final Triple tp2 = new Triple( nTP1, $m1(), $V2() );

	// ?V2 --> ex:x1
	final Binding inputBinding = BindingFactory.binding( $V2(), $x1() );
	final ExecutionContext execCxt = createTestExecCxt();
	final QueryIterator input = QueryIterSingleton.create(inputBinding, execCxt);

	final QueryIterator it = QueryIterTripleStarPattern.create( input, tp2, execCxt );
	assertTrue( it.hasNext() );

	final Binding outputBinding = it.nextBinding();
	assertEquals( 2, outputBinding.size() );

	assertEquals( $s(), outputBinding.get($V1()) );
	assertEquals( $x1(), outputBinding.get($V2()) );

	assertFalse( it.hasNext() ); 
	it.close();
}
 
Example 3
Source File: QueryIterTripleStarPatternTest.java    From RDFstarTools with Apache License 2.0 6 votes vote down vote up
@Test
public void matchObjectRequiresRedundancyAugmentation()
{
	// ex:s ex:p ?V3
	final Triple tp = new Triple( $s(), $p(), $V3() );

	final Binding inputBinding = BindingFactory.binding();
	final ExecutionContext execCxt = createTestExecCxt();
	final QueryIterator input = QueryIterSingleton.create(inputBinding, execCxt);

	final QueryIterator it = QueryIterTripleStarPattern.create( input, tp, execCxt );
	assertTrue( it.hasNext() );

	final Binding outputBinding = it.nextBinding();
	assertEquals( 1, outputBinding.size() );

	assertEquals( $o(), outputBinding.get($V3()) );

	assertFalse( it.hasNext() ); 
	it.close();
}
 
Example 4
Source File: QueryIterTripleStarPatternTest.java    From RDFstarTools with Apache License 2.0 6 votes vote down vote up
@Test
public void matchSubjectWithGivenObjectRequiresRedundancyAugmentation()
{
	// ?V1 ex:p ?V3
	final Triple tp = new Triple( $V1(), $p(), $V3() );

	// ?V3 --> ex:x1
	final Binding inputBinding = BindingFactory.binding( $V3(), $o() );
	final ExecutionContext execCxt = createTestExecCxt();
	final QueryIterator input = QueryIterSingleton.create(inputBinding, execCxt);

	final QueryIterator it = QueryIterTripleStarPattern.create( input, tp, execCxt );
	assertTrue( it.hasNext() );

	final Binding outputBinding = it.nextBinding();
	assertEquals( 2, outputBinding.size() );

	assertEquals( $s(), outputBinding.get($V1()) );
	assertEquals( $o(), outputBinding.get($V3()) );

	assertFalse( it.hasNext() ); 
	it.close();
}
 
Example 5
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 6
Source File: QueryIterTripleStarPatternTest.java    From RDFstarTools with Apache License 2.0 5 votes vote down vote up
@Test
public void matchWholeSubjectTriple()
{
	// ?V1 ex:m1 ex:x1
	final Triple tp = new Triple( $V1(), $m1(), $x1() );

	final ExecutionContext execCxt = createTestExecCxt();
	final Binding inputBinding = BindingFactory.binding();
	final QueryIterator input = QueryIterSingleton.create(inputBinding, execCxt);

	final QueryIterator it = QueryIterTripleStarPattern.create( input, tp, execCxt );
	assertTrue( it.hasNext() );

	final Binding outputBinding = it.nextBinding();
	assertEquals( 1, outputBinding.size() );
	assertEquals( $V1(), outputBinding.vars().next() );

	final Node outputValue = outputBinding.get( $V1() );  
	assertTrue( outputValue instanceof Node_Triple );

	final Triple outputTriple = ( (Node_Triple) outputValue ).get();
	assertEquals( $s(), outputTriple.getSubject() );
	assertEquals( $p(), outputTriple.getPredicate() );
	assertEquals( $o(), outputTriple.getObject() );

	assertFalse( it.hasNext() ); 
	it.close();
}
 
Example 7
Source File: QueryIterTripleStarPatternTest.java    From RDFstarTools with Apache License 2.0 5 votes vote down vote up
@Test
public void matchWholeObjectTriple()
{
	// ex:x1 ex:m1 ?V1
	final Triple tp = new Triple( $x1(), $m1(), $V1() );

	final ExecutionContext execCxt = createTestExecCxt();
	final Binding inputBinding = BindingFactory.binding();
	final QueryIterator input = QueryIterSingleton.create(inputBinding, execCxt);

	final QueryIterator it = QueryIterTripleStarPattern.create( input, tp, execCxt );
	assertTrue( it.hasNext() );

	final Binding outputBinding = it.nextBinding();
	assertEquals( 1, outputBinding.size() );
	assertEquals( $V1(), outputBinding.vars().next() );

	final Node outputValue = outputBinding.get( $V1() );  
	assertTrue( outputValue instanceof Node_Triple );

	final Triple outputTriple = ( (Node_Triple) outputValue ).get();
	assertEquals( $s(), outputTriple.getSubject() );
	assertEquals( $p(), outputTriple.getPredicate() );
	assertEquals( $o(), outputTriple.getObject() );

	assertFalse( it.hasNext() ); 
	it.close();
}
 
Example 8
Source File: QueryIterTripleStarPatternTest.java    From RDFstarTools with Apache License 2.0 5 votes vote down vote up
@Test
public void matchMetaTriplesBasedOnWholeSubjectTriple()
{
	//  << ex:s ex:p ex:o >> ?V1 ?V2
	final Triple tp1 = new Triple( $s(), $p(), $o() );
	final Node nTP1 = new Node_TripleStarPattern( tp1 );
	final Triple tp2 = new Triple( nTP1, $V1(), $V2() );

	final Binding inputBinding = BindingFactory.binding();
	final ExecutionContext execCxt = createTestExecCxt();
	final QueryIterator input = QueryIterSingleton.create(inputBinding, execCxt);

	final QueryIterator it = QueryIterTripleStarPattern.create( input, tp2, execCxt );
	assertTrue( it.hasNext() );

	Binding outputBinding = null;

	outputBinding = it.nextBinding();
	assertEquals( 2, outputBinding.size() );

	assertEquals( $m1(), outputBinding.get($V1()) );
	assertEquals( $x2(), outputBinding.get($V2()) );

	outputBinding = it.nextBinding();
	assertEquals( 2, outputBinding.size() );

	assertEquals( $m1(), outputBinding.get($V1()) );
	assertEquals( $x1(), outputBinding.get($V2()) );

	assertFalse( it.hasNext() ); 
	it.close();
}
 
Example 9
Source File: QueryIterTripleStarPatternTest.java    From RDFstarTools with Apache License 2.0 5 votes vote down vote up
@Test
public void matchSubjectInSubjectTripleAndObjectInMetaTriple()
{
	// << ?V1 ex:p ex:o >> ex:m1 ?V2
	final Triple tp1 = new Triple( $V1(), $p(), $o() );
	final Node nTP1 = new Node_TripleStarPattern( tp1 );
	final Triple tp2 = new Triple( nTP1, $m1(), $V2() );

	final Binding inputBinding = BindingFactory.binding();
	final ExecutionContext execCxt = createTestExecCxt();
	final QueryIterator input = QueryIterSingleton.create(inputBinding, execCxt);

	final QueryIterator it = QueryIterTripleStarPattern.create( input, tp2, execCxt );
	assertTrue( it.hasNext() );

	Binding outputBinding = it.nextBinding();
	assertEquals( 2, outputBinding.size() );

	assertEquals( $s(), outputBinding.get($V1()) );
	assertEquals( $x2(), outputBinding.get($V2()) );

	outputBinding = it.nextBinding();
	assertEquals( 2, outputBinding.size() );

	assertEquals( $s(), outputBinding.get($V1()) );
	assertEquals( $x1(), outputBinding.get($V2()) );

	assertFalse( it.hasNext() ); 
	it.close();
}
 
Example 10
Source File: QueryIterTripleStarPatternTest.java    From RDFstarTools with Apache License 2.0 5 votes vote down vote up
@Test
public void matchSubjectInMetaTripleWithGivenObjectInObjectTriple()
{
	// ?V1 ?V2 << ex:s ex:p ?V3 >>
	final Triple tp1 = new Triple( $s(), $p(), $V3() );
	final Node nTP1 = new Node_TripleStarPattern( tp1 );
	final Triple tp2 = new Triple( $V1(), $V2(), nTP1 );

	// ?V3 --> ex:o
	final Binding inputBinding = BindingFactory.binding( $V3(), $o() );
	final ExecutionContext execCxt = createTestExecCxt();
	final QueryIterator input = QueryIterSingleton.create(inputBinding, execCxt);

	final QueryIterator it = QueryIterTripleStarPattern.create( input, tp2, execCxt );
	assertTrue( it.hasNext() );

	Binding outputBinding = it.nextBinding();
	assertEquals( 3, outputBinding.size() );

	assertEquals( $x2(), outputBinding.get($V1()) );
	assertEquals( $m2(), outputBinding.get($V2()) );
	assertEquals( $o(), outputBinding.get($V3()) );

	outputBinding = it.nextBinding();
	assertEquals( 3, outputBinding.size() );

	assertEquals( $x1(), outputBinding.get($V1()) );
	assertEquals( $m1(), outputBinding.get($V2()) );
	assertEquals( $o(), outputBinding.get($V3()) );

	assertFalse( it.hasNext() ); 
	it.close();
}
 
Example 11
Source File: QueryIterTripleStarPatternTest.java    From RDFstarTools with Apache License 2.0 5 votes vote down vote up
@Test
public void matchSubjectInMetaTripleWithGivenSubjectInObjectTriple()
{
	// ?V1 ?V2 << ?V3 ex:p ex:o >>
	final Triple tp1 = new Triple( $s(), $p(), $V3() );
	final Node nTP1 = new Node_TripleStarPattern( tp1 );
	final Triple tp2 = new Triple( $V1(), $V2(), nTP1 );

	// ?V2 --> ex:m1, ?V3 --> ex:s
	final Binding inputBindingX = BindingFactory.binding( $V2(), $m1() );
	final Binding inputBinding = BindingFactory.binding( inputBindingX, $V3(), $o() );
	final ExecutionContext execCxt = createTestExecCxt();
	final QueryIterator input = QueryIterSingleton.create(inputBinding, execCxt);

	final QueryIterator it = QueryIterTripleStarPattern.create( input, tp2, execCxt );
	assertTrue( it.hasNext() );

	final Binding outputBinding = it.nextBinding();
	assertEquals( 3, outputBinding.size() );

	assertEquals( $x1(), outputBinding.get($V1()) );
	assertEquals( $m1(), outputBinding.get($V2()) );
	assertEquals( $o(), outputBinding.get($V3()) );

	assertFalse( it.hasNext() ); 
	it.close();
}
 
Example 12
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) ;
}