Search in sources :

Example 11 with QuerySolution

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();
    }
}
Also used : Study(org.eol.globi.domain.Study) ExporterRDF(org.eol.globi.export.ExporterRDF) StringWriter(java.io.StringWriter) Query(com.hp.hpl.jena.query.Query) ByteArrayInputStream(java.io.ByteArrayInputStream) QuerySolution(com.hp.hpl.jena.query.QuerySolution) 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 12 with QuerySolution

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;
}
Also used : Query(com.hp.hpl.jena.query.Query) HashMap(java.util.HashMap) ParameterizedSparqlString(com.hp.hpl.jena.query.ParameterizedSparqlString) ParameterizedSparqlString(com.hp.hpl.jena.query.ParameterizedSparqlString) QueryExecution(com.hp.hpl.jena.query.QueryExecution) QuerySolution(com.hp.hpl.jena.query.QuerySolution) TreeNode(ca.corefacility.bioinformatics.irida.util.TreeNode) ResultSet(com.hp.hpl.jena.query.ResultSet) HashSet(java.util.HashSet)

Example 13 with QuerySolution

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;
}
Also used : QuerySolution(com.hp.hpl.jena.query.QuerySolution) SparqlQueryException(uk.ac.ebi.spot.goci.sparql.exception.SparqlQueryException) ResultSet(com.hp.hpl.jena.query.ResultSet) ParameterizedSparqlString(com.hp.hpl.jena.query.ParameterizedSparqlString) URI(java.net.URI) DataIntegrityViolationException(uk.ac.ebi.spot.goci.pussycat.exception.DataIntegrityViolationException)

Example 14 with QuerySolution

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;
}
Also used : HashMap(java.util.HashMap) QuerySolution(com.hp.hpl.jena.query.QuerySolution) BandInformation(uk.ac.ebi.spot.goci.pussycat.layout.BandInformation)

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