001/* An actor producing a sequence of 0 and 1.
002
003 Copyright (c) 2000-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.domains.modal.kernel.test;
029
030import ptolemy.actor.TypedCompositeActor;
031import ptolemy.actor.lib.SequenceSource;
032import ptolemy.data.IntToken;
033import ptolemy.data.Token;
034import ptolemy.data.type.BaseType;
035import ptolemy.kernel.util.IllegalActionException;
036import ptolemy.kernel.util.InternalErrorException;
037import ptolemy.kernel.util.NameDuplicationException;
038
039///////////////////////////////////////////////////////////////////
040//// ZeroOneSource
041
042/**
043 This actor produces the sequence 011101011011000... as source of the AMI
044 (Alternating Mark 1) test.
045 @author Xiaojun Liu
046 @version $Id$
047 @since Ptolemy II 8.0
048 @Pt.ProposedRating Red (liuxj)
049 @Pt.AcceptedRating Red (cxh)
050 */
051public class ZeroOneSource extends SequenceSource {
052    /** Construct an actor with the given container and name.
053     *  @param container The container.
054     *  @param name The name of this actor.
055     *  @exception IllegalActionException If the actor cannot be contained
056     *   by the proposed container.
057     *  @exception NameDuplicationException If the container already has an
058     *   actor with this name.
059     */
060    public ZeroOneSource(TypedCompositeActor container, String name)
061            throws NameDuplicationException, IllegalActionException {
062        super(container, name);
063        output.setTypeEquals(BaseType.INT);
064    }
065
066    ///////////////////////////////////////////////////////////////////
067    ////                         public methods                    ////
068
069    /** Send the current output in the sequence.
070     */
071    @Override
072    public void fire() {
073        try {
074            if (_iterationCount >= _seq.length) {
075                output.broadcast(_zero);
076            } else {
077                output.broadcast(new IntToken(_seq[_iterationCount]));
078            }
079        } catch (IllegalActionException ex) {
080            // Should not be thrown because this is an output port.
081            throw new InternalErrorException(ex.getMessage());
082        }
083    }
084
085    /** Initialize the iteration counter.
086     *  @exception IllegalActionException If the parent class throws it,
087     *   which could occur if, for example, the director will not accept
088     *   sequence actors.
089     */
090    @Override
091    public void initialize() throws IllegalActionException {
092        super.initialize();
093        _iterationCount = 0;
094    }
095
096    /** Increment the iteration counter, and if it equals the
097     *  value of the <i>firingCountLimit</i> parameter, return false.
098     *  Otherwise, return true.
099     *  @exception IllegalActionException If firingCountLimit has
100     *   an invalid expression.
101     */
102    @Override
103    public boolean postfire() throws IllegalActionException {
104        _iterationCount++;
105        return super.postfire();
106    }
107
108    ///////////////////////////////////////////////////////////////////
109    ////                         private variables                 ////
110    private int[] _seq = { 0, 1, 1, 1, 0, 1, 0, 1, 1, 0, 1, 1, 0, 0 };
111
112    private int _iterationCount = 0;
113
114    private Token _zero = new IntToken(0);
115}