Package sim.des

Class Queue

java.lang.Object
sim.portrayal.SimplePortrayal2D
sim.portrayal.simple.InternalPortrayal2D
All Implemented Interfaces:
Serializable, Named, Parented, ProvidesBarData, Receiver, Resettable, sim.engine.Steppable, sim.portrayal.Portrayal, sim.portrayal.Portrayal2D

public class Queue extends Middleman implements sim.engine.Steppable
A blocking resource queue with a capacity: you can think of Queue as a warehouse with a maximum amount of space. Resources placed in the queue by default are offered to downstream recievers immediately. You can change this behavior by setting setOffersImmediately(false). Whenever it is stepped by the Schedule, the Queue will also offer to its receivers. You can prevent this by not scheduling it in the first place. Like all Providers, the Queue will make an offer if possible to any Receiver that requests one via provide(...).
See Also:
  • Constructor Details

    • Queue

      public Queue(sim.engine.SimState state, Resource typical)
      Builds a queue with the given typical resource type.
  • Method Details

    • buildDefaultPortrayal

      public sim.portrayal.SimplePortrayal2D buildDefaultPortrayal(double scale)
      Description copied from class: DESPortrayal
      Builds the "base portrayal" for the object, if the image path and class haven't been set (and thus the portrayal isn't an ImagePortrayal2D). The default sets to a simple gray and black quare.
      Overrides:
      buildDefaultPortrayal in class DESPortrayal
    • getCapacity

      public double getCapacity()
      Returns the maximum available resources that may be aquired by the Queue.
    • hideCapacity

      public boolean hideCapacity()
    • setCapacity

      public void setCapacity(double d)
      Set the maximum available resources that may be aquired by the Queue.

      Throws a runtime exception if the capacity is negative or NaN.

    • getOffersImmediately

      public boolean getOffersImmediately()
      Returns whether the Queue offers items immediately upon accepting (when possible) in zero time, as opposed to when it is stepped.
    • hideOffersImmediately

      public boolean hideOffersImmediately()
    • setOffersImmediately

      public void setOffersImmediately(boolean val)
      Sets whether the Queue offers items immediately upon accepting (when possible) in zero time, as opposed to when it is stepped.
    • accept

      public boolean accept(Provider provider, Resource amount, double atLeast, double atMost)
      Description copied from class: Middleman
      Offers a resource from a Provider to the Middleman. By default it does nothing: it returns FALSE, indicating that the offer is refused. You can override this as you see fit. This isn't abstract because you might wish to use a custom Middleman to conduct transactions only, rather than accepting offers.

      Note: if you implement this method, you probably want to check for offer cycles, invalid types, and whether the user has set the agent to refuse offers. This can be done with the following code snippet:

         if (isOffering())
                 {
                 throwCyclicOffers();
                 }
             if (resource.getType() != getTypicalReceived().getType())
                     {
                     throwUnequalReceivedTypeException(provided);
                     }
             if (getRefusesOffers())
                     {
                     return false;
                     }
         if (!(atLeast >= 0 invalid input: '&'invalid input: '&' atMost >= atLeast invalid input: '&'invalid input: '&' atMost > 0 invalid input: '&'invalid input: '&' atMost invalid input: '<'= resource.getAmount()))
                 {
                 throwInvalidAtLeastAtMost(atLeast, atMost, provided);
                 }
             try
                     {
             offering = true;
             
             //// DO YOUR OFFER ACCEPTANCE OR REFUSAL WORK HERE
             
                     }
             finally
                     {
                     offering = false;
                     }
             

      Here is the general documentation for Receiver.accept():

      If the resource is a COUNTABLE or UNCOUNTABLE resource of some kind, The provider may respond by removing between atLeast and atMost, inclusive, from the given amount, and returning TRUE, or returning FALSE if it refuses the offer.

      If the resource is an ENTITY of some kind, The provider may respond by taking the entity and returning TRUE, or returning FALSE if it refuses the offer. atLeast and atMost may be ignored, but generally atLeast should be 0 and atMost should be 1.

      May throw a RuntimeException if the resource does not match the typical resource of the receiver, or if a cycle was detected in accepting offers (A offers to B, which offers to C, which then offers to A). At present does not check that atLeast and atMost are valid.

      It must be the case that 0 <= atLeast < = atMost <= resource.getAmount(), or else a RuntimeException may be thrown.

      Receivers must never accept 0 of any resource. Thus if atLeast = 0, then this has a special meaning: it means that the receiver must accept > atLeast, rather than >= atLeast. Similarly, Providers should never provide atMost=0.

      Specified by:
      accept in interface Receiver
      Overrides:
      accept in class Middleman
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • step

      public void step(sim.engine.SimState state)
      Upon being stepped, the Queue offers to registered receivers. You don't have to schedule the Queue at all; in which case this method would never be called.
      Specified by:
      step in interface sim.engine.Steppable
      Overrides:
      step in class Provider