private <T> void addToDataVectors( NamedStructureEquivalenceSets<T> eSets, Map<String, StringBuilder> vectors) { for (Set<NamedStructureEquivalenceSet<T>> eSet : eSets.getSameNamedStructures().values()) { // make sure the number of nodes with this name is large enough int count = 0; for (NamedStructureEquivalenceSet<T> eClass : eSet) { count += eClass.getNodes().size(); } if (count < THRESHOLD) { continue; } for (StringBuilder sb : vectors.values()) { sb.append('0'); } char className = 'a'; boolean nameOnly = _nameOnly; for (NamedStructureEquivalenceSet<T> eClass : eSet) { for (String node : eClass.getNodes()) { StringBuilder sb = vectors.get(node); if (nameOnly) { sb.setCharAt(sb.length() - 1, '1'); } else { sb.setCharAt(sb.length() - 1, className); } } className++; } } }
private <T> void addNamedStructureOutliers( OutliersHypothesis hypothesis, NamedStructureEquivalenceSets<T> equivSet, SortedSet<NamedStructureOutlierSet<?>> rankedOutliers) { String structType = equivSet.getStructureClassName(); for (Map.Entry<String, SortedSet<NamedStructureEquivalenceSet<T>>> entry : equivSet.getSameNamedStructures().entrySet()) { String name = entry.getKey(); SortedSet<NamedStructureEquivalenceSet<T>> eClasses = entry.getValue(); NamedStructureEquivalenceSet<T> max = eClasses.stream() .max(Comparator.comparingInt(es -> es.getNodes().size())) .orElseThrow( () -> new BatfishException( "Named structure " + name + " has no equivalence classes")); SortedSet<String> conformers = max.getNodes(); SortedSet<String> outliers = eClasses.stream() .filter(eClass -> eClass != max) .flatMap(eClass -> eClass.getNodes().stream()) .collect(ImmutableSortedSet.toImmutableSortedSet(Comparator.naturalOrder())); rankedOutliers.add( new NamedStructureOutlierSet<>( hypothesis, structType, name, max.getNamedStructure(), conformers, outliers)); } }
public void addEntry( String structureName, String hostname, T structure, boolean assumeAllUnique) { Map<Integer, Set<NamedStructureEquivalenceSet<T>>> sameNamedStructuresByHash = _sameNamedStructuresByNameAndHash.computeIfAbsent(structureName, s -> new HashMap<>()); String structureJson = writeObject(structure); int hash = structureJson.hashCode(); Set<NamedStructureEquivalenceSet<T>> eqSetsWithSameHash = sameNamedStructuresByHash.computeIfAbsent(hash, h -> new HashSet<>()); if (assumeAllUnique || eqSetsWithSameHash.isEmpty()) { eqSetsWithSameHash.add(new NamedStructureEquivalenceSet<>(hostname, structure)); } else { Optional<NamedStructureEquivalenceSet<T>> potentialMatchingSet = eqSetsWithSameHash.stream() .filter( s -> checkJsonStringEquals(structureJson, writeObject(s.getNamedStructure()))) .findAny(); if (potentialMatchingSet.isPresent()) { NamedStructureEquivalenceSet<T> matchingSet = potentialMatchingSet.get(); matchingSet.setNodes( new ImmutableSortedSet.Builder<String>(Comparator.naturalOrder()) .addAll(matchingSet.getNodes()) .add(hostname) .build()); } else { eqSetsWithSameHash.add(new NamedStructureEquivalenceSet<>(hostname, structure)); } } }
SortedSet<String> presentNodes = eSetSets.stream() .flatMap(eSet -> eSet.getNodes().stream()) .collect(ImmutableSortedSet.toImmutableSortedSet(Comparator.naturalOrder())); T struct = eSetSets.first().getNamedStructure();