private <TDataContract> int getMaxEnsembleCount(
      String dataContractName, EnsembleDefinition ensembleDefinition) {
    List<RoleDefinition> roles = ensembleDefinition.getRoles();
    int minCardinalitiesSum = 0;
    for (RoleDefinition roleDefinition : roles) {
      if (roleDefinition.getType().toString().equals(dataContractName)) {
        minCardinalitiesSum += roleDefinition.getCardinalityMin();
      }
    }

    if (minCardinalitiesSum == 0) {
      return Integer.MAX_VALUE;
    }

    return getNumInstances(dataContractName) / Math.max(1, minCardinalitiesSum);
  }
  /*
  public int[] getGlobalIndices(List<? extends BaseDataContract> instances) {
  	int[] result = new int[instances.size()];

  	for (int i = 0; i < instances.size(); i++) {
  		result[i] = Integer.parseInt(instances.get(i).id);
  		usedComponentIds.add(result[i]);
  	}

  	return result;
  }

  public Set<Integer> getUsedComponentIds() {
  	return usedComponentIds;
  }
  */
  public <TDataContract> int getMaxEnsembleCount(
      EdlDocument edlDocument, EnsembleDefinition ensembleDefinition) {
    int result = Integer.MAX_VALUE;

    // TODO Extend to int ranges
    if (Extensions.hasDataContractBoundId(ensembleDefinition)) {
      QualifiedName idType = ensembleDefinition.getId().getType();
      result = components.get(idType.getName()).length;
    } else {
      for (DataContractDefinition contract : edlDocument.getDataContracts()) {
        int maxEnsembleCount = getMaxEnsembleCount(contract.getName(), ensembleDefinition);
        if (maxEnsembleCount < result) {
          result = maxEnsembleCount;
        }
      }
    }

    return result;
  }