Class Philosopher

  • All Implemented Interfaces:
    java.lang.Cloneable, Actor, Executable, FiringsRecordable, Initializable, TypedActor, BranchActor, Changeable, Debuggable, DebugListener, Derivable, Instantiable, ModelErrorHandler, MoMLExportable, Moveable, Nameable

    public class Philosopher
    extends CSPActor
    A philosopher sits at a table with 4 other philosophers. Between each two philosophers there is a chopstick. A Philosopher eats when he has both chopsticks next to him. A Philosopher thinks for a while, then tries to eat. When he succeeds in obtaining both chopsticks he eats for a while, then puts both chopsticks back on the table and continues thinking.

    Due to the rendezvous nature of communication in the CSP domain, a philosopher stalls if it tries to get a chopstick but cannot. When it acquires the chopstick, it eats for a while and then sends a message to the chopstick to say that it is finished using it. Note this actor has been slowed down with Thread.sleep() statements to mimic the eating nature of the philosophers in real time.

    This actor is parameterized by three parameters: "eatingRate" which controls the distribution of the eating times, and "thinkingRate" which controls the distribution of the thinking times. Both these rates characterize a uniform distribution between 0 and the rate.

    Since:
    Ptolemy II 0.3
    Version:
    $Id$
    Author:
    Neil Smyth
    Pt.AcceptedRating:
    Red (cxh)
    Pt.ProposedRating:
    Red (nsmyth)
    • Field Detail

      • leftIn

        public TypedIOPort leftIn
        The port through which this philosopher is granted access to the left chopstick.
      • leftOut

        public TypedIOPort leftOut
        The port through which this philosopher releases access to the left chopstick.
      • rightIn

        public TypedIOPort rightIn
        The port through which this philosopher is granted access to the right chopstick.
      • rightOut

        public TypedIOPort rightOut
        The port through which this philosopher releases access to the right chopstick.
      • eating

        public Parameter eating
        This parameter determines the rate at which this philosopher will spend time eating.
      • thinking

        public Parameter thinking
        This parameter determines the rate at which this philosopher will spend time thinking.
      • gotLeft

        public boolean gotLeft
      • gotRight

        public boolean gotRight
      • waitingLeft

        public boolean waitingLeft
      • waitingRight

        public boolean waitingRight
    • Constructor Detail

      • Philosopher

        public Philosopher​(TypedCompositeActor container,
                           java.lang.String name)
                    throws IllegalActionException,
                           NameDuplicationException
        Construct a Philosopher in the specified container with the specified name. The name must be unique within the container or an exception is thrown. The container argument must not be null, or a NullPointerException will be thrown. The actor is created with two input ports and two output ports, all of width one. The input ports are called "leftIn" and "rightIn", and similarly, the output ports are called "leftOut" and "rightOut".

        The default values of the eatingRate and thinkingRate parameters are 1.0.

        Parameters:
        container - The TypedCompositeActor that contains this actor.
        name - The actor's name.
        Throws:
        IllegalActionException - If the entity cannot be contained by the proposed container.
        NameDuplicationException - If the name argument coincides with an entity already in the container.
    • Method Detail

      • addPhilosopherListener

        public void addPhilosopherListener​(PhilosopherListener newListener)
        Register a PhilosopherListener with this Philosopher.
      • fire

        public void fire()
                  throws IllegalActionException
        Executes the code in this actor. This actor randomly chooses whether to grab the chopstick to the left or right of it first. When it has one chopstick, it then tries to grab the other chopstick beside it. This process mimics the eating action of the philosopher twenty times, and then finishes normally.
        Specified by:
        fire in interface Executable
        Overrides:
        fire in class AtomicActor<TypedIOPort>
        Throws:
        IllegalActionException - If an error occurs during executing the process.
      • postfire

        public boolean postfire()
        Return true if this actor is enabled to proceed with additional iterations; return false otherwise.
        Specified by:
        postfire in interface Executable
        Overrides:
        postfire in class CSPActor
        Returns:
        True if continued execution is enabled; false otherwise.
      • _notifyListeners

        protected void _notifyListeners()
        Notify any PhilosopherListeners that have registered an interest/dependency in this Philosopher.