-
Notifications
You must be signed in to change notification settings - Fork 2
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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; | ||
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; | ||
|
@@ -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; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
} | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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), | ||
|
@@ -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. | ||
|
@@ -97,6 +97,9 @@ default Set<TermId> getParentTermIds(TermId termId) { | |
return result; | ||
} | ||
|
||
public boolean existsPath(final TermId sourceID, TermId destID); | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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}. | ||
* | ||
|
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; | ||
|
@@ -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)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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)); | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please remove empty line here and below. |
||
} | ||
|
||
|
||
} |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.