Class Chopstick

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

    public class Chopstick
    extends CSPActor
    Chopstick in the Dining Philosophers demo. Each Chopstick can only be used by one philosopher at a time. When it is not being used it can be claimed by either of the two philosophers next to it. Once it has been claimed, it is not available until it is released by the philosopher holding it.

    This actor has four ports, each of width one: two are used to communicate with the philosopher on the left, and two are used to communicate with the buffer on the right. Two ports are needed to communicate with each philosopher as the philosopher holds the chopstick for some random time.

    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 the left philosopher releases access to this chopstick.
      • leftOut

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

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

        public TypedIOPort rightOut
        The port through which the right philosopher receives access to this chopstick.
    • Constructor Detail

      • Chopstick

        public Chopstick()
                  throws IllegalActionException,
                         NameDuplicationException
        Construct a Chopstick in the default workspace with an empty string as its name. 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 object is added to the workspace directory. Increment the version number of the workspace.
        Throws:
        IllegalActionException - If the port or parameter cannot be contained by this actor.
        NameDuplicationException - If the port name coincides with a port already in this actor, or if the parameter name coincides with a parameter already in this actor
      • Chopstick

        public Chopstick​(TypedCompositeActor container,
                         java.lang.String name)
                  throws IllegalActionException,
                         NameDuplicationException
        Construct a Chopstick 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".

        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

      • fire

        public void fire()
                  throws IllegalActionException
        Executes the code in this actor. This actor uses a CDO construct when it is waiting to be used by either of the philosophers next to it. Once one of the philosophers is using it, this actor waits to receive a message that the philosopher is finished eating (using it). It is a good example of using a CDO. This process continues executing until a TerminateProcessException is thrown.
        Specified by:
        fire in interface Executable
        Overrides:
        fire in class AtomicActor<TypedIOPort>
        Throws:
        IllegalActionException - If an error occurs during executing the process.
        TerminateProcessException - If the process termination is requested by the director.