Search in sources :

Example 1 with QuerySolution

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();
    }
}
Also used : InteractType(org.eol.globi.domain.InteractType) Study(org.eol.globi.domain.Study) Query(com.hp.hpl.jena.query.Query) StudyImpl(org.eol.globi.domain.StudyImpl) IOException(java.io.IOException) DatasetLocal(org.eol.globi.service.DatasetLocal) QueryExecution(com.hp.hpl.jena.query.QueryExecution) DateTime(org.joda.time.DateTime) Date(java.util.Date) Specimen(org.eol.globi.domain.Specimen) QuerySolution(com.hp.hpl.jena.query.QuerySolution) Model(com.hp.hpl.jena.rdf.model.Model) ResultSet(com.hp.hpl.jena.query.ResultSet) LocationImpl(org.eol.globi.domain.LocationImpl) Location(org.eol.globi.domain.Location)

Example 2 with QuerySolution

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();
    }
}
Also used : Query(com.hp.hpl.jena.query.Query) QuerySolution(com.hp.hpl.jena.query.QuerySolution) Literal(com.hp.hpl.jena.rdf.model.Literal) Model(com.hp.hpl.jena.rdf.model.Model) ResultSet(com.hp.hpl.jena.query.ResultSet) QueryExecution(com.hp.hpl.jena.query.QueryExecution) Test(org.junit.Test)

Example 3 with QuerySolution

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;
}
Also used : QuerySolution(com.hp.hpl.jena.query.QuerySolution) TreeSet(java.util.TreeSet) ResultSet(com.hp.hpl.jena.query.ResultSet) QueryExecution(com.hp.hpl.jena.query.QueryExecution)

Example 4 with QuerySolution

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;
}
Also used : QuerySolution(com.hp.hpl.jena.query.QuerySolution) TreeSet(java.util.TreeSet) ResultSet(com.hp.hpl.jena.query.ResultSet) QueryExecution(com.hp.hpl.jena.query.QueryExecution)

Example 5 with QuerySolution

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;
}
Also used : HashMap(java.util.HashMap) QuerySolution(com.hp.hpl.jena.query.QuerySolution) ResultSet(com.hp.hpl.jena.query.ResultSet) QueryExecution(com.hp.hpl.jena.query.QueryExecution)

Aggregations

QuerySolution (com.hp.hpl.jena.query.QuerySolution)14 ResultSet (com.hp.hpl.jena.query.ResultSet)10 QueryExecution (com.hp.hpl.jena.query.QueryExecution)9 Query (com.hp.hpl.jena.query.Query)4 ParameterizedSparqlString (com.hp.hpl.jena.query.ParameterizedSparqlString)3 Model (com.hp.hpl.jena.rdf.model.Model)3 HashMap (java.util.HashMap)3 TreeSet (java.util.TreeSet)3 Literal (com.hp.hpl.jena.rdf.model.Literal)2 URI (java.net.URI)2 ArrayList (java.util.ArrayList)2 Study (org.eol.globi.domain.Study)2 Test (org.junit.Test)2 DataIntegrityViolationException (uk.ac.ebi.spot.goci.pussycat.exception.DataIntegrityViolationException)2 SparqlQueryException (uk.ac.ebi.spot.goci.sparql.exception.SparqlQueryException)2 TreeNode (ca.corefacility.bioinformatics.irida.util.TreeNode)1 RDFNode (com.hp.hpl.jena.rdf.model.RDFNode)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 IOException (java.io.IOException)1 StringWriter (java.io.StringWriter)1