001/* A constant source.
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.expr.Parameter;
031import ptolemy.kernel.CompositeEntity;
032import ptolemy.kernel.util.IllegalActionException;
033import ptolemy.kernel.util.NameDuplicationException;
034import ptolemy.kernel.util.Workspace;
035
036///////////////////////////////////////////////////////////////////
037//// Const
038
039/**
040 Produce a constant output. The value of the
041 output is that of the token contained by the <i>value</i> parameter,
042 which by default is an IntToken with value 1. The type of the output
043 is that of <i>value</i> parameter.
044 <p>
045 If the trigger port is connected, then this actor fires only if a token is
046 provided on any channel of the trigger port. The value of that token
047 does not matter. Specifically, if there is no such token, the prefire()
048 method returns false.</p>
049
050 @author Yuhong Xiong, Edward A. Lee
051 @version $Id$
052 @since Ptolemy II 0.2
053 @Pt.ProposedRating Green (eal)
054 @Pt.AcceptedRating Green (bilung)
055 */
056public class Const extends LimitedFiringSource {
057    /** Construct a constant source with the given container and name.
058     *  Create the <i>value</i> parameter, initialize its value to
059     *  the default value of an IntToken with value 1.
060     *  @param container The container.
061     *  @param name The name of this actor.
062     *  @exception IllegalActionException If the entity cannot be contained
063     *   by the proposed container.
064     *  @exception NameDuplicationException If the container already has an
065     *   actor with this name.
066     */
067    public Const(CompositeEntity container, String name)
068            throws NameDuplicationException, IllegalActionException {
069        super(container, name);
070        value = new Parameter(this, "value");
071        value.setExpression("1");
072        value.moveToFirst();
073
074        // Set the type constraint.
075        output.setTypeAtLeast(value);
076
077        _attachText("_iconDescription",
078                "<svg>\n" + "<rect x=\"0\" y=\"0\" "
079                        + "width=\"60\" height=\"20\" "
080                        + "style=\"fill:white\"/>\n" + "</svg>\n");
081    }
082
083    ///////////////////////////////////////////////////////////////////
084    ////                     ports and parameters                  ////
085
086    /** The value produced by this constant source.
087     *  By default, it contains an IntToken with value 1.  If the
088     *  type of this token is changed during the execution of a model,
089     *  then the director will be asked to redo type resolution.
090     *
091     *  <p>The Ptolemy II Expression language defines the syntax
092     *  for this parameter.  In Vergil, documentation is available
093     *  via the Help button in the Edit Parameters window.  Documentation
094     *  is available on-line at
095     *  <a href="http://ptolemy.eecs.berkeley.edu/books/Systems/chapters/Expressions.pdf#in_browser">http://ptolemy.eecs.berkeley.edu/books/Systems/chapters/Expressions.pdf</a>.
096     *  </p>
097     */
098    public Parameter value;
099
100    ///////////////////////////////////////////////////////////////////
101    ////                         public methods                    ////
102
103    /** Clone the actor into the specified workspace. This calls the
104     *  base class and then sets the value public variable in the new
105     *  object to equal the cloned parameter in that new object.
106     *  @param workspace The workspace for the new object.
107     *  @return A new actor.
108     *  @exception CloneNotSupportedException If a derived class contains
109     *   an attribute that cannot be cloned.
110     */
111    @Override
112    public Object clone(Workspace workspace) throws CloneNotSupportedException {
113        Const newObject = (Const) super.clone(workspace);
114
115        // Set the type constraint.
116        newObject.output.setTypeAtLeast(newObject.value);
117
118        return newObject;
119    }
120
121    /** Send the token in the <i>value</i> parameter to the output.
122     *  @exception IllegalActionException If it is thrown by the
123     *   send() method sending out the token.
124     */
125    @Override
126    public void fire() throws IllegalActionException {
127        super.fire();
128        output.send(0, value.getToken());
129    }
130}