001/* Interface for defining how an object can be invoked.
002
003 Copyright (c) 1997-2013 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;
028
029import ptolemy.kernel.util.IllegalActionException;
030
031///////////////////////////////////////////////////////////////////
032//// Executable
033
034/**
035 This interface defines the <i>action methods</i>, which determine
036 how an object can be invoked. It should be implemented by actors
037 and directors. In an execution of an application,
038 the preinitialize() and initialize() methods should be
039 invoked exactly once, followed by any number of iterations, followed
040 by exactly one invocation of the wrapup() method. An <i>iteration</i>
041 is defined to be any number of invocations of prefire() and fire(),
042 where fire() is invoked only if prefire() returns true, followed by
043 exactly one invocation of the postfire() method.
044 The postfire() method returns false if no further iterations
045 should occur. The initialize(), fire() and postfire() methods may produce
046 output data.  The preinitialize() method runs
047 before type resolution has been done, and is permitted to make
048 changes in the topology of the model.
049
050 @author Mudit Goel, Edward A. Lee, Lukito Muliadi, Steve Neuendorffer
051 @version $Id$
052 @since Ptolemy II 0.2
053 @Pt.ProposedRating Green (eal)
054 @Pt.AcceptedRating Green (davisj)
055 */
056public interface Executable extends Initializable {
057
058    ///////////////////////////////////////////////////////////////////
059    ////                         public methods                    ////
060
061    /** Fire the actor.  This may be invoked several times between
062     *  invocations of prefire() and postfire(). Output data may
063     *  (and normally will) be produced.
064     *  Typically, the fire() method performs the computation associated
065     *  with an actor. This method is not required to have bounded
066     *  execution.  However, after endFire() is called, this method should
067     *  return in bounded time.
068     *
069     *  @exception IllegalActionException If firing is not permitted.
070     */
071    public void fire() throws IllegalActionException;
072
073    /** Return true if this executable does not change state in either
074     *  the prefire() or the fire() method. A class that returns true
075     *  is said to obey the <i>actor abstract semantics</i>. In particular,
076     *  such an actor can be used in domains that have a fixed point
077     *  semantics and may repeatedly fire the actor before committing
078     *  to state changes.
079     *
080     *  @return True if this executable only updates its states during
081     *   an iteration in the postfire() method.
082     */
083    public boolean isFireFunctional();
084
085    /** Return true if this executable is strict, meaning all inputs must
086     *  be known before iteration. Normally, classes that implement this
087     *  interface are strict, so this method will return true.
088     *  However, some classes can perform an iteration even if some
089     *  inputs are not known (i.e., these classes tolerate a return value
090     *  of false from the isKnown() method of Receiver).
091     *
092     *  @return True if this executable is strict, meaning all inputs must
093     *   be known before iteration.
094     * @exception IllegalActionException Thrown by subclass.
095     */
096    public boolean isStrict() throws IllegalActionException;
097
098    /** Invoke a specified number of iterations of the actor. An
099     *  iteration here is equivalent to invoking prefire(), fire(), and
100     *  postfire(), in that order. In an iteration, if prefire()
101     *  returns true, then fire() will be called once, followed by
102     *  postfire(). Otherwise, if prefire() returns false, fire()
103     *  and postfire() are not invoked, and this method returns
104     *  NOT_READY. If postfire() returns false, then no more
105     *  iterations are invoked, and this method returns STOP_ITERATING.
106     *  Otherwise, it returns COMPLETED.
107     *  <p>
108     *  An implementation of this method is not required to
109     *  actually invoke prefire(), fire(), and postfire(). An
110     *  implementation of this method must, however,
111     *  perform the equivalent operations.
112     *  <p>
113     *  Note that this method for iterating an actor should
114     *  be used only in domains where a single invocation of
115     *  prefire() and fire() is sufficient in an iteration.
116     *
117     *  @param count The number of iterations to perform.
118     *  @return NOT_READY, STOP_ITERATING, or COMPLETED.
119     *  @exception IllegalActionException If iterating is not
120     *   permitted, or if prefire(), fire(), or postfire() throw it.
121     */
122    public int iterate(int count) throws IllegalActionException;
123
124    /** This method should be invoked once per iteration, after the last
125     *  invocation of fire() in that iteration. The postfire() method should
126     *  not produce output data on output ports of the actor.
127     *  It returns true if the execution can proceed into the next iteration,
128     *  false if the actor does not wish to be fired again.
129     *  This method typically wraps up an iteration, which may involve
130     *  updating local state or updating displays.
131     *
132     *  @return True if the execution can continue.
133     *  @exception IllegalActionException If postfiring is not permitted.
134     */
135    public boolean postfire() throws IllegalActionException;
136
137    /** This method should be invoked prior to each invocation of fire().
138     *  It returns true if the fire() method can be invoked, given the
139     *  current status of the inputs and parameters of the actor. Thus
140     *  this method will typically check preconditions for a firing, if
141     *  there are any. In an opaque, non-atomic entity,
142     *  it may move data into an inner subsystem.
143     *
144     *  @return True if the iteration can proceed.
145     *  @exception IllegalActionException If prefiring is not permitted.
146     */
147    public boolean prefire() throws IllegalActionException;
148
149    /** Request that execution of this Executable stop as soon
150     *  as possible.  An implementation of this method should
151     *  pass on the request to any contained executable objects.
152     *  An implementation should also return false from postfire()
153     *  to indicate to the caller that no further execution of
154     *  this Executable is appropriate.  After this method is
155     *  called, the executable object should not be fired again.
156     *  The stopFire() method, by contrast, requests that the
157     *  current iteration be completed, but not that the entire
158     *  execution be stopped.  I.e., the Executable may be fired
159     *  again after stopFire() is called.
160     */
161    public void stop();
162
163    /** Request that execution of the current iteration complete.  If
164     *  an iteration is always a finite computation (the usual case),
165     *  i.e.  the fire() method always returns in finite time, then
166     *  nothing needs to be done in this method, except possibly to
167     *  pass on the request to any contained executable objects.  This
168     *  method is used to request that an unbounded computation
169     *  suspend, returning control to the caller.  Thus, if the fire()
170     *  method does not normally return in finite time, then this
171     *  method is used to request that it return.  It should suspend
172     *  execution in such a way that if the fire() method is called
173     *  again, execution will resume at the point where it was
174     *  suspended.  However, it should not assume the fire() method
175     *  will be called again.  It is possible that the wrapup() method
176     *  will be called next.
177     */
178    public void stopFire();
179
180    /** Terminate any currently executing model with extreme prejudice.
181     *  This method is not intended to be used as a normal route of
182     *  stopping execution. To normally stop execution, call the finish()
183     *  method instead. This method should be called only
184     *  when execution fails to terminate by normal means due to certain
185     *  kinds of programming errors (infinite loops, threading errors, etc.).
186     *  <p>
187     *  After this method completes, all resources in use should be
188     *  released and any sub-threads should be killed.
189     *  However, a consistent state is not guaranteed.   The
190     *  topology should probably be recreated before attempting any
191     *  further operations.
192     *  This method should not be synchronized because it must
193     *  happen as soon as possible, no matter what.
194     */
195    public void terminate();
196
197    ///////////////////////////////////////////////////////////////////
198    ////                         public members                    ////
199
200    /** An indicator that the iterate() method completed successfully. */
201    public static final int COMPLETED = 0;
202
203    /** An indicator that the iterate() method did not complete because
204     *  the actor was not ready (prefire() returned false).
205     */
206    public static final int NOT_READY = 1;
207
208    /** An indicator that the actor does not wish to be fired again.
209     */
210    public static final int STOP_ITERATING = 2;
211}