Class Philosopher
- java.lang.Object
-
- ptolemy.kernel.util.NamedObj
-
- ptolemy.kernel.InstantiableNamedObj
-
- ptolemy.kernel.Entity<T>
-
- ptolemy.kernel.ComponentEntity<T>
-
- ptolemy.actor.AtomicActor<TypedIOPort>
-
- ptolemy.actor.TypedAtomicActor
-
- ptolemy.domains.csp.kernel.CSPActor
-
- ptolemy.domains.csp.demo.DiningPhilosophers.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)
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from class ptolemy.kernel.Entity
Entity.ContainedObjectsIterator
-
-
Field Summary
Fields Modifier and Type Field Description Parameter
eating
This parameter determines the rate at which this philosopher will spend time eating.boolean
gotLeft
boolean
gotRight
TypedIOPort
leftIn
The port through which this philosopher is granted access to the left chopstick.TypedIOPort
leftOut
The port through which this philosopher releases access to the left chopstick.TypedIOPort
rightIn
The port through which this philosopher is granted access to the right chopstick.TypedIOPort
rightOut
The port through which this philosopher releases access to the right chopstick.Parameter
thinking
This parameter determines the rate at which this philosopher will spend time thinking.boolean
waitingLeft
boolean
waitingRight
-
Fields inherited from class ptolemy.actor.TypedAtomicActor
_typesValid
-
Fields inherited from class ptolemy.actor.AtomicActor
_actorFiringListeners, _initializables, _notifyingActorFiring, _stopRequested
-
Fields inherited from class ptolemy.kernel.util.NamedObj
_changeListeners, _changeLock, _changeRequests, _debugging, _debugListeners, _deferChangeRequests, _elementName, _isPersistent, _verbose, _workspace, ATTRIBUTES, CLASSNAME, COMPLETE, CONTENTS, DEEP, FULLNAME, LINKS
-
Fields inherited from interface ptolemy.actor.Executable
COMPLETED, NOT_READY, STOP_ITERATING
-
-
Constructor Summary
Constructors Constructor Description Philosopher(TypedCompositeActor container, java.lang.String name)
Construct a Philosopher in the specified container with the specified name.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected void
_notifyListeners()
Notify any PhilosopherListeners that have registered an interest/dependency in this Philosopher.void
addPhilosopherListener(PhilosopherListener newListener)
Register a PhilosopherListener with this Philosopher.void
fire()
Executes the code in this actor.boolean
postfire()
Return true if this actor is enabled to proceed with additional iterations; return false otherwise.void
preinitialize()
Set this actor to its initial state, i.e.-
Methods inherited from class ptolemy.domains.csp.kernel.CSPActor
_cancelDelay, _continue, _waitForDeadlock, chooseBranch, clone, delay, getBranchController, initialize, terminate
-
Methods inherited from class ptolemy.actor.TypedAtomicActor
_containedTypeConstraints, _customTypeConstraints, _defaultTypeConstraints, _fireAt, _fireAt, attributeTypeChanged, clone, isBackwardTypeInferenceEnabled, newPort, typeConstraintList, typeConstraints
-
Methods inherited from class ptolemy.actor.AtomicActor
_actorFiring, _actorFiring, _declareDelayDependency, addActorFiringListener, addInitializable, connectionsChanged, createReceivers, declareDelayDependency, getCausalityInterface, getDirector, getExecutiveDirector, getManager, inputPortList, isFireFunctional, isStrict, iterate, newReceiver, outputPortList, prefire, pruneDependencies, recordFiring, removeActorFiringListener, removeDependency, removeInitializable, setContainer, stop, stopFire, wrapup
-
Methods inherited from class ptolemy.kernel.ComponentEntity
_adjustDeferrals, _checkContainer, _getContainedObject, _propagateExistence, getContainer, instantiate, isAtomic, isOpaque, moveDown, moveToFirst, moveToIndex, moveToLast, moveUp, propagateExistence, setName
-
Methods inherited from class ptolemy.kernel.Entity
_addPort, _description, _exportMoMLContents, _removePort, _validateSettables, connectedPortList, connectedPorts, containedObjectsIterator, getAttribute, getPort, getPorts, linkedRelationList, linkedRelations, portList, removeAllPorts, setClassDefinition, uniqueName
-
Methods inherited from class ptolemy.kernel.InstantiableNamedObj
_setParent, exportMoML, getChildren, getElementName, getParent, getPrototypeList, isClassDefinition, isWithinClassDefinition
-
Methods inherited from class ptolemy.kernel.util.NamedObj
_addAttribute, _adjustOverride, _attachText, _cloneFixAttributeFields, _containedDecorators, _copyChangeRequestList, _debug, _debug, _debug, _debug, _debug, _executeChangeRequests, _getIndentPrefix, _isMoMLSuppressed, _markContentsDerived, _notifyHierarchyListenersAfterChange, _notifyHierarchyListenersBeforeChange, _propagateValue, _removeAttribute, _splitName, _stripNumericSuffix, addChangeListener, addDebugListener, addHierarchyListener, attributeChanged, attributeDeleted, attributeList, attributeList, decorators, deepContains, depthInHierarchy, description, description, event, executeChangeRequests, exportMoML, exportMoML, exportMoML, exportMoML, exportMoMLPlain, getAttribute, getAttributes, getChangeListeners, getClassName, getDecoratorAttribute, getDecoratorAttributes, getDerivedLevel, getDerivedList, getDisplayName, getFullName, getModelErrorHandler, getName, getName, getSource, handleModelError, isDeferringChangeRequests, isOverridden, isPersistent, lazyContainedObjectsIterator, message, notifyOfNameChange, propagateValue, propagateValues, removeAttribute, removeChangeListener, removeDebugListener, removeHierarchyListener, requestChange, setClassName, setDeferringChangeRequests, setDerivedLevel, setDisplayName, setModelErrorHandler, setPersistent, setSource, sortContainedObjects, toplevel, toString, validateSettables, workspace
-
Methods inherited from class java.lang.Object
equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface ptolemy.actor.Actor
createReceivers, getCausalityInterface, getDirector, getExecutiveDirector, getManager, inputPortList, newReceiver, outputPortList
-
Methods inherited from interface ptolemy.kernel.util.Derivable
getDerivedLevel, getDerivedList, propagateValue
-
Methods inherited from interface ptolemy.actor.Executable
isFireFunctional, isStrict, iterate, prefire, stop, stopFire
-
Methods inherited from interface ptolemy.actor.Initializable
addInitializable, removeInitializable, wrapup
-
Methods inherited from interface ptolemy.kernel.util.Nameable
description, getContainer, getDisplayName, getFullName, getName, getName, setName
-
-
-
-
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 interfaceExecutable
- Overrides:
fire
in classAtomicActor<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 interfaceExecutable
- Overrides:
postfire
in classCSPActor
- Returns:
- True if continued execution is enabled; false otherwise.
-
preinitialize
public void preinitialize() throws IllegalActionException
Set this actor to its initial state, i.e. not having nor waiting for any chopsticks.- Specified by:
preinitialize
in interfaceInitializable
- Overrides:
preinitialize
in classAtomicActor<TypedIOPort>
- Throws:
IllegalActionException
- Not thrown in this base class.
-
_notifyListeners
protected void _notifyListeners()
Notify any PhilosopherListeners that have registered an interest/dependency in this Philosopher.
-
-