use of org.openrdf.query.TupleQuery in project incubator-rya by apache.
the class RdfCloudTripleStoreConnectionTest method testEvaluateMultiLine.
public void testEvaluateMultiLine() throws Exception {
RepositoryConnection conn = repository.getConnection();
URI loadPerc = vf.createURI(litdupsNS, "loadPerc");
URI uri1 = vf.createURI(litdupsNS, "uri1");
URI pred2 = vf.createURI(litdupsNS, "pred2");
URI uri2 = vf.createURI(litdupsNS, "uri2");
conn.add(cpu, loadPerc, uri1);
conn.add(cpu, pred2, uri2);
conn.commit();
String query = "select * where {" + "?x <" + loadPerc.stringValue() + "> ?o1." + "?x <" + pred2.stringValue() + "> ?o2." + "}";
TupleQuery tupleQuery = conn.prepareTupleQuery(QueryLanguage.SPARQL, query);
tupleQuery.setBinding(RdfCloudTripleStoreConfiguration.CONF_QUERYPLAN_FLAG, RdfCloudTripleStoreConstants.VALUE_FACTORY.createLiteral(true));
CountTupleHandler cth = new CountTupleHandler();
tupleQuery.evaluate(cth);
conn.close();
assertEquals(cth.getCount(), 1);
}
use of org.openrdf.query.TupleQuery in project incubator-rya by apache.
the class RulesetCopyIT method runQuery.
private Set<BindingSet> runQuery(final String query, final Configuration conf) throws Exception {
SailRepository repository = null;
SailRepositoryConnection conn = null;
try {
final Sail extSail = RyaSailFactory.getInstance(conf);
repository = new SailRepository(extSail);
conn = repository.getConnection();
final ResultHandler handler = new ResultHandler();
final TupleQuery tq = conn.prepareTupleQuery(QueryLanguage.SPARQL, query);
tq.evaluate(handler);
return handler.getSolutions();
} finally {
if (conn != null) {
conn.close();
}
if (repository != null) {
repository.shutDown();
}
}
}
use of org.openrdf.query.TupleQuery in project incubator-rya by apache.
the class SpinConstructRule method loadSpinRules.
/**
* Load a set of SPIN rules from a data store.
* @param conf Contains the connection information. Not null.
* @return A map of rule identifiers to rule objects.
* @throws ForwardChainException if connecting, querying for rules, or
* parsing rules fails.
*/
public static Ruleset loadSpinRules(RdfCloudTripleStoreConfiguration conf) throws ForwardChainException {
Preconditions.checkNotNull(conf);
Map<Resource, Rule> rules = new ConcurrentHashMap<>();
// Connect to Rya
SailRepository repository = null;
SailRepositoryConnection conn = null;
try {
repository = new SailRepository(RyaSailFactory.getInstance(conf));
} catch (Exception e) {
throw new ForwardChainException("Couldn't initialize SAIL from configuration", e);
}
// Load and parse the individual SPIN rules from the data store
String ruleQueryString = "SELECT ?type ?rule ?text WHERE {\n" + " ?type <" + SPIN.RULE_PROPERTY.stringValue() + "> ?rule .\n" + " {\n" + " ?rule a <" + SP.CONSTRUCT_CLASS.stringValue() + "> .\n" + " ?rule <" + SP.TEXT_PROPERTY.stringValue() + "> ?text .\n" + " } UNION {\n" + " ?rule a ?template .\n" + " ?template <" + SPIN.BODY_PROPERTY + ">? ?body .\n" + " ?body a <" + SP.CONSTRUCT_CLASS.stringValue() + "> .\n" + " ?body <" + SP.TEXT_PROPERTY.stringValue() + "> ?text .\n" + " }\n" + "}";
SPARQLParser parser = new SPARQLParser();
try {
conn = repository.getConnection();
TupleQuery ruleQuery = conn.prepareTupleQuery(QueryLanguage.SPARQL, ruleQueryString);
ruleQuery.evaluate(new TupleQueryResultHandlerBase() {
@Override
public void handleSolution(BindingSet bs) throws TupleQueryResultHandlerException {
// For each rule identifier found, instantiate a SpinRule
Value requiredType = bs.getValue("type");
Value ruleIdentifier = bs.getValue("rule");
Value ruleText = bs.getValue("text");
if (requiredType instanceof Resource && ruleIdentifier instanceof Resource && ruleText instanceof Literal) {
ParsedQuery parsedRule;
try {
parsedRule = parser.parseQuery(ruleText.stringValue(), null);
if (parsedRule instanceof ParsedGraphQuery) {
SpinConstructRule rule = new SpinConstructRule((Resource) requiredType, (Resource) ruleIdentifier, (ParsedGraphQuery) parsedRule);
if (rule.hasAnonymousConsequent()) {
logger.error("Skipping unsupported rule " + ruleIdentifier + " -- consequent refers to bnode, which is not" + " currently supported (creating new bnodes at each" + " application could lead to infinite recursion).");
} else {
rules.put((Resource) ruleIdentifier, rule);
}
}
} catch (Exception e) {
throw new TupleQueryResultHandlerException(e);
}
}
}
});
} catch (TupleQueryResultHandlerException | QueryEvaluationException | MalformedQueryException | RepositoryException e) {
throw new ForwardChainException("Couldn't retrieve SPIN rules", e);
} finally {
if (conn != null) {
try {
conn.close();
} catch (RepositoryException e) {
logger.warn("Error closing repository connection", e);
}
}
if (repository.isInitialized()) {
try {
repository.shutDown();
} catch (RepositoryException e) {
logger.warn("Error shutting down repository", e);
}
}
}
return new Ruleset(rules.values());
}
use of org.openrdf.query.TupleQuery in project gocd by gocd.
the class SesameGraph method renderSPARQLResultsAsXML.
public void renderSPARQLResultsAsXML(String sparql, OutputStream stream) {
try {
Query query = conn.prepareQuery(QueryLanguage.SPARQL, sparql);
contextualize(query);
if (query instanceof TupleQuery) {
renderTupleQuery(query, new SPARQLResultsXMLWriter(stream));
} else {
renderBooleanQuery(query, new SPARQLBooleanXMLWriter(stream));
}
stream.flush();
} catch (UnsupportedSPARQLStatementException e) {
throw e;
} catch (Exception e) {
throw new ShineRuntimeException("Could not render sparql results as XML: <<" + sparql + ">>", e);
}
}
use of org.openrdf.query.TupleQuery in project gocd by gocd.
the class SesameGraph method getTupleQueryResult.
private TupleQueryResult getTupleQueryResult(String sparqlSelect) {
try {
TupleQuery tupleQuery = conn.prepareTupleQuery(QueryLanguage.SPARQL, sparqlSelect);
contextualize(tupleQuery);
return tupleQuery.evaluate();
} catch (UnsupportedSPARQLStatementException e) {
throw e;
} catch (Exception e) {
throw new ShineRuntimeException(e);
}
}
Aggregations