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

adding function existsPath together with test #36

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package com.github.phenomics.ontolib.ontology.data;

import java.util.Collection;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please no star imports but explicit ones. I guess you can easily adjust your IDE to do this.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, I forgot: please add note to Changelog.

import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.*;

import com.github.phenomics.ontolib.graph.algo.BreadthFirstSearch;
import com.github.phenomics.ontolib.graph.algo.VertexVisitor;
Expand Down Expand Up @@ -188,6 +185,25 @@ public Set<TermId> getObsoleteTermIds() {
return obsoleteTermIds;
}

@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;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please wrap and indent consistently. I can send around a style file for Eclipse.

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);
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove superflous whitespace for consistencty.





@Override
public Ontology<T, R> subOntology(TermId subOntologyRoot) {
final Set<TermId> childTermIds = OntologyTerms.childrenOf(subOntologyRoot, this);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ default TermId getPrimaryTermId(TermId termId) {

/**
* Return all the {@link TermId}s of all ancestors from {@code termId}.
*
*
* @param termId The {@link TermId} to query ancestor {@link TermId}s for.
* @param includeRoot Whether or not to include the root.
* @return {@link Set} of {@link TermId}s of the ancestors of {@code termId} (including itself),
Expand All @@ -52,7 +52,7 @@ default TermId getPrimaryTermId(TermId termId) {

/**
* Return all the {@link TermId}s of all ancestors from {@code termId}, including root.
*
*
* @param termId The {@link TermId} to query ancestor {@link TermId}s for.
* @return {@link Set} of {@link TermId}s of the ancestors of {@code termId} (including itself),
* including root.
Expand Down Expand Up @@ -97,6 +97,9 @@ default Set<TermId> getParentTermIds(TermId termId) {
return result;
}

public boolean existsPath(final TermId sourceID, TermId destID);

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add javadoc and remove superflous empty lines.


/**
* Construct and return sub ontology, starting from {@code subOntologyRoot}.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.github.phenomics.ontolib.ontology.data;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

import org.junit.Test;
Expand Down Expand Up @@ -36,4 +37,26 @@ public void test() {
ontology.getParentTermIds(id1).toString());
}


/** The example graph has id1->id2, id1->id3, id1->id4, id2->id5, id4->id5 */
@Test
public void testPathExists() {
assertTrue(ontology.existsPath(id1,id2));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please make whitespace consistent, your IDE should offer an option to do this automatically.

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));

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please remove empty line here and below.

}


}