001/* An actor that apply dynamically defined functions to its input.
002
003 Copyright (c) 2003-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 */
027package ptolemy.actor.lib.hoc;
028
029import ptolemy.actor.TypedAtomicActor;
030import ptolemy.actor.TypedIOPort;
031import ptolemy.data.FunctionToken;
032import ptolemy.data.Token;
033import ptolemy.kernel.CompositeEntity;
034import ptolemy.kernel.util.IllegalActionException;
035import ptolemy.kernel.util.NameDuplicationException;
036import ptolemy.kernel.util.Workspace;
037
038///////////////////////////////////////////////////////////////////
039//// MobileFunction
040
041/**
042 This actor extends the TypedAtomicActor. It applies a function to its inputs
043 and outputs the results. But rather than has the function specified
044 statically, this actor allows dynamic change to the function, which means
045 the computation of this actor can be changed during executing. Its second
046 input accept a function token for the new function's definition. The
047 function token can be given by actors in the local model or remote actors.
048
049 Currently, it only accept functions that has one argument. The return type
050 of the function needs to be less than the output type of this actor.
051
052 @author Yang Zhao
053 @deprecated Use ApplyFunction.
054 @version $Id$
055 @since Ptolemy II 4.0
056 @Pt.ProposedRating Red (eal)
057 @Pt.AcceptedRating Red (reviewmoderator)
058 */
059@Deprecated
060public class MobileFunction extends TypedAtomicActor {
061    /** Construct a MobileFunction in the specified workspace with
062     *  no container and an empty string as a name. You can then change
063     *  the name with setName(). If the workspace argument is null, then
064     *  use the default workspace.
065     *  @param workspace The workspace that will list the actor.
066     *  @exception IllegalActionException If the entity 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 MobileFunction(Workspace workspace)
072            throws IllegalActionException, NameDuplicationException {
073        super(workspace);
074        input = new TypedIOPort(this, "input", true, false);
075        function = new TypedIOPort(this, "function", true, false);
076
077        //function.setTypeAtMost(new FunctionType
078        //        (new Type[]{BaseType.UNKNOWN}, BaseType.GENERAL));
079        output = new TypedIOPort(this, "output", false, true);
080    }
081
082    /** Construct a MobileFunction with a name and a container.
083     *  The container argument must not be null, or a
084     *  NullPointerException will be thrown.
085     *  @param container The container.
086     *  @param name The name of this actor.
087     *  @exception IllegalActionException If the container is incompatible
088     *   with this actor.
089     *  @exception NameDuplicationException If the name coincides with
090     *   an actor already in the container.
091     */
092    public MobileFunction(CompositeEntity container, String name)
093            throws IllegalActionException, NameDuplicationException {
094        super(container, name);
095        input = new TypedIOPort(this, "input", true, false);
096        function = new TypedIOPort(this, "function", true, false);
097
098        //function.setTypeAtMost(new FunctionType
099        //        (new Type[]{BaseType.UNKNOWN}, BaseType.GENERAL));
100        output = new TypedIOPort(this, "output", false, true);
101    }
102
103    ///////////////////////////////////////////////////////////////////
104    ////                         public variables                  ////
105
106    /** The input port for incoming data.  The type of this port is
107     *  undeclared, meaning that it will resolve to any data type.
108     */
109    public TypedIOPort input;
110
111    /** The input port for function definition. The type of this port is
112     *  undeclared, but to have this actor work, the designer has to provide
113     *  a matched function token for it.
114     *  Note: The reason that we don't declare the type for it is because
115     *  currently there is not cast supported in the FunctionType class.
116     *  we'll fix this later.
117     */
118    public TypedIOPort function;
119
120    /** The output port.
121     *  Note: Due to the same reason above, the type of the output can't be
122     *  resolved currently. User has to specify the type.
123     */
124    public TypedIOPort output;
125
126    ///////////////////////////////////////////////////////////////////
127    ////                         public methods                    ////
128
129    /** If the function is not specified, then perform identity function;
130     *  otherwise, apply the specified function to its input and output
131     *  the result.
132     *  @exception IllegalActionException If there is no director, or if
133     *   the director's fire() method throws it, or if the actor is not
134     *   opaque.
135     */
136    @Override
137    public void fire() throws IllegalActionException {
138        super.fire();
139        if (_debugging) {
140            _debug("Invoking fire");
141        }
142
143        if (function.hasToken(0)) {
144            _function = (FunctionToken) function.get(0);
145        }
146
147        if (input.hasToken(0)) {
148            if (_function == null) {
149                output.broadcast(input.get(0));
150            } else {
151                // FIXME: it now only considers one input port for data and the
152                // function only has one argument.  how to resolve type and type
153                // signature?
154                Token in = input.get(0);
155                Token[] argList = new Token[] { in };
156                Token t = _function.apply(argList);
157                output.broadcast(t);
158            }
159        }
160    }
161
162    /** Initialize this actor.
163     *  @exception IllegalActionException If the superclass throws it.
164     */
165    @Override
166    public void initialize() throws IllegalActionException {
167        _function = null;
168        super.initialize();
169    }
170
171    /** Return true if the actor either of its input port has token.
172     *  @exception IllegalActionException Not thrown in this base class.
173     */
174    @Override
175    public boolean prefire() throws IllegalActionException {
176        if (!super.prefire()) {
177            return false;
178        }
179        if (_debugging) {
180            _debug("Invoking prefire");
181        }
182
183        if (input.hasToken(0) || function.hasToken(0)) {
184            return true;
185        }
186
187        return false;
188    }
189
190    ///////////////////////////////////////////////////////////////////
191    ////                         private variables                 ////
192
193    /** The most recently updated function to this actor.
194     *
195     */
196    private FunctionToken _function;
197}