ImmutableOntology implements Ontology { public static Builder builder() { return new Builder(); } ImmutableOntology(
      ImmutableSortedMap<String, String> metaInfo,
      DefaultDirectedGraph<TermId, IdLabeledEdge> graph,
      TermId rootTermId,
      Collection<TermId> nonObsoleteTermIds,
      Collection<TermId> obsoleteTermIds,
      ImmutableMap<TermId, Term> termMap,
      ImmutableMap<Integer, Relationship> relationMap); @Override Map<String, String> getMetaInfo(); @Override DefaultDirectedGraph<TermId, IdLabeledEdge> getGraph(); @Override Map<TermId, Term> getTermMap(); @Override Map<Integer, Relationship> getRelationMap(); @Override boolean isRootTerm(TermId termId); @Override Set<TermId> getAncestorTermIds(TermId termId, boolean includeRoot); @Override Set<TermId> getAllAncestorTermIds(Collection<TermId> termIds, boolean includeRoot); @Override TermId getRootTermId(); @Override Set<TermId> getAllTermIds(); @Override Collection<Term> getTerms(); @Override Set<TermId> getNonObsoleteTermIds(); @Override Set<TermId> getObsoleteTermIds(); @Override Ontology subOntology(TermId subOntologyRoot); static Builder builder();  }
@Test void testBuilder() { TermId rootId = TermId.of("owl:Thing"); Term root = Term.of(rootId, "root"); TermId childId = TermId.of("HP:0000001"); Term child = Term.of(childId, "child"); TermId child2Id = TermId.of("HP:0000002"); Term child2 = Term.of(child2Id, "child2"); TermId child3Id = TermId.of("HP:0000003"); TermId child3SecondaryId = TermId.of("HP:3333333"); Term child3 = Term.builder() .id(child3Id) .name("child3") .altTermIds(ImmutableList.of(child3SecondaryId)) .build(); TermId obsoleteId = TermId.of("HP:0000004"); Term obsolete = Term.builder().id(obsoleteId).name("obsolete").obsolete(true).build(); ImmutableList<Term> terms = ImmutableList.of(root, child, child2, child3, obsolete); Relationship relationship1 = new Relationship(childId, rootId, 1, RelationshipType.IS_A); Relationship relationship2 = new Relationship(child2Id, rootId, 2, RelationshipType.IS_A); Relationship relationship3 = new Relationship(child3Id, child2Id, 3, RelationshipType.IS_A); Relationship relationship4 = new Relationship(childId, obsoleteId, 4, RelationshipType.IS_A); ImmutableList<Relationship> relationships = ImmutableList.of(relationship1, relationship2, relationship3, relationship4); Ontology instance = ImmutableOntology.builder() .terms(terms) .relationships(relationships) .build(); assertEquals(rootId, instance.getRootTermId()); assertTrue(instance.isRootTerm(rootId)); Set<TermId> allTermIds = terms.stream().map(Term::getId).collect(Collectors.toSet()); assertEquals(allTermIds, instance.getAllTermIds()); Set<TermId> nonObsoleteTermIds = terms.stream() .filter(term -> !term.isObsolete()) .map(Term::getId) .collect(Collectors.toSet()); assertEquals(nonObsoleteTermIds, instance.getNonObsoleteTermIds()); assertEquals(child3Id, instance.getPrimaryTermId(child3Id)); assertEquals(child3Id, instance.getPrimaryTermId(child3SecondaryId)); assertEquals(ImmutableSet.of(rootId), instance.getParentTermIds(child2Id)); assertEquals(ImmutableSet.of(child2Id), instance.getParentTermIds(child3Id)); assertEquals(ImmutableSet.of(child3Id, child2Id, rootId), instance.getAncestorTermIds(child3Id)); assertEquals(ImmutableSet.of(child3Id, child2Id), instance.getAncestorTermIds(child3Id, false)); }
@Test void testBuilderNoRoot() { TermId childId = TermId.of("HP:0000001"); Term child = Term.of(childId, "child"); TermId child2Id = TermId.of("HP:0000002"); Term child2 = Term.of(child2Id, "child2"); assertThrows(PhenolRuntimeException.class, () -> ImmutableOntology.builder() .terms(ImmutableList.of(child, child2)) .relationships(ImmutableList.of()) .build(), "No root candidate found."); }