001/*
002 @Copyright (c) 1998-2014 The Regents of the University of California.
003 All rights reserved.
004
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
008 above copyright notice and the following two paragraphs appear in all
009 copies 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 */
027package ptolemy.domains.sdf.kernel.test;
028
029import ptolemy.actor.TypedAtomicActor;
030import ptolemy.actor.TypedIOPort;
031import ptolemy.data.IntToken;
032import ptolemy.data.Token;
033import ptolemy.data.expr.Parameter;
034import ptolemy.kernel.CompositeEntity;
035import ptolemy.kernel.util.IllegalActionException;
036import ptolemy.kernel.util.NameDuplicationException;
037import ptolemy.kernel.util.Workspace;
038
039/**
040 * This actor will consume all tokens on its input port and write their
041 * values to a string.  The value of the string can then be obtained
042 * for use in test scripts, etc.
043 *
044 * This actor is aware of the rate that is set on its input port and will
045 * consume an appropriate number of tokens with each firing.
046 * This actor is type Polymorphic.
047 *
048 * @version $Id$
049 * @since Ptolemy II 0.4
050 * @Pt.ProposedRating Red
051 * @Pt.AcceptedRating Red
052 * @author Steve Neuendorffer
053 */
054public class SDFTestConsumer extends TypedAtomicActor {
055    public SDFTestConsumer(CompositeEntity container, String name)
056            throws IllegalActionException, NameDuplicationException {
057        super(container, name);
058        input = new TypedIOPort(this, "input", true, false);
059        input_tokenConsumptionRate = new Parameter(input,
060                "tokenConsumptionRate", new IntToken("1"));
061        _history = new StringBuffer("");
062    }
063
064    public TypedIOPort input;
065
066    public Parameter input_tokenConsumptionRate;
067
068    /** Clone the actor into the specified workspace. This calls the
069     *  base class and then creates new ports and parameters.  The new
070     *  actor will have the same parameter values as the old.
071     *  @param workspace The workspace for the new object.
072     *  @return A new actor.
073     *  @exception CloneNotSupportedException If one of the attributes
074     *   cannot be cloned.
075     */
076    @Override
077    public Object clone(Workspace workspace) throws CloneNotSupportedException {
078        SDFTestConsumer newObject = (SDFTestConsumer) super.clone(workspace);
079        newObject._history = new StringBuffer(_history.toString());
080        return newObject;
081    }
082
083    /**
084     * Fire the Actor
085     * Consume an input token, and append its value to the history.
086     * @exception IllegalActionException If a contained method throws it.
087     */
088    @Override
089    public void fire() throws IllegalActionException {
090        int tokens = ((IntToken) input_tokenConsumptionRate.getToken())
091                .intValue();
092        int i;
093
094        for (i = 0; i < tokens; i++) {
095            Token t = input.get(0);
096            _history.append(t.toString() + "\n");
097        }
098    }
099
100    /**
101     * Return a string representing the values of the tokens that have been
102     * consumed so far by this actor, since its creation.
103     */
104    public String getHistory() {
105        return _history.toString();
106    }
107
108    private StringBuffer _history;
109}