001/** Actor that reads a stream of double tokens and places them into a
002 Upper Triangular Matrix.
003
004 Copyright (c) 1999-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.domains.pn.demo.QR;
030
031import ptolemy.actor.lib.Transformer;
032import ptolemy.data.DoubleMatrixToken;
033import ptolemy.data.DoubleToken;
034import ptolemy.data.IntToken;
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//// StreamToMatrix
044
045/**
046
047 Convert a stream of Double Tokens into a Matrix. The Matrix is
048 considered to be an Upper triangular Matrix.
049 @author Bart Kienhuis
050 @version $Id: StreamToMatrix.java,v 1.2 1999/11/30 03:55:57 kienhuis
051 @since Ptolemy II 0.4
052 @Pt.ProposedRating Red (kienhuis)
053 @Pt.AcceptedRating Red (cxh)
054 Exp $
055 */
056public class StreamToMatrix extends Transformer {
057    ///////////////////////////////////////////////////////////////////
058    ////                         public methods                    ////
059
060    /** Construct an actor with the given container and name.
061     *  @param container The container.
062     *  @param name The name of this actor.
063     *  @exception IllegalActionException If the actor cannot be contained
064     *   by the proposed container.
065     *  @exception NameDuplicationException If the container already has an
066     *   actor with this name.
067     */
068    public StreamToMatrix(CompositeEntity container, String name)
069            throws IllegalActionException, NameDuplicationException {
070        super(container, name);
071
072        input.setTypeEquals(BaseType.DOUBLE);
073        output.setTypeEquals(BaseType.DOUBLE_MATRIX);
074        dimension = new Parameter(this, "dimension", new IntToken(6));
075
076        // Initialize the dimension
077        attributeChanged(dimension);
078    }
079
080    /** If the argument is the dimension parameter, update the
081     *  the row and column values.
082     *  @param attribute The attribute that changed.
083     *  @exception IllegalActionException if no an integer value can
084     *        be obtained from the dimension parameter.
085     */
086    @Override
087    public void attributeChanged(Attribute attribute)
088            throws IllegalActionException {
089        if (attribute == dimension) {
090            _rows = ((IntToken) dimension.getToken()).intValue();
091            _columns = ((IntToken) dimension.getToken()).intValue();
092        } else {
093            super.attributeChanged(attribute);
094        }
095    }
096
097    /** Reads a stream of DoubleTokens and places these tokens in a
098     *  Matrix. The Matrix produced is an Upper Triangular Matrix.
099     *  @exception IllegalActionException If there is no director.
100     */
101    @Override
102    public void fire() throws IllegalActionException {
103        super.fire();
104        int runL = 0;
105        double[][] image = new double[_rows][_columns];
106
107        for (int i = 0; i < _rows; i++) {
108            for (int j = 0; j < _columns; j++) {
109                if (j >= runL) {
110                    image[i][j] = ((DoubleToken) input.get(0)).doubleValue();
111                } else {
112                    image[i][j] = 0.0;
113                }
114            }
115
116            runL++;
117        }
118
119        output.broadcast(new DoubleMatrixToken(image));
120    }
121
122    /** Initialize the row and column number.
123     *  @exception IllegalActionException If the parent class throws it.
124     */
125    @Override
126    public void initialize() throws IllegalActionException {
127        super.initialize();
128
129        // Get the correct value from the parameters
130        _rows = ((IntToken) dimension.getToken()).intValue();
131        _columns = ((IntToken) dimension.getToken()).intValue();
132    }
133
134    ///////////////////////////////////////////////////////////////////
135    ////                         public variables                  ////
136
137    /** The dimension of the matrix.
138     */
139    public Parameter dimension;
140
141    ///////////////////////////////////////////////////////////////////
142    ////                         private variables                 ////
143    private int _rows = 6;
144
145    private int _columns = 6;
146}