001/* DummyTransformer is a simple transformer actor implementing the
002   BufferingProfile interface.
003   It is used for testing the OptimizingSDFDirector.
004
005 Copyright (c) 1997-2015 The Regents of the University of California.
006 All rights reserved.
007 Permission is hereby granted, without written agreement and without
008 license or royalty fees, to use, copy, modify, and distribute this
009 software and its documentation for any purpose, provided that the above
010 copyright notice and the following two paragraphs appear in all copies
011 of this software.
012
013 IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY
014 FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
015 ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF
016 THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF
017 SUCH DAMAGE.
018
019 THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
020 INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
021 MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE
022 PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF
023 CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES,
024 ENHANCEMENTS, OR MODIFICATIONS.
025 PT_COPYRIGHT_VERSION_2
026 COPYRIGHTENDKEY
027
028 */
029
030package ptolemy.domains.sdf.optimize.lib;
031
032import ptolemy.data.Token;
033import ptolemy.domains.sdf.optimize.SharedBufferTransformer;
034import ptolemy.kernel.CompositeEntity;
035import ptolemy.kernel.util.IllegalActionException;
036import ptolemy.kernel.util.NameDuplicationException;
037
038/**
039<h1>Class comments</h1>
040A DummyTransformer is a simple actor with one input port and one output port
041imitation a filter type of actor
042It is used for testing the OptimizingSDFDirector.
043<p>
044See {@link ptolemy.domains.sdf.optimize.OptimizingSDFDirector},
045{@link ptolemy.domains.sdf.optimize.SharedBufferTransformer},
046{@link ptolemy.domains.sdf.optimize.OptimizingSDFScheduler} and
047{@link ptolemy.domains.sdf.optimize.BufferingProfile} for more information.
048</p>
049@see ptolemy.domains.sdf.optimize.OptimizingSDFDirector
050@see ptolemy.domains.sdf.optimize.OptimizingSDFDirector
051@see ptolemy.domains.sdf.optimize.SharedBufferTransformer
052@see ptolemy.domains.sdf.optimize.BufferingProfile
053
054@author Marc Geilen
055@version $Id$
056@since Ptolemy II 10.0
057@Pt.ProposedRating Red (mgeilen)
058@Pt.AcceptedRating Red ()
059 */
060
061public class DummyTransformer extends SharedBufferTransformer {
062
063    /**
064     * Constructs an instance of a dummy transformer actor for testing purposes.
065     * It mimics a typical image processing operation on a shared frame buffer,
066     * where it can perform the operation in-place, modifying the frame buffer,
067     * or operate in a copying mode where it produces a new frame buffer, leaving
068     * the original intact for other operations.
069     *  @param container The container.
070     *  @param name The name of this actor.
071     *  @exception IllegalActionException If the actor cannot be contained
072     *   by the proposed container.
073     *  @exception NameDuplicationException If the container already has an
074     *   actor with this name.
075     */
076    public DummyTransformer(CompositeEntity container, String name)
077            throws NameDuplicationException, IllegalActionException {
078        super(container, name);
079    }
080
081    ///////////////////////////////////////////////////////////////////
082    ////                    protected fields                       ////
083
084    /**
085     * Fire the actor in shared firing mode.
086     * It makes a copy of the frame buffer referred to by the reference token.
087     * @exception IllegalActionException If the input token is not
088     * an instance of DummyReferenceToken.
089     */
090    @Override
091    protected void _fireCopying() throws IllegalActionException {
092        if (input.hasToken(0)) {
093            Token t = input.get(0);
094            if (!(t instanceof DummyReferenceToken)) {
095                throw new IllegalActionException(
096                        "Token is of wrong type. Expected DummyReferenceToken");
097            }
098            DummyReferenceToken rt = (DummyReferenceToken) t;
099            // Get and duplicate the frame
100            DummyFrame f = ((DummyFrame) rt.getReference()).clone();
101            f.value++;
102            // send a new token
103            output.send(0, new DummyReferenceToken(f));
104        }
105    }
106
107    /**
108     * Fire the actor in exclusive firing mode.
109     * It directly modifies the frame buffer referred to by the reference token.
110     * @exception IllegalActionException If thrown while calling hasToken()
111     * or while getting the token.
112     */
113    @Override
114    protected void _fireExclusive() throws IllegalActionException {
115        if (input.hasToken(0)) {
116            Token t = input.get(0);
117            DummyReferenceToken rt = (DummyReferenceToken) t;
118            // Get the frame without duplicating
119            DummyFrame f = (DummyFrame) rt.getReference();
120            f.value++;
121            // send the original token
122            output.send(0, t);
123        }
124    }
125
126}