Java Code Examples for org.eclipse.rdf4j.query.BindingSet#getBinding()

The following examples show how to use org.eclipse.rdf4j.query.BindingSet#getBinding() . 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: TimeAwareHBaseSail.java    From Halyard with Apache License 2.0 6 votes vote down vote up
@Override
public void removeStatement(UpdateContext op, Resource subj, IRI pred, Value obj, Resource... contexts) throws SailException {
    BindingSet bs;
    Binding b;
    Value v;
    long timestamp = (op != null && (bs = op.getBindingSet()) != null && (b = bs.getBinding(TIMESTAMP_CALLBACK_BINDING_NAME)) != null) ? longValueOfTimeStamp((Literal) b.getValue()) : System.currentTimeMillis();
    try {
        for (Resource ctx : normalizeContexts(contexts)) {
            for (KeyValue kv : HalyardTableUtils.toKeyValues(subj, pred, obj, ctx, true, timestamp)) { //calculate the kv's corresponding to the quad (or triple)
                delete(kv);
            }
        }
    } catch (IOException e) {
        throw new SailException(e);
    }
}
 
Example 2
Source File: TimeAwareHBaseSail.java    From Halyard with Apache License 2.0 6 votes vote down vote up
@Override
public CloseableIteration<BindingSet, QueryEvaluationException> evaluate(TupleExpr tupleExpr, Dataset dataset, BindingSet bindings, boolean includeInferred) throws SailException {
    Binding b = bindings.getBinding(TIMESTAMP_CALLBACK_BINDING_NAME);
    final TimestampCallbackBinding timestampBinding = (b instanceof TimestampCallbackBinding) ? (TimestampCallbackBinding) b : null;
    CloseableIteration<BindingSet, QueryEvaluationException> iter = super.evaluate(tupleExpr, dataset, bindings, includeInferred);
    //push back the actual timestamp binding to the callback binding if requested
    if (timestampBinding != null) {
        iter = new FilterIteration<BindingSet, QueryEvaluationException>(iter) {
            @Override
            protected boolean accept(BindingSet bindings) throws QueryEvaluationException {
                Binding b = bindings.getBinding(TIMESTAMP_BINDING_NAME);
                //push back actual time if the timestamp binding is not provided
                timestampBinding.v = b == null ? SimpleValueFactory.getInstance().createLiteral(System.currentTimeMillis()) : b.getValue();
                return true;
            }
        };
    }
    return iter;
}
 
Example 3
Source File: SPARQLQueryBuilder.java    From inception with Apache License 2.0 5 votes vote down vote up
private void extractDescription(KBHandle aTargetHandle, BindingSet aSourceBindings)
{
    Binding description = aSourceBindings.getBinding(VAR_DESCRIPTION_NAME);
    Binding descCandidate = aSourceBindings.getBinding(VAR_DESCRIPTION_CANDIDATE_NAME);
    if (description != null) {
        aTargetHandle.setDescription(description.getValue().stringValue());
    }
    else if (descCandidate != null) {
        aTargetHandle.setDescription(descCandidate.getValue().stringValue());
    }
}
 
Example 4
Source File: SPARQLQueryBuilder.java    From inception with Apache License 2.0 5 votes vote down vote up
private void extractDomain(KBHandle aTargetHandle, BindingSet aSourceBindings)
{
    Binding domain = aSourceBindings.getBinding(VAR_DOMAIN_NAME);
    if (domain != null) {
        aTargetHandle.setDomain(domain.getValue().stringValue());
    }
}
 
Example 5
Source File: SPARQLQueryBuilder.java    From inception with Apache License 2.0 5 votes vote down vote up
private void extractRange(KBHandle aTargetHandle, BindingSet aSourceBindings)
{
    Binding range = aSourceBindings.getBinding(VAR_RANGE_NAME);
    if (range != null) {
        aTargetHandle.setRange(range.getValue().stringValue());
    }
}
 
Example 6
Source File: NoReification.java    From inception with Apache License 2.0 5 votes vote down vote up
private List<Statement> listStatements(TupleQuery aQuery, boolean aIncludeInferred)
{
    aQuery.setIncludeInferred(aIncludeInferred);
    
    try (TupleQueryResult result = aQuery.evaluate()) {
        ValueFactory vf = SimpleValueFactory.getInstance();
        
        List<Statement> statements = new ArrayList<>();
        while (result.hasNext()) {
            BindingSet bindings = result.next();
            if (bindings.size() == 0) {
                continue;
            }
            
            // LOG.trace("[{}] Bindings: {}", toHexString(hashCode()), bindings);
            
            Binding subj = bindings.getBinding(VAR_SUBJECT_NAME);
            Binding pred = bindings.getBinding(VAR_PREDICATE_NAME);
            Binding obj = bindings.getBinding(VAR_OBJECT_NAME);

            IRI subject = vf.createIRI(subj.getValue().stringValue());
            IRI predicate = vf.createIRI(pred.getValue().stringValue());
            Statement stmt = vf.createStatement(subject, predicate, obj.getValue());
            
            // Avoid duplicate statements
            if (!statements.contains(stmt)) {
                statements.add(stmt);
            }
        }
        return statements;
    }
}
 
Example 7
Source File: WikiDataReification.java    From inception with Apache License 2.0 5 votes vote down vote up
private List<Statement> getQualifiersById(RepositoryConnection aConnection, KnowledgeBase kb,
        String aStatementId)
{
    ValueFactory vf = aConnection.getValueFactory();
    
    String QUERY = String.join("\n",
            "SELECT DISTINCT ?p ?o WHERE {",
            "  ?id ?p ?o .",
            "}",
            "LIMIT " + kb.getMaxResults());
    Resource id = vf.createBNode(aStatementId);
    TupleQuery tupleQuery = aConnection.prepareTupleQuery(QueryLanguage.SPARQL, QUERY);
    tupleQuery.setBinding("id", id);

    tupleQuery.setIncludeInferred(false);
    TupleQueryResult result;

    try {
        result = tupleQuery.evaluate();
    }
    catch (QueryEvaluationException e) {
        log.warn("No such statementId in knowledge base", e);
        return null;
    }

    List<Statement> statements = new ArrayList<>();
    while (result.hasNext()) {
        BindingSet bindings = result.next();
        Binding p = bindings.getBinding("p");
        Binding o = bindings.getBinding("o");

        if (!p.getValue().stringValue().contains(PREDICATE_NAMESPACE)) {
            IRI predicate = vf.createIRI(p.getValue().stringValue());
            Value object = o.getValue();
            Statement qualifierStatement = vf.createStatement(id, predicate, object);
            statements.add(qualifierStatement);
        }
    }
    return statements;
}
 
Example 8
Source File: HashJoinImpl.java    From CostFed with GNU Affero General Public License v3.0 5 votes vote down vote up
private BindingSet calcKey(BindingSet bindings, Set<String> commonVars) {
    QueryBindingSet q = new QueryBindingSet();
    for (String varName : commonVars) {
        Binding b = bindings.getBinding(varName);
        if (b != null) {
            q.addBinding(b);
        }
    }
    return q;
}
 
Example 9
Source File: BottomUpJoinIterator.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private BindingSet calcKey(BindingSet bindings, Set<String> commonVars) {
	QueryBindingSet q = new QueryBindingSet();
	for (String varName : commonVars) {
		Binding b = bindings.getBinding(varName);
		if (b != null) {
			q.addBinding(b);
		}
	}
	return q;
}
 
Example 10
Source File: TimeAwareHBaseSail.java    From Halyard with Apache License 2.0 5 votes vote down vote up
@Override
public void addStatement(UpdateContext op, Resource subj, IRI pred, Value obj, Resource... contexts) throws SailException {
    BindingSet bs;
    Binding b;
    Value v;
    long timestamp = (op != null && (bs = op.getBindingSet()) != null && (b = bs.getBinding(TIMESTAMP_CALLBACK_BINDING_NAME)) != null) ? longValueOfTimeStamp((Literal) b.getValue()) : System.currentTimeMillis();
    for (Resource ctx : normalizeContexts(contexts)) {
        addStatementInternal(subj, pred, obj, ctx, timestamp);
    }
}
 
Example 11
Source File: OneOfVisitorTest.java    From rya with Apache License 2.0 5 votes vote down vote up
private static void assertBindingSet(final Iterator<BindingSet> bindingSetIter, final Iterator<Resource> expectedValues) {
    while (expectedValues.hasNext()) {
        final Resource expectedValue = expectedValues.next();
        assertTrue(bindingSetIter.hasNext());
        final BindingSet bindingSet = bindingSetIter.next();
        assertTrue(bindingSet instanceof QueryBindingSet);
        assertEquals(1, bindingSet.getBindingNames().size());
        final Binding binding = bindingSet.getBinding("s");
        assertNotNull(binding);
        final Value actualValue = binding.getValue();
        assertEquals(expectedValue, actualValue);
    }
}
 
Example 12
Source File: BindingSetUtil.java    From rya with Apache License 2.0 5 votes vote down vote up
/**
 * Create a new {@link BindingSet} that only includes the bindings whose names appear within the {@code variableOrder}.
 * If no binding is found for a variable, then that binding is just omitted from the resulting object.
 *
 * @param variableOrder - Defines which bindings will be kept. (not null)
 * @param bindingSet - Contains the source {@link Binding}s. (not null)
 * @return A new {@link BindingSet} containing only the specified bindings.
 */
public static BindingSet keepBindings(final VariableOrder variableOrder, final BindingSet bindingSet) {
    requireNonNull(variableOrder);
    requireNonNull(bindingSet);

    final MapBindingSet result = new MapBindingSet();
    for(final String bindingName : variableOrder) {
        if(bindingSet.hasBinding(bindingName)) {
            final Binding binding = bindingSet.getBinding(bindingName);
            result.addBinding(binding);
        }
    }
    return result;
}
 
Example 13
Source File: SimpleBindingSetOps.java    From semagrow with Apache License 2.0 5 votes vote down vote up
/**
 * Project a binding set to a potentially smaller binding set that
 * contain only the variable bindings that are in vars set.
 * @param bindings
 * @param vars
 * @return
 */
@Override
public BindingSet project(Collection<String> vars, BindingSet bindings) {
    QueryBindingSet q = new QueryBindingSet();

    for (String varName : vars) {
        Binding b = bindings.getBinding(varName);
        if (b != null) {
            q.addBinding(b);
        }
    }
    return q;
}
 
Example 14
Source File: WikiDataReification.java    From inception with Apache License 2.0 4 votes vote down vote up
private List<Statement> getStatementsById(RepositoryConnection aConnection, KnowledgeBase kb,
        String aStatementId)
{
    ValueFactory vf = aConnection.getValueFactory();
    
    String QUERY = String
        .join("\n",
            "SELECT DISTINCT ?s ?p ?ps ?o WHERE {",
            "  ?s  ?p  ?id .",
            "  ?id ?ps ?o .",
            "  FILTER(STRSTARTS(STR(?ps), STR(?ps_ns)))",
            "}",
            "LIMIT 10");
    Resource id = vf.createBNode(aStatementId);
    TupleQuery tupleQuery = aConnection.prepareTupleQuery(QueryLanguage.SPARQL, QUERY);
    tupleQuery.setBinding("id", id);
    tupleQuery.setBinding("ps_ns", vf.createIRI(PREDICATE_NAMESPACE));

    tupleQuery.setIncludeInferred(false);
    TupleQueryResult result;

    try {
        result = tupleQuery.evaluate();
    }
    catch (QueryEvaluationException e) {
        log.warn("No such statementId in knowledge base", e);
        return null;
    }

    List<Statement> statements = new ArrayList<>();
    while (result.hasNext()) {
        BindingSet bindings = result.next();
        Binding s = bindings.getBinding("s");
        Binding p = bindings.getBinding("p");
        Binding o = bindings.getBinding("o");
        Binding ps = bindings.getBinding("ps");

        IRI instance = vf.createIRI(s.getValue().stringValue());
        IRI predicate = vf.createIRI(p.getValue().stringValue());
        Statement root = vf.createStatement(instance, predicate, id);

        IRI valuePredicate = vf.createIRI(ps.getValue().stringValue());
        Value object = o.getValue();
        Statement valueStatement = vf.createStatement(id, valuePredicate, object);
        statements.add(root);
        statements.add(valueStatement);
    }
    return statements;
}