use of org.openrdf.query.QueryEvaluationException in project blueprints by tinkerpop.
the class GraphSailTest method testCodePlay.
@Test
public void testCodePlay() throws Exception {
Sail sail = new GraphSail(new TinkerGraph());
sail.initialize();
try {
SailConnection sc = sail.getConnection();
try {
sc.begin();
ValueFactory vf = sail.getValueFactory();
sc.addStatement(vf.createURI("http://tinkerpop.com#1"), vf.createURI("http://tinkerpop.com#knows"), vf.createURI("http://tinkerpop.com#3"), vf.createURI("http://tinkerpop.com"));
sc.addStatement(vf.createURI("http://tinkerpop.com#1"), vf.createURI("http://tinkerpop.com#name"), vf.createLiteral("marko"), vf.createURI("http://tinkerpop.com"));
sc.addStatement(vf.createURI("http://tinkerpop.com#3"), vf.createURI("http://tinkerpop.com#name"), vf.createLiteral("josh"), vf.createURI("http://tinkerpop.com"));
CloseableIteration<? extends Statement, SailException> results = sc.getStatements(null, null, null, false);
try {
System.out.println("get statements: ?s ?p ?o ?g");
while (results.hasNext()) {
System.out.println(results.next());
}
} finally {
results.close();
}
System.out.println("\nget statements: http://tinkerpop.com#3 ?p ?o ?g");
results = sc.getStatements(vf.createURI("http://tinkerpop.com#3"), null, null, false);
try {
while (results.hasNext()) {
System.out.println(results.next());
}
} finally {
results.close();
}
SPARQLParser parser = new SPARQLParser();
CloseableIteration<? extends BindingSet, QueryEvaluationException> sparqlResults;
String queryString = "SELECT ?x ?y WHERE { ?x <http://tinkerpop.com#knows> ?y }";
ParsedQuery query = parser.parseQuery(queryString, "http://tinkerPop.com");
System.out.println("\nSPARQL: " + queryString);
sparqlResults = sc.evaluate(query.getTupleExpr(), query.getDataset(), new EmptyBindingSet(), false);
try {
while (sparqlResults.hasNext()) {
System.out.println(sparqlResults.next());
}
} finally {
sparqlResults.close();
}
Graph graph = ((GraphSail) sail).getBaseGraph();
System.out.println();
for (Vertex v : graph.getVertices()) {
System.out.println("------");
System.out.println(v);
for (String key : v.getPropertyKeys()) {
System.out.println(key + "=" + v.getProperty(key));
}
}
for (Edge e : graph.getEdges()) {
System.out.println("------");
System.out.println(e);
for (String key : e.getPropertyKeys()) {
System.out.println(key + "=" + e.getProperty(key));
}
}
} finally {
sc.rollback();
sc.close();
}
} finally {
sail.shutDown();
}
}
use of org.openrdf.query.QueryEvaluationException in project stanbol by apache.
the class SesameYard method find.
@Override
public final QueryResultList<Representation> find(FieldQuery parsedQuery) throws YardException, IllegalArgumentException {
if (parsedQuery == null) {
throw new IllegalArgumentException("The parsed query MUST NOT be NULL!");
}
final SparqlFieldQuery query = SparqlFieldQueryFactory.getSparqlFieldQuery(parsedQuery);
RepositoryConnection con = null;
TupleQueryResult results = null;
try {
con = repository.getConnection();
con.begin();
//execute the query
int limit = QueryUtils.getLimit(query, getConfig().getDefaultQueryResultNumber(), getConfig().getMaxQueryResultNumber());
results = executeSparqlFieldQuery(con, query, limit, true);
//parse the results and generate the Representations
//create an own valueFactors so that all the data of the query results
//are added to the same Sesame Model
Model model = new TreeModel();
RdfValueFactory valueFactory = new RdfValueFactory(model, sesameFactory);
List<Representation> representations = limit > 0 ? new ArrayList<Representation>(limit) : new ArrayList<Representation>();
Map<String, URI> bindings = new HashMap<String, URI>(query.getFieldVariableMappings().size());
for (Entry<String, String> mapping : query.getFieldVariableMappings().entrySet()) {
bindings.put(mapping.getValue(), sesameFactory.createURI(mapping.getKey()));
}
while (results.hasNext()) {
BindingSet result = results.next();
Value value = result.getValue(query.getRootVariableName());
if (value instanceof URI) {
URI subject = (URI) value;
//link the result with the query result
model.add(queryRoot, queryResult, subject);
//now copy over the other selected data
for (String binding : result.getBindingNames()) {
URI property = bindings.get(binding);
if (property != null) {
model.add(subject, property, result.getValue(binding));
}
//else no mapping for the query.getRootVariableName()
}
//create a representation and add it to the results
representations.add(valueFactory.createRdfRepresentation(subject));
}
//ignore non URI results
}
con.commit();
return new SesameQueryResultList(model, query, representations);
} catch (RepositoryException e) {
throw new YardException("Unable to execute findReferences query", e);
} catch (QueryEvaluationException e) {
throw new YardException("Unable to execute findReferences query", e);
} finally {
if (results != null) {
//close the result if present
try {
results.close();
} catch (QueryEvaluationException ignore) {
/* ignore */
}
}
if (con != null) {
try {
con.close();
} catch (RepositoryException ignore) {
/* ignore */
}
}
}
}
Aggregations