org.apache.jena.sparql.engine.binding.BindingFactory Java Examples

The following examples show how to use org.apache.jena.sparql.engine.binding.BindingFactory. 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: LogUtils.java    From sparql-generate with Apache License 2.0 6 votes vote down vote up
private static ElementData compress(List<Var> variables, List<Binding> input) {
    ElementData el = new ElementData();
    variables.forEach(el::add);
    if (input.size() < 10) {
        input.forEach((b) -> el.add(compress(variables, b)));
        return el;
    }
    for (int i = 0; i < 5; i++) {
        el.add(compress(variables, input.get(i)));
    }
    BindingMap binding = BindingFactory.create();
    Node n = NodeFactory.createLiteral("[ " + (input.size() - 10) + " more ]");
    variables.forEach((v) -> binding.add(v, n));
    el.add(binding);
    for (int i = input.size() - 5; i < input.size(); i++) {
        el.add(compress(variables, input.get(i)));
    }
    return el;
}
 
Example #3
Source File: LogUtils.java    From sparql-generate with Apache License 2.0 6 votes vote down vote up
private static ElementData compress(List<Binding> input) {
    ElementData el = new ElementData();

    if (input.size() < 10) {
        input.forEach((b) -> {
            addCompressedToElementData(el, b);
        });
        return el;
    }
    for (int i = 0; i < 5; i++) {
        addCompressedToElementData(el, input.get(i));
    }
    BindingMap binding = BindingFactory.create();
    Node n = NodeFactory.createLiteral("[ " + (input.size() - 10) + " more ]");
    el.getVars().forEach((v) -> binding.add(v, n));
    el.add(binding);
    for (int i = input.size() - 5; i < input.size(); i++) {
        addCompressedToElementData(el, input.get(i));
    }
    return el;
}
 
Example #4
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 #5
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 #6
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 #7
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 #8
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 #9
Source File: IteratorPlan.java    From sparql-generate with Apache License 2.0 6 votes vote down vote up
List<Binding> getListBinding(
        final Binding binding,
        final List<List<NodeValue>> nodeValues) {
    final List<Binding> listBindings = new ArrayList<>();
    nodeValues.forEach((listNodeValues) -> {
        if (vars.size() > listNodeValues.size()) {
            LOG.warn("Too many variables, some will not be bound: " + listNodeValues);
            return;
        }
        final BindingMap b = BindingFactory.create(binding);
        for (int i = 0; i < vars.size(); i++) {
            if (listNodeValues.get(i) != null) {
                Node n = listNodeValues.get(i).asNode();
                b.add(vars.get(i), n);
            }
        }
        listBindings.add(b);
    });
    return listBindings;
}
 
Example #10
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 #11
Source File: VarUtils.java    From sparql-generate with Apache License 2.0 5 votes vote down vote up
public static Binding getBinding(
        final QuerySolution sol) {
    final BindingMap binding = BindingFactory.create();
    for (Iterator<String> it = sol.varNames(); it.hasNext();) {
        String varName = it.next();
        binding.add(allocVar(varName), sol.get(varName).asNode());
    }
    return binding;
}
 
Example #12
Source File: EvalUtils.java    From sparql-generate with Apache License 2.0 5 votes vote down vote up
public static Binding createBinding(QuerySolution sol) {
	final BindingMap binding = BindingFactory.create();
	for (Iterator<String> it = sol.varNames(); it.hasNext();) {
		final String varName = it.next();
		binding.add(VarUtils.allocVar(varName), sol.get(varName).asNode());
	}
	return binding;
}
 
Example #13
Source File: LogUtils.java    From sparql-generate with Apache License 2.0 5 votes vote down vote up
public static Binding compress(List<Var> variables, Binding input) {
    final BindingMap binding = BindingFactory.create();
    for (Var v : variables) {
        Node n = input.get(v);
        if (n != null) {
            binding.add(v, compress(input.get(v)));
        }
    }
    return binding;
}
 
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);
    }
}
 
Example #15
Source File: RootPlan.java    From sparql-generate with Apache License 2.0 5 votes vote down vote up
private List<Binding> getValues(List<Var> variables, List<Binding> values) {
	Objects.requireNonNull(variables);
	if (values == null) {
		values = new ArrayList<>();
		values.add(BindingFactory.binding());
	}
	return values;
}
 
Example #16
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 #17
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 #18
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 #19
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 #20
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 #21
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 #22
Source File: QueryIterTripleStarPattern.java    From RDFstarTools with Apache License 2.0 5 votes vote down vote up
protected Binding mapper(Triple r)
{
    final BindingMap results = BindingFactory.create(binding);

    if ( ! insert(r, s, p, o, results, sIsTripleWithVars, oIsTripleWithVars) )
        return null;

    return results;
}
 
Example #23
Source File: FunctionTest.java    From tarql with BSD 2-Clause "Simplified" License 4 votes vote down vote up
protected Node eval(String expression) {
	Expr expr = ExprUtils.parse(expression, prefixes);
	return expr.eval(BindingFactory.root(), env).asNode();
}