001/* 002 * Copyright (c) 2005-2010 The Regents of the University of California. 003 * All rights reserved. 004 * 005 * '$Author: welker $' 006 * '$Date: 2010-05-06 05:21:26 +0000 (Thu, 06 May 2010) $' 007 * '$Revision: 24234 $' 008 * 009 * Permission is hereby granted, without written agreement and without 010 * license or royalty fees, to use, copy, modify, and distribute this 011 * software and its documentation for any purpose, provided that the above 012 * copyright notice and the following two paragraphs appear in all copies 013 * of this software. 014 * 015 * IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY 016 * FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES 017 * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF 018 * THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF 019 * SUCH DAMAGE. 020 * 021 * THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES, 022 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 023 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE 024 * PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF 025 * CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, 026 * ENHANCEMENTS, OR MODIFICATIONS. 027 * 028 */ 029 030package org.resurgence.actor; 031 032import ptolemy.actor.TypedAtomicActor; 033import ptolemy.actor.TypedIOPort; 034import ptolemy.data.IntToken; 035import ptolemy.data.type.BaseType; 036import ptolemy.kernel.CompositeEntity; 037import ptolemy.kernel.util.IllegalActionException; 038import ptolemy.kernel.util.NameDuplicationException; 039 040////////////////////////////////////////////////////////////////////////// 041//// TokenCounter 042 043/** 044 * This actor consumes and counts all tokens from the input port and prints out 045 * their overall number. The relations connected to the input port are scanned 046 * one after the other. 047 * 048 * @author Wibke Sudholt, University and ETH Zurich, November 2004 049 * @version $Id: TokenCounter.java 24234 2010-05-06 05:21:26Z welker $ 050 */ 051public class TokenCounter extends TypedAtomicActor { 052 053 /** 054 * Construct a TokenCounter with the given container and name. 055 * 056 * @param container 057 * The container. 058 * @param name 059 * The name of this actor. 060 * @exception IllegalActionException 061 * If the entity cannot be contained by the proposed 062 * container. 063 * @exception NameDuplicationException 064 * If the container already has an actor with this name. 065 */ 066 public TokenCounter(CompositeEntity container, String name) 067 throws NameDuplicationException, IllegalActionException { 068 super(container, name); 069 070 token = new TypedIOPort(this, "token", true, false); 071 token.setTypeEquals(BaseType.UNKNOWN); 072 token.setMultiport(true); 073 074 number = new TypedIOPort(this, "number", false, true); 075 number.setTypeEquals(BaseType.INT); 076 } 077 078 // ///////////////////////////////////////////////////////////////// 079 // // ports and parameters //// 080 081 /** 082 * The input port, which contains all tokens. 083 */ 084 public TypedIOPort token = null; 085 /** 086 * The output port, which contains the number of tokens. 087 */ 088 public TypedIOPort number = null; 089 090 // ///////////////////////////////////////////////////////////////// 091 // // public methods //// 092 093 /** 094 * Consume and count all tokens and print out their number. 095 * 096 * @exception IllegalActionException 097 * If there's no director. 098 */ 099 public void fire() throws IllegalActionException { 100 super.fire(); 101 _counter = 0; 102 // Consume and count the input tokens. 103 for (int i = 0; i < token.getWidth(); i++) { 104 // FIXME: This does not work for the PN Director. 105 while (token.hasToken(i)) { 106 token.get(i); 107 _counter++; 108 } 109 } 110 // Print out the number of tokens. 111 number.send(0, new IntToken(_counter)); 112 } 113 114 /** 115 * Post fire the actor. Return false to indicate that the process has 116 * finished. If it returns true, the process will continue indefinitely. 117 */ 118 public boolean postfire() { 119 return false; 120 } 121 122 // ///////////////////////////////////////////////////////////////// 123 // // protected members //// 124 125 // ///////////////////////////////////////////////////////////////// 126 // // private methods //// 127 128 // ///////////////////////////////////////////////////////////////// 129 // // private members //// 130 131 private int _counter; 132}