001/* An actor that disassembles a DoubleMatrixToken to a multiport output. 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.actor.lib; 029 030import ptolemy.data.DoubleMatrixToken; 031import ptolemy.data.type.BaseType; 032import ptolemy.kernel.CompositeEntity; 033import ptolemy.kernel.util.IllegalActionException; 034import ptolemy.kernel.util.NameDuplicationException; 035 036/////////////////////////////////////////////////////////////////// 037//// VectorDisassembler 038 039/** 040 <p>An actor that disassembles a DoubleMatrixToken to a multiport output. 041 </p><p>On each firing, read one column or row vector (i.e. a 042 DoubleMatrixToken with one column or row) from the <i>input</i> port and 043 send out individual DoubleTokens to each channel of the <i>output</i> 044 port. If the width of the <i>output</i> port (say, <i>n</i>) is less than 045 the number of rows (say, <i>m</i>) in the input token, then the first 046 <i>n</i> elements in the vector will be sent, and the remaining tokens are 047 discarded. If <i>n</i> is greater than <i>m</i>, then the last 048 <i>n-m</i> channels of the output port will never send tokens out. 049 This class throws an exception if the input is not a column vector. 050 051 </p><p>For sequential domains like SDF, the combination of 052 domains.sdf.lib.MatrixToDouble and a Distributor is equivalent to this 053 actor. However, that combination will not work in CT, so we need this 054 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 VectorAssembler 062 */ 063public class VectorDisassembler 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 VectorDisassembler(CompositeEntity container, String name) 073 throws NameDuplicationException, IllegalActionException { 074 super(container, name); 075 076 input.setTypeEquals(BaseType.DOUBLE_MATRIX); 077 input.setMultiport(false); 078 output.setTypeEquals(BaseType.DOUBLE); 079 output.setMultiport(true); 080 _attachText("_iconDescription", 081 "<svg>\n" + "<rect x=\"0\" y=\"0\" width=\"6\" " 082 + "height=\"40\" style=\"fill:blue\"/>\n" + "</svg>\n"); 083 } 084 085 /////////////////////////////////////////////////////////////////// 086 //// public methods //// 087 088 /** If there is a token at the input, read one column or row vector 089 * (i.e. a DoubleMatrixToken with one column or row) from the 090 * input port, and for each channel i of the output port, 091 * send send the ith element of this column or row vector to this 092 * channel. Otherwise, do nothing. 093 * 094 * Note: The output tokens are copies of the corresponding 095 * elements in the input token. 096 * 097 * @exception IllegalActionException If there is no director, or 098 * the input token has more than one columns and rows. 099 */ 100 @Override 101 public void fire() throws IllegalActionException { 102 super.fire(); 103 if (input.hasToken(0)) { 104 DoubleMatrixToken vector = (DoubleMatrixToken) input.get(0); 105 106 if (vector.getColumnCount() == 1) { 107 int min = Math.min(vector.getRowCount(), output.getWidth()); 108 109 for (int i = 0; i < min; i++) { 110 output.send(i, vector.getElementAsToken(i, 0)); 111 } 112 } else if (vector.getRowCount() == 1) { 113 int min = Math.min(vector.getColumnCount(), output.getWidth()); 114 115 for (int i = 0; i < min; i++) { 116 output.send(i, vector.getElementAsToken(0, i)); 117 } 118 } else { 119 throw new IllegalActionException(this, "The input must " 120 + "be a DoubleMatrixToken with one column or row. " 121 + "But the input is " + vector); 122 } 123 124 } 125 } 126}