001/* Produce the index of the largest of the inputs. 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.actor.lib; 029 030import ptolemy.data.DoubleToken; 031import ptolemy.data.IntToken; 032import ptolemy.data.type.BaseType; 033import ptolemy.kernel.CompositeEntity; 034import ptolemy.kernel.util.IllegalActionException; 035import ptolemy.kernel.util.NameDuplicationException; 036 037/////////////////////////////////////////////////////////////////// 038//// MaxIndex 039 040/** 041 Produce the channel number of the largest of the inputs. 042 This actor has one input port, which is multiport of type double 043 and one output port, which is not a multiport, and has type int. 044 The tokens on each channel of the input port will be 045 compared and an IntToken giving the channel number of the largest 046 one will be output. 047 048 @deprecated Use Maximum instead. 049 @author Jeff Tsay and Edward A. Lee 050 @version $Id$ 051 @since Ptolemy II 1.0 052 @Pt.ProposedRating Yellow (eal) 053 @Pt.AcceptedRating Yellow (ssachs) 054 */ 055@Deprecated 056public class MaxIndex extends Transformer { 057 /** Construct an actor in the specified container with the specified 058 * name. 059 * @param container The container. 060 * @param name The name of this actor within the container. 061 * @exception IllegalActionException If the actor cannot be contained 062 * by the proposed container. 063 * @exception NameDuplicationException If the name coincides with 064 * an actor already in the container. 065 */ 066 public MaxIndex(CompositeEntity container, String name) 067 throws IllegalActionException, NameDuplicationException { 068 super(container, name); 069 input.setTypeEquals(BaseType.DOUBLE); 070 input.setMultiport(true); 071 output.setTypeEquals(BaseType.INT); 072 } 073 074 /////////////////////////////////////////////////////////////////// 075 //// public methods //// 076 077 /** Read at most one token from each channel of the input port 078 * and produce the channel number of the largest one. 079 * If none of the input channels has a token, do nothing. 080 * 081 * @exception IllegalActionException If there is no director. 082 */ 083 @Override 084 public void fire() throws IllegalActionException { 085 super.fire(); 086 double maxValue = Double.NEGATIVE_INFINITY; 087 int maxIndex = -1; 088 boolean foundFirst = false; 089 090 for (int i = 0; i < input.getWidth(); i++) { 091 if (input.hasToken(i)) { 092 double val = ((DoubleToken) input.get(i)).doubleValue(); 093 094 if (foundFirst) { 095 if (maxValue < val) { 096 maxValue = val; 097 maxIndex = i; 098 } 099 } else { 100 maxValue = val; 101 maxIndex = i; 102 foundFirst = true; 103 } 104 } 105 } 106 107 if (foundFirst) { 108 output.send(0, new IntToken(maxIndex)); 109 } 110 } 111}