Nic*_*ett 1 java semantic-web sparql jena
假设我有一些jena查询对象:
String query = "SELECT * WHERE{ ?s <some_uri> ?o ...etc. }";
Query q = QueryFactory.create(query, Syntax.syntaxARQ);
Run Code Online (Sandbox Code Playgroud)
我想使用ElementWalker并在查询中添加三元组,如下所示:
给出一个查询
SELECT * WHERE {
?s ?p ?o;
?p2 ?o2.
?s2 ?p3 ?o3.
}
Run Code Online (Sandbox Code Playgroud)
我希望添加一个三元组查询看起来像:
SELECT * WHERE {
?s ?p ?o;
?p2 ?o2.
-->?s <some_url> "value". //added
?s2 ?p3 ?o3.
}
Run Code Online (Sandbox Code Playgroud)
我知道有很多方法可以添加到顶层(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);
}
});
Run Code Online (Sandbox Code Playgroud)
上面的代码的问题是它成功地将三元组添加到ElementPathBlock(el),但更改不会延续到查询本身.我希望在查询中访问此特定的ElementPathBlock时成功修改查询.
任何帮助赞赏.
此代码使用walker ?d ?d ?d在主题所在的任何三元组之后添加?d.由于查询中包含主题的多个三元组,因此会插入?d多个?d ?d ?dget 实例,并保留更改.
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 );
}
}
Run Code Online (Sandbox Code Playgroud)
输出是:
== 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
}
}
Run Code Online (Sandbox Code Playgroud)