001/* Removes element occurrences from an array.
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 */
028
029package ptolemy.actor.lib;
030
031import ptolemy.actor.TypedAtomicActor;
032import ptolemy.actor.TypedIOPort;
033import ptolemy.actor.parameters.PortParameter;
034import ptolemy.data.ArrayToken;
035import ptolemy.data.BooleanToken;
036import ptolemy.data.Token;
037import ptolemy.data.expr.Parameter;
038import ptolemy.data.type.ArrayType;
039import ptolemy.kernel.CompositeEntity;
040import ptolemy.kernel.util.IllegalActionException;
041import ptolemy.kernel.util.NameDuplicationException;
042import ptolemy.kernel.util.Workspace;
043
044///////////////////////////////////////////////////////////////////
045//// ArrayRemoveElement
046/**
047 Remove occurrences of a specified element from an array.
048 This actor reads an array from the <i>array</i> input port and
049 an element from the <i>element</i> port-parameter and
050 removes all occurances that match the element from the array.
051 The output may be an empty array, in which case it will have
052 the same type as the input.
053
054 @author Efrat Jaeger and Edward A. Lee
055 @version $Id$
056 @since Ptolemy II 4.0.1
057 */
058
059public class ArrayRemoveElement extends TypedAtomicActor {
060
061    /** Construct an actor with the given container and name.
062     *  @param container The container.
063     *  @param name The name of this actor.
064     *  @exception IllegalActionException If the actor cannot be contained
065     *   by the proposed container.
066     *  @exception NameDuplicationException If the container already has an
067     *   actor with this name.
068     */
069    public ArrayRemoveElement(CompositeEntity container, String name)
070            throws NameDuplicationException, IllegalActionException {
071        super(container, name);
072
073        array = new TypedIOPort(this, "array", true, false);
074        output = new TypedIOPort(this, "output", false, true);
075
076        // Set parameters.
077        element = new PortParameter(this, "element");
078        new Parameter(element.getPort(), "_showName", BooleanToken.TRUE);
079
080        array.setTypeAtLeast(ArrayType.arrayOf(element));
081        output.setTypeAtLeast(array);
082    }
083
084    ///////////////////////////////////////////////////////////////////
085    ////                         parameters                        ////
086
087    /** Input array. The type of this port is at least an array
088     *  of the type of the <i>element</i> port.
089     */
090    public TypedIOPort array;
091
092    /** The resulting output array. Note that the output will
093     *  be a new array with the same type as the input array.
094     */
095    public TypedIOPort output;
096
097    /** The element to be removed.
098     */
099    public PortParameter element;
100
101    ///////////////////////////////////////////////////////////////////
102    ////                         public methods                    ////
103
104    /** Override the base class to set type constraints.
105     *  @param workspace The workspace for the cloned object.
106     *  @exception CloneNotSupportedException If cloned ports cannot have
107     *   as their container the cloned entity (this should not occur), or
108     *   if one of the attributes cannot be cloned.
109     *  @return A new ComponentEntity.
110     */
111    @Override
112    public Object clone(Workspace workspace) throws CloneNotSupportedException {
113        ArrayRemoveElement newObject = (ArrayRemoveElement) super.clone(
114                workspace);
115        try {
116            newObject.array
117                    .setTypeAtLeast(ArrayType.arrayOf(newObject.element));
118            newObject.output.setTypeAtLeast(newObject.array);
119        } catch (IllegalActionException e) {
120            throw new CloneNotSupportedException("Clone failed: " + e);
121        }
122        return newObject;
123    }
124
125    /** If there is an <i>array</i> input, consume it and create a new
126     *  array that contains all elements of the input that are not equal
127     *  to the value given by the <i>element</i> port-parameter.
128     */
129    @Override
130    public void fire() throws IllegalActionException {
131        super.fire();
132        // NOTE: This has be outside the if because we need to ensure
133        // that if an element token is provided that it is consumed even
134        // if there is no input token.
135        element.update();
136        Token elementToken = element.getToken();
137        if (array.hasToken(0)) {
138            ArrayToken inputArray = (ArrayToken) array.get(0);
139            Token[] outputElements = new Token[inputArray.length()];
140            int outputSize = 0;
141            for (int i = 0; i < inputArray.length(); i++) {
142                Token inputElement = inputArray.getElement(i);
143                if (!elementToken.equals(inputElement)) {
144                    outputElements[outputSize] = inputElement;
145                    outputSize++;
146                }
147            }
148            Token result = null;
149            if (outputSize > 0) {
150                result = new ArrayToken(outputElements, outputSize);
151            } else {
152                result = new ArrayToken(inputArray.getElementType());
153            }
154            output.broadcast(result);
155        }
156    }
157
158    /** Clear port parameter value.
159     */
160    @Override
161    public void wrapup() throws IllegalActionException {
162        super.wrapup();
163        element.setExpression("");
164    }
165}