use of org.openrdf.query.GraphQuery in project incubator-rya by apache.
the class RdfController method performGraphQuery.
private void performGraphQuery(final String query, final RepositoryConnection conn, final String auth, final String infer, final String nullout, final RDFHandler handler) throws RepositoryException, MalformedQueryException, QueryEvaluationException, RDFHandlerException {
final GraphQuery graphQuery = conn.prepareGraphQuery(QueryLanguage.SPARQL, query);
if (auth != null && auth.length() > 0) {
graphQuery.setBinding(RdfCloudTripleStoreConfiguration.CONF_QUERY_AUTH, VALUE_FACTORY.createLiteral(auth));
}
if (infer != null && infer.length() > 0) {
graphQuery.setBinding(RdfCloudTripleStoreConfiguration.CONF_INFER, VALUE_FACTORY.createLiteral(Boolean.parseBoolean(infer)));
}
if (nullout != null && nullout.length() > 0) {
// output nothing, but still run query
// TODO this seems like a strange use case.
graphQuery.evaluate(new RDFHandler() {
@Override
public void startRDF() throws RDFHandlerException {
}
@Override
public void endRDF() throws RDFHandlerException {
}
@Override
public void handleNamespace(final String prefix, final String uri) throws RDFHandlerException {
}
@Override
public void handleStatement(final Statement st) throws RDFHandlerException {
}
@Override
public void handleComment(final String comment) throws RDFHandlerException {
}
});
} else {
final long startTime = System.currentTimeMillis();
graphQuery.evaluate(handler);
log.info(String.format("Query Time = %.3f\n", (System.currentTimeMillis() - startTime) / 1000.));
}
}
use of org.openrdf.query.GraphQuery in project incubator-rya by apache.
the class SailExecutionStrategy method executeConstructRule.
/**
* Executes a CONSTRUCT query through the SAIL and inserts the results into
* the DAO.
* @param rule A construct query; not null.
* @param metadata Metadata to add to any inferred triples; not null.
* @return The number of inferred triples.
* @throws ForwardChainException if query execution or data insert fails.
*/
@Override
public long executeConstructRule(AbstractConstructRule rule, StatementMetadata metadata) throws ForwardChainException {
Preconditions.checkNotNull(rule);
Preconditions.checkNotNull(metadata);
if (!initialized) {
initialize();
}
ParsedGraphQuery graphQuery = rule.getQuery();
long statementsAdded = 0;
logger.info("Applying inference rule " + rule + "...");
for (String line : graphQuery.getTupleExpr().toString().split("\n")) {
logger.debug("\t" + line);
}
InferredStatementHandler<?> handler = new InferredStatementHandler<>(dao, metadata);
try {
GraphQuery executableQuery = new SailGraphQuery(graphQuery, conn) {
};
executableQuery.evaluate(handler);
statementsAdded = handler.getNumStatementsAdded();
logger.info("Added " + statementsAdded + " inferred statements.");
return statementsAdded;
} catch (QueryEvaluationException e) {
throw new ForwardChainException("Error evaluating query portion of construct rule", e);
} catch (RDFHandlerException e) {
throw new ForwardChainException("Error processing results of construct rule", e);
}
}
Aggregations