### Question:
BagOfTermsWithFrequencies implements InferWithHPOHierarchy { @Override public void infer() { this.inferred = new LinkedHashMap<>(termCounts); termCounts.entrySet().forEach(entry -> { Set<TermId> ancesstors = OntologyAlgorithm.getAncestorTerms(hpo, entry.getKey(), false); ancesstors.forEach(t -> { inferred.putIfAbsent(t, 0); inferred.put(t, inferred.get(t) + entry.getValue()); }); }); } BagOfTermsWithFrequencies(String patientId, Ontology hpo);  BagOfTermsWithFrequencies(String patientId, Map<TermId, Integer> termCounts, Ontology hpo); void addTerm(TermId termId, int count); String getPatientId(); Map<TermId, Integer> getOriginalTermCounts(); Map<TermId, Integer> getInferredTermCounts(); @Override void infer(); @Override String toString();  }### Answer:
@Test public void infer() { assertNotNull(hpo); BagOfTermsWithFrequencies bag1 = new BagOfTermsWithFrequencies(patientId, hpo); assertNotNull(bag1); bag1.addTerm(TermId.of(HP_PREFIX, "0003074"), 5); assertEquals(bag1.getOriginalTermCounts().size(), 1); bag1.addTerm(TermId.of(HP_PREFIX, "0011297"), 3); bag1.addTerm(TermId.of(HP_PREFIX, "0040064"), 1); assertEquals(bag1.getOriginalTermCounts().size(), 3); bag1.infer(); Map<TermId, Integer> inferred = bag1.getInferredTermCounts(); TermId all = TermId.of(HP_PREFIX, "0000001"); assertEquals(inferred.get(all).longValue(), 9); TermId phenotypicAbnormality = TermId.of(HP_PREFIX, "0000118"); assertEquals(inferred.get(phenotypicAbnormality).longValue(), 9); TermId abnormalityOfLimbs = TermId.of(HP_PREFIX, "0040064"); assertEquals(inferred.get(abnormalityOfLimbs).longValue(), 4); TermId abnormalGlucoseHomeostasis = TermId.of(HP_PREFIX, "0011014"); assertEquals(inferred.get(abnormalGlucoseHomeostasis).longValue(), 5); }