AGSparqlSetWithVariables.java
package com.franz.agbase.examples;
import com.franz.agbase.AllegroGraphException;
import com.franz.agbase.AllegroGraph;
import com.franz.agbase.AllegroGraphConnection;
import com.franz.agbase.BlankNode;
import com.franz.agbase.LiteralNode;
import com.franz.agbase.SPARQLQuery;
import com.franz.agbase.ValueSetIterator;
public class AGSparqlSetWithVariables {
@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("ex", "http://example.org/");
BlankNode a = (BlankNode)ts.createBNode("_:a");
BlankNode b = (BlankNode)ts.createBNode("_:b");
LiteralNode aliceName = ts.createLiteral("Alice");
ts.addStatement(a,"!ex:employeeName", aliceName);
ts.addStatement(a,"!ex:employeeId",ts.createLiteral(12345));
ts.addStatement(b,"!ex:employeeName",ts.createLiteral("Bob"));
ts.addStatement(b,"!ex:employeeId",ts.createLiteral(67890));
String query =
"PREFIX ex: <http://example.org/> " +
"SELECT ?s ?o " +
"WHERE { ?s ex:employeeName ?o }";
SPARQLQuery sq = new SPARQLQuery();
sq.setTripleStore(ts);
sq.setQuery(query);
ValueSetIterator it = sq.select();
AGUtils.showResults(it);
Object[] varvals = new Object[2];
varvals[0] = "o";
varvals[1] = aliceName;
sq.setWithVariables(ts, varvals);
it = sq.select();
AGUtils.showResults(it);
ts.closeTripleStore();
ags.disable();
}
}
Up |
Next