Exemplo n.º 1
0
  /**
   * Returns the first process waiting in the queue that complies to the given condition. If there
   * is no such process waiting, <code>null</code> is returned.
   *
   * @return SimProcess : Returns the first process in the queue which complies to the given
   *     condition.
   * @param cond Condition : The Condition <code>cond</code> is describing the condition to which
   *     the process must comply to. This has to be implemented by the user in the class: <code>
   *     Condition</code> in the method: <code>check()</code>.
   */
  public P first(Condition<P> cond) {
    if (_queue.isEmpty()) { // nobody home to be checked
      return null;
    } // return null

    for (P process = _queue.first(); process != null; process = _queue.succ(process)) {
      if (cond.check(process)) return process;
    }

    // if no SimProcess complies to the condition just return null
    return null;
  } // end method
Exemplo n.º 2
0
  /**
   * Lets the current process wait in the CondQueue until a certain condition, given as a parameter,
   * has become true. When the process finds its condition to have become true or <code>checkAll
   * </code> is set to <code>true</code> the next process in the queue will be activated, too. If
   * the capacity limit of the queue is reached, the process will not be enqueued and <code>false
   * </code> returned.
   *
   * @return boolean : Is <code>true</code> if the process can be enqueued successfully, <code>false
   *     </code> otherwise (i.e. capacity limit of the queue is reached).
   * @param cond Condition : The condition that has to become true before the process can continue.
   * @see desmoj.core.simulator.Condition
   */
  public boolean waitUntil(Condition<P> cond) {
    _where = "boolean waitUntil (desmoj.core.simulator.Condition cond)";

    P currentProcess = (P) currentSimProcess();

    if (!checkProcess(currentProcess, _where)) // check the current process
    {
      return false;
    } // if it is not valid just return

    if (!this.isModelCompatible(cond)) // if cond is not modelcompatible
    {
      sendWarning(
          "Attempt to use a Condition object that does not "
              + "belong to this model. The attempted action is ignored!",
          "CondQueue: " + getName() + " Method: boolean waitUntil (Condition cond)",
          "The condition is not modelcompatible.",
          "Make sure that conditions given in a CondQueue waitUntil() method "
              + "are modelcompatible with the CondQueue object.");
      return false;
    }

    if (queueLimit <= length()) // check if capac. limit of queue is reached
    {
      if (currentlySendDebugNotes()) {
        sendDebugNote(
            "refuses to insert "
                + currentProcess.getQuotedName()
                + " in waiting-queue, because the capacity limit is reached. ");
      }

      if (currentlySendTraceNotes()) {
        sendTraceNote(
            "is refused to be enqueued in "
                + this.getQuotedName()
                + "because the capacity limit ("
                + getQueueLimit()
                + ") of the "
                + "queue is reached");
      }

      _refused++; // count the refused ones

      return false; // capacity limit is reached
    }

    _queue.insert(currentProcess); // insert every process in the queue for
    // statistic reasons

    boolean waitingCanceled = false;

    if (!cond.check(currentProcess)) // condition is not true
    {
      if (currentlySendTraceNotes()) {
        sendTraceNote(
            "waits in '"
                + getName()
                + "'"
                + " until '"
                + cond.getName()
                + "' "); // send a traceNote
      }

      boolean proceed = false; // can we proceed? is the condition
      // true?

      do { // the process is stuck in here
        currentProcess.setBlocked(true); // as long as ...see while
        currentProcess.skipTraceNote(); // don't tell the user, that we
        // ...
        currentProcess.passivate(); // passivate the current process

        // waiting canceled?
        if (!currentProcess.isBlocked()) {
          waitingCanceled = true;
          break;
        }

        proceed = cond.check(currentProcess); // has the condition
        // become true?

        if (proceed || _checkAll) // check also the next process in
        // the
        // q?
        {
          activateAsNext(_queue.succ(currentProcess));
          // activate the next process in the queue
        }
      } while (!proceed); // as long as the condition is not true
    } // end if

    // the condition is true now, so we are free...yeah!
    if (currentlySendTraceNotes()) {
      if (waitingCanceled) {
        sendTraceNote("resumes after waiting in '" + getName() + "' canceled"); // send a traceNote
      } else {
        sendTraceNote(
            "leaves '"
                + getName()
                + "', because '"
                + cond.getName()
                + "' is true"); // send a traceNote
      }
    }
    _queue.remove(currentProcess); // get the process out of the queue
    currentProcess.setBlocked(false); // we are not blocked (anymore),
    // yeah!

    return true;
  }