Search in sources :

Example 1 with TupleQueryResultHandlerException

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

the class DAWGTestResultSetWriter method handleSolution.

@Override
public void handleSolution(BindingSet bindingSet) throws TupleQueryResultHandlerException {
    try {
        BNode solutionNode = vf.createBNode();
        reportStatement(resultSetNode, SOLUTION, solutionNode);
        for (Binding binding : bindingSet) {
            BNode bindingNode = vf.createBNode();
            reportStatement(solutionNode, BINDING, bindingNode);
            reportStatement(bindingNode, VARIABLE, vf.createLiteral(binding.getName()));
            Value value = binding.getValue();
            // generated for the result format
            if (value instanceof BNode) {
                BNode mappedBNode = bnodeMap.get(value);
                if (mappedBNode == null) {
                    mappedBNode = vf.createBNode();
                    bnodeMap.put((BNode) value, mappedBNode);
                }
                value = mappedBNode;
            }
            reportStatement(bindingNode, VALUE, value);
        }
    } catch (RDFHandlerException e) {
        throw new TupleQueryResultHandlerException(e);
    }
}
Also used : Binding(org.eclipse.rdf4j.query.Binding) BNode(org.eclipse.rdf4j.model.BNode) RDFHandlerException(org.eclipse.rdf4j.rio.RDFHandlerException) TupleQueryResultHandlerException(org.eclipse.rdf4j.query.TupleQueryResultHandlerException) Value(org.eclipse.rdf4j.model.Value)

Example 2 with TupleQueryResultHandlerException

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

the class AbstractSPARQLJSONWriter method endQueryResult.

@Override
public void endQueryResult() throws TupleQueryResultHandlerException {
    try {
        if (!documentOpen) {
            startDocument();
        }
        if (!headerOpen) {
            startHeader();
        }
        if (!headerComplete) {
            endHeader();
        }
        if (!tupleVariablesFound) {
            throw new IllegalStateException("Could not end query result as startQueryResult was not called first.");
        }
        // bindings array
        jg.writeEndArray();
        // results braces
        jg.writeEndObject();
        endDocument();
    } catch (IOException e) {
        throw new TupleQueryResultHandlerException(e);
    } catch (TupleQueryResultHandlerException e) {
        throw e;
    } catch (QueryResultHandlerException e) {
        throw new TupleQueryResultHandlerException(e);
    }
}
Also used : TupleQueryResultHandlerException(org.eclipse.rdf4j.query.TupleQueryResultHandlerException) IOException(java.io.IOException) TupleQueryResultHandlerException(org.eclipse.rdf4j.query.TupleQueryResultHandlerException) QueryResultHandlerException(org.eclipse.rdf4j.query.QueryResultHandlerException)

Example 3 with TupleQueryResultHandlerException

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

the class AbstractSPARQLJSONWriter method writeValue.

protected void writeValue(Value value) throws IOException, QueryResultHandlerException {
    jg.writeStartObject();
    if (value instanceof IRI) {
        jg.writeStringField("type", "uri");
        jg.writeStringField("value", ((IRI) value).toString());
    } else if (value instanceof BNode) {
        jg.writeStringField("type", "bnode");
        jg.writeStringField("value", ((BNode) value).getID());
    } else if (value instanceof Literal) {
        Literal lit = (Literal) value;
        if (Literals.isLanguageLiteral(lit)) {
            jg.writeObjectField("xml:lang", lit.getLanguage().orElse(null));
        } else {
            IRI datatype = lit.getDatatype();
            boolean ignoreDatatype = datatype.equals(XMLSchema.STRING) && xsdStringToPlainLiteral();
            if (!ignoreDatatype) {
                jg.writeObjectField("datatype", lit.getDatatype().stringValue());
            }
        }
        jg.writeObjectField("type", "literal");
        jg.writeObjectField("value", lit.getLabel());
    } else {
        throw new TupleQueryResultHandlerException("Unknown Value object type: " + value.getClass());
    }
    jg.writeEndObject();
}
Also used : IRI(org.eclipse.rdf4j.model.IRI) BNode(org.eclipse.rdf4j.model.BNode) TupleQueryResultHandlerException(org.eclipse.rdf4j.query.TupleQueryResultHandlerException) Literal(org.eclipse.rdf4j.model.Literal)

Example 4 with TupleQueryResultHandlerException

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

the class BinaryQueryResultWriter method startQueryResult.

@Override
public void startQueryResult(List<String> bindingNames) throws TupleQueryResultHandlerException {
    tupleVariablesFound = true;
    if (!documentStarted) {
        startDocument();
    }
    // Copy supplied column headers list and make it unmodifiable
    bindingNames = new ArrayList<String>(bindingNames);
    this.bindingNames = Collections.unmodifiableList(bindingNames);
    try {
        out.writeInt(this.bindingNames.size());
        for (String bindingName : this.bindingNames) {
            writeString(bindingName);
        }
        List<Value> nullTuple = Collections.nCopies(this.bindingNames.size(), (Value) null);
        previousBindings = new ListBindingSet(this.bindingNames, nullTuple);
        nextNamespaceID = 0;
    } catch (IOException e) {
        throw new TupleQueryResultHandlerException(e);
    }
}
Also used : ListBindingSet(org.eclipse.rdf4j.query.impl.ListBindingSet) TupleQueryResultHandlerException(org.eclipse.rdf4j.query.TupleQueryResultHandlerException) Value(org.eclipse.rdf4j.model.Value) IOException(java.io.IOException)

Example 5 with TupleQueryResultHandlerException

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

the class SPARQLResultsTSVWriter method handleSolution.

@Override
public void handleSolution(BindingSet bindingSet) throws TupleQueryResultHandlerException {
    if (!tupleVariablesFound) {
        throw new IllegalStateException("Must call startQueryResult before handleSolution");
    }
    try {
        for (int i = 0; i < bindingNames.size(); i++) {
            String name = bindingNames.get(i);
            Value value = bindingSet.getValue(name);
            if (value != null) {
                writeValue(value);
            }
            if (i < bindingNames.size() - 1) {
                writer.write("\t");
            }
        }
        writer.write("\n");
    } catch (IOException e) {
        throw new TupleQueryResultHandlerException(e);
    }
}
Also used : TupleQueryResultHandlerException(org.eclipse.rdf4j.query.TupleQueryResultHandlerException) Value(org.eclipse.rdf4j.model.Value) IOException(java.io.IOException)

Aggregations

TupleQueryResultHandlerException (org.eclipse.rdf4j.query.TupleQueryResultHandlerException)24 IOException (java.io.IOException)12 QueryResultHandlerException (org.eclipse.rdf4j.query.QueryResultHandlerException)9 Value (org.eclipse.rdf4j.model.Value)6 Binding (org.eclipse.rdf4j.query.Binding)4 TupleQueryResultBuilder (org.eclipse.rdf4j.query.impl.TupleQueryResultBuilder)4 RDFHandlerException (org.eclipse.rdf4j.rio.RDFHandlerException)4 BNode (org.eclipse.rdf4j.model.BNode)2 Literal (org.eclipse.rdf4j.model.Literal)2 Resource (org.eclipse.rdf4j.model.Resource)2 LinkedBlockingQueue (java.util.concurrent.LinkedBlockingQueue)1 HttpResponse (org.apache.http.HttpResponse)1 Graph (org.eclipse.rdf4j.model.Graph)1 IRI (org.eclipse.rdf4j.model.IRI)1 GraphImpl (org.eclipse.rdf4j.model.impl.GraphImpl)1 GraphUtilException (org.eclipse.rdf4j.model.util.GraphUtilException)1 ListBindingSet (org.eclipse.rdf4j.query.impl.ListBindingSet)1 MapBindingSet (org.eclipse.rdf4j.query.impl.MapBindingSet)1 SimpleBinding (org.eclipse.rdf4j.query.impl.SimpleBinding)1 BooleanQueryResultFormat (org.eclipse.rdf4j.query.resultio.BooleanQueryResultFormat)1