001/* An actor that converts a matrix to sequence of output tokens.
002
003 Copyright (c) 1998-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.sdf.lib;
029
030import ptolemy.actor.Director;
031import ptolemy.data.DoubleMatrixToken;
032import ptolemy.data.DoubleToken;
033import ptolemy.data.IntToken;
034import ptolemy.data.expr.Parameter;
035import ptolemy.data.type.BaseType;
036import ptolemy.domains.sdf.kernel.SDFDirector;
037import ptolemy.kernel.CompositeEntity;
038import ptolemy.kernel.util.Attribute;
039import ptolemy.kernel.util.IllegalActionException;
040import ptolemy.kernel.util.NameDuplicationException;
041
042///////////////////////////////////////////////////////////////////
043/// MatrixToDouble
044
045/**
046 This actor converts a matrix input sequence of output tokens.
047 The input must be a DoubleMatrixToken and the output will be a sequence
048 of instances of DoubleToken.  The number of outputs produced
049 on each firing is the number of elements in the matrix.
050 This is assumed to equal the product <i>rows</i> times <i>columns</i>,
051 although this is not enforced unless the actor is under the control
052 of an instance of SDFDirector.  The SDF director requires this information
053 to construct its schedule. The first row is produced first, then
054 the second row, then the third, etc.
055
056 <p>Note that this actor is not likely to work in the CT domain, use
057 actor.lib.VectorDisassembler instead.
058
059 @author Edward A. Lee
060 @deprecated Use MatrixToSequence instead.
061 @version $Id$
062 @since Ptolemy II 2.0
063 @Pt.ProposedRating Yellow (eal)
064 @Pt.AcceptedRating Red (eal)
065 */
066@Deprecated
067public class MatrixToDouble extends SDFConverter {
068    /** Construct an actor with the given container and name.
069     *  @param container The container.
070     *  @param name The name of this actor.
071     *  @exception IllegalActionException If the actor cannot be contained
072     *   by the proposed container.
073     *  @exception NameDuplicationException If the container already has an
074     *   actor with this name.
075     */
076    public MatrixToDouble(CompositeEntity container, String name)
077            throws NameDuplicationException, IllegalActionException {
078        super(container, name);
079
080        rows = new Parameter(this, "rows");
081        rows.setExpression("2");
082        rows.setTypeEquals(BaseType.INT);
083
084        columns = new Parameter(this, "columns");
085        columns.setExpression("2");
086        columns.setTypeEquals(BaseType.INT);
087
088        output.setTypeEquals(BaseType.DOUBLE);
089        input.setTypeEquals(BaseType.DOUBLE_MATRIX);
090
091        output_tokenProductionRate.setExpression("rows * columns");
092    }
093
094    ///////////////////////////////////////////////////////////////////
095    ////                     ports and parameters                  ////
096
097    /** The number of columns.  It is of type integer and has a default
098     *  value of 2.  It must be greater than zero. This information is
099     *  only used if the actor is under the control of an SDFDirector.
100     */
101    public Parameter columns;
102
103    /** The number of rows.  It is of type integer and has a default
104     *  value of 2.  It must be greater than zero. This information is
105     *  only used if the actor is under the control of an SDFDirector.
106     */
107    public Parameter rows;
108
109    ///////////////////////////////////////////////////////////////////
110    ////                         public methods                    ////
111
112    /** Ensure that the rows and columns parameters are both positive.
113     *  @param attribute The attribute that has changed.
114     *  @exception IllegalActionException If the parameters are out of range.
115     */
116    @Override
117    public void attributeChanged(Attribute attribute)
118            throws IllegalActionException {
119        if (attribute == rows || attribute == columns) {
120            _rows = ((IntToken) rows.getToken()).intValue();
121            _columns = ((IntToken) columns.getToken()).intValue();
122
123            if (_rows <= 0 || _columns <= 0) {
124                throw new IllegalActionException(this,
125                        "Number of rows and columns is required to be positive.");
126            }
127        } else {
128            super.attributeChanged(attribute);
129        }
130    }
131
132    /** Consume a matrix input and produce consecutive output tokens.
133     *  @exception IllegalActionException If there is no director, or if
134     *   the director is an SDFDirector and the number of rows and columns
135     *   of the input matrix does not match the declared parameter values.
136     */
137    @Override
138    public final void fire() throws IllegalActionException {
139        super.fire();
140
141        DoubleMatrixToken matrix = (DoubleMatrixToken) input.get(0);
142        int inputRows = matrix.getRowCount();
143        int inputColumns = matrix.getColumnCount();
144
145        // If the director is an SDFDirector, check the dimensions
146        // of the matrix.
147        Director director = getDirector();
148
149        if (director instanceof SDFDirector) {
150            if (inputRows * inputColumns != _rows * _columns) {
151                throw new IllegalActionException(this,
152                        "Received a matrix whose dimension does not "
153                                + "match the declared dimensions.");
154            }
155        }
156
157        int totalSize = inputRows * inputColumns;
158        DoubleToken[] result = new DoubleToken[totalSize];
159        int k = 0;
160
161        for (int i = 0; i < inputRows; i++) {
162            for (int j = 0; j < inputColumns; j++) {
163                result[k++] = (DoubleToken) matrix.getElementAsToken(i, j);
164            }
165        }
166
167        output.send(0, result, totalSize);
168    }
169
170    ///////////////////////////////////////////////////////////////////
171    ////                         private variables                 ////
172    // The number of rows.
173    private int _rows;
174
175    // The number of columns.
176    private int _columns;
177}