本文整理汇总了Java中org.openrdf.query.QueryLanguage类的典型用法代码示例。如果您正苦于以下问题:Java QueryLanguage类的具体用法?Java QueryLanguage怎么用?Java QueryLanguage使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
QueryLanguage类属于org.openrdf.query包,在下文中一共展示了QueryLanguage类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: rewrite
import org.openrdf.query.QueryLanguage; //导入依赖的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: before
import org.openrdf.query.QueryLanguage; //导入依赖的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);
}
示例3: getDistinctObj
import org.openrdf.query.QueryLanguage; //导入依赖的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();
}
}
示例4: getDistinctSbj
import org.openrdf.query.QueryLanguage; //导入依赖的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();
}
}
示例5: getSubjectCount
import org.openrdf.query.QueryLanguage; //导入依赖的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();
}
}
示例6: getObjectCount
import org.openrdf.query.QueryLanguage; //导入依赖的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();
}
}
示例7: getTripleCount
import org.openrdf.query.QueryLanguage; //导入依赖的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();
}
}
示例8: getPredicates
import org.openrdf.query.QueryLanguage; //导入依赖的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;
}
示例9: _select
import org.openrdf.query.QueryLanguage; //导入依赖的package包/类
/**
* {@inheritDoc}
*
* Logs the query at INFO and logs the optimized AST at TRACE.
*/
@Override
protected Stream<BindingSet> _select(
final String queryStr, final String extQueryId) {
logQuery(queryStr);
return Code.wrapThrow(() -> {
final BigdataSailTupleQuery query = (BigdataSailTupleQuery)
cxn().prepareTupleQuery(QueryLanguage.SPARQL, queryStr);
setMaxQueryTime(query);
final UUID queryId = setupQuery(query.getASTContainer(),
QueryType.SELECT, extQueryId);
sparqlLog.trace(() -> "optimized AST:\n"+query.optimize());
/*
* Result is closed automatically by GraphStreamer.
*/
final TupleQueryResult result = query.evaluate();
final Optional<Runnable> onClose =
Optional.of(() -> finalizeQuery(queryId));
return new GraphStreamer<>(result, onClose).stream();
});
}
示例10: _project
import org.openrdf.query.QueryLanguage; //导入依赖的package包/类
/**
* {@inheritDoc}
*
* Logs the query at INFO and logs the optimized AST at TRACE.
*/
@Override
protected Stream<Statement> _project(
final String queryStr, final String extQueryId) {
logQuery(queryStr);
return Code.wrapThrow(() -> {
final BigdataSailGraphQuery query = (BigdataSailGraphQuery)
cxn().prepareGraphQuery(QueryLanguage.SPARQL, queryStr);
setMaxQueryTime(query);
final UUID queryId = setupQuery(query.getASTContainer(),
QueryType.CONSTRUCT, extQueryId);
sparqlLog.trace(() -> "optimized AST:\n"+query.optimize());
/*
* Result is closed automatically by GraphStreamer.
*/
final GraphQueryResult result = query.evaluate();
final Optional<Runnable> onClose =
Optional.of(() -> finalizeQuery(queryId));
return new GraphStreamer<>(result, onClose).stream();
});
}
示例11: _ask
import org.openrdf.query.QueryLanguage; //导入依赖的package包/类
/**
* {@inheritDoc}
*
* Logs the query at INFO.
*/
@Override
protected boolean _ask(
final String queryStr, final String extQueryId) {
logQuery(queryStr);
return Code.wrapThrow(() -> { /* try */
final BigdataSailBooleanQuery query = (BigdataSailBooleanQuery)
cxn().prepareBooleanQuery(QueryLanguage.SPARQL, queryStr);
setMaxQueryTime(query);
final UUID queryId = setupQuery(query.getASTContainer(),
QueryType.ASK, extQueryId);
// sparqlLog.trace(() -> "optimized AST:\n"+query.optimize());
try {
return query.evaluate();
} finally {
finalizeQuery(queryId);
}
});
}
示例12: _update
import org.openrdf.query.QueryLanguage; //导入依赖的package包/类
/**
* {@inheritDoc}
*
* Logs the query at INFO.
*/
@Override
protected void _update(
final String queryStr, final String extQueryId) {
logQuery(queryStr);
Code.wrapThrow(() -> {
final BigdataSailUpdate query = (BigdataSailUpdate)
cxn().prepareUpdate(QueryLanguage.SPARQL, queryStr);
final UUID queryId =
setupQuery(query.getASTContainer(),
null /* QueryType.UPDATE */, extQueryId);
try {
query.execute();
} finally {
finalizeQuery(queryId);
}
});
}
示例13: doUpdate
import org.openrdf.query.QueryLanguage; //导入依赖的package包/类
public static final void doUpdate( UpdateExecutor query,
RepositoryConnection rc, boolean dobindings ) throws RepositoryException,
MalformedQueryException, UpdateExecutionException {
String sparql = processNamespaces( dobindings ? query.getSparql()
: query.bindAndGetSparql(), query.getNamespaces() );
ValueFactory vfac = new ValueFactoryImpl();
Update upd = rc.prepareUpdate( QueryLanguage.SPARQL, sparql );
if ( dobindings ) {
upd.setIncludeInferred( query.usesInferred() );
query.setBindings( upd, vfac );
}
upd.execute();
query.done();
}
示例14: getConstruct
import org.openrdf.query.QueryLanguage; //导入依赖的package包/类
public static Model getConstruct( QueryExecutor<Model> query,
RepositoryConnection rc, boolean dobindings )
throws RepositoryException, MalformedQueryException, QueryEvaluationException {
String sparql = processNamespaces( dobindings ? query.getSparql()
: query.bindAndGetSparql(), query.getNamespaces() );
GraphQuery tq = rc.prepareGraphQuery( QueryLanguage.SPARQL, sparql );
tq.setIncludeInferred( query.usesInferred() );
if ( dobindings ) {
query.setBindings( tq, rc.getValueFactory() );
}
GraphQueryResult gqr = tq.evaluate();
while ( gqr.hasNext() ) {
query.getResults().add( gqr.next() );
}
gqr.close();
return query.getResults();
}
示例15: executeQuery
import org.openrdf.query.QueryLanguage; //导入依赖的package包/类
Set<BindingSet> executeQuery(URL queryFile) throws Exception {
SailRepositoryConnection conn = repository.getConnection();
try {
InputStream queryIS = queryFile.openStream();
BufferedReader br = new BufferedReader(new java.io.InputStreamReader(queryIS, "UTF-8"));
String query = br.lines().collect(Collectors.joining("\n"));
br.close();
TupleQuery tupleQuery = conn.prepareTupleQuery(QueryLanguage.SPARQL, query);
TupleQueryResult result = tupleQuery.evaluate();
Set<BindingSet> solutions = new HashSet<>();
while (result.hasNext()) {
solutions.add(result.next());
}
return solutions;
} finally {
closeQuietly(conn);
}
}