/**
   * Adds tokens to the output places of a given element
   *
   * @param element element to fire
   */
  private void addTokensOutputPlaces(int element) {

    /*
     * Pseudo-code: 1. Retrieve all elements in the OUT set of 'element' 2.
     * Increase the number of tokens for every output element
     */

    HNSet set = null;

    // update global counter for number of tokens
    // note that OR-situations count as a single token.

    set = hNet.getOutputSet(element);
    if (set.size() == 0) { // element is connected to the end place
      numberTokens++;
      endPlace++;
    } else {
      numberTokens += set.size();
    }

    // update marking...
    for (int iSet = 0; iSet < set.size(); iSet++) {
      increaseNumberTokens(element, set.get(iSet));
    }
  }
  /** Creates an initial marking. */
  private void createMarking() {

    HNSet set = null;

    marking = null;
    marking = new HashMap[size];

    // Building the marking data structure. This structure is an array of
    // hashmaps.
    // For a given hashmap, we have:
    // - Key: Integer = element
    // - Value: HashMap = subsets + the number of tokens associated to them
    // The idea is to keep track of which tasks fired and the number of
    // tokens at their output subsets.
    // Initially, the number of tokens is 0.
    for (int i = 0; i < size; i++) {

      set = hNet.getOutputSet(i);
      HashMap map = new HashMap();
      for (int j = 0; j < set.size(); j++) {
        map.put(set.get(j), new Integer(0));
      }
      marking[i] = map;
    }

    auxMapping = new MappingToSubsets(hNet);
    this.reset();
  }
  /**
   * Removes one token from the marked output places (subsets) of the tasks in <i>tasks</i> that
   * contain <i>element</i>.
   *
   * @param element element to fire
   * @param tasks whose output places point to element
   */
  private void removeTokensOutputPlaces(int element, HNSubSet tasks) {

    HNSubSet subset = null;
    HNSet subsets = null;

    Integer tokens = null;
    int taskToFire;
    int task;

    // Checking if element is a start element
    if (hNet.getInputSet(element).size() == 0) {
      if (startPlace > 0) {
        startPlace--;
        numberTokens--;
      }
    } else {
      taskToFire = element;
      for (int iTasks = 0; iTasks < tasks.size(); iTasks++) {
        task = tasks.get(iTasks);
        subsets = auxMapping.getRelatedSubsets(taskToFire, task);
        if (subsets != null) {
          for (int iSubsets = 0; iSubsets < subsets.size(); iSubsets++) {
            subset = subsets.get(iSubsets);
            tokens = getNumberTokens(task, subset);
            if (tokens.intValue() > 0) {
              decreaseNumberTokens(task, subset);
              numberTokens--;
            }
          }
        }
      }
    }
  }
  private void insertInMapping(int taskInSubset, int task, HNSubSet subset) {

    HNSet hSet = mapping[taskInSubset][task];

    if (hSet == null) {
      hSet = new HNSet();
    }

    hSet.add(subset);
    mapping[taskInSubset][task] = hSet;
  }
  private boolean allSubsetsAreMarked(int inputTask, HNSet outputSet) {

    HNSubSet subset = null;

    for (int iOutputSet = 0; iOutputSet < outputSet.size(); iOutputSet++) {
      subset = outputSet.get(iOutputSet);
      if (((Integer) marking[inputTask].get(subset)).intValue() <= 0) {
        return false;
      }
    }

    return true;
  }
  private HNSet getAlreadyMarkedPlaces(HNSet set, int task) {
    HNSet markedPlaces = null;
    HNSubSet subset = null;

    markedPlaces = new HNSet();

    for (int iSet = 0; iSet < set.size(); iSet++) {
      subset = set.get(iSet);
      if (subset.contains(task)) {
        markedPlaces.add(subset);
      }
    }

    return markedPlaces;
  }
  private void buildMapping(HeuristicsNet hn) {

    HNSet outputSubsets;
    HNSubSet outputSubset;
    int taskInSubset;

    mapping = new HNSet[hn.size()][hn.size()];

    for (int task = 0; task < hn.size(); task++) {
      outputSubsets = hn.getOutputSet(task);
      for (int iOutputSubsets = 0; iOutputSubsets < outputSubsets.size(); iOutputSubsets++) {
        // inserting for every element
        outputSubset = outputSubsets.get(iOutputSubsets);
        for (int iSubset = 0; iSubset < outputSubset.size(); iSubset++) {
          taskInSubset = outputSubset.get(iSubset);
          insertInMapping(taskInSubset, task, outputSubset);
        }
      }
    }
  }
  private CombinationTasksToFire findBestCombination(
      CombinationTasksToFire bCombination,
      HNSet inputSet,
      int rootTask,
      CombinationTasksToFire combination,
      HNSubSet treatedTasks) {

    int task = -1;
    HNSet alreadyMarkedPlaces = null;
    HNSet temp_inputSet = null;
    HNSubSet noTokensFromTasks = null;
    HNSubSet subset = null;

    if ((bCombination.getTasks().size() == 0)
        || (bCombination.getNumberMissingTokens() > combination.getNumberMissingTokens())) {
      if (rootTask != ROOT_TASK_ID) {
        alreadyMarkedPlaces = getAlreadyMarkedPlaces(inputSet, rootTask);
        noTokensFromTasks = HNSet.getUnionSet(alreadyMarkedPlaces);
        inputSet.removeAll(alreadyMarkedPlaces);
        combination.getTasks().add(rootTask);
      }

      if (inputSet.size() == 0) {
        bCombination = combination.copy();
      } else {

        // akam: I stoppe here - 10/07/2005
        if (rootTask != ROOT_TASK_ID) {
          temp_inputSet = new HNSet();
          for (int iInputSet = 0; iInputSet < inputSet.size(); iInputSet++) {
            subset = inputSet.get(iInputSet);
            subset.removeAll(noTokensFromTasks);
            subset.removeAll(treatedTasks);
            if (subset.size() == 0) {
              combination.setTokens(combination.getNumberMissingTokens() + 1);
            } else {
              temp_inputSet.add(subset);
            }
          }
          inputSet = temp_inputSet;
        }

        for (int iInputSet = 0; iInputSet < inputSet.size(); iInputSet++) {
          subset = inputSet.get(iInputSet);
          while (subset.size() > 0) {
            task = subset.get(generator.nextInt(subset.size()));
            bCombination =
                findBestCombination(
                    bCombination,
                    inputSet.deepCopy(),
                    task,
                    combination.copy(),
                    treatedTasks.deepCopy());
            treatedTasks.add(task);
            subset.remove(task);
          }
        }
      }
    }

    return bCombination;
  }
  private CombinationTasksToFire findBestSetTasks(int element) {
    CombinationTasksToFire bCombination = null;
    CombinationTasksToFire combination = null;
    HNSubSet noTokensFromTasks = null;
    HNSubSet treatedTasks = null;
    HNSet inputSet = null;
    HNSet temp_inputSet = null;

    HNSubSet subset = null;

    int numberMissingTokens = 0;
    int rootTask = ROOT_TASK_ID;

    bCombination = new CombinationTasksToFire();

    inputSet = hNet.getInputSet(element);

    if (inputSet.size() == 0) {
      if (startPlace <= 0) {
        numberMissingTokens++; // one token is missing
      }
    } else {
      // inputSubset is not empty. Search for tasks that "have tokens" to
      // element

      noTokensFromTasks = getTasksWithEmptyOutputPlaces(element);

      // >>>>>>>>>>>>>>>>>> Hint!!! I think that's why I don't run into
      // problems...
      // /// Idea -> shrink the subsets without using a temp variable, get
      // / the size before shrinking, shrink them, reorder the set and
      // /remove the empty set (do this via a method in the class
      // /HNSet, get the new size. This is the number of missing tokens.

      // make a copy to avoid destroying the original net
      inputSet = inputSet.deepCopy();
      temp_inputSet = new HNSet();
      // removing the tasks whose output subsets that contain element are
      // empty
      for (int iInputSubsets = 0; iInputSubsets < inputSet.size(); iInputSubsets++) {
        subset = inputSet.get(iInputSubsets);
        subset.removeAll(noTokensFromTasks);
        if (subset.size() == 0) {
          numberMissingTokens += 1;
        } else {
          temp_inputSet.add(subset);
        }
      }
      inputSet = temp_inputSet;
      // retrieving the best combination of tasks that can fire to enable
      // element

      if (inputSet.size() > 0) {
        combination = new CombinationTasksToFire();
        treatedTasks = new HNSubSet();
        bCombination =
            findBestCombination(bCombination, inputSet, rootTask, combination, treatedTasks);
      }
    }

    bCombination.setElementToFire(element);
    bCombination.setTokens(bCombination.getNumberMissingTokens() + numberMissingTokens);

    return bCombination;
  }