ptolemy.domains.rendezvous.lib
Class ResourcePool

java.lang.Object
  extended by ptolemy.kernel.util.NamedObj
      extended by ptolemy.kernel.InstantiableNamedObj
          extended by ptolemy.kernel.Entity
              extended by ptolemy.kernel.ComponentEntity
                  extended by ptolemy.actor.AtomicActor
                      extended by ptolemy.actor.TypedAtomicActor
                          extended by ptolemy.domains.rendezvous.lib.ResourcePool
All Implemented Interfaces:
java.io.Serializable, java.lang.Cloneable, Actor, Executable, FiringsRecordable, Initializable, TypedActor, Changeable, Debuggable, DebugListener, Derivable, Instantiable, ModelErrorHandler, MoMLExportable, Moveable, Nameable

public class ResourcePool
extends TypedAtomicActor

This actor manages a pool of resources, where each resource is represented by a token with an arbitrary value. Resources are granted on the grant output port and released on the release input port. These ports are both multiports, so resources can be granted to multiple users of the resources, and released by multiple actors.

The initial pool of resources is provided by the initialPool parameter, which is an array of arbitrary type. The grant output port and release input port are constrained to have compatible types. Specifically, the grant output port must be able to send tokens with types that match the elements of this array, and it must also be able to send tokens with types that match inputs provided at the release input.

This actor is designed for use in the rendezvous domain, where it will execute in its own thread. At all times, it is ready to rendezvous with any other actor connected to its release input port. When such a rendezvous occurs, the token provided at that input is added to the resource pool. In addition, whenever the resource pool is non-empty, this actor is ready to rendezvous with any actor connected to its grant output port. When such a rendezvous occurs, it sends the first token in the resource pool to that output port and removes that token from the resource pool.

The behavior of this actor is similar to that of the Merge actor, except that the Merge actor does no buffering. That is, while this actor is always ready to rendezvous with any input, the Merge actor is ready to rendezvous with an input only after it has delivered the previous input to the output.

Since:
Ptolemy II 5.1
Version:
$Id: ResourcePool.java 57040 2010-01-27 20:52:32Z cxh $
Author:
Edward A. Lee
See Also:
Merge, Serialized Form
Accepted Rating:
Red (cxh)
Proposed Rating:
Yellow (eal)

Nested Class Summary
 
Nested classes/interfaces inherited from class ptolemy.kernel.Entity
Entity.ContainedObjectsIterator
 
Field Summary
private  java.util.List _pool
          The current resource pool.
private  boolean _postfireReturns
          Flag indicating what postfire should return.
private  java.lang.Thread _readThread
          The read thread, if it exists.
 TypedIOPort grant
          The output port through which this actor grants resources.
 Parameter initialPool
          The initial resource pool.
 TypedIOPort release
          The input port through which other actors release resources.
 
Fields inherited from class ptolemy.actor.AtomicActor
_actorFiringListeners, _initializables, _notifyingActorFiring, _stopRequested
 
Fields inherited from class ptolemy.kernel.util.NamedObj
_changeListeners, _changeLock, _changeRequests, _debugging, _debugListeners, _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
ResourcePool(CompositeEntity container, java.lang.String name)
          Construct an actor in the specified container with the specified name.
 
Method Summary
 void attributeChanged(Attribute attribute)
          Override the base class to reset the resource pool to match the specified initialPool value.
 java.lang.Object clone(Workspace workspace)
          Override the base class to set the type constraints.
 void fire()
          If the input width is greater than zero and it has not already been done, start a thread to read a token from the release input port and store it in the pool.
 void initialize()
          Initialize.
 boolean postfire()
          Return false if it is time to stop the process.
 
Methods inherited from class ptolemy.actor.TypedAtomicActor
_addPort, _fireAt, _fireAt, attributeTypeChanged, clone, newPort, typeConstraintList, typeConstraints
 
Methods inherited from class ptolemy.actor.AtomicActor
_actorFiring, _actorFiring, addActorFiringListener, addInitializable, connectionsChanged, createReceivers, declareDelayDependency, getCausalityInterface, getDirector, getExecutiveDirector, getManager, inputPortList, isFireFunctional, isStrict, iterate, newReceiver, outputPortList, prefire, preinitialize, pruneDependencies, recordFiring, removeActorFiringListener, removeDependency, removeInitializable, setContainer, stop, stopFire, terminate, 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
_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, _debug, _debug, _debug, _debug, _debug, _getIndentPrefix, _isMoMLSuppressed, _markContentsDerived, _propagateValue, _recordDecoratedAttributes, _removeAttribute, _splitName, _stripNumericSuffix, addChangeListener, addDebugListener, attributeList, attributeList, 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, propagateValue, propagateValues, removeChangeListener, removeDebugListener, 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.actor.Executable
isFireFunctional, isStrict, iterate, prefire, stop, stopFire, terminate
 
Methods inherited from interface ptolemy.actor.Initializable
addInitializable, preinitialize, removeInitializable, wrapup
 
Methods inherited from interface ptolemy.kernel.util.Nameable
description, getContainer, getDisplayName, getFullName, getName, getName, setName
 
Methods inherited from interface ptolemy.kernel.util.Derivable
getDerivedLevel, getDerivedList, propagateValue
 

Field Detail

grant

public TypedIOPort grant
The output port through which this actor grants resources. This port has type equal to the element type of the initialPool parameter.


release

public TypedIOPort release
The input port through which other actors release resources. This port has type equal to the element type of the initialPool parameter.


initialPool

public Parameter initialPool
The initial resource pool. This is an array with default value {1} (an integer array with one entry with value 1).


_pool

private java.util.List _pool
The current resource pool.


_postfireReturns

private boolean _postfireReturns
Flag indicating what postfire should return.


_readThread

private java.lang.Thread _readThread
The read thread, if it exists.

Constructor Detail

ResourcePool

public ResourcePool(CompositeEntity container,
                    java.lang.String name)
             throws IllegalActionException,
                    NameDuplicationException
Construct an actor 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.

Parameters:
container - The container.
name - The name.
Throws:
IllegalActionException - If the actor cannot be contained by the proposed container.
NameDuplicationException - If the name coincides with an actor already in the container.
Method Detail

attributeChanged

public void attributeChanged(Attribute attribute)
                      throws IllegalActionException
Override the base class to reset the resource pool to match the specified initialPool value.

Overrides:
attributeChanged in class NamedObj
Parameters:
attribute - The attribute that changed.
Throws:
IllegalActionException - If the change is not acceptable to this container (not thrown in this base class).

clone

public java.lang.Object clone(Workspace workspace)
                       throws java.lang.CloneNotSupportedException
Override the base class to set the type constraints.

Overrides:
clone in class AtomicActor
Parameters:
workspace - The workspace for the cloned object.
Returns:
A new ResourcePool actor.
Throws:
java.lang.CloneNotSupportedException - If cloned ports cannot have as their container the cloned entity (this should not occur), or if one of the attributes cannot be cloned.
See Also:
NamedObj.exportMoML(Writer, int, String), NamedObj.setDeferringChangeRequests(boolean)

fire

public void fire()
          throws IllegalActionException
If the input width is greater than zero and it has not already been done, start a thread to read a token from the release input port and store it in the pool. Then, in the calling thread, if there is at least one resource in the pool, write the first resource in the pool to any grant output channel.

Specified by:
fire in interface Executable
Overrides:
fire in class AtomicActor
Throws:
IllegalActionException - If an error occurs during executing the process.
TerminateProcessException - If the process termination is requested by the director.

initialize

public void initialize()
                throws IllegalActionException
Initialize.

Specified by:
initialize in interface Initializable
Overrides:
initialize in class AtomicActor
Throws:
IllegalActionException - If a derived class throws it.

postfire

public boolean postfire()
Return false if it is time to stop the process.

Specified by:
postfire in interface Executable
Overrides:
postfire in class AtomicActor
Returns:
False a TerminateProcessException was thrown during I/O.