本文整理匯總了Java中org.openrdf.query.TupleQuery類的典型用法代碼示例。如果您正苦於以下問題:Java TupleQuery類的具體用法?Java TupleQuery怎麽用?Java TupleQuery使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
TupleQuery類屬於org.openrdf.query包,在下文中一共展示了TupleQuery類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: before
import org.openrdf.query.TupleQuery; //導入依賴的package包/類
@Before
public void before()
throws RepositoryException, IOException, RDFParseException,
MalformedQueryException, QueryResultParseException,
QueryResultHandlerException {
repo = new SailRepository(new MemoryStore());
repo.initialize();
conn = repo.getConnection();
vf = conn.getValueFactory();
conn.add(getResource(data), "file://", RDFFormat.TURTLE);
SPARQLResultsXMLParserFactory factory =
new SPARQLResultsXMLParserFactory();
parser = factory.getParser();
parser.setValueFactory(vf);
List<Rule> rules;
rules = Rules.fromOntology(getResource(data));
QueryRewriter rewriter = new QueryRewriter(conn, rules);
query = (TupleQuery) rewriter.rewrite(QueryLanguage.SPARQL, queryString);
nonInfQuery = conn.prepareTupleQuery(QueryLanguage.SPARQL, queryString);
System.out.println("== QUERY (" + this.name + ") ==");
System.out.println(nonInfQuery);
System.out.println("== REWRITTEN QUERY (" + this.name + ") ==");
System.out.println(query);
}
示例2: getDistinctObj
import org.openrdf.query.TupleQuery; //導入依賴的package包/類
/**
* Get total number of distinct objects for a predicate
* @param pred Predicate
* @param m model
* @return triples
*/
public static long getDistinctObj(String pred, String endpoint) throws Exception {
String strQuery = "SELECT (COUNT(DISTINCT ?o) AS ?objs) " + //
"WHERE " +
"{" +
"?s <" + pred + "> ?o " +
"} " ;
SPARQLRepository repo = new SPARQLRepository(endpoint);
repo.initialize();
RepositoryConnection conn = repo.getConnection();
try {
TupleQuery query = conn.prepareTupleQuery(QueryLanguage.SPARQL, strQuery);
TupleQueryResult rs = query.evaluate();
return Long.parseLong(rs.next().getValue("objs").stringValue());
} finally {
conn.close();
repo.shutDown();
}
}
示例3: getDistinctSbj
import org.openrdf.query.TupleQuery; //導入依賴的package包/類
/**
* Get total number of distinct objects for a predicate
* @param pred Predicate
* @param m model
* @return triples
*/
public static long getDistinctSbj(String pred, String endpoint) throws Exception {
String strQuery = "SELECT (COUNT(DISTINCT ?s) AS ?subjs) " + //
"WHERE " +
"{" +
"?s <" + pred + "> ?o " +
"} " ;
SPARQLRepository repo = new SPARQLRepository(endpoint);
repo.initialize();
RepositoryConnection conn = repo.getConnection();
try {
TupleQuery query = conn.prepareTupleQuery(QueryLanguage.SPARQL, strQuery);
TupleQueryResult rs = query.evaluate();
return Long.parseLong(rs.next().getValue("subjs").stringValue());
} finally {
conn.close();
repo.shutDown();
}
}
示例4: getSubjectCount
import org.openrdf.query.TupleQuery; //導入依賴的package包/類
/**
* Get total number of distinct subjects of a dataset
* @return count
*/
public static long getSubjectCount(String endpoint) throws Exception {
String strQuery = "SELECT (COUNT(DISTINCT ?s) AS ?sbjts) " + //
"WHERE " +
"{" +
"?s ?p ?o " +
"} " ;
SPARQLRepository repo = new SPARQLRepository(endpoint);
repo.initialize();
RepositoryConnection conn = repo.getConnection();
try {
TupleQuery query = conn.prepareTupleQuery(QueryLanguage.SPARQL, strQuery);
TupleQueryResult rs = query.evaluate();
return Long.parseLong(rs.next().getValue("sbjts").stringValue());
} finally {
conn.close();
repo.shutDown();
}
}
示例5: getObjectCount
import org.openrdf.query.TupleQuery; //導入依賴的package包/類
/**
* Get total number of distinct objects of a dataset
* @return count
*/
public static long getObjectCount(String endpoint) throws Exception {
String strQuery = "SELECT (COUNT(DISTINCT ?o) AS ?objts) " + //
"WHERE " +
"{" +
"?s ?p ?o " +
"} " ;
SPARQLRepository repo = new SPARQLRepository(endpoint);
repo.initialize();
RepositoryConnection conn = repo.getConnection();
try {
TupleQuery query = conn.prepareTupleQuery(QueryLanguage.SPARQL, strQuery);
TupleQueryResult rs = query.evaluate();
return Long.parseLong(rs.next().getValue("objts").stringValue());
} finally {
conn.close();
repo.shutDown();
}
}
示例6: getTripleCount
import org.openrdf.query.TupleQuery; //導入依賴的package包/類
/**
* Get total number of triple for a predicate
* @param pred Predicate
* @param m model
* @return triples
*/
public static Long getTripleCount(String pred, String endpoint) throws Exception {
String strQuery = "SELECT (COUNT(?s) AS ?triples) " + //
"WHERE " +
"{" +
"?s <"+pred+"> ?o " +
"} " ;
SPARQLRepository repo = new SPARQLRepository(endpoint);
repo.initialize();
RepositoryConnection conn = repo.getConnection();
try {
TupleQuery query = conn.prepareTupleQuery(QueryLanguage.SPARQL, strQuery);
TupleQueryResult rs = query.evaluate();
return Long.parseLong(rs.next().getValue("triples").stringValue());
} finally {
conn.close();
repo.shutDown();
}
}
示例7: getPredicates
import org.openrdf.query.TupleQuery; //導入依賴的package包/類
/**
* Get Predicate List
* @param endPointUrl SPARQL endPoint Url
* @param graph Named graph
* @return predLst Predicates List
*/
private static List<String> getPredicates(String endPointUrl, String graph) throws Exception
{
List<String> predLst = new ArrayList<String>();
String strQuery = getPredQuery(graph);
SPARQLRepository repo = new SPARQLRepository(endPointUrl);
repo.initialize();
RepositoryConnection conn = repo.getConnection();
try {
TupleQuery query = conn.prepareTupleQuery(QueryLanguage.SPARQL, strQuery);
TupleQueryResult res = query.evaluate();
while (res.hasNext())
{
String pred = res.next().getValue("p").toString();
predLst.add(pred);
}
} finally {
conn.close();
repo.shutDown();
}
return predLst;
}
示例8: runSPARQL
import org.openrdf.query.TupleQuery; //導入依賴的package包/類
/**
* Execute a SELECT SPARQL query against the graph
*
* @param qs
* SELECT SPARQL query
* @return list of solutions, each containing a hashmap of bindings
*/
public List runSPARQL(String qs) {
try {
RepositoryConnection con = therepository.getConnection();
try {
TupleQuery query = con.prepareTupleQuery(org.openrdf.query.QueryLanguage.SPARQL, qs);
TupleQueryResult qres = query.evaluate();
ArrayList reslist = new ArrayList();
while (qres.hasNext()) {
BindingSet b = qres.next();
Set names = b.getBindingNames();
HashMap hm = new HashMap();
for (Object n : names) {
hm.put((String) n, b.getValue((String) n));
}
reslist.add(hm);
}
return reslist;
} finally {
con.close();
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
示例9: doTupleQuery
import org.openrdf.query.TupleQuery; //導入依賴的package包/類
public static List<HashMap<String, Value>> doTupleQuery(RepositoryConnection con, String query) {
List<HashMap<String, Value>> results = new ArrayList<HashMap<String, Value>>();
try {
TupleQuery resultsTable = con.prepareTupleQuery(QueryLanguage.SPARQL, query);
TupleQueryResult bindings = resultsTable.evaluate();
for (@SuppressWarnings("unused")
int row = 0; bindings.hasNext(); row++) {
HashMap<String, Value> riga = new HashMap<String, Value>();
BindingSet pairs = bindings.next();
List<String> names = bindings.getBindingNames();
for (int i = 0; i < names.size(); i++) {
String name = names.get(i);
Value value = pairs.getValue(name);
riga.put(name, value != null && value.stringValue().equals("") ? null : value);
}
results.add(riga);
}
} catch (Exception e) {
e.printStackTrace();
}
return results;
}
示例10: init
import org.openrdf.query.TupleQuery; //導入依賴的package包/類
private void init(RepositoryConnection conn, String queryName) throws IOException, OpenRDFException {
trails = new ArrayList<>();
File queryFile = NanolyticsUtils.getProvViewFile(queryName);
varNames = new ArrayList<>();
String queryString = NanolyticsUtils.getQuery(queryFile, varNames);
varNames = ImmutableList.copyOf(varNames);
TupleQuery query = conn.prepareTupleQuery(QueryLanguage.SPARQL, queryString);
TupleQueryResult result = query.evaluate();
while (result.hasNext()) {
BindingSet bs = result.next();
List<URI> uris = new ArrayList<>();
for (String n : varNames) {
if (bs.hasBinding(n)) {
Value v = bs.getBinding(n).getValue();
if (!(v instanceof URI)) {
throw new RuntimeException("Not a URI: " + v);
}
uris.add((URI) v);
} else {
uris.add(null);
}
}
trails.add(new ProvTrail(uris));
}
trails = ImmutableList.copyOf(trails);
}
示例11: init
import org.openrdf.query.TupleQuery; //導入依賴的package包/類
private void init(RepositoryConnection conn, String queryName) throws IOException, OpenRDFException {
metrics = new HashMap<String,Object>();
File queryFile = NanolyticsUtils.getProvMetricFile(queryName);
varNames = new ArrayList<>();
String queryString = NanolyticsUtils.getQuery(queryFile, varNames);
varNames = ImmutableList.copyOf(varNames);
TupleQuery query = conn.prepareTupleQuery(QueryLanguage.SPARQL, queryString);
TupleQueryResult result = query.evaluate();
if (!result.hasNext()) {
throw new RuntimeException("Metric calculation failed");
}
BindingSet bs = result.next();
if (result.hasNext()) {
throw new RuntimeException("Metric calculation led to multiple results");
}
for (String n : varNames) {
if (bs.hasBinding(n)) {
metrics.put(n, bs.getBinding(n).getValue());
}
}
}
示例12: testWithoutSubquery
import org.openrdf.query.TupleQuery; //導入依賴的package包/類
/**
* This test works. The expected result is 6 rows ranging from "Model1Class 1" through "Model1Class 6".
*
* @throws RepositoryException
* @throws QueryEvaluationException
* @throws TupleQueryResultHandlerException
*
* @throws MalformedQueryException
*/
public void testWithoutSubquery() throws RepositoryException, QueryEvaluationException, TupleQueryResultHandlerException, MalformedQueryException {
final String query = "SELECT ?i ?i_label ?i_class ?i_v1"
+ "WHERE {"
+ "?i <http://www.w3.org/2000/01/rdf-schema#label> ?i_label ."
+ "?i a ?i_class ."
+ "?i_class <http://www.w3.org/2000/01/rdf-schema#subClassOf>* <http://dragon-research.com/cham/model/model1#Model1Class> ."
+ "OPTIONAL { ?i <http://dragon-research.com/cham/model/model1#name> ?i_v1 } ."
+ "}"
+ "ORDER BY ?i_label";
final RepositoryConnection conn = repository.getConnection();
final TupleQuery tupleQuery = conn.prepareTupleQuery(QueryLanguage.SPARQL, query);
RdfCloudTripleStoreConnectionTest.CountTupleHandler countTupleHandler = new RdfCloudTripleStoreConnectionTest.CountTupleHandler();
tupleQuery.evaluate(countTupleHandler);
assertEquals(6, countTupleHandler.getCount());
conn.close();
}
示例13: testEvaluate
import org.openrdf.query.TupleQuery; //導入依賴的package包/類
public void testEvaluate() throws Exception {
RepositoryConnection conn = repository.getConnection();
URI loadPerc = vf.createURI(litdupsNS, "loadPerc");
URI uri1 = vf.createURI(litdupsNS, "uri1");
conn.add(cpu, loadPerc, uri1);
conn.commit();
String query = "select * where {" +
"?x <" + loadPerc.stringValue() + "> ?o1." +
"}";
TupleQuery tupleQuery = conn.prepareTupleQuery(QueryLanguage.SPARQL, query);
CountTupleHandler cth = new CountTupleHandler();
tupleQuery.evaluate(cth);
assertEquals(cth.getCount(), 1);
conn.close();
}
示例14: testPOObjRange
import org.openrdf.query.TupleQuery; //導入依賴的package包/類
public void testPOObjRange() throws Exception {
RepositoryConnection conn = repository.getConnection();
URI loadPerc = vf.createURI(litdupsNS, "loadPerc");
Literal six = vf.createLiteral("6");
Literal sev = vf.createLiteral("7");
Literal ten = vf.createLiteral("10");
conn.add(cpu, loadPerc, six);
conn.add(cpu, loadPerc, sev);
conn.add(cpu, loadPerc, ten);
conn.commit();
String query = "PREFIX org.apache: <" + NAMESPACE + ">\n" +
"select * where {" +
"?x <" + loadPerc.stringValue() + "> ?o.\n" +
"FILTER(org.apache:range(?o, '6', '8'))." +
"}";
TupleQuery tupleQuery = conn.prepareTupleQuery(QueryLanguage.SPARQL, query);
CountTupleHandler cth = new CountTupleHandler();
tupleQuery.evaluate(cth);
conn.close();
assertEquals(2, cth.getCount());
}
示例15: testPOPredRange
import org.openrdf.query.TupleQuery; //導入依賴的package包/類
public void testPOPredRange() throws Exception {
RepositoryConnection conn = repository.getConnection();
URI loadPerc = vf.createURI(litdupsNS, "loadPerc1");
URI loadPerc2 = vf.createURI(litdupsNS, "loadPerc2");
URI loadPerc3 = vf.createURI(litdupsNS, "loadPerc3");
URI loadPerc4 = vf.createURI(litdupsNS, "loadPerc4");
Literal six = vf.createLiteral("6");
Literal sev = vf.createLiteral("7");
Literal ten = vf.createLiteral("10");
conn.add(cpu, loadPerc, six);
conn.add(cpu, loadPerc2, sev);
conn.add(cpu, loadPerc4, ten);
conn.commit();
String query = "PREFIX org.apache: <" + NAMESPACE + ">\n" +
"select * where {" +
"?x ?p ?o.\n" +
"FILTER(org.apache:range(?p, <" + loadPerc.stringValue() + ">, <" + loadPerc3.stringValue() + ">))." +
"}";
TupleQuery tupleQuery = conn.prepareTupleQuery(QueryLanguage.SPARQL, query);
CountTupleHandler cth = new CountTupleHandler();
tupleQuery.evaluate(cth);
conn.close();
assertEquals(cth.getCount(), 2);
}