001/* A Wire with one trigger port that accepts read requests.
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.domains.de.lib;
029
030import ptolemy.data.Token;
031import ptolemy.data.expr.Parameter;
032import ptolemy.kernel.CompositeEntity;
033import ptolemy.kernel.util.IllegalActionException;
034import ptolemy.kernel.util.NameDuplicationException;
035
036///////////////////////////////////////////////////////////////////
037//// Wire
038
039/**
040 A Wire is a stateful actor in DE.  It should have an equal number
041 of input and output channels.  If it receives input on <i>any</i> of
042 its channels, it will produce the most recent received on each
043 input channel to the corresponding output channel for <i>all</i>
044 channels. We can think its behavior similar to a wire in VHDL, where
045 the value is always the most recently received. If no input has been
046 received on an input channel, then the corresponding output channel
047 will get the value given by <i>initialValue</i>, possibly converted
048 to the type of the output. The type of the output is at least that
049 of the input and that of <i>initialValue</i>. Hence, for example,
050 if the input
051 is a double and <i>initialValue</i> is an int, then the output will
052 be a double.
053
054 @author Adam Cataldo, Edward A. Lee (contributor)
055 @version $Id$
056 @since Ptolemy II 5.2
057 @Pt.ProposedRating Red (acataldo)
058 @Pt.AcceptedRating Red (acataldo)
059 @see ptolemy.domains.de.lib.MostRecent
060 @deprecated Use Sampler instead.
061 */
062@Deprecated
063public class Wire extends DETransformer {
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 Wire(CompositeEntity container, String name)
073            throws NameDuplicationException, IllegalActionException {
074        super(container, name);
075
076        input.setMultiport(true);
077        output.setMultiport(true);
078        output.setTypeAtLeast(input);
079
080        initialValue = new Parameter(this, "initialValue");
081        output.setTypeAtLeast(initialValue);
082        initialValue.setExpression("0");
083
084        _attachText("_iconDescription", "<svg>\n" + "<rect x=\"-20\" y=\"-20\" "
085                + "width=\"40\" height=\"2\" " + "style=\"fill:black\"/>\n"
086                + "<rect x=\"-20\" y=\"-10\" " + "width=\"40\" height=\"2\" "
087                + "style=\"fill:black\"/>\n" + "<rect x=\"-20\" y=\"0\" "
088                + "width=\"40\" height=\"2\" " + "style=\"fill:black\"/>\n"
089                + "<rect x=\"-20\" y=\"10\" " + "width=\"40\" height=\"2\" "
090                + "style=\"fill:black\"/>\n" + "</svg>\n");
091    }
092
093    ///////////////////////////////////////////////////////////////////
094    ////                     ports and parameters                  ////
095
096    /** The value that is output when no input has yet been received
097     *  on the corresponding channel. The output type at least the type
098     *  of this parameter and the type of the input. The default value
099     *  is 0 (an int).
100     */
101    public Parameter initialValue;
102
103    ///////////////////////////////////////////////////////////////////
104    ////                         public methods                    ////
105
106    /** If there is a token in on any channel of the <i>input</i> port,
107     *  output the most recent value on all the <i>output</i> port
108     *  channels.
109     *  @exception IllegalActionException If the base class throws one.
110     */
111    @Override
112    public void fire() throws IllegalActionException {
113        super.fire();
114        int inputWidth = input.getWidth();
115        int outputWidth = output.getWidth();
116        int commonWidth = Math.min(inputWidth, outputWidth);
117
118        // If the <i>initialValue</i> parameter was not set, or if the
119        // width of the input has changed.
120        if (_lastInputs == null || _lastInputs.length != inputWidth) {
121            _lastInputs = new Token[inputWidth];
122            Token defaultValue = initialValue.getToken();
123            for (int i = 0; i < inputWidth; i++) {
124                _lastInputs[i] = defaultValue;
125            }
126        }
127
128        for (int i = 0; i < commonWidth; i++) {
129            if (input.hasToken(i)) {
130                _lastInputs[i] = input.get(i);
131            }
132            output.send(i, _lastInputs[i]);
133        }
134    }
135
136    /** Initialize the actor.
137     *  @exception IllegalActionException If the superclass throws it.
138     */
139    @Override
140    public void initialize() throws IllegalActionException {
141        super.initialize();
142        _lastInputs = null;
143    }
144
145    /** Return true if there is any token on an input port.
146     *  @exception IllegalActionException If the base class throws it.
147     */
148    @Override
149    public boolean prefire() throws IllegalActionException {
150        boolean writeRequest = false;
151        int inputWidth = input.getWidth();
152
153        for (int i = 0; i < inputWidth; i++) {
154            writeRequest = writeRequest || input.hasToken(i);
155        }
156
157        return writeRequest || super.prefire();
158    }
159
160    ///////////////////////////////////////////////////////////////////
161    ////                         private variables                 ////
162
163    /** The recorded inputs last seen. */
164    protected Token[] _lastInputs;
165}