001/* An actor that outputs the difference between successive inputs. 002 003 Copyright (c) 1998-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.Token; 031import ptolemy.kernel.CompositeEntity; 032import ptolemy.kernel.util.IllegalActionException; 033import ptolemy.kernel.util.NameDuplicationException; 034 035/////////////////////////////////////////////////////////////////// 036//// Differential 037 038/** 039 Output the current input minus the previous input, or if there 040 has been no previous input, the current input itself. 041 042 @author Edward A. Lee 043 @version $Id$ 044 @since Ptolemy II 2.0 045 @Pt.ProposedRating Red (eal) 046 @Pt.AcceptedRating Red (cxh) 047 */ 048public class Differential extends Transformer { 049 /** Construct an actor with the given container and name. 050 * @param container The container. 051 * @param name The name of this actor. 052 * @exception IllegalActionException If the actor cannot be contained 053 * by the proposed container. 054 * @exception NameDuplicationException If the container already has an 055 * actor with this name. 056 */ 057 public Differential(CompositeEntity container, String name) 058 throws NameDuplicationException, IllegalActionException { 059 super(container, name); 060 } 061 062 /////////////////////////////////////////////////////////////////// 063 //// public methods //// 064 065 /** Consume at most one token from the <i>input</i> port and output 066 * its value minus the value of the input read in the previous 067 * iteration. If there has been no previous iteration, then 068 * output the current input. If there is no input, then produce 069 * no output. 070 * @exception IllegalActionException If subtraction is not 071 * supported by the supplied tokens. 072 */ 073 @Override 074 public void fire() throws IllegalActionException { 075 super.fire(); 076 if (input.hasToken(0)) { 077 _currentInput = input.get(0); 078 079 if (_lastInput != null) { 080 output.broadcast(_currentInput.subtract(_lastInput)); 081 } else { 082 output.broadcast(_currentInput); 083 } 084 } 085 } 086 087 /** Reset to indicate that no input has yet been seen. 088 * @exception IllegalActionException If the parent class throws it. 089 */ 090 @Override 091 public void initialize() throws IllegalActionException { 092 super.initialize(); 093 _lastInput = null; 094 } 095 096 /** Record the most recent input as the latest input. 097 * @exception IllegalActionException If the base class throws it. 098 */ 099 @Override 100 public boolean postfire() throws IllegalActionException { 101 _lastInput = _currentInput; 102 return super.postfire(); 103 } 104 105 /////////////////////////////////////////////////////////////////// 106 //// private members //// 107 private Token _currentInput; 108 109 private Token _lastInput; 110}