/* An actor that computes a specified rounded value of the input. Copyright (c) 1998-2014 The Regents of the University of California. All rights reserved. Permission is hereby granted, without written agreement and without license or royalty fees, to use, copy, modify, and distribute this software and its documentation for any purpose, provided that the above copyright notice and the following two paragraphs appear in all copies of this software. IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. PT_COPYRIGHT_VERSION_2 COPYRIGHTENDKEY */ package ptolemy.actor.lib.conversions; import ptolemy.actor.lib.Transformer; import ptolemy.data.DoubleToken; import ptolemy.data.IntToken; import ptolemy.data.Token; import ptolemy.data.type.BaseType; import ptolemy.kernel.CompositeEntity; import ptolemy.kernel.util.Attribute; import ptolemy.kernel.util.IllegalActionException; import ptolemy.kernel.util.InvalidStateException; import ptolemy.kernel.util.NameDuplicationException; import ptolemy.kernel.util.StringAttribute; import ptolemy.kernel.util.Workspace; // NOTE: If you update the list of functions, then you will want // to update the list in actor/lib/math.xml. /////////////////////////////////////////////////////////////////// //// Round /** Produce an output token on each firing with a value that is equal to the specified rounded value of the input. The input type is DoubleToken. The output type is IntToken. The functions are a subset of those in the java.lang.Math class. They are:
* This method should be called instead of the usual prefire(), * fire(), postfire() methods when this actor is used in a * domain that supports vectorized actors. This leads to more * efficient execution. * @param count The number of iterations to perform. * @return COMPLETED if the actor was successfully iterated the * specified number of times. Otherwise, return NOT_READY, and do * not consume any input tokens. * @exception IllegalActionException Not thrown in this base class. */ @Override public int iterate(int count) throws IllegalActionException { // Check whether we need to reallocate the output token array. if (count > _resultArray.length) { _resultArray = new IntToken[count]; } if (input.hasToken(0, count)) { // NOTE: inArray.length may be > count, in which case // only the first count tokens are valid. Token[] inArray = input.get(0, count); for (int i = 0; i < count; i++) { double value = ((DoubleToken) inArray[i]).doubleValue(); if (Double.isNaN(value)) { throw new IllegalActionException( "Input is Double.NaN, " + "there is no way to represent a NaN as an integer."); } _resultArray[i] = new IntToken(_doFunction(value)); } output.send(0, _resultArray, count); return COMPLETED; } else { return NOT_READY; } } /** Return false if there is no available input token, and otherwise * return whatever the superclass returns (presumably true). * @exception IllegalActionException If there is no director. */ @Override public boolean prefire() throws IllegalActionException { if (!input.hasToken(0)) { return false; } return super.prefire(); } /////////////////////////////////////////////////////////////////// //// private methods //// /** Calculate the function on the given argument. * @param in The input value. * @return The result of applying the function. */ private int _doFunction(double in) { int result; switch (_function) { case _CEIL: result = (int) Math.ceil(in); break; case _FLOOR: result = (int) Math.floor(in); break; case _ROUND: result = (int) Math.round(in); break; case _TRUNCATE: if (in > 0) { result = (int) Math.floor(in); } else { result = (int) Math.ceil(in); } break; default: throw new InvalidStateException( "Invalid value for _function private variable. " + "Round actor (" + getFullName() + ")" + " on function type " + _function); } return result; } /////////////////////////////////////////////////////////////////// //// private variables //// private IntToken[] _resultArray = new IntToken[0]; // An indicator for the function to compute. // This variable has values specified in the constants below private int _function; // Constants used for more efficient execution. private static final int _CEIL = 0; private static final int _FLOOR = 1; private static final int _ROUND = 2; private static final int _TRUNCATE = 3; }