001/* A polymorphic switch, which routes inputs to specified output channels.
002
003 Copyright (c) 1997-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.TypedIOPort;
031import ptolemy.data.IntToken;
032import ptolemy.data.Token;
033import ptolemy.data.type.BaseType;
034import ptolemy.kernel.CompositeEntity;
035import ptolemy.kernel.util.IllegalActionException;
036import ptolemy.kernel.util.NameDuplicationException;
037import ptolemy.kernel.util.StringAttribute;
038
039///////////////////////////////////////////////////////////////////
040//// Switch
041
042/**
043 <p>A polymorphic switch, which routes inputs to specified output channels.
044 This actor has two input ports, the <i>input</i> port for data,
045 and the <i>control</i> port to select which output channel to use.
046 When it fires, if an input token is available at the <i>control</i>
047 input, that token is read, and its value is noted.  If an input
048 token is available on the <i>input</i> port, then that token is
049 read, sent to the output channel specified by the most recently
050 received value on the <i>control</i> port.  If no token has been
051 received on the <i>control</i> port, then the token is sent to
052 channel zero.  If the value of the most recently received token
053 on the <i>control</i> port is out of range (less than zero,
054 or greater than or equal to the width of the output), then no
055 output is produced, and the token is lost.
056</p><p>
057 Note that it may be tempting to call an instance of this
058 class "switch", but recall that "switch" is a Java keyword, and
059 thus it cannot be the name of an object.</p>
060
061 @author Edward A. Lee
062 @version $Id$
063 @since Ptolemy II 2.0
064 @Pt.ProposedRating Yellow (eal)
065 @Pt.AcceptedRating Yellow (cxh)
066 */
067public class Switch extends Transformer {
068    /** Construct an actor in the specified container with the specified
069     *  name.
070     *  @param container The container.
071     *  @param name The name of this actor within the container.
072     *  @exception IllegalActionException If the actor cannot be contained
073     *   by the proposed container.
074     *  @exception NameDuplicationException If the name coincides with
075     *   an actor already in the container.
076     */
077    public Switch(CompositeEntity container, String name)
078            throws IllegalActionException, NameDuplicationException {
079        super(container, name);
080
081        output.setMultiport(true);
082
083        control = new TypedIOPort(this, "control", true, false);
084        control.setTypeEquals(BaseType.INT);
085
086        // Put the control input on the bottom of the actor.
087        StringAttribute controlCardinal = new StringAttribute(control,
088                "_cardinal");
089        controlCardinal.setExpression("SOUTH");
090    }
091
092    ///////////////////////////////////////////////////////////////////
093    ////                     ports and parameters                  ////
094
095    /** Input port for control tokens, which specify the output channel
096     *  to produce data on.  The type is int. */
097    public TypedIOPort control;
098
099    ///////////////////////////////////////////////////////////////////
100    ////                         public methods                    ////
101
102    /** Read a control token, if there is one, and transfer an input
103     *  token, if there is one, to the output channel specified by
104     *  the most recent control token, if it is in range.
105     *  @exception IllegalActionException If there is no director.
106     */
107    @Override
108    public void fire() throws IllegalActionException {
109        super.fire();
110        if (control.hasToken(0)) {
111            _control = ((IntToken) control.get(0)).intValue();
112        }
113
114        if (input.hasToken(0)) {
115            Token token = input.get(0);
116
117            if (_control >= 0 && _control < output.getWidth()) {
118                output.send(_control, token);
119            }
120        }
121    }
122
123    /** Initialize this actor so that channel zero of <i>input</i> is read
124     *  from until a token arrives on the <i>control</i> input.
125     *  @exception IllegalActionException If the parent class throws it.
126     */
127    @Override
128    public void initialize() throws IllegalActionException {
129        super.initialize();
130        _control = 0;
131    }
132
133    ///////////////////////////////////////////////////////////////////
134    ////                         private variables                 ////
135    // The most recently read control token.
136    private int _control = 0;
137}