如何使用ElementWalker动态地将三元组添加到Jena查询?

时间:2013-07-24 17:17:43

标签: java sparql semantic-web jena

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

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.
}

我希望增加一个三重奏。查询看起来像:

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

我知道有一些方法可以添加到顶层(Jena Tutorial),但是当我使用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时成功修改查询。

任何帮助表示感谢。

1 个答案:

答案 0 :(得分:5)

此代码使用walker在主题为?d ?d ?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 );
    }
}

输出结果为:

== 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
    }
  }