001/* An actor that outputs a random sequence with a uniform distribution.
002
003 Copyright (c) 1998-2014 The Regents of the University of California.
004 All rights reserved.
005 Permission is hereby granted, without written agreement and without
006 license or royalty fees, to use, copy, modify, and distribute this
007 software and its documentation for any purpose, provided that the above
008 copyright notice and the following two paragraphs appear in all copies
009 of this software.
010
011 IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY
012 FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
013 ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF
014 THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF
015 SUCH DAMAGE.
016
017 THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
018 INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
019 MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE
020 PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF
021 CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES,
022 ENHANCEMENTS, OR MODIFICATIONS.
023
024 PT_COPYRIGHT_VERSION_2
025 COPYRIGHTENDKEY
026
027 */
028package ptolemy.actor.lib;
029
030import ptolemy.actor.parameters.PortParameter;
031import ptolemy.data.BooleanToken;
032import ptolemy.data.DoubleToken;
033import ptolemy.data.expr.SingletonParameter;
034import ptolemy.data.type.BaseType;
035import ptolemy.kernel.CompositeEntity;
036import ptolemy.kernel.util.IllegalActionException;
037import ptolemy.kernel.util.NameDuplicationException;
038
039///////////////////////////////////////////////////////////////////
040//// Uniform
041
042/**
043 Produce a random sequence with a uniform distribution.  On each iteration,
044 a new random number is produced.  The output port is of type DoubleToken.
045 The values that are generated are independent and identically distributed
046 with bounds defined by the parameters.  In addition, the
047 seed can be specified as a parameter to control the sequence that is
048 generated.
049
050 @author Edward A. Lee
051 @version $Id$
052 @since Ptolemy II 1.0
053 @Pt.ProposedRating Yellow (eal)
054 @Pt.AcceptedRating Yellow (cxh)
055 @see ptolemy.actor.lib.Bernoulli
056 @see ptolemy.actor.lib.DiscreteRandomSource
057 @see ptolemy.actor.lib.Rician
058 @see ptolemy.actor.lib.Triangular
059 */
060public class Uniform extends RandomSource {
061    /** Construct an actor with the given container and name.
062     *  @param container The container.
063     *  @param name The name of this actor.
064     *  @exception IllegalActionException If the actor cannot be contained
065     *   by the proposed container.
066     *  @exception NameDuplicationException If the container already has an
067     *   actor with this name.
068     */
069    public Uniform(CompositeEntity container, String name)
070            throws NameDuplicationException, IllegalActionException {
071        super(container, name);
072
073        output.setTypeEquals(BaseType.DOUBLE);
074
075        lowerBound = new PortParameter(this, "lowerBound",
076                new DoubleToken(0.0));
077        lowerBound.setTypeEquals(BaseType.DOUBLE);
078        new SingletonParameter(lowerBound.getPort(), "_showName")
079                .setToken(BooleanToken.TRUE);
080
081        upperBound = new PortParameter(this, "upperBound",
082                new DoubleToken(1.0));
083        upperBound.setTypeEquals(BaseType.DOUBLE);
084        new SingletonParameter(upperBound.getPort(), "_showName")
085                .setToken(BooleanToken.TRUE);
086    }
087
088    ///////////////////////////////////////////////////////////////////
089    ////                     ports and parameters                  ////
090
091    /** The lower bound.
092     *  This parameter contains a DoubleToken, initially with value 0.0.
093     */
094    public PortParameter lowerBound;
095
096    /** The upper bound.
097     *  This parameter contains a DoubleToken, initially with value 0.0.
098     */
099    public PortParameter upperBound;
100
101    ///////////////////////////////////////////////////////////////////
102    ////                         public methods                    ////
103
104    /** Send a random number with a uniform distribution to the output.
105     *  This number is only changed in the prefire() method, so it will
106     *  remain constant throughout an iteration.
107     *  @exception IllegalActionException If there is no director.
108     */
109    @Override
110    public void fire() throws IllegalActionException {
111        lowerBound.update();
112        upperBound.update();
113        super.fire();
114        output.send(0, new DoubleToken(_current));
115    }
116
117    ///////////////////////////////////////////////////////////////////
118    ////                         protected methods                 ////
119
120    /** Generate a new random number.
121     *  @exception IllegalActionException If parameter values are incorrect.
122     */
123    @Override
124    protected void _generateRandomNumber() throws IllegalActionException {
125        double lowerValue = ((DoubleToken) lowerBound.getToken()).doubleValue();
126        double upperValue = ((DoubleToken) upperBound.getToken()).doubleValue();
127
128        if (lowerValue > upperValue) {
129            throw new IllegalActionException(this,
130                    "Invalid bounds: lowerBound is greater than upperBound.");
131        }
132
133        double rawNum = _random.nextDouble();
134        _current = rawNum * (upperValue - lowerValue) + lowerValue;
135    }
136
137    ///////////////////////////////////////////////////////////////////
138    ////                         private variables                 ////
139    // The random number for the current iteration.
140    private double _current;
141}