AGSparqlOptionals.java
package com.franz.ag.examples;
import com.franz.ag.*;
public class AGSparqlOptionals {
@param
@throws
public static void main(String[] args) throws AllegroGraphException {
AllegroGraphConnection ags = new AllegroGraphConnection();
try {
ags.enable();
} catch (Exception e) {
throw new AllegroGraphException("Server connection problem", e);
}
AllegroGraph ts = ags.renew("sparql", AGPaths.TRIPLE_STORES);
ts.registerNamespace("foaf", "http://xmlns.com/foaf/0.1/");
BlankNode a = (BlankNode)ts.createBNode("_:a");
BlankNode b = (BlankNode)ts.createBNode("_:b");
ts.addStatement(a,"!rdf:type","!foaf:Person");
ts.addStatement(a,"!foaf:name",ts.createLiteral("Alice"));
ts.addStatement(a,"!foaf:mbox","<mailto:[email protected]>");
ts.addStatement(a,"!foaf:mbox","<mailto:[email protected]>");
ts.addStatement(b,"!rdf:type","!foaf:Person");
ts.addStatement(b,"!foaf:name",ts.createLiteral("Bob"));
ts.addStatement(b,"!foaf:homepage","<http://work.example.org/alice/>");
String query =
"PREFIX foaf: <http://xmlns.com/foaf/0.1/> " +
"SELECT ?name ?mbox " +
"WHERE { ?x foaf:name ?name . " +
"OPTIONAL { ?x foaf:mbox ?mbox } " +
"}";
AGUtils.doSparqlSelect(ts, query);
query =
"PREFIX foaf: <http://xmlns.com/foaf/0.1/> " +
"SELECT ?name ?mbox ?hpage " +
"WHERE { ?x foaf:name ?name . " +
"OPTIONAL { ?x foaf:mbox ?mbox } . " +
"OPTIONAL { ?x foaf:homepage ?hpage } " +
"}";
AGUtils.doSparqlSelect(ts, query);
ts.closeTripleStore();
ags.disable();
}
}
Up |
Next