/* An actor that outputs the absolute value of the input. Copyright (c) 1998-2006 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; import java.util.Set; import ptolemy.actor.TypedIOPort; import ptolemy.data.ScalarToken; import ptolemy.data.type.BaseType; import ptolemy.data.type.MonotonicFunction; import ptolemy.data.type.Type; import ptolemy.graph.Inequality; import ptolemy.graph.InequalityTerm; import ptolemy.kernel.CompositeEntity; import ptolemy.kernel.util.IllegalActionException; import ptolemy.kernel.util.NameDuplicationException; import ptolemy.kernel.util.Workspace; ////////////////////////////////////////////////////////////////////////// //// AbsoluteValue /** Produce an output token on each firing with a value that is equal to the absolute value of the input. The input can have any scalar type. If the input type is not Complex, the output has the same type as the input. If the input type is Complex, the output type is Double, in which case, the output value is the magnitude of the input complex. @author Edward A. Lee @version $Id$ @since Ptolemy II 0.3 @Pt.ProposedRating Yellow (eal) @Pt.AcceptedRating Yellow (yuhong) */ public class AbsoluteValue extends Transformer { /** Construct an actor with the given container and name. * @param container The container. * @param name The name of this actor. * @exception IllegalActionException If the actor cannot be contained * by the proposed container. * @exception NameDuplicationException If the container already has an * actor with this name. */ public AbsoluteValue(CompositeEntity container, String name) throws NameDuplicationException, IllegalActionException { super(container, name); output.setTypeAtLeast(new FunctionTerm(input)); } /////////////////////////////////////////////////////////////////// //// public methods //// /** Clone the actor into the specified workspace. This calls the * base class and then sets the type constraints. * @param workspace The workspace for the new object. * @return A new actor. * @exception CloneNotSupportedException If a derived class has * an attribute that cannot be cloned. */ public Object clone(Workspace workspace) throws CloneNotSupportedException { AbsoluteValue newObject = (AbsoluteValue) super.clone(workspace); newObject.output.setTypeAtLeast(new FunctionTerm(newObject.input)); return newObject; } /** Compute the absolute value of the input. If there is no input, then * produce no output. * @exception IllegalActionException If there is no director. */ public void fire() throws IllegalActionException { super.fire(); if (input.hasToken(0)) { ScalarToken in = (ScalarToken) input.get(0); output.send(0, in.absolute()); } } /** Return the following type constraints: If the input type is Complex, * the output type is no less than Double, otherwise, the output type * is no less than the input; The output type is no greater than Scalar. * @return A list of inequalities. */ public Set<Inequality> typeConstraints() { // type constraints are stored in the output port. return output.typeConstraints(); } /////////////////////////////////////////////////////////////////// //// private variables //// /////////////////////////////////////////////////////////////////// //// inner classes //// // This class implements a monotonic function of the input port // type. The result of the function is the same as the input type // if is not Complex; otherwise, the result is Double. private static class FunctionTerm extends MonotonicFunction { // FindBugs suggested making this class a static inner class: // // "This class is an inner class, but does not use its embedded // reference to the object which created it. This reference makes // the instances of the class larger, and may keep the reference // to the creator object alive longer than necessary. If // possible, the class should be made into a static inner class." // The constructor takes a port argument so that the clone() // method can construct an instance of this class for the // input port on the clone. private FunctionTerm(TypedIOPort port) { _port = port; } /////////////////////////////////////////////////////////////// //// public inner methods //// /** Return the function result. * @return A Type. */ public Object getValue() { Type inputType = _port.getType(); if (inputType == BaseType.COMPLEX) { return BaseType.DOUBLE; } else if (inputType == BaseType.COMPLEX_MATRIX) { return BaseType.DOUBLE_MATRIX; } else { return inputType; } } /** Return the variables in this term. If the type of the input port * is a variable, return a one element array containing the * InequalityTerm of that port; otherwise, return an array of zero * length. * @return An array of InequalityTerm. */ public InequalityTerm[] getVariables() { if (_port.getTypeTerm().isSettable()) { InequalityTerm[] variable = new InequalityTerm[1]; variable[0] = _port.getTypeTerm(); return variable; } else { return new InequalityTerm[0]; } } /////////////////////////////////////////////////////////////// //// private inner variable //// private TypedIOPort _port; } }