Search in sources :

Example 1 with IBindingSet

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);
}
Also used : XPath(javax.xml.xpath.XPath) XPathExpression(javax.xml.xpath.XPathExpression) HashMap(java.util.HashMap) NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) IConstant(com.bigdata.bop.IConstant) Document(org.w3c.dom.Document) OutputVariable(org.wikidata.query.rdf.blazegraph.mwapi.ApiTemplate.OutputVariable) IBindingSet(com.bigdata.bop.IBindingSet) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) HashMap(java.util.HashMap) NamedNodeMap(org.w3c.dom.NamedNodeMap)

Example 2 with IBindingSet

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());
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) IBindingSet(com.bigdata.bop.IBindingSet) XSDNumericIV(com.bigdata.rdf.internal.impl.literal.XSDNumericIV) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) OutputVariable(org.wikidata.query.rdf.blazegraph.mwapi.ApiTemplate.OutputVariable) Test(org.junit.Test)

Example 3 with IBindingSet

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());
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) IBindingSet(com.bigdata.bop.IBindingSet) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) OutputVariable(org.wikidata.query.rdf.blazegraph.mwapi.ApiTemplate.OutputVariable) Test(org.junit.Test)

Example 4 with IBindingSet

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());
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) IBindingSet(com.bigdata.bop.IBindingSet) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) OutputVariable(org.wikidata.query.rdf.blazegraph.mwapi.ApiTemplate.OutputVariable) Test(org.junit.Test)

Example 5 with IBindingSet

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;
}
Also used : IBindingSet(com.bigdata.bop.IBindingSet) IVariable(com.bigdata.bop.IVariable) HashSet(java.util.HashSet)

Aggregations

IBindingSet (com.bigdata.bop.IBindingSet)6 OutputVariable (org.wikidata.query.rdf.blazegraph.mwapi.ApiTemplate.OutputVariable)5 ByteArrayInputStream (java.io.ByteArrayInputStream)3 InputStream (java.io.InputStream)3 Test (org.junit.Test)3 IConstant (com.bigdata.bop.IConstant)2 ImmutableMap (com.google.common.collect.ImmutableMap)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 XPath (javax.xml.xpath.XPath)2 XPathExpression (javax.xml.xpath.XPathExpression)2 Document (org.w3c.dom.Document)2 NamedNodeMap (org.w3c.dom.NamedNodeMap)2 Node (org.w3c.dom.Node)2 NodeList (org.w3c.dom.NodeList)2 IVariable (com.bigdata.bop.IVariable)1 XSDNumericIV (com.bigdata.rdf.internal.impl.literal.XSDNumericIV)1 HashSet (java.util.HashSet)1