ptolemy.domains.wireless.lib
Class NodeRandomizer

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.wireless.lib.NodeRandomizer
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 NodeRandomizer
extends TypedAtomicActor

This actor, when fired, randomizes the locations of all actors in the same container that contain an attribute named "randomize" with value true. It can also optionally perform the randomization in its initialize() method. In both cases, the randomization is performed in a change request, so it will take effect after the current iteration.

This actor assumes that the location is represented in an object of class Locatable (which is an attribute that implements Settable) with name "_location". This is how Vergil represents locations.

The range parameter is an array of arrays giving the range of possible values for each dimension of the location. For example, if the location is in two dimensions, then range has the form {{x1, x2}, {y1, y2}}, indicating that the X value of the location is uniformly distributed between x1 and x2, and that the Y value is uniformly distributed between y1 and y2.

If the resetOnEachRun parameter is true (the default value), then each run resets the random number generator. If the seed is non-zero, then this makes each run identical. This is useful for constructing tests. If the seed is zero, then a new seed is generated on each run (using the current time and the hash code of this object).

The maxPrecision parameter specifies the number of digits to be used when generating a random location; results are rounded to this precision. If the value is 0 (the default value), then use the maximum precision allowed by the Java double type and the random number generator.

Since:
Ptolemy II 4.0
Version:
$Id: NodeRandomizer.java 57040 2010-01-27 20:52:32Z cxh $
Author:
Sanjeev Kohli, N. Vinay Krishnan, Cheng Tien Ee, Edward Lee, Xiaojun Liu and Elaine Cheong.
See Also:
Locatable, Serialized Form
Accepted Rating:
Red (sanjeev)
Proposed Rating:
Yellow (eal)

Nested Class Summary
 
Nested classes/interfaces inherited from class ptolemy.kernel.Entity
Entity.ContainedObjectsIterator
 
Field Summary
private  java.math.MathContext _mathContext
           
private  java.util.Random _random
           
 Parameter maxPrecision
          The maximum precision (number of digits to be used, results are rounded to this precision) of the generated locations.
 Parameter randomizeInInitialize
          If set to true, randomly distribute nodes in initialize().
 Parameter range
          The range of values for locations to be set when randomizing.
 Parameter resetOnEachRun
          If set to true, reset the random number generator with the seed value each time preinitialize() is invoked.
 Parameter seed
          The seed that controls the random number generation to use when randomizing.
 TypedIOPort trigger
          A trigger input to cause this actor to fire in domains that require a trigger.
 
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
NodeRandomizer(CompositeEntity container, java.lang.String name)
          Construct an actor in the given container with the given name.
 
Method Summary
protected  java.lang.String _getLocationSetMoML(CompositeEntity container, Entity node, double[] location)
          Return moml that will set the location of the specified node.
protected  void _randomize()
          Randomly distribute the nodes in the model that contain a boolean valued parameter named "randomize" with value true.
 void attributeChanged(Attribute attribute)
          Override the base class to react to changes in maxPrecision.
 void fire()
          Issue a change request to randomize the locations of the nodes.
 void initialize()
          Override the base class to randomize the positions of the nodes if randomizeInInitialize is set to true.
 
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, clone, connectionsChanged, createReceivers, declareDelayDependency, getCausalityInterface, getDirector, getExecutiveDirector, getManager, inputPortList, isFireFunctional, isStrict, iterate, newReceiver, outputPortList, postfire, 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, postfire, 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

maxPrecision

public Parameter maxPrecision
The maximum precision (number of digits to be used, results are rounded to this precision) of the generated locations. If equal to 0, the resulting random locations will not be rounded. The default value is 0.


randomizeInInitialize

public Parameter randomizeInInitialize
If set to true, randomly distribute nodes in initialize(). The default value is false.


range

public Parameter range
The range of values for locations to be set when randomizing. This is an array of arrays that defaults to {{0.0, 500.0}, {0.0, 500.0}}, indicating that the X and Y values are uniformly distributed between 0.0 and 500.0.


resetOnEachRun

public Parameter resetOnEachRun
If set to true, reset the random number generator with the seed value each time preinitialize() is invoked. The default value is true, which means that if the seed value is non-zero, the node layout will be the same each time this model is run. When the value is false and the seed value is non-zero, the random number generator will not be reset, and the node layout will be different each time this model is run within the same session (within the lifetime of the JVM), although the overall pattern of node layouts will be the same across different JVM sessions.


seed

public Parameter seed
The seed that controls the random number generation to use when randomizing. A seed of zero is interpreted to mean that no seed is specified, which means that each execution of the model could result in distinct data. For the value 0, the seed is set to System.currentTimeMillis() + hashCode(), which means that with extremely high probability, two distinct actors will have distinct seeds. However, current time may not have enough resolution to ensure that two subsequent executions of the same model have distinct seeds. This parameter contains a LongToken, initially with value 0.


trigger

public TypedIOPort trigger
A trigger input to cause this actor to fire in domains that require a trigger. This has undeclared type.


_mathContext

private java.math.MathContext _mathContext

_random

private java.util.Random _random
Constructor Detail

NodeRandomizer

public NodeRandomizer(CompositeEntity container,
                      java.lang.String name)
               throws IllegalActionException,
                      NameDuplicationException
Construct an actor in the given container with the given name. The container argument must not be null, or a NullPointerException will be thrown. If the name argument is null, then the name is set to the empty string. Increment the version number of the workspace.

Parameters:
container - Container of the actor.
name - Name of the actor.
Throws:
IllegalActionException - If the actor is not compatible with the specified container.
NameDuplicationException - If the name collides with an entity in the container.
Method Detail

attributeChanged

public void attributeChanged(Attribute attribute)
                      throws IllegalActionException
Override the base class to react to changes in maxPrecision.

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).

fire

public void fire()
          throws IllegalActionException
Issue a change request to randomize the locations of the nodes.

Specified by:
fire in interface Executable
Overrides:
fire in class AtomicActor
Throws:
IllegalActionException - If the superclass throws it.

initialize

public void initialize()
                throws IllegalActionException
Override the base class to randomize the positions of the nodes if randomizeInInitialize is set to true. Also initialize the random number generator so that if a nonzero seed is provided, then the results are repeatable. If resetOnEachRun is set to true (default value), then the seed is reset for each run.

Specified by:
initialize in interface Initializable
Overrides:
initialize in class AtomicActor
Throws:
IllegalActionException - If the initialize() method of one of the associated actors throws it, or if the range parameter is malformed.

_randomize

protected void _randomize()
                   throws IllegalActionException
Randomly distribute the nodes in the model that contain a boolean valued parameter named "randomize" with value true. This method distributes the location according to uniform random variables (one for each dimension) with ranges given by the range parameter. Subclasses can override this to perform some other randomization. This delegates to the method _setLocationOfNode() to actually set the location of the actor.

Throws:
IllegalActionException - If the range parameter is malformed.

_getLocationSetMoML

protected java.lang.String _getLocationSetMoML(CompositeEntity container,
                                               Entity node,
                                               double[] location)
                                        throws IllegalActionException
Return moml that will set the location of the specified node. The moml should set the _location attribute, which is the location as used in Vergil, the visual editor. Derived classes may override this to store the location differently.

Parameters:
container - The container.
location - The specified location.
node - The node for which to set the location.
Returns:
MoML that sets the location of the specified node.
Throws:
IllegalActionException - If the location attribute cannot be set.