Search in sources :

Example 6 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.
 * @param recordsCount Count of records processed up to this batch
 * @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, int recordsCount) throws SAXException, IOException, XPathExpressionException {
    if (outputVars.isEmpty()) {
        log.debug("MWAPI: outputVars is empty");
        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);
    IBindingSet[] results = new IBindingSet[nodes.getLength()];
    if (results.length == 0) {
        log.debug("MWAPI: item xpath {} returned 0 node (empty?)", template.getItemsPath());
        return new ResultWithContinue(results, searchContinue);
    }
    final Map<OutputVariable, XPathExpression> compiledVars = new HashMap<>();
    // Thanks, Oracle!
    for (OutputVariable v : outputVars) {
        compiledVars.put(v, xpath.get().compile(v.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> v : compiledVars.entrySet()) {
            final IConstant constant;
            if (v.getKey().isOrdinal()) {
                constant = makeConstant(lexiconRelation.getValueFactory(), i + recordsCount);
                results[i].set(v.getKey().getVar(), constant);
                continue;
            }
            final Node value = (Node) v.getValue().evaluate(node, XPathConstants.NODE);
            if (value != null && value.getNodeValue() != null) {
                if (v.getKey().isURI()) {
                    constant = makeConstant(lexiconRelation.getValueFactory(), v.getKey().getURI(value.getNodeValue()));
                } else {
                    constant = makeConstant(lexiconRelation.getValueFactory(), value.getNodeValue());
                }
                results[i].set(v.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)

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