use of com.hp.hpl.jena.query.QuerySolution in project eol-globi-data by jhpoelen.
the class StudyImporterForJSONLD method importStudy.
@Override
public void importStudy() throws StudyImporterException {
Model model;
try {
model = buildModel();
} catch (IOException e) {
throw new StudyImporterException("failed to import [" + getResourceURI() + "]", e);
}
Query query;
try {
query = QueryFactory.create(IOUtils.toString(new DatasetLocal().getResource("find-jsonld-interactions.rq"), CharsetConstant.UTF8));
} catch (IOException e) {
throw new StudyImporterException("failed to find sparql query", e);
}
QueryExecution exec = QueryExecutionFactory.create(query, model);
try {
ResultSet results = exec.execSelect();
while (results.hasNext()) {
QuerySolution solution = results.nextSolution();
String subj = solution.get("subj").asResource().getURI();
String creationDate = solution.get("creationDate").asLiteral().getString();
String authorURI = solution.get("author").toString();
String author;
try {
author = nodeFactory.getAuthorResolver().findFullName(authorURI);
} catch (IOException e) {
throw new StudyImporterException("failed to resolve author URI [" + authorURI + "]");
}
final String source1 = author + ". " + new DateTime(parseDate(creationDate)).getYear() + ". " + CitationUtil.createLastAccessedString(getResourceURI().toString());
Study study = nodeFactory.getOrCreateStudy(new StudyImpl(getResourceURI() + subj, source1, null, subj));
study.setExternalId(subj);
Specimen source = createSpecimen(solution, study, "subjTaxon");
Specimen target = createSpecimen(solution, study, "targetTaxon");
String interactType = solution.get("p").asResource().getLocalName();
InteractType interactType1 = InteractType.typeOf(StringUtils.replace(interactType, "RO_", "RO:"));
if (interactType1 == null) {
throw new StudyImporterException("failed to map interaction type [" + interactType + "]");
}
String collTime = solution.get("collTime").asLiteral().getString();
Date date = parseDate(collTime);
nodeFactory.setUnixEpochProperty(source, date);
nodeFactory.setUnixEpochProperty(target, date);
Location loc = nodeFactory.getOrCreateLocation(new LocationImpl(solution.get("collLat").asLiteral().getDouble(), solution.get("collLng").asLiteral().getDouble(), null, null));
target.caughtIn(loc);
source.caughtIn(loc);
source.interactsWith(target, interactType1);
}
} catch (NodeFactoryException e) {
throw new StudyImporterException("failed to import jsonld data in [" + getResourceURI() + "]", e);
} finally {
exec.close();
}
}
use of com.hp.hpl.jena.query.QuerySolution in project eol-globi-data by jhpoelen.
the class SPARQLTest method executeQuerySampleData.
@Test
public void executeQuerySampleData() {
Model model = ModelFactory.createDefaultModel();
model.read(getClass().getResourceAsStream("data.ttl"), null, "TTL");
String queryString = "PREFIX foaf: <http://xmlns.com/foaf/0.1/> " + "SELECT ?name WHERE { " + " ?person foaf:mbox <mailto:alice@example.org> . " + " ?person foaf:name ?name . " + "}";
Query query = QueryFactory.create(queryString);
QueryExecution exec = QueryExecutionFactory.create(query, model);
try {
ResultSet results = exec.execSelect();
int counter = 0;
while (results.hasNext()) {
QuerySolution solution = results.nextSolution();
Literal name = solution.getLiteral("name");
assertThat(name.toString(), is("Alice"));
counter++;
}
assertThat(counter, is(1));
} finally {
exec.close();
}
}
use of com.hp.hpl.jena.query.QuerySolution in project nextprot-api by calipho-sib.
the class RdfHelpServiceImpl method getRdfTypeValues.
private Set<String> getRdfTypeValues(String rdfTypeInfoName, int limit) {
Set<String> values = new TreeSet<String>();
// TODO add a method with a map of named parameters in the sparql dictionary
String queryBase = sparqlDictionary.getSparqlOnly("typevalues");
String query = sparqlDictionary.getSparqlPrefixes();
query += queryBase.replace(":SomeRdfType", rdfTypeInfoName).replace(":LimitResults", String.valueOf(limit));
QueryExecution qExec = sparqlService.queryExecution(query);
ResultSet rs = qExec.execSelect();
while (rs.hasNext()) {
QuerySolution sol = rs.next();
String value = (String) getDataFromSolutionVar(sol, "value");
if (value.startsWith("annotation:")) {
values.add("Example: " + value);
break;
} else {
values.add(value);
}
}
qExec.close();
// Reduce the json if the list is not complete, just put a simple example
if (values.size() == limit) {
Iterator<String> it = values.iterator();
String sample1 = it.next();
values.clear();
values.add("Example: " + sample1);
}
return values;
}
use of com.hp.hpl.jena.query.QuerySolution in project nextprot-api by calipho-sib.
the class RdfHelpServiceImpl method getValuesForTriple.
private Set<String> getValuesForTriple(String rdfTypeName, String predicate, int limit) {
Set<String> values = new TreeSet<String>();
String queryBase = sparqlDictionary.getSparqlOnly("getliteralvalues");
String query = sparqlDictionary.getSparqlPrefixes();
query += queryBase.replace(":SomeRdfType", rdfTypeName).replace(":SomePredicate", predicate).replace(":LimitResults", String.valueOf(limit));
QueryExecution qExec = sparqlService.queryExecution(query);
ResultSet rs = qExec.execSelect();
while (rs.hasNext()) {
QuerySolution sol = rs.next();
values.add((String) getDataFromSolutionVar(sol, "value"));
}
qExec.close();
// Reduce the json if the list is not complete, just put a simple example
if (values.size() == limit) {
Iterator<String> it = values.iterator();
String sample1 = it.next();
values.clear();
values.add("Example: " + sample1);
}
return values;
}
use of com.hp.hpl.jena.query.QuerySolution in project nextprot-api by calipho-sib.
the class RdfHelpServiceImpl method getRdfTypeProperties.
private Map<String, String> getRdfTypeProperties(String rdfType) {
Map<String, String> properties = new HashMap<String, String>();
String queryBase = sparqlDictionary.getSparqlOnly("typenames");
String query = sparqlDictionary.getSparqlPrefixes();
query += queryBase.replace(":SomeRdfType", rdfType);
QueryExecution qExec = sparqlService.queryExecution(query);
ResultSet rs = qExec.execSelect();
if (rs.hasNext()) {
QuerySolution sol = rs.next();
properties.put("rdfType", (String) getDataFromSolutionVar(sol, "rdfType"));
properties.put("label", (String) getDataFromSolutionVar(sol, "label"));
properties.put("comment", (String) getDataFromSolutionVar(sol, "comment"));
properties.put("instanceCount", (String) getDataFromSolutionVar(sol, "instanceCount"));
properties.put("instanceSample", (String) getDataFromSolutionVar(sol, "instanceSample"));
}
qExec.close();
return properties;
}
Aggregations