001/* An actor that must have preinitialize() invoked each iteration.
002
003 Copyright (c) 2016-2018 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.test;
028
029import ptolemy.actor.lib.Ramp;
030import ptolemy.kernel.CompositeEntity;
031import ptolemy.kernel.util.IllegalActionException;
032import ptolemy.kernel.util.NameDuplicationException;
033
034/**
035 * Preinitialize() must be invoked after wrapup.
036 *
037 * <p>Test for problems similar to what we see in Matlab Engine, where wrapup()
038 * closes the connection.  If this actor is used in a RunComposite, preinitialize()
039 * must be called before the other action methods.</p>
040 *
041 * <p>See ptolemy/matlab/test/MatlabRunComposite.xml.</p>
042 *
043 * @author Christopher Brooks
044 * @version $Id$
045 * @since Ptolemy II 11.0
046 * @Pt.ProposedRating Red (cxh)
047 * @Pt.AcceptedRating Red (cxh)
048 */
049public class PreinitializeMustBeInvoked extends Ramp {
050    /** Construct an actor with the given container and name.
051     *  @param container The container.
052     *  @param name The name of this actor.
053     *  @exception IllegalActionException If the actor cannot be contained
054     *   by the proposed container.
055     *  @exception NameDuplicationException If the container already has an
056     *   actor with this name.
057     */
058    public PreinitializeMustBeInvoked(CompositeEntity container, String name)
059            throws NameDuplicationException, IllegalActionException {
060        super(container, name);
061    }
062
063    ///////////////////////////////////////////////////////////////////
064    ////                         public methods                    ////
065
066    @Override
067    public void fire() throws IllegalActionException {
068        super.fire();
069        if (!_preinitializedCalled) {
070            throw new IllegalActionException("fire()" + _ERROR_MESSAGE);
071        }
072    }
073
074    @Override
075    public void initialize() throws IllegalActionException {
076        super.initialize();
077        if (!_preinitializedCalled) {
078            throw new IllegalActionException("initialize()" + _ERROR_MESSAGE);
079        }
080    }
081
082    @Override
083    public boolean prefire() throws IllegalActionException {
084        if (!_preinitializedCalled) {
085            throw new IllegalActionException("prefire()" + _ERROR_MESSAGE);
086        }
087        return super.prefire();
088    }
089
090    @Override
091    public void preinitialize() throws IllegalActionException {
092        super.preinitialize();
093        _preinitializedCalled = true;
094    }
095
096    @Override
097    public void wrapup() throws IllegalActionException {
098        super.wrapup();
099        _preinitializedCalled = false;
100    }
101
102    private String _ERROR_MESSAGE = " was called after wrapup() was called but preinitialize() was not called after wrapup().";
103
104    /** True if preinitialize() has been called and wraup() has not
105     * yet been called.
106     */
107    private boolean _preinitializedCalled = false;
108}