001/* A lookup table that outputs internally stored data given an index Parameter.
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.ArrayToken;
031import ptolemy.data.IntToken;
032import ptolemy.data.expr.Parameter;
033import ptolemy.data.type.ArrayType;
034import ptolemy.data.type.BaseType;
035import ptolemy.kernel.CompositeEntity;
036import ptolemy.kernel.util.IllegalActionException;
037import ptolemy.kernel.util.InternalErrorException;
038import ptolemy.kernel.util.NameDuplicationException;
039import ptolemy.kernel.util.Workspace;
040
041///////////////////////////////////////////////////////////////////
042//// LookupTable
043
044/**
045 <p>Output to the <i>output</i> port the value in the array of tokens
046 specified by the <i>table</i> parameter at the index specified by the
047 <i>input</i> port.  The index must be an integer.  If the index is out
048 of range, no token produced.</p>
049
050 <p>LookupTable is different from ArrayElement in that in
051 ArrayElement, the array is read in as input, and the index is a parameter,
052 In LookupTable, the array is a parameter, and the index is read
053 in as an input.</p>
054
055 <p>Note that there are three similar actors here:</p>
056 <dl>
057 <dt>LookupTable</dt>
058 <dd>array is a parameter, index is a port.</dd>
059 <dt>ArrayElement</dt>
060 <dd>array is a port, index is a parameter</dd>
061 <dt>Array<i>XXX</i> (not yet developed)</dt>
062 <dd>array and index are both ports</dd>
063 </dl>
064
065 @see ArrayElement
066 @author Paul Whitaker, Christopher Hylands
067 @version $Id$
068 @since Ptolemy II 2.0
069 @Pt.ProposedRating Yellow (cxh)
070 @Pt.AcceptedRating Yellow (cxh)
071 */
072public class LookupTable extends Transformer {
073    /** Construct an actor with the given container and name.
074     *  @param container The container.
075     *  @param name The name of this actor.
076     *  @exception IllegalActionException If the actor cannot be contained
077     *   by the proposed container.
078     *  @exception NameDuplicationException If the container already has an
079     *   actor with this name.
080     */
081    public LookupTable(CompositeEntity container, String name)
082            throws NameDuplicationException, IllegalActionException {
083        super(container, name);
084
085        // Set parameters.
086        table = new Parameter(this, "table");
087        table.setExpression("{0, 1}");
088
089        // Set type constraints.
090        input.setTypeEquals(BaseType.INT);
091        output.setTypeAtLeast(ArrayType.elementType(table));
092    }
093
094    ///////////////////////////////////////////////////////////////////
095    ////                         parameters                        ////
096
097    /** The table array that we look up elements in.  This parameter
098     *  is an array with default value {0, 1}.
099     */
100    public Parameter table;
101
102    ///////////////////////////////////////////////////////////////////
103    ////                         public methods                    ////
104
105    /** Clone the actor into the specified workspace. This calls the
106     *  base class and then sets up the type constraints.
107     *  @param workspace The workspace for the new object.
108     *  @return A new actor.
109     *  @exception CloneNotSupportedException If a derived class contains
110     *   an attribute that cannot be cloned.
111     */
112    @Override
113    public Object clone(Workspace workspace) throws CloneNotSupportedException {
114        LookupTable newObject = (LookupTable) super.clone(workspace);
115
116        try {
117            newObject.output
118                    .setTypeAtLeast(ArrayType.elementType(newObject.table));
119        } catch (IllegalActionException e) {
120            // Should have been caught before this.
121            throw new InternalErrorException(e);
122        }
123        return newObject;
124    }
125
126    /** Consume at most one token from the input port and produce
127     *  the element at the index specified by this token from the
128     *  table array on the output port.  If there is no token
129     *  on the input or the token is out of range, then no output
130     *  is produced.
131     *  @exception IllegalActionException If there is no director.
132     */
133    @Override
134    public void fire() throws IllegalActionException {
135        super.fire();
136        if (input.hasToken(0)) {
137            ArrayToken token = (ArrayToken) table.getToken();
138
139            if (token != null) {
140                int indexValue = ((IntToken) input.get(0)).intValue();
141
142                if (indexValue >= 0 && indexValue < token.length()) {
143                    output.broadcast(token.getElement(indexValue));
144                }
145            }
146        }
147    }
148}