Search in sources :

Example 1 with TreeNode

use of ca.corefacility.bioinformatics.irida.util.TreeNode in project irida by phac-nml.

the class ProjectsControllerTest method testSearchTaxonomy.

@Test
public void testSearchTaxonomy() {
    String searchTerm = "bac";
    TreeNode<String> root = new TreeNode<>("Bacteria");
    TreeNode<String> child = new TreeNode<>("ChildBacteria");
    child.setParent(root);
    root.addChild(child);
    List<TreeNode<String>> resultList = new ArrayList<>();
    resultList.add(root);
    // the elements that should be at the root
    List<String> results = Lists.newArrayList(searchTerm, "Bacteria");
    when(taxonomyService.search(searchTerm)).thenReturn(resultList);
    List<Map<String, Object>> searchTaxonomy = controller.searchTaxonomy(searchTerm);
    verify(taxonomyService).search(searchTerm);
    assertFalse(searchTaxonomy.isEmpty());
    assertEquals(2, searchTaxonomy.size());
    for (Map<String, Object> element : searchTaxonomy) {
        assertTrue(element.containsKey("text"));
        assertTrue(element.containsKey("id"));
        assertTrue(results.contains(element.get("text")));
    }
}
Also used : TreeNode(ca.corefacility.bioinformatics.irida.util.TreeNode) Matchers.anyString(org.mockito.Matchers.anyString) ExtendedModelMap(org.springframework.ui.ExtendedModelMap) Test(org.junit.Test)

Example 2 with TreeNode

use of ca.corefacility.bioinformatics.irida.util.TreeNode 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)

Aggregations

TreeNode (ca.corefacility.bioinformatics.irida.util.TreeNode)2 ParameterizedSparqlString (com.hp.hpl.jena.query.ParameterizedSparqlString)1 Query (com.hp.hpl.jena.query.Query)1 QueryExecution (com.hp.hpl.jena.query.QueryExecution)1 QuerySolution (com.hp.hpl.jena.query.QuerySolution)1 ResultSet (com.hp.hpl.jena.query.ResultSet)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 Test (org.junit.Test)1 Matchers.anyString (org.mockito.Matchers.anyString)1 ExtendedModelMap (org.springframework.ui.ExtendedModelMap)1