Java Code Examples for com.google.common.collect.ImmutableMultiset#Builder

The following examples show how to use com.google.common.collect.ImmutableMultiset#Builder . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: FibonacciQueueTest.java    From grakn with GNU Affero General Public License v3.0 6 votes vote down vote up
@Test
public void testLotsOfRandomInserts() {
    int lots = 50000;
    final FibonacciQueue<Integer> queue = FibonacciQueue.create();
    // Insert lots of random numbers.
    final ImmutableMultiset.Builder<Integer> insertedBuilder = ImmutableMultiset.builder();
    for (int i = 0; i < lots; i++) {
        int r = random.nextInt();
        insertedBuilder.add(r);
        queue.add(r);
    }
    final Multiset<Integer> inserted = insertedBuilder.build();
    assertEquals(lots, queue.size());
    // Ensure it contains the same multiset of values that we put in
    assertEquals(inserted, ImmutableMultiset.copyOf(queue));
    // Ensure the numbers come out in increasing order.
    final List<Integer> polled = Lists.newLinkedList();
    while (!queue.isEmpty()) {
        polled.add(queue.poll());
    }
    assertTrue(Ordering.<Integer>natural().isOrdered(polled));
    // Ensure the same multiset of values came out that we put in
    assertEquals(inserted, ImmutableMultiset.copyOf(polled));
    assertEquals(0, queue.size());
}
 
Example 2
Source File: CorpusAnalysis.java    From tac-kbp-eal with MIT License 6 votes vote down vote up
public static ImmutableMultiset<Symbol> toMentionTypeCounts(
    final ImmutableSet<TypeRoleFillerRealis> targetEquivClasses,
    final ImmutableMultimap<TypeRoleFillerRealis, AssessedResponse> equivClassToAssessedResponse) {
  final ImmutableMultiset.Builder<Symbol> mentionTypes = ImmutableMultiset.builder();

  for(final TypeRoleFillerRealis equivClass : targetEquivClasses) {
    final AssessedResponse assessedResponse = Collections.max(equivClassToAssessedResponse.get(equivClass), Old2014ID);

    if(assessedResponse.response().role() == TIME) {
      mentionTypes.add(TIME);
    }
    else {
      final Optional<FillerMentionType> mentionType =
          assessedResponse.assessment().mentionTypeOfCAS();
      if (mentionType.isPresent()) {
        mentionTypes.add(Symbol.from(mentionType.get().name()));
      }
    }
  }

  return Multisets.copyHighestCountFirst(mentionTypes.build());
}
 
Example 3
Source File: CorpusAnalysis.java    From tac-kbp-eal with MIT License 6 votes vote down vote up
public static ImmutableMultiset<Symbol> toEventHopperCounts(final ImmutableSet<EventArgumentLinking> linkings) {
  final ImmutableMultiset.Builder<Symbol> ret = ImmutableMultiset.builder();

  for(final EventArgumentLinking eal : linkings) {
    for (final TypeRoleFillerRealisSet ef : eal.eventFrames()) {
      final ImmutableSet<Symbol> eventTypes = ImmutableSet.copyOf(FluentIterable.from(ef.asSet())
          .transform(type()));

      if(eventTypes.size()==1) {
        final Symbol eventType = FluentIterable.from(eventTypes).first().get();
        ret.add(eventType);
      }
      else {
        log.info("ERROR: a responseLinking set from document {} has multiple event types", eal.docID().toString());
      }
    }
  }

  return Multisets.copyHighestCountFirst(ret.build());
}
 
Example 4
Source File: CorpusAnalysis.java    From tac-kbp-eal with MIT License 6 votes vote down vote up
public static ImmutableMultiset<Symbol> toNumberOfDocsPerEventType(
    final ImmutableSet<TypeRoleFillerRealis> equivClasses) {

  // for each docid, a set of event types
  final ImmutableMap<Symbol, ImmutableSet<Symbol>> eventTypesInEachDoc = getEventTypesInEachDoc(equivClasses);

  final ImmutableMultiset.Builder<Symbol> ret = ImmutableMultiset.builder();

  for(final Map.Entry<Symbol, ImmutableSet<Symbol>> entry : eventTypesInEachDoc.entrySet()) {
    for(final Symbol et : entry.getValue()) {
      ret.add(et);
    }
  }

  return Multisets.copyHighestCountFirst(ret.build());
}
 
Example 5
Source File: ElementCostOfDataStructures.java    From memory-measurer with Apache License 2.0 5 votes vote down vote up
public ImmutableMultiset construct(int entries) {
  ImmutableMultiset.Builder builder = ImmutableMultiset.builder();
  Object key = newEntry();
  for (int i = 0; i < entries; i++) {
    builder.add(key);
  }
  return builder.build();
}
 
Example 6
Source File: TestJdbcQueryBuilder.java    From presto with Apache License 2.0 5 votes vote down vote up
private static Multiset<List<Object>> read(ResultSet resultSet)
        throws SQLException
{
    ImmutableMultiset.Builder<List<Object>> result = ImmutableMultiset.builder();
    while (resultSet.next()) {
        ImmutableList.Builder<Object> row = ImmutableList.builder();
        for (int column = 1; column <= resultSet.getMetaData().getColumnCount(); column++) {
            row.add(resultSet.getObject(column));
        }
        result.add(row.build());
    }
    return result.build();
}
 
Example 7
Source File: ElementCostOfDataStructures.java    From memory-measurer with Apache License 2.0 5 votes vote down vote up
public ImmutableMultiset construct(int entries) {
  ImmutableMultiset.Builder builder = ImmutableMultiset.builder();
  for (int i = 0; i < entries; i++) {
    builder.add(newEntry());
  }
  return builder.build();
}
 
Example 8
Source File: AttributeAggregate.java    From attic-aurora with Apache License 2.0 5 votes vote down vote up
private static ImmutableMultiset.Builder<Pair<String, String>> addAttributes(
    ImmutableMultiset.Builder<Pair<String, String>> builder,
    Iterable<IAttribute> attributes) {

  for (IAttribute attribute : attributes) {
    for (String value : attribute.getValues()) {
      builder.add(Pair.of(attribute.getName(), value));
    }
  }
  return builder;
}
 
Example 9
Source File: VerifyBamId.java    From dataflow-java with Apache License 2.0 5 votes vote down vote up
@ProcessElement
public void processElement(ProcessContext c) throws Exception {
  AlleleFreq af = null;
  af = c.element().getValue().getOnly(refFreqTag, null);
  if (af == null || af.getAltBases() == null) {
    // no ref stats
    return;
  }
  if (af.getAltBases().size() != 1) {
    throw new IllegalArgumentException("Wrong number (" + af.getAltBases().size() + ") of"
        + " alternate bases for Position " + c.element().getKey());
  }

  Iterable<ReadBaseQuality> reads = c.element().getValue().getAll(readCountsTag);

  ImmutableMultiset.Builder<ReadQualityCount> rqSetBuilder = ImmutableMultiset.builder();
  for (ReadBaseQuality r : reads) {
    ReadQualityCount.Base b;
    if (af.getRefBases().equals(r.getBase())) {
      b = ReadQualityCount.Base.REF;
    } else if (af.getAltBases().get(0).equals(r.getBase())) {
      b = ReadQualityCount.Base.NONREF;
    } else {
      b = ReadQualityCount.Base.OTHER;
    }
    ReadQualityCount rqc = new ReadQualityCount();
    rqc.setBase(b);
    rqc.setQuality(r.getQuality());
    rqSetBuilder.add(rqc);
  }

  ReadCounts rc = new ReadCounts();
  rc.setRefFreq(af.getRefFreq());
  for (Multiset.Entry<ReadQualityCount> entry : rqSetBuilder.build().entrySet()) {
    ReadQualityCount rq = entry.getElement();
    rq.setCount(entry.getCount());
    rc.addReadQualityCount(rq);
  }
  c.output(KV.of(c.element().getKey(), rc));
}
 
Example 10
Source File: DerivedQuerySelector2016.java    From tac-kbp-eal with MIT License 5 votes vote down vote up
private ImmutableSetMultimap<RoleAndID, DocAndHopper> indexArgsToEventHopper(final Symbol docID,
    final EREDocument ereDoc, final ImmutableMultiset.Builder<String> allEREEventTypes) {
  final ImmutableSetMultimap.Builder<RoleAndID, DocAndHopper> argsToDocEventsB =
      ImmutableSetMultimap.builder();

  for (final EREEvent ereEvent : ereDoc.getEvents()) {
    boolean loggedType = false;

    for (final EREEventMention ereEventMention : ereEvent.getEventMentions()) {
      for (final EREArgument ereEventArg : ereEventMention.getArguments()) {
        if (ereEventArg instanceof EREEntityArgument) {
          final Optional<EREEntity> entityFiller = ((EREEntityArgument) ereEventArg).ereEntity();
          if (entityFiller.isPresent()) {
            final Optional<Symbol> mappedEventType =
                ScoringUtils.mapERETypesToDotSeparated(ontologyMapper, ereEventMention);
            final Optional<Symbol> mappedEventRole =
                ontologyMapper.eventRole(Symbol.from(ereEventArg.getRole()));

            if (mappedEventType.isPresent() && mappedEventRole.isPresent()) {
              if (!loggedType) {
                // we only want to log events which meet the criteria above, but we only
                // want to count each document event once
                allEREEventTypes.add(mappedEventType.get().asString());
                loggedType = true;
              }
              argsToDocEventsB.put(
                  RoleAndID.of(
                      mappedEventType.get().asString(),
                      mappedEventRole.get().asString(), entityFiller.get().getID()),
                  DocAndHopper.of(docID.asString(), ereEvent.getID(),
                      mappedEventType.get().asString()));
            }
          }
        }
      }
    }
  }

  return argsToDocEventsB.build();
}
 
Example 11
Source File: ElementCostOfDataStructures.java    From memory-measurer with Apache License 2.0 5 votes vote down vote up
public ImmutableMultiset construct(int entries) {
  ImmutableMultiset.Builder builder = ImmutableMultiset.builder();
  Object key = newEntry();
  for (int i = 0; i < entries; i++) {
    builder.add(key);
  }
  return builder.build();
}
 
Example 12
Source File: ElementCostOfDataStructures.java    From memory-measurer with Apache License 2.0 5 votes vote down vote up
public ImmutableMultiset construct(int entries) {
  ImmutableMultiset.Builder builder = ImmutableMultiset.builder();
  for (int i = 0; i < entries; i++) {
    builder.add(newEntry());
  }
  return builder.build();
}
 
Example 13
Source File: AbstractUtilizationPlacementStrategy.java    From bboxdb with Apache License 2.0 5 votes vote down vote up
/**
 * Calculate the usage of each system
 * 
 * @return
 * @throws ZookeeperException
 * @throws ZookeeperNotFoundException 
 * @throws BBoxDBException 
 */
protected Multiset<BBoxDBInstance> calculateSystemUsage() 
		throws ZookeeperException, ZookeeperNotFoundException, BBoxDBException {
			
	final DistributionGroupAdapter zookeeperAdapter 
		= ZookeeperClientFactory.getZookeeperClient().getDistributionGroupAdapter();
	
	final List<String> distributionGroups = zookeeperAdapter.getDistributionGroups();
	
	// The overall usage
    final ImmutableMultiset.Builder<BBoxDBInstance> builder = ImmutableMultiset.builder();
    
	// Calculate usage for each distribution group
	for(final String groupName : distributionGroups) {
		final SpacePartitioner spacepartitioner = SpacePartitionerCache
			.getInstance().getSpacePartitionerForGroupName(groupName);
		
		final DistributionRegion region = spacepartitioner.getRootNode();
		final Multiset<BBoxDBInstance> regionSystemUsage 
			= DistributionRegionHelper.getSystemUtilization(region);
	
		// Merge results
		builder.addAll(regionSystemUsage);
	}
	
    return builder.build();
}
 
Example 14
Source File: ConcurrentHashMultimap.java    From titus-control-plane with Apache License 2.0 5 votes vote down vote up
/**
 * Copies contents into an immutable <tt>Multiset</tt> view.
 *
 * @return a copied view of all keys, with counts for each one of them
 */
@Override
public Multiset<K> keys() {
    final ImmutableMultiset.Builder<K> builder = ImmutableMultiset.builder();
    entries.forEach((key, values) -> builder.setCount(key, values.size()));
    return builder.build();
}
 
Example 15
Source File: CorefAnnotation.java    From tac-kbp-eal with MIT License 4 votes vote down vote up
public CorefAnnotation copyMerging(CorefAnnotation toMerge) {
  checkArgument(docId == toMerge.docId());
  // (1) initialize our mapping of CASes to coref indices with the
  // mappings seen in *baseline*. This is used to track the mappings during
  // construction, but ret (below) is used to construct the result
  final Map<KBPString, Integer> newCASToCoref = Maps.newHashMap(CASesToIDs);
  final Builder ret = strictCopyBuilder();

  // the next empty index for adding a new coref cluster
  // this potentially could fail due to overflow, but the chances are tiny and
  // the stakes are low, so we won't worry about it
  int nextIndex = newCASToCoref.isEmpty() ? 1 : (1 + Collections.max(newCASToCoref.values()));

  // (2) gather a map of coref IDs to the CASes in the corresponding cluster
  // from *additional*
  final Multimap<Integer, KBPString> additionalCorefToCAS = toMerge.idToCASes;

  for (final Map.Entry<KBPString, Integer> newCorefRelation : toMerge.CASesToIDs.entrySet()) {
    final int corefId = newCorefRelation.getValue();
    final KBPString CAS = newCorefRelation.getKey();

    if (!newCASToCoref.containsKey(CAS)) {
      // this CAS was not previously coreffed
      final StringBuilder msg = new StringBuilder();

      // use the baseline cluster which contains most  clustermates of CAS
      // in "additional"
      final Collection<KBPString> clusterMates = additionalCorefToCAS.get(
          corefId);
      msg.append("\t\tFor CAS ").append(CAS).append(" no coref ID was found in baseline. It has ")
          .append(clusterMates.size()).append(" clustermates\n");

      // We use an ImmutableMultiset to have deterministic iteration order
      // and therefore deterministic tie-breaking if two clusters tie
      // on clustermate count
      final ImmutableMultiset.Builder<Integer> bCorefIDsOfClusterMatesInBaseline =
          ImmutableMultiset.builder();
      for (final KBPString clusterMate : clusterMates) {
        final Integer corefForClusterMate = newCASToCoref.get(clusterMate);
        if (corefForClusterMate != null) {
          // we don't have to worry about iterating over the target CAS itself
          // because we know it will never pass this check
          bCorefIDsOfClusterMatesInBaseline.add(corefForClusterMate);
          msg.append("\t\t\t").append(clusterMate).append(" ---> ")
              .append(corefForClusterMate).append("\n");
        } else {
          msg.append("\t\t\t").append(clusterMate).append(" ---> unknown\n");
        }
      }

      final ImmutableMultiset<Integer> corefIDsOfClusterMatesInBaseline =
          bCorefIDsOfClusterMatesInBaseline.build();

      final int newCorefIdx;
      if (!corefIDsOfClusterMatesInBaseline.isEmpty()) {
        // we know this will be non-null due to the above check
        newCorefIdx = getFirst(copyHighestCountFirst(corefIDsOfClusterMatesInBaseline), null);
        msg.append("\t\tMapping to dominant cluster mate cluster ").append(newCorefIdx)
            .append("\n");
      } else {
        // if we had no clustermates with known coref indices, start a new cluster.
        // When it comes time to assign coref to our clustermates, if any,
        // then we know they will have at least one clustermate with a known coref ID
        newCorefIdx = nextIndex;
        nextIndex = nextFreeIndex(nextIndex, newCASToCoref);
        msg.append("\t\tMapping to new cluster ").append(newCorefIdx).append("\n");
      }
      log.info(msg.toString());
      newCASToCoref.put(CAS, newCorefIdx);
      ret.corefCAS(CAS, newCorefIdx);
    }
  }

  for (final KBPString unannotatedCAS : toMerge.unannotated) {
    ret.addUnannotatedCAS(unannotatedCAS);
  }

  return ret.build();
}
 
Example 16
Source File: DerivedQuerySelector2016.java    From tac-kbp-eal with MIT License 4 votes vote down vote up
private ImmutableSetMultimap<String, EntryPoint> gatherEntryPointsFound(
    final ImmutableSet<Symbol> docIDsToScore, final File baseSystemDir,
    final Set<String> systemsToUse, final ImmutableMap<Symbol, File> goldDocIDToFileMap,
    final CoreNLPXMLLoader coreNLPXMLLoader,
    final ImmutableMap<Symbol, File> coreNLPProcessedRawDocs) throws IOException {
  final ImmutableSetMultimap.Builder<String, EntryPoint> entryPointsFoundBySystemB =
      ImmutableSetMultimap.builder();

  // we log the distribution of event types in the corpus for diagnostic purposes
  final ImmutableMultiset.Builder<String> allEREEventTypes = ImmutableMultiset.builder();

  for (Symbol docID : docIDsToScore) {
    // EvalHack - 2016 dry run contains some files for which Serif spuriously adds this document ID
    docID = Symbol.from(docID.asString().replace("-kbp", ""));
    final EREDocument ereDoc = getEREDocument(docID, goldDocIDToFileMap);
    final CoreNLPDocument coreNLPDoc =
        coreNLPXMLLoader.loadFrom(coreNLPProcessedRawDocs.get(docID));
    final EREAligner ereAligner = EREAligner.create(ereDoc, Optional.of(coreNLPDoc),
        ontologyMapper);

    // build an index of RoleAndIds to hoppers for lookup in the loop below
    final ImmutableSetMultimap<RoleAndID, DocAndHopper> argsToDocEvents =
        indexArgsToEventHopper(docID, ereDoc, allEREEventTypes);

    for (final String system : systemsToUse) {
      final File systemDir = new File(baseSystemDir, system);

      try (final SystemOutputStore systemOutput = KBPEA2016OutputLayout.get().open(systemDir)) {
        final ImmutableSet.Builder<RoleAndID> alignedEREArgs = ImmutableSet.builder();

        // first, gather all document-level arguments which this system found which can
        // be aligned to ERE
        final Iterable<Response> responses = FluentIterable
            .from(systemOutput.read(docID).arguments().responses())
            .filter(BANNED_ROLES_FILTER)
            .filter(Predicates.compose(not(in(BANNED_EVENTS)), type()));

        for (final Response response : responses) {
          final Optional<ScoringCorefID> argID =
              ereAligner.argumentForResponse(response);
          // query entry points can only be entities, not value fillers
          if (argID.isPresent() && argID.get().scoringEntityType().isEntityType()) {
            alignedEREArgs.add(RoleAndID.of(response.type().asString(), response.role().asString(),
                argID.get().withinTypeID()));
          }
        }

        // second, map these to document-level hoppers
        for (final RoleAndID alignedArg : alignedEREArgs.build()) {
          for (final DocAndHopper docAndHopper : argsToDocEvents.get(alignedArg)) {
            entryPointsFoundBySystemB.put(system, EntryPoint.of(docAndHopper, alignedArg));
          }
        }
      }
    }
  }

  log.info("Distribution of event types in ERE: {}",
      Multisets.copyHighestCountFirst(allEREEventTypes.build()));

  return entryPointsFoundBySystemB.build();
}
 
Example 17
Source File: Collection.java    From txtUML with Eclipse Public License 1.0 4 votes vote down vote up
@Override
ImmutableMultiset<E> createBackend(Consumer<Builder<E>> backendBuilder) {
	ImmutableMultiset.Builder<E> builder = ImmutableMultiset.builder();
	backendBuilder.accept(builder::add);
	return builder.build();
}
 
Example 18
Source File: Guavate.java    From Strata with Apache License 2.0 3 votes vote down vote up
/**
 * Collector used at the end of a stream to build an immutable multiset.
 * <p>
 * A collector is used to gather data at the end of a stream operation.
 * This method returns a collector allowing streams to be gathered into
 * an {@link ImmutableMultiset}.
 * 
 * @param <T>  the type of element in the multiset
 * @return the immutable multiset collector
 */
public static <T> Collector<T, ImmutableMultiset.Builder<T>, ImmutableMultiset<T>> toImmutableMultiset() {
  return Collector.of(
      ImmutableMultiset.Builder<T>::new,
      ImmutableMultiset.Builder<T>::add,
      (l, r) -> l.addAll(r.build()),
      ImmutableMultiset.Builder<T>::build,
      Collector.Characteristics.UNORDERED);
}