Search in sources :

Example 16 with BindingSet

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

the class AbstractHTTPQuery method getBindingsArray.

public Binding[] getBindingsArray() {
    BindingSet bindings = this.getBindings();
    Binding[] bindingsArray = new Binding[bindings.size()];
    Iterator<Binding> iter = bindings.iterator();
    for (int i = 0; i < bindings.size(); i++) {
        bindingsArray[i] = iter.next();
    }
    return bindingsArray;
}
Also used : Binding(org.eclipse.rdf4j.query.Binding) BindingSet(org.eclipse.rdf4j.query.BindingSet)

Example 17 with BindingSet

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

the class SPARQLConnection method toStatementIteration.

/**
 * Converts a {@link TupleQueryResult} resulting from the {@link #EVERYTHING_WITH_GRAPH} to a statement by
 * using the respective values from the {@link BindingSet} or (if provided) the ones from the arguments.
 *
 * @param iter
 *        the {@link TupleQueryResult}
 * @param subj
 *        the subject {@link Resource} used as input or <code>null</code> if wildcard was used
 * @param pred
 *        the predicate {@link IRI} used as input or <code>null</code> if wildcard was used
 * @param obj
 *        the object {@link Value} used as input or <code>null</code> if wildcard was used
 * @return the converted iteration
 */
protected Iteration<Statement, QueryEvaluationException> toStatementIteration(TupleQueryResult iter, final Resource subj, final IRI pred, final Value obj) {
    return new ConvertingIteration<BindingSet, Statement, QueryEvaluationException>(iter) {

        @Override
        protected Statement convert(BindingSet b) throws QueryEvaluationException {
            Resource s = subj == null ? (Resource) b.getValue("s") : subj;
            IRI p = pred == null ? (IRI) b.getValue("p") : pred;
            Value o = obj == null ? b.getValue("o") : obj;
            Resource ctx = (Resource) b.getValue("ctx");
            return SimpleValueFactory.getInstance().createStatement(s, p, o, ctx);
        }
    };
}
Also used : BindingSet(org.eclipse.rdf4j.query.BindingSet) IRI(org.eclipse.rdf4j.model.IRI) ConvertingIteration(org.eclipse.rdf4j.common.iteration.ConvertingIteration) ExceptionConvertingIteration(org.eclipse.rdf4j.common.iteration.ExceptionConvertingIteration) Resource(org.eclipse.rdf4j.model.Resource) Value(org.eclipse.rdf4j.model.Value)

Example 18 with BindingSet

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

the class SPARQLConnection method getContextIDs.

public RepositoryResult<Resource> getContextIDs() throws RepositoryException {
    TupleQueryResult iter = null;
    RepositoryResult<Resource> result = null;
    boolean allGood = false;
    try {
        TupleQuery query = prepareTupleQuery(SPARQL, NAMEDGRAPHS, "");
        iter = query.evaluate();
        result = new RepositoryResult<Resource>(new ExceptionConvertingIteration<Resource, RepositoryException>(new ConvertingIteration<BindingSet, Resource, QueryEvaluationException>(iter) {

            @Override
            protected Resource convert(BindingSet bindings) throws QueryEvaluationException {
                return (Resource) bindings.getValue("_");
            }
        }) {

            @Override
            protected RepositoryException convert(Exception e) {
                return new RepositoryException(e);
            }
        });
        allGood = true;
        return result;
    } catch (MalformedQueryException e) {
        throw new RepositoryException(e);
    } catch (QueryEvaluationException e) {
        throw new RepositoryException(e);
    } finally {
        if (!allGood) {
            try {
                if (result != null) {
                    result.close();
                }
            } finally {
                if (iter != null) {
                    iter.close();
                }
            }
        }
    }
}
Also used : BindingSet(org.eclipse.rdf4j.query.BindingSet) ExceptionConvertingIteration(org.eclipse.rdf4j.common.iteration.ExceptionConvertingIteration) Resource(org.eclipse.rdf4j.model.Resource) TupleQuery(org.eclipse.rdf4j.query.TupleQuery) SPARQLTupleQuery(org.eclipse.rdf4j.repository.sparql.query.SPARQLTupleQuery) RepositoryException(org.eclipse.rdf4j.repository.RepositoryException) QueryEvaluationException(org.eclipse.rdf4j.query.QueryEvaluationException) RDFParseException(org.eclipse.rdf4j.rio.RDFParseException) RepositoryException(org.eclipse.rdf4j.repository.RepositoryException) UpdateExecutionException(org.eclipse.rdf4j.query.UpdateExecutionException) MalformedQueryException(org.eclipse.rdf4j.query.MalformedQueryException) UnknownTransactionStateException(org.eclipse.rdf4j.repository.UnknownTransactionStateException) UnsupportedQueryLanguageException(org.eclipse.rdf4j.query.UnsupportedQueryLanguageException) RDFHandlerException(org.eclipse.rdf4j.rio.RDFHandlerException) IOException(java.io.IOException) QueryEvaluationException(org.eclipse.rdf4j.query.QueryEvaluationException) MalformedQueryException(org.eclipse.rdf4j.query.MalformedQueryException) TupleQueryResult(org.eclipse.rdf4j.query.TupleQueryResult)

Example 19 with BindingSet

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

the class SPARQLResultsTSVParser method parse.

@Override
public void parse(InputStream in) throws IOException, QueryResultParseException, TupleQueryResultHandlerException {
    InputStreamReader r = new InputStreamReader(in, Charset.forName("UTF-8"));
    BufferedReader reader = new BufferedReader(r);
    List<String> bindingNames = null;
    String nextLine;
    while ((nextLine = reader.readLine()) != null) {
        if (bindingNames == null) {
            // header is mandatory in SPARQL TSV
            String[] names = nextLine.split("\t", -1);
            bindingNames = new ArrayList<String>(names.length);
            for (String name : names) {
                // strip the '?' prefix
                if ('?' == name.charAt(0)) {
                    bindingNames.add(name.substring(1));
                } else {
                    bindingNames.add(name);
                }
            }
            if (handler != null) {
                handler.startQueryResult(bindingNames);
            }
        } else {
            // process solution
            String[] lineTokens = nextLine.split("\t", -1);
            List<Value> values = new ArrayList<Value>();
            for (String valueString : lineTokens) {
                Value v = null;
                if (valueString.startsWith("_:")) {
                    v = valueFactory.createBNode(valueString.substring(2));
                } else if (valueString.startsWith("<") && valueString.endsWith(">")) {
                    try {
                        v = valueFactory.createIRI(valueString.substring(1, valueString.length() - 1));
                    } catch (IllegalArgumentException e) {
                        v = valueFactory.createLiteral(valueString);
                    }
                } else if (valueString.startsWith("\"")) {
                    v = parseLiteral(valueString);
                } else if (!"".equals(valueString)) {
                    if (valueString.matches("^[\\+\\-]?[\\d\\.].*")) {
                        IRI datatype = null;
                        if (XMLDatatypeUtil.isValidInteger(valueString)) {
                            if (XMLDatatypeUtil.isValidNegativeInteger(valueString)) {
                                datatype = XMLSchema.NEGATIVE_INTEGER;
                            } else {
                                datatype = XMLSchema.INTEGER;
                            }
                        } else if (XMLDatatypeUtil.isValidDecimal(valueString)) {
                            datatype = XMLSchema.DECIMAL;
                        } else if (XMLDatatypeUtil.isValidDouble(valueString)) {
                            datatype = XMLSchema.DOUBLE;
                        }
                        if (datatype != null) {
                            v = valueFactory.createLiteral(valueString, datatype);
                        } else {
                            v = valueFactory.createLiteral(valueString);
                        }
                    } else {
                        v = valueFactory.createLiteral(valueString);
                    }
                }
                values.add(v);
            }
            BindingSet bindingSet = new ListBindingSet(bindingNames, values.toArray(new Value[values.size()]));
            if (handler != null) {
                handler.handleSolution(bindingSet);
            }
        }
    }
    if (bindingNames != null && handler != null) {
        handler.endQueryResult();
    }
}
Also used : IRI(org.eclipse.rdf4j.model.IRI) BindingSet(org.eclipse.rdf4j.query.BindingSet) ListBindingSet(org.eclipse.rdf4j.query.impl.ListBindingSet) InputStreamReader(java.io.InputStreamReader) ListBindingSet(org.eclipse.rdf4j.query.impl.ListBindingSet) ArrayList(java.util.ArrayList) BufferedReader(java.io.BufferedReader) Value(org.eclipse.rdf4j.model.Value)

Example 20 with BindingSet

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

the class SPARQLResultsCSVParser method parse.

@Override
public void parse(InputStream in) throws IOException, QueryResultParseException, TupleQueryResultHandlerException {
    CSVReader reader = new CSVReader(new InputStreamReader(in, Charset.forName("UTF-8")));
    List<String> bindingNames = null;
    String[] nextLine;
    try {
        while ((nextLine = reader.readNext()) != null) {
            if (bindingNames == null) {
                // header is mandatory in SPARQL CSV
                bindingNames = Arrays.asList(nextLine);
                if (handler != null) {
                    handler.startQueryResult(bindingNames);
                }
            } else {
                // process solution
                List<Value> values = new ArrayList<Value>();
                for (String valueString : nextLine) {
                    Value v = null;
                    if (valueString.startsWith("_:")) {
                        v = valueFactory.createBNode(valueString.substring(2));
                    } else if (!"".equals(valueString)) {
                        if (valueString.matches("^[\\+\\-]?[\\d\\.].*")) {
                            IRI datatype = null;
                            if (XMLDatatypeUtil.isValidInteger(valueString)) {
                                if (XMLDatatypeUtil.isValidNegativeInteger(valueString)) {
                                    datatype = XMLSchema.NEGATIVE_INTEGER;
                                } else {
                                    datatype = XMLSchema.INTEGER;
                                }
                            } else if (XMLDatatypeUtil.isValidDecimal(valueString)) {
                                datatype = XMLSchema.DECIMAL;
                            } else if (XMLDatatypeUtil.isValidDouble(valueString)) {
                                datatype = XMLSchema.DOUBLE;
                            }
                            if (datatype != null) {
                                v = valueFactory.createLiteral(valueString, datatype);
                            } else {
                                v = valueFactory.createLiteral(valueString);
                            }
                        } else {
                            try {
                                v = valueFactory.createIRI(valueString);
                            } catch (IllegalArgumentException e) {
                                v = valueFactory.createLiteral(valueString);
                            }
                        }
                    }
                    values.add(v);
                }
                BindingSet bindingSet = new ListBindingSet(bindingNames, values.toArray(new Value[values.size()]));
                if (handler != null) {
                    handler.handleSolution(bindingSet);
                }
            }
        }
        if (bindingNames != null && handler != null) {
            handler.endQueryResult();
        }
    } finally {
        reader.close();
    }
}
Also used : IRI(org.eclipse.rdf4j.model.IRI) BindingSet(org.eclipse.rdf4j.query.BindingSet) ListBindingSet(org.eclipse.rdf4j.query.impl.ListBindingSet) InputStreamReader(java.io.InputStreamReader) ListBindingSet(org.eclipse.rdf4j.query.impl.ListBindingSet) CSVReader(com.opencsv.CSVReader) Value(org.eclipse.rdf4j.model.Value) ArrayList(java.util.ArrayList)

Aggregations

BindingSet (org.eclipse.rdf4j.query.BindingSet)28 ArrayList (java.util.ArrayList)9 IRI (org.eclipse.rdf4j.model.IRI)8 Value (org.eclipse.rdf4j.model.Value)8 QueryEvaluationException (org.eclipse.rdf4j.query.QueryEvaluationException)6 TupleQueryResult (org.eclipse.rdf4j.query.TupleQueryResult)6 RepositoryException (org.eclipse.rdf4j.repository.RepositoryException)6 IOException (java.io.IOException)5 Literal (org.eclipse.rdf4j.model.Literal)5 Binding (org.eclipse.rdf4j.query.Binding)5 ListBindingSet (org.eclipse.rdf4j.query.impl.ListBindingSet)5 InputStream (java.io.InputStream)4 BNode (org.eclipse.rdf4j.model.BNode)4 AbstractQueryResultIOTupleTest (org.eclipse.rdf4j.query.resultio.AbstractQueryResultIOTupleTest)4 QueryResultCollector (org.eclipse.rdf4j.query.resultio.helpers.QueryResultCollector)4 SPARQLResultsJSONParser (org.eclipse.rdf4j.query.resultio.sparqljson.SPARQLResultsJSONParser)4 Test (org.junit.Test)4 Resource (org.eclipse.rdf4j.model.Resource)3 Var (org.eclipse.rdf4j.query.algebra.Var)3 InputStreamReader (java.io.InputStreamReader)2