Search in sources :

Example 16 with Binding

use of org.eclipse.rdf4j.query.Binding in project rdf4j by eclipse.

the class SPARQLCSVTupleBackgroundTest method matchBindingSets.

private boolean matchBindingSets(List<? extends BindingSet> queryResult1, Iterable<? extends BindingSet> queryResult2, Map<BNode, BNode> bNodeMapping, int idx) {
    boolean result = false;
    if (idx < queryResult1.size()) {
        BindingSet bs1 = queryResult1.get(idx);
        List<BindingSet> matchingBindingSets = findMatchingBindingSets(bs1, queryResult2, bNodeMapping);
        for (BindingSet bs2 : matchingBindingSets) {
            // Map bNodes in bs1 to bNodes in bs2
            Map<BNode, BNode> newBNodeMapping = new HashMap<BNode, BNode>(bNodeMapping);
            for (Binding binding : bs1) {
                if (binding.getValue() instanceof BNode) {
                    newBNodeMapping.put((BNode) binding.getValue(), (BNode) bs2.getValue(binding.getName()));
                }
            }
            // FIXME: this recursive implementation has a high risk of
            // triggering a stack overflow
            // Enter recursion
            result = matchBindingSets(queryResult1, queryResult2, newBNodeMapping, idx + 1);
            if (result == true) {
                // models match, look no further
                break;
            }
        }
    } else {
        // All statements have been mapped successfully
        result = true;
    }
    return result;
}
Also used : Binding(org.eclipse.rdf4j.query.Binding) BindingSet(org.eclipse.rdf4j.query.BindingSet) BNode(org.eclipse.rdf4j.model.BNode) HashMap(java.util.HashMap)

Example 17 with Binding

use of org.eclipse.rdf4j.query.Binding in project rdf4j by eclipse.

the class SPARQLCSVTupleTest method matchBindingSets.

private boolean matchBindingSets(List<? extends BindingSet> queryResult1, Iterable<? extends BindingSet> queryResult2, Map<BNode, BNode> bNodeMapping, int idx) {
    boolean result = false;
    if (idx < queryResult1.size()) {
        BindingSet bs1 = queryResult1.get(idx);
        List<BindingSet> matchingBindingSets = findMatchingBindingSets(bs1, queryResult2, bNodeMapping);
        for (BindingSet bs2 : matchingBindingSets) {
            // Map bNodes in bs1 to bNodes in bs2
            Map<BNode, BNode> newBNodeMapping = new HashMap<BNode, BNode>(bNodeMapping);
            for (Binding binding : bs1) {
                if (binding.getValue() instanceof BNode) {
                    newBNodeMapping.put((BNode) binding.getValue(), (BNode) bs2.getValue(binding.getName()));
                }
            }
            // FIXME: this recursive implementation has a high risk of
            // triggering a stack overflow
            // Enter recursion
            result = matchBindingSets(queryResult1, queryResult2, newBNodeMapping, idx + 1);
            if (result == true) {
                // models match, look no further
                break;
            }
        }
    } else {
        // All statements have been mapped successfully
        result = true;
    }
    return result;
}
Also used : Binding(org.eclipse.rdf4j.query.Binding) BindingSet(org.eclipse.rdf4j.query.BindingSet) BNode(org.eclipse.rdf4j.model.BNode) HashMap(java.util.HashMap)

Example 18 with Binding

use of org.eclipse.rdf4j.query.Binding in project rdf4j by eclipse.

the class AbstractSPARQLJSONWriter method handleSolution.

@Override
public void handleSolution(BindingSet bindingSet) throws TupleQueryResultHandlerException {
    try {
        if (!documentOpen) {
            startDocument();
        }
        if (!headerOpen) {
            startHeader();
        }
        if (!headerComplete) {
            endHeader();
        }
        if (!tupleVariablesFound) {
            throw new IllegalStateException("Must call startQueryResult before handleSolution");
        }
        firstTupleWritten = true;
        jg.writeStartObject();
        Iterator<Binding> bindingIter = bindingSet.iterator();
        while (bindingIter.hasNext()) {
            Binding binding = bindingIter.next();
            jg.writeFieldName(binding.getName());
            writeValue(binding.getValue());
        }
        jg.writeEndObject();
    } catch (IOException e) {
        throw new TupleQueryResultHandlerException(e);
    } catch (TupleQueryResultHandlerException e) {
        throw e;
    } catch (QueryResultHandlerException e) {
        throw new TupleQueryResultHandlerException(e);
    }
}
Also used : Binding(org.eclipse.rdf4j.query.Binding) TupleQueryResultHandlerException(org.eclipse.rdf4j.query.TupleQueryResultHandlerException) IOException(java.io.IOException) TupleQueryResultHandlerException(org.eclipse.rdf4j.query.TupleQueryResultHandlerException) QueryResultHandlerException(org.eclipse.rdf4j.query.QueryResultHandlerException)

Aggregations

Binding (org.eclipse.rdf4j.query.Binding)18 Value (org.eclipse.rdf4j.model.Value)6 BindingSet (org.eclipse.rdf4j.query.BindingSet)5 BNode (org.eclipse.rdf4j.model.BNode)4 TupleQueryResultHandlerException (org.eclipse.rdf4j.query.TupleQueryResultHandlerException)4 IRI (org.eclipse.rdf4j.model.IRI)3 MalformedQueryException (org.eclipse.rdf4j.query.MalformedQueryException)3 QueryEvaluationException (org.eclipse.rdf4j.query.QueryEvaluationException)3 SimpleBinding (org.eclipse.rdf4j.query.impl.SimpleBinding)3 RepositoryException (org.eclipse.rdf4j.repository.RepositoryException)3 IOException (java.io.IOException)2 HashMap (java.util.HashMap)2 Literal (org.eclipse.rdf4j.model.Literal)2 QueryResultHandlerException (org.eclipse.rdf4j.query.QueryResultHandlerException)2 RDFHandlerException (org.eclipse.rdf4j.rio.RDFHandlerException)2 SAXException (org.xml.sax.SAXException)2 ClearNamespacesOperation (org.eclipse.rdf4j.http.protocol.transaction.operations.ClearNamespacesOperation)1 RemoveNamespaceOperation (org.eclipse.rdf4j.http.protocol.transaction.operations.RemoveNamespaceOperation)1 SPARQLUpdateOperation (org.eclipse.rdf4j.http.protocol.transaction.operations.SPARQLUpdateOperation)1 SetNamespaceOperation (org.eclipse.rdf4j.http.protocol.transaction.operations.SetNamespaceOperation)1