001/* An actor that converts an array of integers into a string.
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.conversions;
029
030import ptolemy.actor.lib.Transformer;
031import ptolemy.data.ArrayToken;
032import ptolemy.data.IntToken;
033import ptolemy.data.StringToken;
034import ptolemy.data.type.ArrayType;
035import ptolemy.data.type.BaseType;
036import ptolemy.kernel.CompositeEntity;
037import ptolemy.kernel.util.IllegalActionException;
038import ptolemy.kernel.util.NameDuplicationException;
039
040///////////////////////////////////////////////////////////////////
041/// IntArrayToString
042
043/**
044 Convert an integer-array into a string.  Uses only the low order byte from
045 each integer.  NOTE: Assumes an 8-bit character set.  The output is a string
046 assembled from these bytes.  This actor is designed to facilitate use of the
047 SerialComm serial communication actor which uses the same kind of integer
048 array format as this actor.  Datagram actors can use this format as well.
049 <p>
050
051 @author Winthrop Williams, Steve Neuendorffer
052 @version $Id$
053 @since Ptolemy II 2.0
054 @Pt.ProposedRating Red (winthrop)
055 @Pt.AcceptedRating Red (winthrop)
056 */
057public class IntArrayToString extends Transformer {
058    /** Construct an actor with the given container and name.
059     *  @param container The container.
060     *  @param name The name of this actor.
061     *  @exception IllegalActionException If the actor cannot be contained
062     *   by the proposed container.
063     *  @exception NameDuplicationException If the container already has an
064     *   actor with this name.
065     */
066    public IntArrayToString(CompositeEntity container, String name)
067            throws NameDuplicationException, IllegalActionException {
068        super(container, name);
069
070        input.setTypeEquals(new ArrayType(BaseType.INT));
071
072        output.setTypeEquals(BaseType.STRING);
073
074        _attachText("_iconDescription",
075                "<svg>\n" + "<polygon points=\"-15,-15 15,15 15,-15 -15,15\" "
076                        + "style=\"fill:white\"/>\n" + "</svg>\n");
077    }
078
079    ///////////////////////////////////////////////////////////////////
080    ////                         public methods                    ////
081
082    /** Consume one array token of integer tokens on the input port
083     *  and output a new string token on the output port.  The least
084     *  significant byte of the first integer generates the first
085     *  character in the string, etc.  NOTE: Java has many options
086     *  regarding its character set.  This actor relies on the default
087     *  setting on the platform on which it is run.  However, it
088     *  assumes that this character set is an 8-bit character set.
089     *
090     *  @exception IllegalActionException If there is no director.
091     *  FIXME: Either verify that it does check for the director,
092     *  or remove this statement.  This statement occurs in other
093     *  conversion actor(s) as well.
094     */
095    @Override
096    public void fire() throws IllegalActionException {
097        super.fire();
098        ArrayToken dataIntArrayToken = (ArrayToken) input.get(0);
099        byte[] dataBytes = new byte[dataIntArrayToken.length()];
100
101        for (int j = 0; j < dataIntArrayToken.length(); j++) {
102            IntToken dataIntOneToken = (IntToken) dataIntArrayToken
103                    .getElement(j);
104            dataBytes[j] = (byte) dataIntOneToken.intValue(); //Keep low 8 bits
105        }
106
107        // Note:  Following line may assume 1 byte per character, not sure.
108        String outputValue = new String(dataBytes);
109        output.send(0, new StringToken(outputValue));
110    }
111
112    /** Return false if the input port has no token, otherwise return
113     *  what the superclass returns (presumably true).
114     *  @exception IllegalActionException If there is no director.
115     */
116    @Override
117    public boolean prefire() throws IllegalActionException {
118        if (!input.hasToken(0)) {
119            return false;
120        }
121
122        return super.prefire();
123    }
124}