Skip to content

Commit

Permalink
Adding Ontology.getParents().
Browse files Browse the repository at this point in the history
Fixes: #18
  • Loading branch information
holtgrewe committed Sep 4, 2017
1 parent ed4e478 commit 808c1bd
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import java.io.Serializable;
import java.util.Collection;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

/**
Expand All @@ -17,15 +19,17 @@
* @author <a href="mailto:[email protected]">Manuel Holtgrewe</a>
* @author <a href="mailto:[email protected]">Sebastian Koehler</a>
*/
public interface Ontology<T extends Term,
R extends TermRelation> extends MinimalOntology<T, R>, Serializable {
public interface Ontology<T extends Term, R extends TermRelation>
extends
MinimalOntology<T, R>,
Serializable {

/**
* Translate {@link TermId} to primary one (in case of alternative or deprecated term IDs).
*
* @param termId The {@link TermId} to translate.
* @return Primary {@link TermId} (might be the same as <code>termId</code>), <code>null</code>
* if none could be found.
* @return Primary {@link TermId} (might be the same as <code>termId</code>), <code>null</code> if
* none could be found.
*/
default TermId getPrimaryTermId(TermId termId) {
final Term term = getTermMap().get(termId);
Expand Down Expand Up @@ -78,13 +82,28 @@ default Set<TermId> getAllAncestorTermIds(Collection<TermId> termIds) {
return getAllAncestorTermIds(termIds, true);
}

/**
* Return the {@link TermId}s of the parents of {@code termId}.
*
* @param termId The ID of the term to query the parents for.
* @return The resulting parent {@link TermId}s of {@code termId}.
*/
default Set<TermId> getParentTermIds(TermId termId) {
final Set<TermId> result = new HashSet<>();
final Iterator<TermId> it = getGraph().viaOutEdgeIterator(termId);
while (it.hasNext()) {
result.add(it.next());
}
return result;
}

/**
* Construct and return sub ontology, starting from {@code subOntologyRoot}.
*
* <h5>Sub Ontology Iteration Remark</h5>
*
* <p>
* The constructed sub ontology will use the same maps from {@link TermId} to {@link T} and same
* The constructed sub ontology will use the same maps from {@link TermId} to {@code T} and same
* edge relation maps as the original ontology. However, the functions
* {@link Ontology#getAllTermIds()}, {@link Ontology#getNonObsoleteTermIds()},
* {@link Ontology#getObsoleteTermIds()}, and {@link Ontology#getTerms()} will only contain the
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ public void test() {
"[ImmutableTermId [prefix=ImmutableTermPrefix [value=HP], id=0000001], ImmutableTermId [prefix=ImmutableTermPrefix [value=HP], id=0000002], ImmutableTermId [prefix=ImmutableTermPrefix [value=HP], id=0000003], ImmutableTermId [prefix=ImmutableTermPrefix [value=HP], id=0000004], ImmutableTermId [prefix=ImmutableTermPrefix [value=HP], id=0000005]]",
ontology.getNonObsoleteTermIds().toString());
assertEquals("[]", ontology.getObsoleteTermIds().toString());
assertEquals(
"[ImmutableTermId [prefix=ImmutableTermPrefix [value=HP], id=0000002], ImmutableTermId [prefix=ImmutableTermPrefix [value=HP], id=0000003], ImmutableTermId [prefix=ImmutableTermPrefix [value=HP], id=0000004]]",
ontology.getParentTermIds(id1).toString());
}

}

0 comments on commit 808c1bd

Please sign in to comment.