本文整理匯總了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);
}
}