001/*
002 * Copyright (c) 1998-2010 The Regents of the University of California.
003 * All rights reserved.
004 *
005 * '$Author: welker $'
006 * '$Date: 2010-05-06 05:21:26 +0000 (Thu, 06 May 2010) $' 
007 * '$Revision: 24234 $'
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.geon;
031
032import ptolemy.actor.TypedAtomicActor;
033import ptolemy.actor.TypedIOPort;
034import ptolemy.data.ArrayToken;
035import ptolemy.data.IntToken;
036import ptolemy.data.Token;
037import ptolemy.data.type.ArrayType;
038import ptolemy.data.type.BaseType;
039import ptolemy.kernel.CompositeEntity;
040import ptolemy.kernel.util.IllegalActionException;
041import ptolemy.kernel.util.NameDuplicationException;
042
043//////////////////////////////////////////////////////////////////////////
044//// Duplicator
045/**
046 * This actor takes as inputs a token and an integer number n and duplicates the
047 * token n times.
048 * 
049 * @author Efrat Jaeger
050 * @version $Id: Duplicator.java 24234 2010-05-06 05:21:26Z welker $
051 * @since Ptolemy II 4.0.1
052 */
053
054public class Duplicator extends TypedAtomicActor {
055
056        /**
057         * Construct an actor with the given container and name.
058         * 
059         * @param container
060         *            The container.
061         * @param name
062         *            The name of this actor.
063         * @exception IllegalActionException
064         *                If the actor cannot be contained by the proposed
065         *                container.
066         * @exception NameDuplicationException
067         *                If the container already has an actor with this name.
068         */
069        public Duplicator(CompositeEntity container, String name)
070                        throws NameDuplicationException, IllegalActionException {
071                super(container, name);
072
073                input = new TypedIOPort(this, "input", true, false);
074
075                numCopies = new TypedIOPort(this, "numCopies", true, false);
076                numCopies.setTypeEquals(BaseType.INT);
077
078                output = new TypedIOPort(this, "output", false, true);
079                output.setTypeAtLeast(ArrayType.arrayOf(input));
080        }
081
082        // /////////////////////////////////////////////////////////////////
083        // // ports and parameters ////
084
085        /** Input token. */
086        public TypedIOPort input;
087
088        /**
089         * Number of copies.
090         */
091        public TypedIOPort numCopies;
092
093        /**
094         * Output token.
095         */
096        public TypedIOPort output;
097
098        // /////////////////////////////////////////////////////////////////
099        // // public methods ////
100
101        /**
102         * broadcast the input token numCopies times.
103         */
104        public void fire() throws IllegalActionException {
105                int _numCopies = ((IntToken) numCopies.get(0)).intValue();
106                Token copies[] = new Token[_numCopies];
107                Token value = input.get(0);
108
109                for (int i = 0; i < _numCopies; i++) {
110                        copies[i] = value;
111                }
112                output.send(0, new ArrayToken(copies));
113        }
114}