001/* An actor that converts a SmoothToken (one that has a double value and an array of derivatives to a DoubleToken.
002
003 Copyright (c) 1998-2018 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.conversions;
029
030import ptolemy.data.DoubleToken;
031import ptolemy.data.SmoothToken;
032import ptolemy.data.type.BaseType;
033import ptolemy.kernel.CompositeEntity;
034import ptolemy.kernel.util.IllegalActionException;
035import ptolemy.kernel.util.NameDuplicationException;
036
037///////////////////////////////////////////////////////////////////
038//// SmoothTDouble
039
040/**
041 Convert a {@link SmoothToken} (one that has a double value and an array of
042 derivatives) to a DoubleToken, discarding the derivative information.
043 Normally, such a conversion is not necessary
044 because any actor that can accept a DoubleToken can also transparently
045 accept a SmoothToken. However, when an input port receives a SmoothToken,
046 the port becomes <i>persistent</i>, meaning that it will always have a token.
047 If an actor reads from this input port at a time when no SmoothToken has arrived,
048 then the most recently received SmoothToken will be extrapolated, using its
049 derivative information, to obtain a value at that time. If you wish for a
050 downstream port to not be persistent, then you can use this actor to convert
051 the signal.  Downstream input ports will be absent at all times except those
052 when an actual token is sent.
053
054 @author Thierry S. Nouidui, Christopher Brooks
055@version $Id$
056@since Ptolemy II 11.0
057 @version $Id$
058 @Pt.ProposedRating Green (thn)
059 @Pt.AcceptedRating Red (thn)
060 */
061public class SmoothToDouble extends Converter {
062
063    /** Construct an actor with the given container and name.
064     *  @param container The container.
065     *  @param name The name of this actor.
066     *  @exception IllegalActionException If the actor cannot be contained
067     *   by the proposed container.
068     *  @exception NameDuplicationException If the container already has an
069     *   actor with this name.
070     */
071    public SmoothToDouble(CompositeEntity container, String name)
072            throws NameDuplicationException, IllegalActionException {
073        super(container, name);
074        output.setTypeEquals(BaseType.DOUBLE);
075    }
076
077    ///////////////////////////////////////////////////////////////////
078    ////                         public methods                    ////
079
080    /** Read exactly one token from the input and output the token
081     *  the double value of the token if the token is a SmoothToken.
082     *  The derivatives array of the SmoothToken is discarded.
083     *  @exception IllegalActionException If the superclass throws it.
084     */
085    @Override
086    public void fire() throws IllegalActionException {
087        super.fire();
088        DoubleToken inputToken = (DoubleToken) input.get(0);
089        DoubleToken result = new DoubleToken(inputToken.doubleValue());
090        if (_debugging) {
091            _debug("Transferring input " + inputToken + " to output " + result
092                    + " at time " + getDirector().getModelTime());
093        }
094        output.send(0, result);
095    }
096
097    /** Return false if the input port has no token, otherwise return
098     *  what the superclass returns (presumably true).
099     *  @exception IllegalActionException If there is no director.
100     */
101    @Override
102    public boolean prefire() throws IllegalActionException {
103        if (!input.hasNewToken(0)) {
104            if (_debugging) {
105                _debug("No new input at time " + getDirector().getModelTime());
106            }
107            return false;
108        }
109        return super.prefire();
110    }
111}