Java 如何使用ElementWalker向Jena查询动态添加三元组?

Java 如何使用ElementWalker向Jena查询动态添加三元组?,java,sparql,semantic-web,jena,Java,Sparql,Semantic Web,Jena,假设我有一些jena查询对象: String query = "SELECT * WHERE{ ?s <some_uri> ?o ...etc. }"; Query q = QueryFactory.create(query, Syntax.syntaxARQ); 我希望添加三个s.t.查询如下: SELECT * WHERE { ?s ?p ?o; ?p2 ?o2. -->?s <some_url> "value". //adde

假设我有一些jena查询对象:

String query = "SELECT * WHERE{ ?s <some_uri> ?o ...etc. }";
Query q = QueryFactory.create(query, Syntax.syntaxARQ);
我希望添加三个s.t.查询如下:

    SELECT * WHERE {
    ?s ?p ?o;
       ?p2 ?o2.
 -->?s <some_url> "value". //added
    ?s2 ?p3 ?o3.
}
选择*WHERE{
?s?p?o;
?p2?o2。
-->?s“值”。//已添加
?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);
        }
    });
ElementWalker.walk(query.getQueryPattern(),新的ElementVisitorBase(){
公共无效访问(ElementPathBlock el){
//当它是一块三元组时,添加一些三元组
ElementPathBlock elCopy=新建ElementPathBlock();
迭代器三元组=el.patternElts();
int指数=0;
int numAdded=0;
while(triples.hasNext()){
TriplePath t=triples.next();
如果(t.getSubject().equals(/*something*/)){
//在此处添加三元组,类似于:
addTriple(index+numAdded,/*someTriple*/);
numaded++;
}
索引++;
}
el=elCopy;
}
公共无效访问(ElementSubQuery el){
//获取子查询并遍历它
ElementGroup subQP=(ElementGroup)el.getQuery().getQueryPattern();
ElementWalker.步行(subQP,本);
}
公共无效访问(Elementel可选){
//获取可选元素并遍历它们
Element optionalQP=el.getOptionalElement();
元素步行(可选QP,本);
}
});
上面代码的问题是,它成功地将三元组添加到ElementPathBlock(
el
),但更改不会一直持续到查询本身。我希望在访问查询中的这个特定ElementPathBlock时能够成功地修改查询


感谢您的帮助。

此代码使用助行器在主题为
?d
的任何三元组后面添加
?d?d
。由于主题为
?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 );
    }
}

此代码使用助行器在主题为
?d
的任何三元组后添加
?d?d
。由于主题为
?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 );
    }
}

另外,对于阅读本文的其他人来说,还有更多关于ElementWalker如何用于(而不是我的)另一个问题的例子。通过使用它而不是迭代器it=el.patternElts()我可以使用提供的
it.add(…)
来添加三元组。很好,谢谢你的帮助。如果这确实对您有效,请随时让其他用户知道它是有效的!另外,对于阅读本文的其他人来说,还有更多关于ElementWalker如何用于(而不是我的)另一个问题的例子。通过使用它而不是迭代器it=el.patternElts()我可以使用提供的
it.add(…)
来添加三元组。很好,谢谢你的帮助。如果这确实对您有效,请随时让其他用户知道它是有效的!