在不使用jena中的输入输出的情况下更改uri
change uri without use input output in jena
下面是我的简单 sparql 查询。我想更改我的 sparql 查询 uri,但我不知道如何在不使用 input/output.
的情况下更改此特定内容
输入字符串:
PREFIX ab: <http://learningsparql.com/ns/addressbook#>
SELECT ?craigEmail
WHERE
{ ab:craig ab:email ?craigEmail . }
输出字符串:
PREFIX ab: <http://example.com#>
SELECT ?craigEmail
WHERE
{ ab:craig ab:email ?craigEmail . }
你能给我提供jena中这个简单东西的代码吗...
您可以使用 Jena ARQ API 并通过迭代 SPARQL 查询的三重模式元素以编程方式替换前缀 ab
的命名空间:
import org.apache.jena.graph.Node;
import org.apache.jena.graph.NodeFactory;
import org.apache.jena.graph.Triple;
import org.apache.jena.query.Query;
import org.apache.jena.query.QueryFactory;
import org.apache.jena.sparql.core.TriplePath;
import org.apache.jena.sparql.syntax.ElementPathBlock;
import org.apache.jena.sparql.syntax.ElementTriplesBlock;
import org.apache.jena.sparql.syntax.ElementVisitorBase;
import org.apache.jena.sparql.syntax.ElementWalker;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class Test {
public static void main(String[] args) {
Query query = QueryFactory.create("" +
"PREFIX ab: <http://learningsparql.com/ns/addressbook#> " +
"SELECT ?craigEmail WHERE { ab:craig ab:email ?craigEmail . }");
// This will walk through all parts of the query// This will walk through all parts of the query
final String ns = "http://example.com#";
ElementWalker.walk(query.getQueryPattern(), new ElementVisitorBase() { // ...when it's a block of triples...
public void visit(ElementPathBlock el){ // ...go through all the triples...
Iterator<TriplePath> triples = el.patternElts();
List<Triple> newTriples = new ArrayList<>();
while (triples.hasNext()) {
TriplePath triple = triples.next();
if(triple.isTriple()) {
Node s = triple.getSubject();
if(s.isURI()) {
String localName = s.getLocalName();
s = NodeFactory.createURI(ns + localName);
}
Node p = triple.getPredicate();
if(p.isURI()) {
String localName = p.getLocalName();
p = NodeFactory.createURI(ns + localName);
}
Node o = triple.getSubject();
if(o.isURI()) {
String localName = o.getLocalName();
o = NodeFactory.createURI(ns + localName);
}
newTriples.add(Triple.create(s, p, o));
} else {
// TODO handle triple path
}
triples.remove();
}
for(Triple t : newTriples) {
el.addTriple(t);
}
}
@Override
public void visit(ElementTriplesBlock el) {
Iterator<Triple> triples = el.patternElts();
List<Triple> newTriples = new ArrayList<>();
while (triples.hasNext()) {
Triple triple = triples.next();
Node s = triple.getSubject();
if(s.isURI()) {
String localName = s.getLocalName();
s = NodeFactory.createURI(ns + localName);
}
Node p = triple.getPredicate();
if(p.isURI()) {
String localName = p.getLocalName();
p = NodeFactory.createURI(ns + localName);
}
Node o = triple.getSubject();
if(o.isURI()) {
String localName = o.getLocalName();
o = NodeFactory.createURI(ns + localName);
}
newTriples.add(Triple.create(s, p, o));
triples.remove();
}
for(Triple t : newTriples) {
el.addTriple(t);
}
}
});
query.setPrefix("ab", ns);
System.out.println(query);
}
}
下面是我的简单 sparql 查询。我想更改我的 sparql 查询 uri,但我不知道如何在不使用 input/output.
的情况下更改此特定内容输入字符串:
PREFIX ab: <http://learningsparql.com/ns/addressbook#>
SELECT ?craigEmail
WHERE
{ ab:craig ab:email ?craigEmail . }
输出字符串:
PREFIX ab: <http://example.com#>
SELECT ?craigEmail
WHERE
{ ab:craig ab:email ?craigEmail . }
你能给我提供jena中这个简单东西的代码吗...
您可以使用 Jena ARQ API 并通过迭代 SPARQL 查询的三重模式元素以编程方式替换前缀 ab
的命名空间:
import org.apache.jena.graph.Node;
import org.apache.jena.graph.NodeFactory;
import org.apache.jena.graph.Triple;
import org.apache.jena.query.Query;
import org.apache.jena.query.QueryFactory;
import org.apache.jena.sparql.core.TriplePath;
import org.apache.jena.sparql.syntax.ElementPathBlock;
import org.apache.jena.sparql.syntax.ElementTriplesBlock;
import org.apache.jena.sparql.syntax.ElementVisitorBase;
import org.apache.jena.sparql.syntax.ElementWalker;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class Test {
public static void main(String[] args) {
Query query = QueryFactory.create("" +
"PREFIX ab: <http://learningsparql.com/ns/addressbook#> " +
"SELECT ?craigEmail WHERE { ab:craig ab:email ?craigEmail . }");
// This will walk through all parts of the query// This will walk through all parts of the query
final String ns = "http://example.com#";
ElementWalker.walk(query.getQueryPattern(), new ElementVisitorBase() { // ...when it's a block of triples...
public void visit(ElementPathBlock el){ // ...go through all the triples...
Iterator<TriplePath> triples = el.patternElts();
List<Triple> newTriples = new ArrayList<>();
while (triples.hasNext()) {
TriplePath triple = triples.next();
if(triple.isTriple()) {
Node s = triple.getSubject();
if(s.isURI()) {
String localName = s.getLocalName();
s = NodeFactory.createURI(ns + localName);
}
Node p = triple.getPredicate();
if(p.isURI()) {
String localName = p.getLocalName();
p = NodeFactory.createURI(ns + localName);
}
Node o = triple.getSubject();
if(o.isURI()) {
String localName = o.getLocalName();
o = NodeFactory.createURI(ns + localName);
}
newTriples.add(Triple.create(s, p, o));
} else {
// TODO handle triple path
}
triples.remove();
}
for(Triple t : newTriples) {
el.addTriple(t);
}
}
@Override
public void visit(ElementTriplesBlock el) {
Iterator<Triple> triples = el.patternElts();
List<Triple> newTriples = new ArrayList<>();
while (triples.hasNext()) {
Triple triple = triples.next();
Node s = triple.getSubject();
if(s.isURI()) {
String localName = s.getLocalName();
s = NodeFactory.createURI(ns + localName);
}
Node p = triple.getPredicate();
if(p.isURI()) {
String localName = p.getLocalName();
p = NodeFactory.createURI(ns + localName);
}
Node o = triple.getSubject();
if(o.isURI()) {
String localName = o.getLocalName();
o = NodeFactory.createURI(ns + localName);
}
newTriples.add(Triple.create(s, p, o));
triples.remove();
}
for(Triple t : newTriples) {
el.addTriple(t);
}
}
});
query.setPrefix("ab", ns);
System.out.println(query);
}
}