001/*
002 * Copyright (c) 2005-2010 The Regents of the University of California.
003 * All rights reserved.
004 *
005 * '$Author: barseghian $'
006 * '$Date: 2012-09-18 04:38:03 +0000 (Tue, 18 Sep 2012) $' 
007 * '$Revision: 30699 $'
008 * 
009 * Permission is hereby granted, without written agreement and without
010 * license or royalty fees, to use, copy, modify, and distribute this
011 * software and its documentation for any purpose, provided that the above
012 * copyright notice and the following two paragraphs appear in all copies
013 * of this software.
014 *
015 * IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY
016 * FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
017 * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF
018 * THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF
019 * SUCH DAMAGE.
020 *
021 * THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
022 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
023 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE
024 * PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF
025 * CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES,
026 * ENHANCEMENTS, OR MODIFICATIONS.
027 *
028 */
029
030package org.resurgence.actor;
031
032import ptolemy.actor.TypedAtomicActor;
033import ptolemy.actor.TypedIOPort;
034import ptolemy.data.ArrayToken;
035import ptolemy.data.StringToken;
036import ptolemy.data.Token;
037import ptolemy.data.expr.Parameter;
038import ptolemy.data.type.ArrayType;
039import ptolemy.data.type.BaseType;
040import ptolemy.kernel.CompositeEntity;
041import ptolemy.kernel.util.IllegalActionException;
042import ptolemy.kernel.util.NameDuplicationException;
043
044
045/**
046 * This actor reads an array and writes a string with all the elements. The
047 * characters separating the entries can be specified as a parameter.
048 * 
049 * @author Wibke Sudholt, University and ETH Zurich, November 2004
050 * @version $Id: ArrayToString.java 30699 2012-09-18 04:38:03Z barseghian $
051 */
052public class ArrayToString extends TypedAtomicActor {
053
054        /**
055         * Construct an ArrayToString with the given container and name.
056         * 
057         * @param container
058         *            The container.
059         * @param name
060         *            The name of this actor.
061         * @exception IllegalActionException
062         *                If the entity cannot be contained by the proposed
063         *                container.
064         * @exception NameDuplicationException
065         *                If the container already has an actor with this name.
066         */
067        public ArrayToString(CompositeEntity container, String name)
068                        throws NameDuplicationException, IllegalActionException {
069                super(container, name);
070
071                array = new TypedIOPort(this, "array", true, false);
072                array.setTypeAtLeast(ArrayType.ARRAY_BOTTOM);
073
074                string = new TypedIOPort(this, "string", false, true);
075                string.setTypeEquals(BaseType.STRING);
076
077                separator = new Parameter(this, "Element separator",
078                                new StringToken(""));
079        }
080
081        // /////////////////////////////////////////////////////////////////
082        // // ports and parameters ////
083
084        /**
085         * The input port, which contains the array.
086         */
087        public TypedIOPort array = null;
088        /**
089         * The output port, which contains the string.
090         */
091        public TypedIOPort string = null;
092        /**
093         * The parameter, which specifies the element separator.
094         */
095        public Parameter separator = null;
096
097        // /////////////////////////////////////////////////////////////////
098        // // public methods ////
099
100        /**
101         * Take the array and print out the elements.
102         * 
103         * @exception IllegalActionException
104         *                If there's no director.
105         */
106        public void fire() throws IllegalActionException {
107                super.fire();
108                _token = ((ArrayToken) array.get(0)).arrayValue();
109                _elements = (_entry.convert(_token[0])).stringValue();
110                _spacer = ((StringToken) separator.getToken()).stringValue();
111                for (int i = 1; i < _token.length; i++) {
112                        _elements = _elements + _spacer
113                                        + (_entry.convert(_token[i])).stringValue();
114                }
115                string.send(0, new StringToken(_elements));
116        }
117
118        // /////////////////////////////////////////////////////////////////
119        // // protected members ////
120
121        // /////////////////////////////////////////////////////////////////
122        // // private methods ////
123
124        // /////////////////////////////////////////////////////////////////
125        // // private members ////
126
127        private Token[] _token;
128        private String _elements;
129        private StringToken _entry;
130        private String _spacer;
131}