1

假设我有一些耶拿查询对象:

String query = "SELECT * WHERE{ ?s <some_uri> ?o ...etc. }";
Query q = QueryFactory.create(query, Syntax.syntaxARQ);

我想使用 ElementWalker 并将三元组添加到查询中,如下所示:

给定一个查询

SELECT * WHERE {
    ?s ?p ?o;
       ?p2 ?o2.
    ?s2 ?p3 ?o3.
}

我希望添加一个三重 st 查询看起来像:

    SELECT * WHERE {
    ?s ?p ?o;
       ?p2 ?o2.
 -->?s <some_url> "value". //added
    ?s2 ?p3 ?o3.
}

我知道有一些方法可以添加到顶层(耶拿教程),但我想在使用 ElementWalker 遍历它们时以某种方式添加三元组:

ElementWalker.walk(query.getQueryPattern(), new ElementVisitorBase(){

        public void visit(ElementPathBlock el) {

            // when it's a block of triples, add in some triple
            ElementPathBlock elCopy = new ElementPathBlock();
            Iterator<TriplePath> triples = el.patternElts();
            int index = 0;
            int numAdded = 0;
            while (triples.hasNext()) {
                TriplePath t = triples.next();
                if(t.getSubject().equals(/*something*/)){
                    //add triple here, something like:
                    elCopy.addTriple(index+numAdded, /*someTriple*/);
                    numAdded++;
                }
                index++;
            }
            el = elCopy;
        }

        public void visit(ElementSubQuery el) {

            // get the subquery and walk it
            ElementGroup subQP = (ElementGroup) el.getQuery().getQueryPattern();
            ElementWalker.walk(subQP, this);
        }

        public void visit(ElementOptional el) {

            // get optional elements and walk them
            Element optionalQP = el.getOptionalElement();
            ElementWalker.walk(optionalQP, this);
        }
    });

上面代码的问题在于,它成功地将三元组添加到 ElementPathBlock ( el),但更改并没有延续到查询本身。我希望在访问查询内部的这个特定 ElementPathBlock 时成功修改查询。

任何帮助表示赞赏。

4

1 回答 1

5

此代码使用 walker?d ?d ?d在主题为 的任何三元组之后添加?d。由于在查询中有多个三元组,因此插入了?d多个 get 实例?d ?d ?d,并且保留了更改。

import java.util.ListIterator;

import com.hp.hpl.jena.graph.Triple;
import com.hp.hpl.jena.query.Query;
import com.hp.hpl.jena.query.QueryFactory;
import com.hp.hpl.jena.sparql.core.TriplePath;
import com.hp.hpl.jena.sparql.core.Var;
import com.hp.hpl.jena.sparql.syntax.ElementPathBlock;
import com.hp.hpl.jena.sparql.syntax.ElementVisitorBase;
import com.hp.hpl.jena.sparql.syntax.ElementWalker;

public class ElementWalkerExample {
    public static void main(String[] args) {
        final String queryString  = "" +
            "SELECT * WHERE {\n" +
            " ?a ?b ?c1 ;\n" +
            "    ?b ?c2 .\n" +
            " ?d ?e ?f .\n" +
            " ?g ?h ?i .\n" +
            "{ ?p ?q ?r .\n" +
            "  ?d ?e2 ?f2 . }\n" +
            "}";
        final Query query = QueryFactory.create( queryString );
        System.out.println( "== before ==\n"+query );
        ElementWalker.walk( query.getQueryPattern(), 
                new ElementVisitorBase() {
                    @Override
                    public void visit(ElementPathBlock el) {
                        ListIterator<TriplePath> it = el.getPattern().iterator();
                        while ( it.hasNext() ) {
                            final TriplePath tp = it.next();
                            final Var d = Var.alloc( "d" );
                            if ( tp.getSubject().equals( d )) {
                                it.add( new TriplePath( new Triple( d, d, d )));
                            }
                        }
                    }
        });
        System.out.println( "== after ==\n"+query );
    }
}

输出是:

== before ==
SELECT  *
WHERE
  { ?a ?b ?c1 .
    ?a ?b ?c2 .
    ?d ?e ?f .
    ?g ?h ?i
    { ?p ?q ?r .
      ?d ?e2 ?f2
    }
  }

== after ==
SELECT  *
WHERE
  { ?a ?b ?c1 .
    ?a ?b ?c2 .
    ?d ?e ?f .
    ?d ?d ?d .
    ?g ?h ?i
    { ?p ?q ?r .
      ?d ?e2 ?f2 .
      ?d ?d ?d
    }
  }
于 2013-07-24T17:52:56.860 回答