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();
}
}
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();
}
}
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));
}
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));
}
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));
}
}
Aggregations