本文整理汇总了Java中org.openrdf.query.Query类的典型用法代码示例。如果您正苦于以下问题:Java Query类的具体用法?Java Query怎么用?Java Query使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
Query类属于org.openrdf.query包,在下文中一共展示了Query类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: rewrite
import org.openrdf.query.Query; //导入依赖的package包/类
/**
* Rewrite a given query to add inference.
*
* @param ql the query language used for the query
* @param query the query to rewrite
* @param baseuri a base URI to use for the query
* @return rewritten query that includes inference
* @throws MalformedQueryException if the query is malformed
* @throws RepositoryException if there was a problem while rewriting
*/
public Query rewrite(QueryLanguage ql, String query, String baseuri)
throws MalformedQueryException, RepositoryException {
// parse query using Sesame
QueryParserFactory f = QueryParserRegistry.getInstance().get(ql);
QueryParser parser = f.getParser();
ParsedQuery parsed = parser.parseQuery(query, baseuri);
// get SPARQL algebra expression from parsed query
TupleExpr expr = parsed.getTupleExpr();
// rewrite query using visitor pattern
RuleTransformationVisitor visitor
= new RuleTransformationVisitor(rules);
expr.visit(visitor);
// return new query based on rewritten algebra expression
return getExprQuery(parsed, expr);
}
示例2: testPrepareQuery4
import org.openrdf.query.Query; //导入依赖的package包/类
@Test
public void testPrepareQuery4() throws Exception{
URL url = MarkLogicRepositoryConnectionTest.class.getResource(TEST_DIR_PREFIX+"tigers.ttl");
testAdminCon.add(url, "", RDFFormat.TURTLE);
Assert.assertEquals(107L, testAdminCon.size());
String query1 = "ASK "+
"WHERE"+
"{"+
" ?s <#position> ?o."+
"}";
Query bq = testAdminCon.prepareQuery(query1, "http://marklogic.com/baseball/players");
bq.setBinding("o", vf.createLiteral("pitcher"));
boolean result1 = ((BooleanQuery)bq).evaluate();
Assert.assertTrue(result1);
}
示例3: getQueryType
import org.openrdf.query.Query; //导入依赖的package包/类
@Override
public QueryType getQueryType(String query) {
SailRepositoryConnection connection;
try {
connection = sr.getConnection();
final Query prepareQuery = connection.prepareQuery(
QueryLanguage.SPARQL, query);
if (prepareQuery instanceof BooleanQuery)
return QueryType.BOOLEANQUERY;
if (prepareQuery instanceof GraphQuery)
return QueryType.CONSTRUCTQUERY;
if (prepareQuery instanceof TupleQuery)
return QueryType.TUPLEQUERY;
} catch (RepositoryException | MalformedQueryException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return QueryType.DESCRIBEQUERY;
}
示例4: prepareQuery
import org.openrdf.query.Query; //导入依赖的package包/类
@Override
public Query prepareQuery(QueryLanguage ql, String query, String baseURI)
throws RepositoryException, MalformedQueryException
{
validateQueryLanguage(ql);
ParsedQuery parsedQuery = QueryParserUtil.parseQuery(QueryLanguage.SPARQL, query, baseURI);
if (parsedQuery instanceof ParsedTupleQuery) {
return prepareTupleQuery(ql, query, baseURI);
}
else if (parsedQuery instanceof ParsedBooleanQuery) {
return prepareBooleanQuery(ql, query, baseURI);
}
else if (parsedQuery instanceof ParsedGraphQuery) {
return prepareGraphQuery(ql, query, baseURI);
}
throw new MalformedQueryException("Unrecognized query type:\n" + query); //$NON-NLS-1$
}
示例5: contextualize
import org.openrdf.query.Query; //导入依赖的package包/类
private void contextualize(Query query) throws Exception {
if (contextVar == null) {
return;
}
TupleExpr tupleExpr = ((SailQuery) query).getParsedQuery().getTupleExpr();
tupleExpr.visit(new QueryModelVisitorBase() {
public void meet(StatementPattern node) throws Exception {
if (node.getContextVar() != null) {
throw new UnsupportedSPARQLStatementException("Attempted to execute a SPARQL statement with a GRAPH clause against a context aware graph.");
}
node.setContextVar(contextVar);
}
});
}
示例6: getExprQuery
import org.openrdf.query.Query; //导入依赖的package包/类
/**
* Creates a new query based on a tuple expression and original query. The
* new query will have the same type null
* ({@link org.openrdf.query.TupleQuery},
* {@link org.openrdf.query.GraphQuery} or
* {@link org.openrdf.query.BooleanQuery}) as the given original query.
*
* @param orig the original query
* @param expr the expression used for the new query
* @return new query based on expression
*/
protected Query getExprQuery(ParsedQuery orig, TupleExpr expr) {
if (orig instanceof ParsedTupleQuery) {
return new SailTupleExprQuery(
new ParsedTupleQuery(expr), conn);
} else if (orig instanceof ParsedGraphQuery) {
return new SailGraphExprQuery(
new ParsedGraphQuery(expr), conn);
} else {
return new SailBooleanExprQuery(
new ParsedBooleanQuery(expr), conn);
}
}
示例7: evaluate
import org.openrdf.query.Query; //导入依赖的package包/类
public List<List<Object>> evaluate(String queries) throws Exception {
List<List<Object>> report = new ArrayList<List<Object>>();
List<String> qnames = Arrays.asList(queries.split(" "));
for (String curQueryName : qnames)
{
List<Object> reportRow = new ArrayList<Object>();
report.add(reportRow);
String curQuery = qp.getQuery(curQueryName);
reportRow.add(curQueryName);
long startTime = System.currentTimeMillis();
//ParsedOperation pO = QueryParserUtil.parseOperation(QueryLanguage.SPARQL, curQuery, null);
RepositoryConnection repCon = this.repository.getConnection();
try {
Query tempq = repCon.prepareQuery(QueryLanguage.SPARQL, curQuery);
TupleQuery q = (TupleQuery)tempq;
SyncTupleQueryResultHandler rhandler = new SyncTupleQueryResultHandler();
q.evaluate(rhandler);
long runTime = System.currentTimeMillis() - startTime;
reportRow.add((Long)rhandler.resultCount); reportRow.add((Long)runTime);
log.info(curQueryName + ": Query exection time (msec): "+ runTime + ", Total Number of Records: " + rhandler.resultCount);
} catch (Exception e) {
reportRow.add(null); reportRow.add(null);
} finally {
repCon.close();
}
}
return report;
}
示例8: testSPARQLQueryWithPrepareQuery
import org.openrdf.query.Query; //导入依赖的package包/类
@Test
public void testSPARQLQueryWithPrepareQuery()
throws Exception {
String queryString = "select ?s ?p ?o { ?s ?p ?o } limit 1 ";
Query q = conn.prepareQuery(QueryLanguage.SPARQL, queryString);
try {
if (q instanceof TupleQuery) {
TupleQueryResult result = ((TupleQuery) q).evaluate();
while (result.hasNext()) {
BindingSet tuple = result.next();
Assert.assertEquals("s", tuple.getBinding("s").getName());
Assert.assertEquals("http://semanticbible.org/ns/2006/NTNames#AttaliaGeodata", tuple.getBinding("s").getValue().stringValue());
}
result.close();
}
}
catch(Exception ex)
{
throw ex;
}
finally {
conn.close();
Thread.sleep(1000);
}
}
示例9: testPrepareQuery2
import org.openrdf.query.Query; //导入依赖的package包/类
@Test
public void testPrepareQuery2() throws Exception{
Reader ir = new BufferedReader(new InputStreamReader(MarkLogicRepositoryConnectionTest.class.getResourceAsStream(TEST_DIR_PREFIX + "property-paths.ttl")));
testAdminCon.add(ir, "", RDFFormat.TURTLE, null);
StringBuilder queryBuilder = new StringBuilder(128);
queryBuilder.append(" prefix : <http://learningsparql.com/ns/papers#> ");
queryBuilder.append(" prefix c: <http://learningsparql.com/ns/citations#>");
queryBuilder.append(" SELECT ?s");
queryBuilder.append(" WHERE { ");
queryBuilder.append(" ?s ^c:cites :paperK2 . ");
queryBuilder.append(" FILTER (?s != :paperK2)");
queryBuilder.append(" } ");
queryBuilder.append(" ORDER BY ?s ");
Query query = testAdminCon.prepareQuery(queryBuilder.toString());
query.setBinding("whatcode", vf.createLiteral("33333"));
TupleQueryResult result = null;
if (query instanceof TupleQuery) {
result = ((TupleQuery) query).evaluate();
}
try {
assertThat(result, is(notNullValue()));
while (result.hasNext()) {
BindingSet solution = result.next();
assertThat(solution.hasBinding("s"), is(equalTo(true)));
Value totalResult = solution.getValue("s");
Assert.assertEquals(vf.createURI("http://learningsparql.com/ns/papers#paperJ"),totalResult);
}
}
finally {
result.close();
}
}
示例10: sparqlEndpoint
import org.openrdf.query.Query; //导入依赖的package包/类
@RequestMapping(value = "/sparql")
public ModelAndView sparqlEndpoint(@RequestParam(value = "query", required = true) String queryStr, @RequestParam(value = "output", required = false) String output, HttpServletRequest request,
HttpServletResponse response) throws Exception {
if (queryStr != null) {
synchronized (this) {
Query query = sparqlService.getQuery(queryStr);
SparqlQueryType queryType = SparqlQueryType.getQueryType(query);
Object queryResult = sparqlService.evaluateQuery(queryStr);
View view = getView(queryType);
FileFormatServiceRegistry<? extends FileFormat, ?> registry = getRegistryInstance(queryType);
Object factory = ProtocolUtil.getAcceptableService(request, response, registry);
Map<String, Object> model = new HashMap<String, Object>();
model.put(QueryResultView.FILENAME_HINT_KEY, "query-result");
model.put(QueryResultView.QUERY_RESULT_KEY, queryResult);
model.put(QueryResultView.FACTORY_KEY, factory);
model.put(QueryResultView.HEADERS_ONLY, false);
return new ModelAndView(view, model);
}
} else {
throw new SparqlTutorialException("Missing parameter: ");
}
}
示例11: prepareQuery
import org.openrdf.query.Query; //导入依赖的package包/类
@Override
public Query prepareQuery(String sparqlQuery) {
try {
return conn.prepareQuery(QueryLanguage.SPARQL, sparqlQuery);
} catch (RepositoryException | MalformedQueryException e) {
e.printStackTrace();
throw new SparqlTutorialException(e);
}
}
示例12: performNodeBindings
import org.openrdf.query.Query; //导入依赖的package包/类
private void performNodeBindings(final Map<String, Node> bindings, final Query query) {
ValueFactory valueFactory = ValueFactoryImpl.getInstance();
for (String i : bindings.keySet()) {
Node n = bindings.get(i);
if (n != null) {
query.setBinding(i, getSesameValue(n, valueFactory));
}
}
}
示例13: renderTupleQuery
import org.openrdf.query.Query; //导入依赖的package包/类
private void renderTupleQuery(Query query, TupleQueryResultWriter writer) throws QueryEvaluationException, TupleQueryResultHandlerException {
TupleQueryResult tupleQueryResult = ((TupleQuery) query).evaluate();
writer.startQueryResult(tupleQueryResult.getBindingNames());
while (tupleQueryResult.hasNext()) {
writer.handleSolution(tupleQueryResult.next());
}
writer.endQueryResult();
}
示例14: setMaxQueryTime
import org.openrdf.query.Query; //导入依赖的package包/类
/**
* Utility function to set the Query timeout to the global
* setting if it is configured.
*/
protected void setMaxQueryTime(final Query query) {
if (maxQueryTime > 0) {
query.setMaxQueryTime(maxQueryTime);
}
}
示例15: testPrepareQuery1
import org.openrdf.query.Query; //导入依赖的package包/类
@Test
public void testPrepareQuery1() throws Exception {
testAdminCon.add(MarkLogicRepositoryConnectionTest.class.getResourceAsStream(TEST_DIR_PREFIX + "companies_100.ttl"), "",
RDFFormat.TURTLE, null);
Assert.assertEquals(testAdminCon.size(), 1600L);
StringBuilder queryBuilder = new StringBuilder(128);
queryBuilder.append("PREFIX demor: <http://demo/resource#>");
queryBuilder.append(" PREFIX demov: <http://demo/verb#>");
queryBuilder.append(" PREFIX vcard: <http://www.w3.org/2006/vcard/ns#>");
queryBuilder.append(" SELECT (COUNT(?company) AS ?total)");
queryBuilder.append(" WHERE { ");
queryBuilder.append(" ?company a vcard:Organization .");
queryBuilder.append(" ?company demov:industry ?industry .");
queryBuilder.append(" ?company vcard:hasAddress/vcard:postal-code ?zip .");
queryBuilder.append(" ?company vcard:hasAddress/vcard:postal-code ?whatcode ");
queryBuilder.append(" } ");
Query query = testAdminCon.prepareQuery(QueryLanguage.SPARQL, queryBuilder.toString());
query.setBinding("whatcode", vf.createLiteral("33333"));
TupleQueryResult result = null;
if (query instanceof TupleQuery) {
result = ((TupleQuery) query).evaluate();
}
try {
assertThat(result, is(notNullValue()));
while (result.hasNext()) {
BindingSet solution = result.next();
assertThat(solution.hasBinding("total"), is(equalTo(true)));
Value totalResult = solution.getValue("total");
Assert.assertEquals(vf.createLiteral("12",XMLSchema.UNSIGNED_LONG),totalResult);
}
}
finally {
result.close();
}
}