Search in sources :

Example 11 with ListBindingSet

use of org.eclipse.rdf4j.query.impl.ListBindingSet 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 12 with ListBindingSet

use of org.eclipse.rdf4j.query.impl.ListBindingSet 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)

Example 13 with ListBindingSet

use of org.eclipse.rdf4j.query.impl.ListBindingSet in project rdf4j by eclipse.

the class QueryResultsTest method testBNodeBindingSet1.

@Test
public void testBNodeBindingSet1() throws QueryEvaluationException {
    tqr1.append(new ListBindingSet(twoBindingNames, foo, bnode1));
    tqr1.append(new ListBindingSet(twoBindingNames, bar, bnode2));
    tqr2.append(new ListBindingSet(twoBindingNames, foo, bnode2));
    tqr2.append(new ListBindingSet(twoBindingNames, bar, bnode1));
    assertTrue(QueryResults.equals(tqr1, tqr2));
}
Also used : ListBindingSet(org.eclipse.rdf4j.query.impl.ListBindingSet) Test(org.junit.Test)

Example 14 with ListBindingSet

use of org.eclipse.rdf4j.query.impl.ListBindingSet in project rdf4j by eclipse.

the class QueryResultsTest method testBNodeBindingSet5.

@Test
public void testBNodeBindingSet5() throws QueryEvaluationException {
    tqr1.append(new ListBindingSet(twoBindingNames, bnode1, bnode2));
    tqr1.append(new ListBindingSet(twoBindingNames, foo, bnode2));
    tqr2.append(new ListBindingSet(twoBindingNames, bnode2, bnode1));
    tqr2.append(new ListBindingSet(twoBindingNames, foo, bnode2));
    assertFalse(QueryResults.equals(tqr1, tqr2));
}
Also used : ListBindingSet(org.eclipse.rdf4j.query.impl.ListBindingSet) Test(org.junit.Test)

Example 15 with ListBindingSet

use of org.eclipse.rdf4j.query.impl.ListBindingSet in project rdf4j by eclipse.

the class QueryResultsTest method testStreamTupleResult.

@Test
public void testStreamTupleResult() {
    BindingSet a = new ListBindingSet(twoBindingNames, foo, lit1);
    BindingSet b = new ListBindingSet(twoBindingNames, bar, lit2);
    tqr1.append(a);
    tqr1.append(b);
    tqr1.append(a);
    tqr1.append(b);
    tqr1.append(b);
    List<BindingSet> list = QueryResults.stream(tqr1).filter(bs -> bs.getValue("a").equals(foo)).collect(Collectors.toList());
    assertNotNull(list);
    assertFalse(list.isEmpty());
    for (BindingSet bs : list) {
        assertTrue(bs.getValue("a").equals(foo));
    }
}
Also used : Arrays(java.util.Arrays) Assert.assertNotNull(org.junit.Assert.assertNotNull) AbstractCloseableIteration(org.eclipse.rdf4j.common.iteration.AbstractCloseableIteration) Literal(org.eclipse.rdf4j.model.Literal) Assert.assertTrue(org.junit.Assert.assertTrue) ValueFactory(org.eclipse.rdf4j.model.ValueFactory) ListBindingSet(org.eclipse.rdf4j.query.impl.ListBindingSet) SimpleValueFactory(org.eclipse.rdf4j.model.impl.SimpleValueFactory) Test(org.junit.Test) Collectors(java.util.stream.Collectors) Model(org.eclipse.rdf4j.model.Model) ArrayList(java.util.ArrayList) List(java.util.List) MutableTupleQueryResult(org.eclipse.rdf4j.query.impl.MutableTupleQueryResult) EmptyBindingSet(org.eclipse.rdf4j.query.impl.EmptyBindingSet) RDF(org.eclipse.rdf4j.model.vocabulary.RDF) Assert.assertFalse(org.junit.Assert.assertFalse) Map(java.util.Map) Statement(org.eclipse.rdf4j.model.Statement) IRI(org.eclipse.rdf4j.model.IRI) BNode(org.eclipse.rdf4j.model.BNode) Before(org.junit.Before) ListBindingSet(org.eclipse.rdf4j.query.impl.ListBindingSet) EmptyBindingSet(org.eclipse.rdf4j.query.impl.EmptyBindingSet) ListBindingSet(org.eclipse.rdf4j.query.impl.ListBindingSet) Test(org.junit.Test)

Aggregations

ListBindingSet (org.eclipse.rdf4j.query.impl.ListBindingSet)20 Test (org.junit.Test)15 ArrayList (java.util.ArrayList)6 Value (org.eclipse.rdf4j.model.Value)5 IRI (org.eclipse.rdf4j.model.IRI)3 BindingSet (org.eclipse.rdf4j.query.BindingSet)3 IOException (java.io.IOException)2 InputStreamReader (java.io.InputStreamReader)2 List (java.util.List)2 TupleQueryResult (org.eclipse.rdf4j.query.TupleQueryResult)2 EmptyBindingSet (org.eclipse.rdf4j.query.impl.EmptyBindingSet)2 IteratingTupleQueryResult (org.eclipse.rdf4j.query.impl.IteratingTupleQueryResult)2 MutableTupleQueryResult (org.eclipse.rdf4j.query.impl.MutableTupleQueryResult)2 CSVReader (com.opencsv.CSVReader)1 BufferedReader (java.io.BufferedReader)1 DataInputStream (java.io.DataInputStream)1 Arrays (java.util.Arrays)1 Map (java.util.Map)1 Collectors (java.util.stream.Collectors)1 AbstractCloseableIteration (org.eclipse.rdf4j.common.iteration.AbstractCloseableIteration)1