001/* Remove nil tokens from the input stream.
002
003 Copyright (c) 2006-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.Token;
031import ptolemy.kernel.CompositeEntity;
032import ptolemy.kernel.util.IllegalActionException;
033import ptolemy.kernel.util.NameDuplicationException;
034
035///////////////////////////////////////////////////////////////////
036//// RemoveNilTokens
037
038/**
039 Read in tokens and silently discard any tokens that are marked as
040 nil.  Non-nill tokens are transferred to the output.
041
042 <p>In the real world, data sources sometimes pass null values
043 when data sources are sparsely populated.  The null values are sometimes
044 called missing values.  The Ptolemy II expression language represents
045 the missing or null tokens with the string "nil".
046
047 <p>This actor should not be used in the SDF domain because
048 the number of tokens sent to the output depends on the input.
049
050 @see ptolemy.data.Token#isNil()
051 @see ptolemy.data.Token#NIL
052
053 @author Christopher Brooks
054 @version $Id$
055 @since Ptolemy II 5.2
056 @Pt.ProposedRating Red (cxh)
057 @Pt.AcceptedRating Red (cxh)
058 */
059public class RemoveNilTokens extends Transformer {
060    /** Construct an actor with the given container and name.
061     *  @param container The container.
062     *  @param name The name of this actor.
063     *  @exception IllegalActionException If the actor cannot be contained
064     *   by the proposed container.
065     *  @exception NameDuplicationException If the container already has an
066     *   actor with this name.
067     */
068    public RemoveNilTokens(CompositeEntity container, String name)
069            throws NameDuplicationException, IllegalActionException {
070        super(container, name);
071    }
072
073    ///////////////////////////////////////////////////////////////////
074    ////                         public methods                    ////
075
076    /** Read tokens from the input and output all non-nil (non-missing)
077     *  tokens.  If we get a nil (aka missing) token, then the token
078     *  is read, but no output is sent.  Thus, this actor cannot
079     *  be used in the SDF domain.
080     *  @exception IllegalActionException If there is no director.
081     */
082    @Override
083    public void fire() throws IllegalActionException {
084        super.fire();
085        if (input.hasToken(0)) {
086            Token in = input.get(0);
087            if (!in.isNil()) {
088                output.send(0, in);
089            }
090        }
091    }
092
093}