public static Map<FeatureStructure, FeatureStructure> getNonIndexedFSesWithOwner(CAS aCas) { TypeSystem ts = aCas.getTypeSystem(); LowLevelCAS llcas = aCas.getLowLevelCAS(); Set<FeatureStructure> allIndexedFS = collectIndexed(aCas); Map<FeatureStructure, FeatureStructure> allReachableFS = new TreeMap<>( Comparator.comparingInt(llcas::ll_getFSRef)); FSIterator<FeatureStructure> i = aCas.getIndexRepository().getAllIndexedFS( aCas.getTypeSystem().getTopType()); i.forEachRemaining(fs -> collect(allReachableFS, allIndexedFS, fs, fs)); // Remove all that are not annotations allReachableFS.entrySet().removeIf(e -> !ts.subsumes(aCas.getAnnotationType(), e.getKey().getType())); // Remove all that are indexed allReachableFS.entrySet().removeIf(e -> e.getKey() == e.getValue()); // All that is left are non-index annotations return allReachableFS; } }
public static void collect(Set<FeatureStructure> aFSes, FeatureStructure aFS) { if (aFS != null && !aFSes.contains(aFS)) { aFSes.add(aFS); for (Feature f : aFS.getType().getFeatures()) { if (!f.getRange().isPrimitive() && !CAS.FEATURE_BASE_NAME_SOFA.equals(f.getShortName())) { collect(aFSes, aFS.getFeatureValue(f)); } } } }
/** * Recursively collect referenced FSes and also record for each the last indexed FS that refers * the them. */ public static void collect(Map<FeatureStructure, FeatureStructure> aFSes, Set<FeatureStructure> aIndexed, FeatureStructure aFS, FeatureStructure aLastIndexed) { if (aFS != null && !aFSes.containsKey(aFS)) { // We might find an annotation indirectly. In that case make sure we consider it as // an indexed annotation instead of wrongly recording it as non-indexed if (aIndexed.contains(aFS)) { aFSes.put(aFS, aFS); } else { aFSes.put(aFS, aLastIndexed); } for (Feature f : aFS.getType().getFeatures()) { if (!f.getRange().isPrimitive() && !CAS.FEATURE_BASE_NAME_SOFA.equals(f.getShortName())) { collect(aFSes, aIndexed, aFS.getFeatureValue(f), aIndexed.contains(aFS) ? aFS : aLastIndexed); } } } }
public static Set<FeatureStructure> collectReachable(CAS aCas) { LowLevelCAS llcas = aCas.getLowLevelCAS(); Set<FeatureStructure> fses = new TreeSet<>(Comparator.comparingInt(llcas::ll_getFSRef)); FSIterator<FeatureStructure> i = aCas.getIndexRepository().getAllIndexedFS( aCas.getTypeSystem().getTopType()); i.forEachRemaining(fs -> collect(fses, fs)); return fses; }