use of com.hp.hpl.jena.query.QuerySolution in project eol-globi-data by jhpoelen.
the class SPARQLTest method executeQuerySampleGloBIData.
@Test
public void executeQuerySampleGloBIData() throws NodeFactoryException, ParseException, IOException {
ExporterRDF exporter = new ExporterRDF();
StringWriter writer = new StringWriter();
Study study = ExportTestUtil.createTestData(nodeFactory);
exporter.exportStudy(study, writer, true);
Model model = ModelFactory.createDefaultModel();
model.read(new ByteArrayInputStream(writer.toString().getBytes("UTF-8")), null, "N-TRIPLE");
System.out.println(writer.toString());
String queryString = "PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> " + "SELECT ?individual WHERE { " + " ?individual rdf:type <http://purl.obolibrary.org/obo/CARO_0010004> . " + "}";
Query query = QueryFactory.create(queryString);
QueryExecution exec = QueryExecutionFactory.create(query, model);
try {
ResultSet results = exec.execSelect();
int numberOfOrganisms = 0;
while (results.hasNext()) {
QuerySolution solution = results.nextSolution();
assertThat(solution.get("individual"), is(notNullValue()));
numberOfOrganisms++;
}
final int expectedNumberOfOrganisms = 3;
assertThat(numberOfOrganisms, is(expectedNumberOfOrganisms));
} finally {
exec.close();
}
}
use of com.hp.hpl.jena.query.QuerySolution in project irida by phac-nml.
the class InMemoryTaxonomyService method search.
/**
* {@inheritDoc}
*/
@Override
public Collection<TreeNode<String>> search(String searchTerm) {
HashMap<String, TreeNode<String>> visited = new HashMap<>();
Set<TreeNode<String>> visitedRoots = new HashSet<>();
if (!Strings.isNullOrEmpty(searchTerm)) {
ParameterizedSparqlString query = new ParameterizedSparqlString(LABEL_SEARCH_QUERY);
query.setLiteral("term", QueryParser.escape(searchTerm));
query.setIri("parent", ROOT_IRI);
Query q = query.asQuery();
QueryExecution qexec = QueryExecutionFactory.create(q, dataset);
ResultSet result = qexec.execSelect();
while (result.hasNext()) {
QuerySolution next = result.next();
buildTrimmedResultTree(next.getResource("s"), searchTerm, visited);
}
// get all the roots
for (Entry<String, TreeNode<String>> entry : visited.entrySet()) {
TreeNode<String> current = entry.getValue();
while (current.getParent() != null) {
current = current.getParent();
}
if (!visitedRoots.contains(current)) {
visitedRoots.add(current);
}
}
}
return visitedRoots;
}
use of com.hp.hpl.jena.query.QuerySolution in project goci by EBISPOT.
the class SparqlTemplate method type.
public URI type(final URI entity) {
String sparql = getPrefixString().concat("SELECT DISTINCT ?type WHERE { <" + entity.toString() + "> rdf:type ?type . " + "FILTER ( ?type != owl:Class ) . " + "FILTER ( ?type != owl:NamedIndividual ) }");
if (typeCache.containsKey(sparql)) {
return typeCache.get(sparql);
}
URI result = query(sparql, new ResultSetMapper<URI>() {
@Override
public URI mapResultSet(ResultSet rs) {
URI result = null;
while (rs.hasNext()) {
if (result != null) {
throw new SparqlQueryException(new DataIntegrityViolationException("More than one non-owl rdf:type for' " + entity.toString() + "'"));
}
QuerySolution qs = rs.next();
result = URI.create(qs.getResource("type").getURI());
}
return result;
}
});
typeCache.put(sparql, result);
return result;
}
use of com.hp.hpl.jena.query.QuerySolution in project goci by EBISPOT.
the class SparqlAssociationRenderlet method sortBandsWithData.
protected Map<BandInformation, BandInformation> sortBandsWithData(SparqlTemplate sparqlTemplate) {
Map<BandInformation, BandInformation> bandMap = new HashMap<BandInformation, BandInformation>();
// use the sparqlTemplate to get all individuals of type "cytogenic region"
getLog().trace("Retrieving all cytogenetic bands to sort into rendering order...");
List<BandInformation> bands = sparqlTemplate.query("SELECT DISTINCT ?band WHERE { ?bandUri a gt:CytogeneticRegion ; rdfs:label ?band . FILTER (STR(?band) != 'NR') .}", new QuerySolutionMapper<BandInformation>() {
@Override
public BandInformation mapQuerySolution(QuerySolution qs) {
return new BandInformation(qs.getLiteral("band").getLexicalForm());
}
});
getLog().trace("Got " + bands.size() + " bands, starting sorting...");
// now we've added all band info, sort the set of unique bands
Collections.sort(bands);
for (int i = 1; i < bands.size(); i++) {
BandInformation band = bands.get(i);
BandInformation previousBand = bands.get(i - 1);
bandMap.put(band, previousBand);
}
getLog().trace("Mapped " + bandMap.keySet().size() + " bands to their 'previous' band");
return bandMap;
}
Aggregations