Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

existsPath #35

Open
pnrobinson opened this issue Dec 14, 2017 · 4 comments
Open

existsPath #35

pnrobinson opened this issue Dec 14, 2017 · 4 comments

Comments

@pnrobinson
Copy link
Collaborator

I would like to have a few more functions in ontolib. I will be making PRs. This is a function for whether there exists a path (declared in Ontology and implemented in ImmutableOntology)

@Override
  public boolean existsPath(final TermId sourceID, TermId destID){
    // special case -- a term cannot have a path to itself in an ontology (DAG)
    if (sourceID.equals(destID)) return false;
    List<TermId> visited = new ArrayList<>();
    BreadthFirstSearch<TermId, ImmutableEdge<TermId>> bfs = new BreadthFirstSearch<>();
    bfs.startFromForward(graph, sourceID, new VertexVisitor<TermId, ImmutableEdge<TermId>>() {
        @Override
        public boolean visit(DirectedGraph<TermId, ImmutableEdge<TermId>> g, TermId termId) {
          visited.add(termId);
          return true;
        }});
    return visited.contains(destID);
  }

The following tests are passed

/** The example graph has id1->id2, id1->id3, id1->id4, id2->id5, id4->id5 */
  @Test
  public void testPathExists() {
    assertTrue(ontology.existsPath(id1,id2));
    assertFalse(ontology.existsPath(id2,id1));
    assertTrue(ontology.existsPath(id1,id3));
    assertFalse(ontology.existsPath(id3,id1));
    assertTrue(ontology.existsPath(id1,id4));
    assertFalse(ontology.existsPath(id4,id1));
    assertTrue(ontology.existsPath(id1,id5));
    assertFalse(ontology.existsPath(id5,id1));
    assertTrue(ontology.existsPath(id2,id5));
    assertFalse(ontology.existsPath(id5,id2));
    assertTrue(ontology.existsPath(id4,id5));
    assertFalse(ontology.existsPath(id5,id4));
  // test that a term cannot have a path to itself.
    assertFalse(ontology.existsPath(id5,id5));

  }
@holtgrewe
Copy link
Contributor

As explained elsewhere (offline in email?), this should go as a static method into a utility class.

@holtgrewe
Copy link
Contributor

Otherwise, good.

@pnrobinson
Copy link
Collaborator Author

OK, I will add a class with utility functions and refactor...

@pnrobinson
Copy link
Collaborator Author

I have added a new class called OntologyAlgorithm, together with OntologyAlgorithmTest. These classes implement various static functions for searching for children, parents, descendents, ancestors, as well as the existsPath. I will delete the first PR and make a new one for the OntologyAlgorithm.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants