예제 #1
1
  /**
   * Assign the given list of units to the emergency.
   *
   * @param units The given list of units.
   * @post All the units in the given list are assigned | forall (u in units) | u.isAssigned()
   * @post All the units in the given list are handling the emergency of this UnitNeeded | forall (u
   *     in units) | u.getEmergency().equals(this.getEmergency())
   * @throws InvalidEmergencyException If the units can't be assigned to the emergency (when
   *     canAssignUnitsToEmergency fails)
   * @see #canAssignUnitsToEmergency(Set)
   */
  @Override
  public synchronized void assignUnitsToEmergency(Set<Unit> units)
      throws InvalidEmergencyException {
    ArrayList<Unit> options = new ArrayList<Unit>(units.size());
    Iterator<Unit> it = units.iterator();
    while (it.hasNext()) {
      options.add(it.next());
    }

    if (!canAssignUnitsToEmergency(units)) {
      throw new InvalidEmergencyException(
          "Units can't be assigned to the emergency, harm to assignment constraints.");
    }
    for (Emergency e : getDisaster().getEmergencies()) {
      ConcreteUnitsNeeded CUN = e.getUnitsNeeded();
      Set<Unit> unitsForEmergency = CUN.generateProposal(options);
      try {
        e.assignUnits(unitsForEmergency);
      } catch (InvalidEmergencyStatusException ex) {
        // We assume this can't happen.
        Logger.getLogger(DerivedUnitsNeeded.class.getName()).log(Level.SEVERE, null, ex);
      }
      for (Unit u : unitsForEmergency) {
        addWorkingUnits(u);
      }
      options.removeAll(unitsForEmergency);
    }
  }