001/* An actor that converts a sequence of input tokens to a matrix.
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.data.DoubleMatrixToken;
031import ptolemy.data.IntToken;
032import ptolemy.data.MatrixToken;
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.Attribute;
039import ptolemy.kernel.util.IllegalActionException;
040import ptolemy.kernel.util.NameDuplicationException;
041
042///////////////////////////////////////////////////////////////////
043/// DoubleToMatrix
044
045/**
046 This actor converts a sequence of input tokens to a matrix.
047 The actor reads <i>rows</i> times <i>columns</i> inputs and
048 inserts their values into a double matrix with <i>rows</i> rows
049 and <i>columns</i> columns. The first row is filled first, then
050 the second row, then the third, etc.
051
052 <p>Note that this actor is not likely to work in the CT domain, use
053 actor.lib.VectorAssembler instead.
054
055 @author Edward A. Lee
056 @deprecated Use SequenceToMatrix instead.
057 @version $Id$
058 @since Ptolemy II 2.0
059 @Pt.ProposedRating Yellow (eal)
060 @Pt.AcceptedRating Red (eal)
061 */
062@Deprecated
063public class DoubleToMatrix extends SDFConverter {
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 DoubleToMatrix(CompositeEntity container, String name)
073            throws NameDuplicationException, IllegalActionException {
074        super(container, name);
075
076        rows = new Parameter(this, "rows");
077        rows.setExpression("2");
078        rows.setTypeEquals(BaseType.INT);
079        columns = new Parameter(this, "columns");
080        columns.setExpression("2");
081        columns.setTypeEquals(BaseType.INT);
082
083        input.setTypeEquals(BaseType.DOUBLE);
084        output.setTypeEquals(BaseType.DOUBLE_MATRIX);
085
086        input_tokenConsumptionRate.setExpression("rows * columns");
087    }
088
089    ///////////////////////////////////////////////////////////////////
090    ////                     ports and parameters                  ////
091
092    /** The number of columns.  It is of type integer and has a default
093     *  value of 2.  It must be greater than zero.
094     */
095    public Parameter columns;
096
097    /** The number of rows.  It is of type integer and has a
098     *  default value of 2.  It must be greater than zero.
099     */
100    public Parameter rows;
101
102    ///////////////////////////////////////////////////////////////////
103    ////                         public methods                    ////
104
105    /** Ensure that the rows and columns parameters are both positive.
106     *  @param attribute The attribute that has changed.
107     *  @exception IllegalActionException If the parameters are out of range.
108     */
109    @Override
110    public void attributeChanged(Attribute attribute)
111            throws IllegalActionException {
112        if (attribute == rows || attribute == columns) {
113            _rows = ((IntToken) rows.getToken()).intValue();
114            _columns = ((IntToken) columns.getToken()).intValue();
115
116            if (_rows <= 0 || _columns <= 0) {
117                throw new IllegalActionException(this,
118                        "Number of rows and columns is required to be positive.");
119            }
120        } else {
121            super.attributeChanged(attribute);
122        }
123    }
124
125    /** Consume consecutive input tokens and produce the output matrix.
126     *  @exception IllegalActionException If there is no director.
127     */
128    @Override
129    public final void fire() throws IllegalActionException {
130        super.fire();
131
132        double[][] result = new double[_rows][_columns];
133
134        for (int i = 0; i < _rows; i++) {
135            Token[] row = input.get(0, _columns);
136
137            for (int j = 0; j < _columns; j++) {
138                result[i][j] = ((ScalarToken) row[j]).doubleValue();
139            }
140        }
141
142        output.send(0, new DoubleMatrixToken(result, MatrixToken.DO_NOT_COPY));
143    }
144
145    ///////////////////////////////////////////////////////////////////
146    ////                         private variables                 ////
147    // The number of rows.
148    private int _rows;
149
150    // The number of columns.
151    private int _columns;
152}