Java Code Examples for org.apache.jena.sparql.engine.binding.BindingFactory#binding()

The following examples show how to use org.apache.jena.sparql.engine.binding.BindingFactory#binding() . 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 matchNothingWithGivenObjectInObjectTriple()
{
	// ?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:x1
	final Binding inputBinding = BindingFactory.binding( $V3(), $x1() );
	final ExecutionContext execCxt = createTestExecCxt();
	final QueryIterator input = QueryIterSingleton.create(inputBinding, execCxt);

	final QueryIterator it = QueryIterTripleStarPattern.create( input, tp2, execCxt );
	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 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 5
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 6
Source File: BindPlan.java    From sparql-generate with Apache License 2.0 6 votes vote down vote up
@Override
protected final Binding exec(Binding binding, Context context) {
    LOG.debug("Start " + this);
    context.set(ARQConstants.sysCurrentTime, NodeFactoryExtra.nowAsDateTime());
    final FunctionEnv env = new FunctionEnvBase(context);
    try {
        final NodeValue n = expr.eval(binding, env);
        if (LOG.isTraceEnabled()) {
            LOG.trace("New binding " + var + " = " + LogUtils.compress(n.asNode()));
        }
        return BindingFactory.binding(binding, var, n.asNode());
    } catch(ExprEvalException ex) {
        LOG.trace("No evaluation for " + this + " " + ex.getMessage());
        return binding;
    }
}
 
Example 7
Source File: ResultSetWritersSPARQLStarTest.java    From RDFstarTools with Apache License 2.0 5 votes vote down vote up
@Test
public void registrationOK() {
	final ResultSetWriterFactory f = ResultSetWriterRegistry.getFactory(SPARQLResultSetText); 
	assertTrue( f instanceof ResultSetWritersSPARQLStar.MyFactory );

	final Node u = NodeFactory.createURI("http://example.com/i");
	final Node n = new Node_Triple(new Triple(u, u, u));
	final Binding b = BindingFactory.binding(Var.alloc("t"), n);
	final ResultSet rs = new TestResultSet(b);
	final ByteArrayOutputStream out = new ByteArrayOutputStream(); 
	f.create(SPARQLResultSetText).write(out, rs, null);
}
 
Example 8
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 9
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 10
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 11
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 12
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 13
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 14
Source File: SourcePlan.java    From sparql-generate with Apache License 2.0 5 votes vote down vote up
final protected Binding exec(
        final Binding binding,
        final Context context) {

    LOG.debug("Start " + this);
    Objects.nonNull(binding);
    // generate the source URI.
    final String sourceUri = getActualSource(binding);
    final String acceptHeader = getAcceptHeader(binding);
    LOG.trace("... resolved to SOURCE <" + sourceUri + "> ACCEPT " + acceptHeader + " AS " + var);
    final LookUpRequest request = new LookUpRequest(sourceUri, acceptHeader);
    final SPARQLExtStreamManager sm = (SPARQLExtStreamManager) context.get(SysRIOT.sysStreamManager);
    Objects.requireNonNull(sm);
    final TypedInputStream stream = sm.open(request);
    if (stream == null) {
        LOG.info("Exec SOURCE <" + sourceUri + "> ACCEPT " + acceptHeader + " AS " + var + " returned nothing.");
        return BindingFactory.binding(binding);
    }
    try (InputStream in = stream.getInputStream()) {
        final String literal = IOUtils.toString(in, "UTF-8");
        final RDFDatatype dt;
        if (stream.getMediaType() != null && stream.getMediaType().getContentType() != null) {
            dt = tm.getSafeTypeByName("http://www.iana.org/assignments/media-types/" + stream.getMediaType().getContentType());
        } else {
            dt = tm.getSafeTypeByName("http://www.w3.org/2001/XMLSchema#string");
        }
        final Node n = NodeFactory.createLiteral(literal, dt);
        LOG.debug("Exec " + this + " returned. "
                + "Enable TRACE level for more.");
        if (LOG.isTraceEnabled()) {
            LOG.trace("Exec " + this + " returned\n" + LogUtils.compress(n));
        }
        return BindingFactory.binding(binding, var, n);
    } catch (IOException | DatatypeFormatException ex) {
        LOG.warn("Exception while looking up " + sourceUri + ":", ex);
        return BindingFactory.binding(binding);
    }
}