+
+ @Override
+ public List<String> getRootParents(final String term)
+ {
+ /*
+ * check in cache first
+ */
+ if (rootParents.containsKey(term))
+ {
+ return rootParents.get(term);
+ }
+
+ List<String> top = new ArrayList<>();
+ List<String> query = new ArrayList<>();
+ query.add(term);
+
+ while (!query.isEmpty())
+ {
+ List<String> nextQuery = new ArrayList<>();
+ for (String q : query)
+ {
+ List<String> theParents = parents.get(q);
+ if (theParents != null)
+ {
+ if (theParents.size() == 1 && theParents.get(0).equals(q))
+ {
+ /*
+ * top-level term
+ */
+ if (!top.contains(q))
+ {
+ top.add(q);
+ }
+ }
+ else
+ {
+ for (String p : theParents)
+ {
+ if (!p.equals(q))
+ {
+ nextQuery.add(p);
+ }
+ }
+ }
+ }
+ }
+ query = nextQuery;
+ }
+
+ rootParents.put(term, top);
+
+ return top.isEmpty() ? null : top;
+ }
+
+ @Override
+ public List<String> getParents(String term)
+ {
+ List<String> result = parents.get(term);
+ return result == null ? new ArrayList<>() : result;
+ }
+
+ @Override
+ public boolean isValidTerm(String term)
+ {
+ return parents.containsKey(term);
+ }