001/* An actor that reads a token from each input channel to assemble a
002 DoubleMatrixToken.
003
004 Copyright (c) 1998-2014 The Regents of the University of California.
005 All rights reserved.
006 Permission is hereby granted, without written agreement and without
007 license or royalty fees, to use, copy, modify, and distribute this
008 software and its documentation for any purpose, provided that the above
009 copyright notice and the following two paragraphs appear in all copies
010 of this software.
011
012 IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY
013 FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
014 ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF
015 THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF
016 SUCH DAMAGE.
017
018 THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
019 INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
020 MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE
021 PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF
022 CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES,
023 ENHANCEMENTS, OR MODIFICATIONS.
024
025 PT_COPYRIGHT_VERSION_2
026 COPYRIGHTENDKEY
027
028 */
029package ptolemy.actor.lib;
030
031import ptolemy.data.BooleanToken;
032import ptolemy.data.DoubleMatrixToken;
033import ptolemy.data.ScalarToken;
034import ptolemy.data.Token;
035import ptolemy.data.expr.Parameter;
036import ptolemy.data.type.BaseType;
037import ptolemy.kernel.CompositeEntity;
038import ptolemy.kernel.util.IllegalActionException;
039import ptolemy.kernel.util.NameDuplicationException;
040
041///////////////////////////////////////////////////////////////////
042//// VectorAssembler
043
044/**
045 <p>On each firing, read exactly one token from each channel of the
046 <i>input</i> port and assemble the tokens into a DoubleMatrixToken
047 with one column.  The DoubleMatrixToken is sent to the <i>output</i>
048 port.  If there is no input token at any channel of the <i>input</i> port,
049 then prefire() will return false.  Note that the elements in the
050 vector are not copied.
051
052 </p><p>For sequential domains like SDF, the combination of a Commutator
053 and domains.sdf.lib.DoubleToMatrix is equivalent to this actor.
054 However, that combination will not work in CT, so we need this actor.</p>
055
056 @author Jie Liu, Elaine Cheong
057 @version $Id$
058 @since Ptolemy II 2.0
059 @Pt.ProposedRating Yellow (celaine)
060 @Pt.AcceptedRating Yellow (celaine)
061 @see VectorDisassembler
062 */
063public class VectorAssembler extends Transformer {
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 this 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 VectorAssembler(CompositeEntity container, String name)
073            throws NameDuplicationException, IllegalActionException {
074        super(container, name);
075        input.setTypeEquals(BaseType.DOUBLE);
076        input.setMultiport(true);
077        output.setTypeEquals(BaseType.DOUBLE_MATRIX);
078        output.setMultiport(false);
079        isColumn = new Parameter(this, "isColumn", BooleanToken.TRUE);
080        isColumn.setTypeEquals(BaseType.BOOLEAN);
081
082        _attachText("_iconDescription",
083                "<svg>\n" + "<rect x=\"0\" y=\"0\" width=\"6\" "
084                        + "height=\"40\" style=\"fill:blue\"/>\n" + "</svg>\n");
085    }
086
087    ///////////////////////////////////////////////////////////////////
088    ////                     ports and parameters                  ////
089
090    /** True if the output vector is a column matrix. Otherwise,
091     *  the output is a row matrix. The default value is true.
092     */
093    public Parameter isColumn;
094
095    ///////////////////////////////////////////////////////////////////
096    ////                         public methods                    ////
097
098    /** Read one token from each channel of the <i>input</i> port,
099     *  assemble those tokens into a DoubleMatrixToken, and send the
100     *  result to the output.
101     *  @exception IllegalActionException If there is no director.
102     */
103    @Override
104    public void fire() throws IllegalActionException {
105        super.fire();
106        int size = input.getWidth();
107
108        double[][] data;
109
110        boolean isColumnValue = ((BooleanToken) isColumn.getToken())
111                .booleanValue();
112
113        if (isColumnValue) {
114            data = new double[size][1];
115
116            for (int i = 0; i < size; i++) {
117                Token token = input.get(i);
118                try {
119                    data[i][0] = ((ScalarToken) token).doubleValue();
120                } catch (ClassCastException ex) {
121                    throw new IllegalActionException(this, ex,
122                            "Cannot cast \"" + token + "\" to a ScalarToken");
123                }
124            }
125        } else {
126            data = new double[1][size];
127
128            for (int i = 0; i < size; i++) {
129                Token token = input.get(i);
130                try {
131                    data[0][i] = ((ScalarToken) token).doubleValue();
132                } catch (ClassCastException ex) {
133                    throw new IllegalActionException(this, ex,
134                            "Cannot cast \"" + token + "\" to a ScalarToken");
135                }
136            }
137        }
138
139        DoubleMatrixToken result = new DoubleMatrixToken(data);
140
141        output.send(0, result);
142    }
143
144    /** Return true if all channels of the <i>input</i> port have
145     *  tokens, false if any channel does not have a token.
146     *  @return True if all channels of the <i>input</i> port have tokens.
147     *  @exception IllegalActionException If the hasToken() call to the
148     *   input port throws it.
149     *  @see ptolemy.actor.IOPort#hasToken(int)
150     */
151    @Override
152    public boolean prefire() throws IllegalActionException {
153        for (int i = 0; i < input.getWidth(); i++) {
154            if (!input.hasToken(i)) {
155                return false;
156            }
157        }
158
159        return super.prefire();
160    }
161}