001/* A simple sink actor that consumes and discards input tokens and
002 then calls System.exit() in wrapup.
003
004 Copyright (c) 2004-2014 The Regents of the University of California.
005 All rights reserved.
006 Permission is hereby granted, without written agreement and without
007 license or royalty fees, to use, copy, modify, and distribute this
008 software and its documentation for any purpose, provided that the above
009 copyright notice and the following two paragraphs appear in all copies
010 of this software.
011
012 IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY
013 FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
014 ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF
015 THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF
016 SUCH DAMAGE.
017
018 THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
019 INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
020 MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE
021 PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF
022 CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES,
023 ENHANCEMENTS, OR MODIFICATIONS.
024
025 PT_COPYRIGHT_VERSION_2
026 COPYRIGHTENDKEY
027
028 */
029package ptolemy.actor.lib;
030
031import ptolemy.kernel.CompositeEntity;
032import ptolemy.kernel.util.IllegalActionException;
033import ptolemy.kernel.util.NameDuplicationException;
034
035///////////////////////////////////////////////////////////////////
036//// Exit
037
038/**
039 A simple sink actor that consumes and discards input tokens and
040 then calls System.exit() in wrapup.
041
042 @see Stop
043 @author Edward A. Lee
044 @version $Id$
045 @since Ptolemy II 4.0
046 @Pt.ProposedRating Red (cxh)
047 @Pt.AcceptedRating Red (cxh)
048 */
049public class Exit extends Sink {
050    /** Construct an actor with an input multiport.
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 Exit(CompositeEntity container, String name)
059            throws NameDuplicationException, IllegalActionException {
060        super(container, name);
061    }
062
063    ///////////////////////////////////////////////////////////////////
064    ////                         public methods                    ////
065
066    /** Consume and discard at most one token from each input channel.
067     *  @exception IllegalActionException If accessing the input throws it.
068     */
069    @Override
070    public void fire() throws IllegalActionException {
071        super.fire();
072        for (int i = 0; i < input.getWidth(); i++) {
073            if (input.hasToken(i)) {
074                input.get(i);
075            }
076        }
077    }
078
079    /** Exit the Java process by calling System.exit() after all the
080     *  wrapup() methods have been called.
081     *  If the ptolemy.ptII.exitAfterWrapup property is <b>not</b>
082     *  set, then when wrapup() is almost finished, we call System.exit()
083     *  after all the wrapup() methods have been called.
084     *  If the ptolemy.ptII.exitAfterWrapup property is set, then
085     *  we throw an Exception.
086     */
087    @Override
088    public void wrapup() throws IllegalActionException {
089        super.wrapup();
090        getManager().exitAfterWrapup();
091    }
092}