001/* An up-down counter.
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.component.test;
029
030import ptolemy.component.AtomicComponent;
031import ptolemy.component.MethodCallPort;
032import ptolemy.data.IntToken;
033import ptolemy.data.TupleToken;
034import ptolemy.kernel.CompositeEntity;
035import ptolemy.kernel.util.IllegalActionException;
036import ptolemy.kernel.util.NameDuplicationException;
037
038///////////////////////////////////////////////////////////////////
039//// Counter
040
041/**
042 This component implements an up-down counter.
043
044 @author Yang Zhao
045 @version $Id$
046 @since Ptolemy II 11.0
047 @Pt.ProposedRating Yellow (ellen_zh)
048 @Pt.AcceptedRating Red (neuendor)
049 */
050public class Counter extends AtomicComponent {
051    /** Construct an actor with the given container and name.
052     *  @param container The container.
053     *  @param name The name of this actor.
054     *  @exception IllegalActionException If the actor cannot be contained
055     *   by the proposed container.
056     *  @exception NameDuplicationException If the container already has an
057     *   actor with this name.
058     */
059    public Counter(CompositeEntity container, String name)
060            throws NameDuplicationException, IllegalActionException {
061        super(container, name);
062        increment = new MethodCallPort(this, "increment", true) {
063            @Override
064            public synchronized TupleToken call(TupleToken args) {
065                System.out.println("---call method Counter.increment");
066
067                IntToken arg = (IntToken) args.getElement(0);
068                _count += arg.intValue();
069
070                IntToken[] t = new IntToken[1];
071                t[0] = new IntToken(_count);
072                return output.call(new TupleToken(t));
073            }
074        };
075
076        //increment.setTypeEquals(BaseType.GENERAL);
077        decrement = new MethodCallPort(this, "decrement", true);
078
079        //decrement.setTypeEquals(BaseType.GENERAL);
080        output = new MethodCallPort(this, "output", false);
081
082        //output.setTypeEquals(BaseType.INT);
083    }
084
085    ///////////////////////////////////////////////////////////////////
086    ////                     ports and parameters                  ////
087
088    /** The increment port. If this input port
089     *  receives a token, then the counter is incremented.  The port
090     *  has type general.
091     */
092    public MethodCallPort increment;
093
094    /** The decrement port. If this input port
095     *  receives a token, then the counter is decremented.  The port
096     *  has type general.
097     */
098    public MethodCallPort decrement;
099
100    /** The output port with type IntToken.
101     */
102    public MethodCallPort output;
103
104    ///////////////////////////////////////////////////////////////////
105    ////                         public methods                    ////
106
107    /** Reset the count of inputs to zero.
108     *  @exception IllegalActionException If the parent class throws it.
109     */
110    @Override
111    public void initialize() throws IllegalActionException {
112        _count = 0;
113    }
114
115    ///////////////////////////////////////////////////////////////////
116    ////                         private members                   ////
117    private int _count = 0;
118}