001/* A base class for actors that transform an input stream into an output stream. 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.domains.pn.lib; 029 030import ptolemy.actor.lib.Transformer; 031import ptolemy.data.IntToken; 032import ptolemy.data.Token; 033import ptolemy.data.expr.Parameter; 034import ptolemy.data.type.BaseType; 035import ptolemy.kernel.CompositeEntity; 036import ptolemy.kernel.util.IllegalActionException; 037import ptolemy.kernel.util.NameDuplicationException; 038 039/////////////////////////////////////////////////////////////////// 040//// Starver 041 042/** 043 On each firing, read at most one input token and send it 044 to the output port. When the number of output tokens that 045 have been produced reaches the value given by <i>limit</i>, 046 then do not produce any more outputs. Subsequent input tokens 047 are consumed and discarded. 048 049 @author Edward A. Lee 050 @version $Id$ 051 @since Ptolemy II 6.1 052 @Pt.ProposedRating Green (eal) 053 @Pt.AcceptedRating Red (cxh) 054 */ 055public class Starver extends Transformer { 056 /** Construct an actor with the given container and name. 057 * @param container The container. 058 * @param name The name of this actor. 059 * @exception IllegalActionException If the actor cannot be contained 060 * by the proposed container. 061 * @exception NameDuplicationException If the container already has an 062 * actor with this name. 063 */ 064 public Starver(CompositeEntity container, String name) 065 throws NameDuplicationException, IllegalActionException { 066 super(container, name); 067 limit = new Parameter(this, "limit"); 068 limit.setTypeEquals(BaseType.INT); 069 limit.setExpression("1"); 070 } 071 072 /////////////////////////////////////////////////////////////////// 073 //// ports and parameters //// 074 075 /** The limit on the number of tokens that can be transferred 076 * from the input to the output. This is an integer that 077 * defaults to 1. If the value is negative, then there is no 078 * limit. 079 */ 080 public Parameter limit; 081 082 /////////////////////////////////////////////////////////////////// 083 //// public methods //// 084 085 /** Read at most one input token, and if the number of outputs 086 * has not yet exceeded the value given by <i>limit</i>, then 087 * produce that token on the output. 088 * @exception IllegalActionException Not thrown in this base class. 089 */ 090 @Override 091 public void fire() throws IllegalActionException { 092 super.fire(); 093 Token token = null; 094 if (input.hasToken(0)) { 095 token = input.get(0); 096 } 097 int limitValue = ((IntToken) limit.getToken()).intValue(); 098 if (token != null && (limitValue < 0 || _count < limitValue)) { 099 output.send(0, token); 100 _count++; 101 } 102 } 103 104 /** Initialize this actor by setting the count of outputs to zero. 105 * @exception IllegalActionException If a derived class throws it. 106 */ 107 @Override 108 public void initialize() throws IllegalActionException { 109 super.initialize(); 110 _count = 0; 111 } 112 113 /////////////////////////////////////////////////////////////////// 114 //// public methods //// 115 116 /** The count of outputs that have been produced. */ 117 private int _count; 118}