001/* An actor that disassemble an ArrayToken to a multiport output.
002
003 Copyright (c) 2003-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.actor.lib;
029
030import ptolemy.data.BooleanToken;
031import ptolemy.data.MatrixToken;
032import ptolemy.data.Token;
033import ptolemy.data.expr.Parameter;
034import ptolemy.data.type.ArrayType;
035import ptolemy.data.type.BaseType;
036import ptolemy.kernel.CompositeEntity;
037import ptolemy.kernel.util.IllegalActionException;
038import ptolemy.kernel.util.NameDuplicationException;
039import ptolemy.kernel.util.Workspace;
040
041///////////////////////////////////////////////////////////////////
042//// MatrixToArray
043
044/**
045 Convert a matrix to an array. The array will contain the first
046 row, followed by the second, etc.
047
048 @author Edward A. Lee
049 @version $Id$
050 @since Ptolemy II 10.0
051 @Pt.ProposedRating Red (zhouye)
052 @Pt.AcceptedRating Red (cxh)
053 */
054public class MatrixToArray extends Transformer {
055    /** Construct an actor with the given container and name.
056     *  @param container The container.
057     *  @param name The name of this actor.
058     *  @exception IllegalActionException If the actor cannot be
059     *   contained by the proposed container.
060     *  @exception NameDuplicationException If the container
061     *   already has an actor with this name.
062     */
063    public MatrixToArray(CompositeEntity container, String name)
064            throws NameDuplicationException, IllegalActionException {
065        super(container, name);
066
067        // Set type constraints.
068        input.setTypeAtMost(BaseType.MATRIX);
069        output.setTypeAtLeast(ArrayType.ARRAY_BOTTOM);
070
071        columnMajor = new Parameter(this, "columnMajor");
072        columnMajor.setTypeEquals(BaseType.BOOLEAN);
073        columnMajor.setExpression("false");
074
075        // Set the icon.
076        _attachText("_iconDescription",
077                "<svg>\n" + "<polygon points=\"-15,-15 15,15 15,-15 -15,15\" "
078                        + "style=\"fill:white\"/>\n" + "</svg>\n");
079    }
080
081    ///////////////////////////////////////////////////////////////////
082    ////                         parameters                        ////
083
084    /** If true, then insert the first column into the array first,
085     *  followed by the second column, etc. If false, then insert the
086     *  first row first, followed by the second, etc.
087     *  This is a boolean that defaults to false.
088     */
089    public Parameter columnMajor;
090
091    ///////////////////////////////////////////////////////////////////
092    ////                         public methods                    ////
093
094    /** Clone the actor into the specified workspace. This calls the
095     *  base class and then creates new ports and parameters.
096     *  @param workspace The workspace for the new object.
097     *  @return A new actor.
098     *  @exception CloneNotSupportedException If a derived class
099     *   contains an attribute that cannot be cloned.
100     */
101    @Override
102    public Object clone(Workspace workspace) throws CloneNotSupportedException {
103        MatrixToArray newObject = (MatrixToArray) super.clone(workspace);
104        newObject.input.setTypeAtMost(BaseType.MATRIX);
105        newObject.output.setTypeAtLeast(ArrayType.ARRAY_BOTTOM);
106        return newObject;
107    }
108
109    /** If there is a token at the input, read the array
110     *  from the input port, and construct and send to the
111     *  output a matrix containing the values from the array.
112     *  @exception IllegalActionException If a runtime
113     *   type conflict occurs.
114     */
115    @Override
116    public void fire() throws IllegalActionException {
117        super.fire();
118        if (input.hasToken(0)) {
119            Token token = input.get(0);
120            if (!(token instanceof MatrixToken)) {
121                // Not a supported type.
122                throw new IllegalActionException(this,
123                        "Input is not a matrix: " + token);
124            }
125            if (((BooleanToken) columnMajor.getToken()).booleanValue()) {
126                output.broadcast(((MatrixToken) token).toArrayColumnMajor());
127            } else {
128                output.broadcast(((MatrixToken) token).toArray());
129            }
130        }
131    }
132}