Jena SPARQL查询执行卡住

时间:2018-05-16 09:27:19

标签: sparql jena virtuoso

我正在运行Virtuoso SPARQL端点以执行查询。但是当我执行查询时,我的执行卡住了。

QueryExecution qexec = null;
    try {
        System.out.println("now inside");
        String queryString = "PREFIX  ns:  <http://example.org/ns#>" +
                "PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>\r\n" + 
                "SELECT  ?title ?price" + 
                "  {" + 
                "    ?x ns:price ?p ." + 
                "    ?x ns:discount ?discount ." + 
                "    ?x <http://purl.org/dc/elements/1.1/title> ?title . " + 
                "    BIND ('12'^^xsd:integer AS ?price)" + 
                "    FILTER( ?price < 20 )" + 
                "  }";
        System.out.println(queryString);
        System.out.println("inside the sparql just before call");
        qexec = QueryExecutionFactory.sparqlService("http://192.168.99.100:8890/sparql", queryString);

        ResultSet results = qexec.execSelect();
        System.out.println("inside the sparql just after call");
        // write to a ByteArrayOutputStream
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        ResultSetFormatter.outputAsJSON(outputStream, results);

        String json = outputStream.toString();
        System.out.println(json);

        return json;
    } catch (Exception e) {
        e.printStackTrace();
    }finally {
        qexec.close();
    }
    return "Error";

示例数据

@prefix dc:   <http://purl.org/dc/elements/1.1/> .
@prefix :     <http://example.org/book/> .
@prefix ns:   <http://example.org/ns#> .

:book1  dc:title     "SPARQL Tutorial" .
:book1  ns:price     42 .
:book1  ns:discount  0.2 .

:book2  dc:title     "The Semantic Web" .
:book2  ns:price     23 .
:book2  ns:discount  0.25 .

执行qexec.execSelect()时,它不应该给出任何错误或输出结果。但它偶尔会发生。

跟踪它时会将所有细节打印到下面的语句

System.out.println("inside the sparql just before call");

但不是在那之后。

我如何追踪它?我如何找出导致问题的原因?

注意:我的查询非常小,因此没有时间问题。

另外,我注意到在更新Jena 3.7.0的新更新版本后,我遇到了这个问题

1 个答案:

答案 0 :(得分:0)

如果我正确阅读了评论,我认为通过更改 -

解决了问题
public class Entry<K, V> {
public final K key;
public final V value;

public Entry(K key, V value) {
    this.key = key;
    this.value = value;
}

@Override
public boolean equals(Object o) {
    if (o instanceof Entry) {
        Entry other = (Entry)o;
        return this.key.equals(other.key) && this.value.equals(other.value);
    }
    return false;
}
}


public class SortedTreeMap<K extends Comparable<? super K>, V> implements ISortedTreeMap<K, V> {

private int size;
private Entry<K,V> data;
private SortedTreeMap<K,V> leftChild, rightChild, parent;
private Comparator<K> keyComparator;

private SortedTreeMap(K key, V value, Comparator<K> keyComparator, SortedTreeMap<K, V> parent){
    data = new Entry<>(key, value);
    this.parent = parent;
    this.keyComparator = keyComparator;
}

public SortedTreeMap(Comparator<K> kComparator) {
    keyComparator = kComparator;
}

private Entry<K,V> getRoot(){
    return this.data;
}

private SortedTreeMap<K,V> getLeftChild(){
    return this.leftChild;
}

private SortedTreeMap<K,V> getRightChild(){
    return this.rightChild;
}

private void setLeftChild(SortedTreeMap<K, V> newLeftChild){
    leftChild = newLeftChild;
}

private void setRightChild(SortedTreeMap<K, V> newRightChild){
    rightChild = newRightChild;
}

private void setRoot(Entry<K, V> newRoot){
    data = newRoot;
}

private boolean hasLeftChild(){
    return leftChild != null;
}

private boolean hasRightChild(){
    return rightChild != null;
}

@Override
public V add(K key, V value) {
    V result = null;

    if(isEmpty()) {
        data = new Entry<>(key, value);
    } else{
        int comparison = keyComparator.compare(key, data.key);
        SortedTreeMap<K,V> newChild = new SortedTreeMap<>(key, value, keyComparator, this);

        if(comparison < 0){
            if (hasLeftChild()) {
                result = leftChild.add(key, value);
            } else{
                setLeftChild(newChild);
            }

        } else if(comparison > 0){
            if (hasRightChild()) {
                result = rightChild.add(key, value);
            } else{
                setRightChild(newChild);
            }
        } else{
            result = data.value;
            this.data = new Entry<>(key, value);
        }
    }
    if (result == null){
        size++;
    }
    return result;
}

@Override
public Iterable<K> keys() {
    return new KeyIterator(this);
}

public class KeyIterator implements Iterable<K>, Iterator<K>{
    private SortedTreeMap<K,V> next;

    KeyIterator(SortedTreeMap<K, V> root){
        next = root;

        while(next.leftChild != null){
            next = next.leftChild;
        }
    }

    @Override
    public Iterator<K> iterator() {
        return this;
    }

    public boolean hasNext(){
        return next != null && !next.isEmpty();
    }

    public K next(){
        if(!hasNext()){
            throw new NoSuchElementException();
        }
        SortedTreeMap<K,V> r = next;

        if(next.rightChild != null){
            next = next.rightChild;
            while(next.leftChild != null){
                next = next.leftChild;
            }
            return r.data.key;
        }

        while(true){
            if(next.parent == null){
                next = null;
                return r.data.key;
            }

            if(next.parent.leftChild == next){
                next = next.parent;
                return r.data.key;
            }
            next = next.parent;
        }
    }
}

- 到 -

qexec = QueryExecutionFactory.sparqlService("http://192.168.99.100:8890/sparql", queryString);

- 这是一种解决方法,不能揭示原始问题的原因,但仍然成功地通过它。