use of com.bigdata.bop.IBindingSet in project wikidata-query-rdf by wikimedia.
the class MWApiServiceCall method parseResponse.
/**
* Parse XML response from WM API.
*
* @param responseStream Response body as stream
* @param binding Current binding set.
* @return Set of resulting bindings, or null if none found.
* @throws SAXException on error
* @throws IOException on error
* @throws XPathExpressionException on error
*/
public ResultWithContinue parseResponse(InputStream responseStream, IBindingSet binding) throws SAXException, IOException, XPathExpressionException {
if (outputVars.isEmpty()) {
return null;
}
Document doc = docBuilder.get().parse(responseStream);
XPath path = xpath.get();
ImmutableMap<String, String> searchContinue = parseContinue(doc, path);
// FIXME: we're re-compiling it each time. Should probably do it only
// once per template.
// Note though that XPathExpression is not thread-safe. Maybe use ThreadLocal?
XPathExpression itemsXPath = path.compile(template.getItemsPath());
NodeList nodes = (NodeList) itemsXPath.evaluate(doc, XPathConstants.NODESET);
if (nodes.getLength() == 0) {
return null;
}
IBindingSet[] results = new IBindingSet[nodes.getLength()];
final Map<OutputVariable, XPathExpression> compiledVars = new HashMap<>();
// Thanks, Oracle!
for (OutputVariable var : outputVars) {
compiledVars.put(var, xpath.get().compile(var.getPath()));
}
for (int i = 0; i < nodes.getLength(); i++) {
final Node node = nodes.item(i);
results[i] = binding.copy(null);
for (Map.Entry<OutputVariable, XPathExpression> var : compiledVars.entrySet()) {
final IConstant constant;
if (var.getKey().isOrdinal()) {
constant = makeConstant(lexiconRelation.getValueFactory(), i);
results[i].set(var.getKey().getVar(), constant);
continue;
}
final Node value = (Node) var.getValue().evaluate(node, XPathConstants.NODE);
if (value != null && value.getNodeValue() != null) {
if (var.getKey().isURI()) {
constant = makeConstant(lexiconRelation.getValueFactory(), var.getKey().getURI(value.getNodeValue()));
} else {
constant = makeConstant(lexiconRelation.getValueFactory(), value.getNodeValue());
}
results[i].set(var.getKey().getVar(), constant);
}
}
}
return new ResultWithContinue(results, searchContinue);
}
use of com.bigdata.bop.IBindingSet in project wikidata-query-rdf by wikimedia.
the class MWApiServiceCallUnitTest method testResults.
@Test
public void testResults() throws Exception {
List<OutputVariable> outputVars = ImmutableList.of(new OutputVariable(makeVariable("var"), "@name"), new OutputVariable(makeVariable("header"), "/api/header/@value"), new OutputVariable(OutputVariable.Type.ITEM, makeVariable("item"), "@id"), new OutputVariable(OutputVariable.Type.ORDINAL, makeVariable("num"), "@id"));
when(template.getItemsPath()).thenReturn("/api/result");
InputStream responseStream = new ByteArrayInputStream("<api><header value=\"heading\"></header><result name=\"result1\" id=\"Q1\"></result><result name=\"result2\"></result></api>".getBytes("UTF-8"));
Iterator<IBindingSet> results = createCall(outputVars).parseResponse(responseStream, binding, 3).getResultIterator();
assertTrue(results.hasNext());
IBindingSet result = results.next();
assertThat(result, binds("var", "result1"));
assertThat(result, binds("header", "heading"));
assertThat(result, bindsItem("item", "Q1"));
assertThat(result, binds("num", new XSDNumericIV<>(3)));
result = results.next();
assertThat(result, binds("var", "result2"));
assertThat(result, binds("header", "heading"));
assertThat(result, binds("num", new XSDNumericIV<>(4)));
assertFalse(results.hasNext());
}
use of com.bigdata.bop.IBindingSet in project wikidata-query-rdf by wikimedia.
the class MWApiServiceCallUnitTest method testResultsMissingVar.
@Test
public void testResultsMissingVar() throws Exception {
List<OutputVariable> outputVars = ImmutableList.of(new OutputVariable(makeVariable("var"), "@name"), new OutputVariable(makeVariable("data"), "text()"), new OutputVariable(makeVariable("header"), "/api/header/@value"));
when(template.getItemsPath()).thenReturn("/api/result");
InputStream responseStream = new ByteArrayInputStream("<api><header value=\"heading\"></header><result name=\"result1\">datadata</result><result>we need moar data</result></api>".getBytes("UTF-8"));
Iterator<IBindingSet> results = createCall(outputVars).parseResponse(responseStream, binding, 0).getResultIterator();
assertTrue(results.hasNext());
IBindingSet result = results.next();
assertThat(result, binds("var", "result1"));
assertThat(result, binds("data", "datadata"));
assertThat(result, binds("header", "heading"));
result = results.next();
assertThat(result, notBinds("var"));
assertThat(result, binds("data", "we need moar data"));
assertThat(result, binds("header", "heading"));
assertFalse(results.hasNext());
}
use of com.bigdata.bop.IBindingSet in project wikidata-query-rdf by wikimedia.
the class MWApiServiceCallUnitTest method testEmptyResult.
@Test
public void testEmptyResult() throws Exception {
List<OutputVariable> outputVars = ImmutableList.of(new OutputVariable(makeVariable("var"), "@test"));
InputStream responseStream = new ByteArrayInputStream("<result></result>".getBytes("UTF-8"));
when(template.getItemsPath()).thenReturn("/api/result");
Iterator<IBindingSet> results = createCall(outputVars).parseResponse(responseStream, binding, 0).getResultIterator();
assertFalse(results.hasNext());
}
use of com.bigdata.bop.IBindingSet in project wikidata-query-rdf by wikimedia.
the class EmptyLabelServiceOptimizer method collectUncertainVars.
private Set<IVariable<?>> collectUncertainVars(StaticAnalysis sa, IBindingSet[] bSets, JoinGroupNode op) {
Set<IVariable<?>> uncertainVars = new HashSet<>();
sa.getMaybeProducedBindings(op, uncertainVars, /* recursive */
true);
for (IBindingSet bSet : bSets) {
bSet.vars().forEachRemaining(v -> uncertainVars.add(v));
}
return uncertainVars;
}
Aggregations