001/* A Delay Line with ArrayToken output. 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.domains.sdf.lib; 029 030import ptolemy.data.ArrayToken; 031import ptolemy.data.Token; 032import ptolemy.data.expr.Parameter; 033import ptolemy.data.type.ArrayType; 034import ptolemy.kernel.CompositeEntity; 035import ptolemy.kernel.util.Attribute; 036import ptolemy.kernel.util.IllegalActionException; 037import ptolemy.kernel.util.InternalErrorException; 038import ptolemy.kernel.util.NameDuplicationException; 039import ptolemy.kernel.util.Workspace; 040 041/////////////////////////////////////////////////////////////////// 042//// DelayLine 043 044/** 045 This actor reads tokens from its input port, and for each token read 046 outputs an array that contains the current token as the first token, 047 followed by some number of previously read tokens. The length of the 048 output array is the same as that of the array in the <i>initialValues</i> 049 parameter. That parameter also provides the initial values when there 050 are no previously read tokens. 051 <p> 052 Note that this actor is not a simple sample delay. 053 Use the SampleDelay actor to achieve a simple sample delay. 054 055 @see ptolemy.domains.sdf.lib.SampleDelay 056 @author Steve Neuendorffer 057 @version $Id$ 058 @since Ptolemy II 1.0 059 @Pt.ProposedRating Yellow (yuhong) 060 @Pt.AcceptedRating Yellow (neuendor) 061 */ 062public class DelayLine extends SDFTransformer { 063 /** Construct an actor with the given container and name. 064 * @param container The container. 065 * @param name The name of this actor. 066 * @exception IllegalActionException If the actor cannot be contained 067 * by the proposed container. 068 * @exception NameDuplicationException If the container already has an 069 * actor with this name. 070 */ 071 public DelayLine(CompositeEntity container, String name) 072 throws NameDuplicationException, IllegalActionException { 073 super(container, name); 074 075 initialValues = new Parameter(this, "initialValues"); 076 initialValues.setExpression("{0, 0, 0, 0}"); 077 078 // set the output type to be an ArrayType, and input type to 079 // be the corresponding token type. 080 output.setTypeAtLeast(ArrayType.arrayOf(input)); 081 output.setTypeAtLeast(initialValues); 082 } 083 084 /////////////////////////////////////////////////////////////////// 085 //// ports and parameters //// 086 087 /** The initial values of the delay line. 088 * This parameter must contain an ArrayToken. 089 * The default value is an array that contains 4 integer tokens. 090 * Changes to this parameter after initialize() has been invoked 091 * are ignored until the next execution of the model. 092 */ 093 public Parameter initialValues; 094 095 /////////////////////////////////////////////////////////////////// 096 //// public methods //// 097 098 /** Override the base class to allow type changes on 099 * <i>initialValues</i>. 100 * @exception IllegalActionException If type changes are not 101 * allowed on the specified attribute. 102 */ 103 @Override 104 public void attributeTypeChanged(Attribute attribute) 105 throws IllegalActionException { 106 if (attribute != initialValues) { 107 super.attributeTypeChanged(attribute); 108 } else { 109 _typesValid = false; // Set flag to invalidate cached type constraints 110 } 111 } 112 113 /** Clone the actor into the specified workspace. This overrides the 114 * base class to handle type constraints. 115 * @param workspace The workspace for the new object. 116 * @return A new actor. 117 * @exception CloneNotSupportedException If a derived class contains 118 * an attribute that cannot be cloned. 119 */ 120 @Override 121 public Object clone(Workspace workspace) throws CloneNotSupportedException { 122 DelayLine newObject = (DelayLine) super.clone(workspace); 123 try { 124 newObject.output.setTypeAtLeast(ArrayType.arrayOf(newObject.input)); 125 } catch (IllegalActionException e) { 126 throw new InternalErrorException(e); 127 } 128 newObject.output.setTypeAtLeast(newObject.initialValues); 129 return newObject; 130 } 131 132 /** Consume a token from the input, push it onto the delay line 133 * and produce the output ArrayToken containing the current state of 134 * the delay line. 135 * @exception IllegalActionException If not enough tokens are available. 136 */ 137 @Override 138 public void fire() throws IllegalActionException { 139 super.fire(); 140 141 // Shift down. 142 System.arraycopy(_delayLine, 0, _delayLine, 1, _delayLine.length - 1); 143 144 // Read the next input. 145 _delayLine[0] = input.get(0); 146 147 // output the output token. 148 output.send(0, new ArrayToken(_delayLine)); 149 } 150 151 /** Initialize this actor by reading the value of <i>initialValues</i>. 152 */ 153 @Override 154 public void initialize() throws IllegalActionException { 155 super.initialize(); 156 _delayLine = ((ArrayToken) initialValues.getToken()).arrayValue(); 157 } 158 159 /////////////////////////////////////////////////////////////////// 160 //// private variables //// 161 162 /** The delay line. */ 163 private Token[] _delayLine = null; 164}