001/* An actor that converts a string to an array of integers.
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.TypedAtomicActor;
031import ptolemy.actor.TypedIOPort;
032import ptolemy.data.ArrayToken;
033import ptolemy.data.IntToken;
034import ptolemy.data.StringToken;
035import ptolemy.data.Token;
036import ptolemy.data.type.ArrayType;
037import ptolemy.data.type.BaseType;
038import ptolemy.kernel.CompositeEntity;
039import ptolemy.kernel.util.IllegalActionException;
040import ptolemy.kernel.util.NameDuplicationException;
041
042///////////////////////////////////////////////////////////////////
043/// StringToIntArray
044
045/**
046 Convert a string to an integer-array.  The output is an array of integers
047 constructed by placing one byte (i.e. one character) of the string into
048 the least significant byte of each integer.  Typically, this byte is the
049 ASCII code of the character.  NOTE: For the time being, this actor assumes
050 an 8-bit character has been set as the Java default on the platform in use.
051 This actor is designed to facilitate use of the SerialComm serial
052 communication actor which uses the same kind of integer array.  Datagram
053 actors can take this format as well.
054 <p>
055 @author Winthrop Williams
056 @version $Id$
057 @since Ptolemy II 2.0
058 @Pt.ProposedRating Red (winthrop)
059 @Pt.AcceptedRating Red (winthrop)
060 */
061public class StringToIntArray extends TypedAtomicActor {
062    /** Construct an actor with the given container and name.
063     *  @param container The container.
064     *  @param name The name of this actor.
065     *  @exception IllegalActionException If the actor cannot be contained
066     *   by the proposed container.
067     *  @exception NameDuplicationException If the container already has an
068     *   actor with this name.
069     */
070    public StringToIntArray(CompositeEntity container, String name)
071            throws NameDuplicationException, IllegalActionException {
072        super(container, name);
073
074        input = new TypedIOPort(this, "input", true, false);
075        input.setTypeEquals(BaseType.STRING);
076
077        output = new TypedIOPort(this, "output", false, true);
078        output.setTypeEquals(new ArrayType(BaseType.INT));
079
080        _attachText("_iconDescription",
081                "<svg>\n" + "<polygon points=\"-15,-15 15,15 15,-15 -15,15\" "
082                        + "style=\"fill:white\"/>\n" + "</svg>\n");
083    }
084
085    ///////////////////////////////////////////////////////////////////
086    ////                         public variables                  ////
087
088    /** The port for the input, which has type <i>string</i>. */
089    public TypedIOPort input;
090
091    /** The output port, which has type <i>{int}</i>. */
092    public TypedIOPort output;
093
094    ///////////////////////////////////////////////////////////////////
095    ////                         public methods                    ////
096
097    /** Consume one string token on the input port and output a new array
098     *  token of integer tokens on the output port.  The low byte of each
099     *  integer is the byte form of one of the characters.  The other
100     *  three bytes of each integer may be 0x000000 or 0xFFFFFF.  The
101     *  first character of the string is copied to the first element of
102     *  the array, and so on.  NOTE: Assumes an 8-bit character set is
103     *  the default setting for this implementation of Java.
104     *
105     *  @exception IllegalActionException If there is no director.
106     *  FIXME: Does this method actually check if there is a director?
107     */
108    @Override
109    public void fire() throws IllegalActionException {
110        super.fire();
111        String inputValue = ((StringToken) input.get(0)).stringValue();
112
113        // DO THE CONVERSION:
114        byte[] dataBytes = inputValue.getBytes(); //(Creates a new copy)
115
116        // FIXME The following line assumes one byte per char.
117        // True for out platform, but not all, under Java.
118        // Replace with length of the created byte array.
119        int bytesAvailable = inputValue.length();
120        Token[] dataIntTokens = new Token[bytesAvailable];
121
122        for (int j = 0; j < bytesAvailable; j++) {
123            dataIntTokens[j] = new IntToken(dataBytes[j]);
124        }
125
126        output.send(0, new ArrayToken(BaseType.INT, dataIntTokens));
127    }
128
129    /** Return false if the input port has no token, otherwise return
130     *  what the superclass returns (presumably true).
131     *  @exception IllegalActionException If there is no director.
132     */
133    @Override
134    public boolean prefire() throws IllegalActionException {
135        if (!input.hasToken(0)) {
136            return false;
137        }
138
139        return super.prefire();
140    }
141}