001/* An actor that outputs a random sequence of booleans. 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.data.BooleanToken; 031import ptolemy.data.DoubleToken; 032import ptolemy.data.expr.Parameter; 033import ptolemy.data.type.BaseType; 034import ptolemy.kernel.CompositeEntity; 035import ptolemy.kernel.util.IllegalActionException; 036import ptolemy.kernel.util.NameDuplicationException; 037 038/////////////////////////////////////////////////////////////////// 039//// Bernoulli 040 041/** 042 Produce a random sequence of booleans. The output is of type BooleanToken. 043 The values that are generated are independent and identically distributed, 044 where the probability of <i>true</i> is given by the parameter 045 <i>trueProbability</i>. 046 The seed can be specified as a parameter to control the sequence that is 047 generated. 048 This actor uses the class java.util.Random to generate random numbers. 049 Note that if the parameters are changed during execution of the 050 model, there is a one iteration delay before the changes take 051 effect. 052 053 @author Edward A. Lee 054 @version $Id$ 055 @since Ptolemy II 0.3 056 @Pt.ProposedRating Green (eal) 057 @Pt.AcceptedRating Green (bilung) 058 @see ptolemy.actor.lib.DiscreteRandomSource 059 @see ptolemy.actor.lib.Rician 060 @see ptolemy.actor.lib.Triangular 061 @see ptolemy.actor.lib.Uniform 062 */ 063public class Bernoulli extends RandomSource { 064 /** Construct an actor with the given container and name. 065 * @param container The container. 066 * @param name The name of this actor. 067 * @exception IllegalActionException If the actor cannot be contained 068 * by the proposed container. 069 * @exception NameDuplicationException If the container already has an 070 * actor with this name. 071 */ 072 public Bernoulli(CompositeEntity container, String name) 073 throws NameDuplicationException, IllegalActionException { 074 super(container, name); 075 076 output.setTypeEquals(BaseType.BOOLEAN); 077 078 trueProbability = new Parameter(this, "trueProbability"); 079 trueProbability.setExpression("0.5"); 080 trueProbability.setTypeEquals(BaseType.DOUBLE); 081 } 082 083 /////////////////////////////////////////////////////////////////// 084 //// ports and parameters //// 085 086 /** The probability of <i>true</i>. 087 * This parameter contains a DoubleToken, initially with value 0.5. 088 */ 089 public Parameter trueProbability; 090 091 /////////////////////////////////////////////////////////////////// 092 //// public methods //// 093 094 /** Send a random boolean to the output. 095 * This number is only changed in the prefire() method, so it will 096 * remain constant throughout an iteration. 097 * @exception IllegalActionException If calling send() or super.fire() 098 * throws it. 099 */ 100 @Override 101 public void fire() throws IllegalActionException { 102 super.fire(); 103 output.send(0, new BooleanToken(_current)); 104 } 105 106 /////////////////////////////////////////////////////////////////// 107 //// protected methods //// 108 109 /** Generate a new random number. 110 * @exception IllegalActionException If parameter values are incorrect. 111 */ 112 @Override 113 protected void _generateRandomNumber() throws IllegalActionException { 114 if (_random.nextDouble() < ((DoubleToken) trueProbability.getToken()) 115 .doubleValue()) { 116 _current = true; 117 } else { 118 _current = false; 119 } 120 } 121 122 /////////////////////////////////////////////////////////////////// 123 //// private variables //// 124 125 /** The random boolean for the current iteration. */ 126 private boolean _current; 127}