001/* Extract maximum element from an array. 002 003 Copyright (c) 2003-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 */ 027package ptolemy.actor.lib; 028 029import ptolemy.actor.TypedIOPort; 030import ptolemy.data.ArrayToken; 031import ptolemy.data.IntToken; 032import ptolemy.data.ScalarToken; 033import ptolemy.data.expr.Parameter; 034import ptolemy.data.type.ArrayType; 035import ptolemy.data.type.BaseType; 036import ptolemy.kernel.CompositeEntity; 037import ptolemy.kernel.util.IllegalActionException; 038import ptolemy.kernel.util.InternalErrorException; 039import ptolemy.kernel.util.NameDuplicationException; 040import ptolemy.kernel.util.Workspace; 041 042/////////////////////////////////////////////////////////////////// 043//// ArrayMaximum 044 045/** 046 Extract the maximum element from an array. This actor reads an array 047 from the <i>input</i> port and sends the largest of its elements to the 048 <i>output</i> port. The index of the largest element is sent to the 049 <i>index</i> output port. If there is more than one entry in the array 050 with the maximum value, then the index of the first such entry 051 is what is produced. 052 053 @author Mark Oliver 054 @version $Id$ 055 @since Ptolemy II 3.0.2 056 @Pt.ProposedRating Red (cxh) 057 @Pt.AcceptedRating Red (cxh) 058 */ 059public class ArrayMaximum extends Transformer { 060 /** Construct an actor with the given container and name. 061 * @param container The container. 062 * @param name The name of this actor. 063 * @exception IllegalActionException If the actor cannot be contained 064 * by the proposed container. 065 * @exception NameDuplicationException If the container already has an 066 * actor with this name. 067 */ 068 public ArrayMaximum(CompositeEntity container, String name) 069 throws NameDuplicationException, IllegalActionException { 070 super(container, name); 071 072 // Create index port 073 index = new TypedIOPort(this, "index", false, true); 074 index.setTypeEquals(BaseType.INT); 075 new Parameter(index, "_showName").setExpression("true"); 076 077 // Type constraints. 078 output.setTypeAtLeast(ArrayType.elementType(input)); 079 } 080 081 /////////////////////////////////////////////////////////////////// 082 //// parameters //// 083 084 /** The port producing the index of the largest element. 085 * This is port has type int. 086 */ 087 public TypedIOPort index; 088 089 /////////////////////////////////////////////////////////////////// 090 //// public methods //// 091 092 /** Override the base class to set type constraints. 093 * @param workspace The workspace for the new object. 094 * @return A new instance of ArrayElement. 095 * @exception CloneNotSupportedException If a derived class contains 096 * an attribute that cannot be cloned. 097 */ 098 @Override 099 public Object clone(Workspace workspace) throws CloneNotSupportedException { 100 ArrayMaximum newObject = (ArrayMaximum) super.clone(workspace); 101 try { 102 newObject.output 103 .setTypeAtLeast(ArrayType.elementType(newObject.input)); 104 } catch (IllegalActionException e) { 105 // Should have been caught before. 106 throw new InternalErrorException(e); 107 } 108 return newObject; 109 } 110 111 /** Consume at most one array from the input port and produce 112 * the maximum of its elements on the <i>output</i> port and the index 113 * of that element on the <i>index</i> port. If there is no token 114 * on the input, then no output is produced. 115 * @exception IllegalActionException If there is no director. 116 */ 117 @Override 118 public void fire() throws IllegalActionException { 119 super.fire(); 120 int indexValue = 0; 121 122 if (input.hasToken(0)) { 123 ArrayToken token = (ArrayToken) input.get(0); 124 ScalarToken currentMax = (ScalarToken) token.getElement(indexValue); 125 ScalarToken temp = null; 126 int i; 127 128 for (i = indexValue + 1; i < token.length(); i++) { 129 temp = (ScalarToken) token.getElement(i); 130 131 if (currentMax.isLessThan(temp).booleanValue() == true) { 132 indexValue = i; 133 currentMax = temp; 134 } 135 } 136 137 output.send(0, currentMax); 138 index.broadcast(new IntToken(indexValue)); 139 } 140 } 141}