001/* An actor that throws an exception when it receives a true token.
002
003 Copyright (c) 1997-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 */
028package ptolemy.actor.lib;
029
030import ptolemy.data.BooleanToken;
031import ptolemy.data.expr.Parameter;
032import ptolemy.data.type.BaseType;
033import ptolemy.kernel.CompositeEntity;
034import ptolemy.kernel.util.IllegalActionException;
035import ptolemy.kernel.util.NameDuplicationException;
036import ptolemy.kernel.util.StringAttribute;
037
038///////////////////////////////////////////////////////////////////
039//// ThrowException
040
041/**
042 An actor that throws an IllegalActionException when it receives a true token
043 on any input channel.  The message reported in the exception is
044 given by the <i>message</i> parameter.
045 By default, inputs are read and checked in the fire() method, but if
046 <i>throwInPostfire</i> is set to true, then they will be checked in postfire() only.
047
048 @author Edward A. Lee
049 @version $Id$
050 @since Ptolemy II 2.1
051 @Pt.ProposedRating Green (eal)
052 @Pt.AcceptedRating Green (neuendor)
053 @see ThrowModelError
054 */
055public class ThrowException extends Sink {
056    /** Construct an actor in the specified container with the specified
057     *  name.
058     *  @param container The container.
059     *  @param name The name of this actor within the container.
060     *  @exception IllegalActionException If the actor cannot be contained
061     *   by the proposed container.
062     *  @exception NameDuplicationException If the name coincides with
063     *   an actor already in the container.
064     */
065    public ThrowException(CompositeEntity container, String name)
066            throws IllegalActionException, NameDuplicationException {
067        super(container, name);
068
069        input.setTypeEquals(BaseType.BOOLEAN);
070
071        message = new StringAttribute(this, "message");
072        message.setExpression("Model triggered an exception.");
073
074        throwInPostfire = new Parameter(this, "throwInPostfire");
075        throwInPostfire.setTypeEquals(BaseType.BOOLEAN);
076        throwInPostfire.setExpression("false");
077    }
078
079    ///////////////////////////////////////////////////////////////////
080    ////                     ports and parameters                  ////
081
082    /** The message reported in the exception, which is a string that
083     *  defaults to "Model triggered an exception".
084     */
085    public StringAttribute message;
086
087    /** True to throw the model error in the postfire method.
088     *  False to throw in fire. This is a boolean that defaults
089     *  to false.
090     */
091    public Parameter throwInPostfire;
092
093    ///////////////////////////////////////////////////////////////////
094    ////                         public methods                    ////
095
096    /** Read one token from each input channel that has a token,
097     *  and if any token is true, invoke the model error handler.
098     *  @exception IllegalActionException If thrown by the parent class.
099     */
100    @Override
101    public void fire() throws IllegalActionException {
102        super.fire();
103        if (!((BooleanToken) throwInPostfire.getToken()).booleanValue()) {
104            boolean result = false;
105
106            // NOTE: We need to consume data on all channels that have data.
107            // If we don't then DE will go into an infinite loop.
108            for (int i = 0; i < input.getWidth(); i++) {
109                if (input.hasToken(i)) {
110                    if (((BooleanToken) input.get(i)).booleanValue()) {
111                        result = true;
112                    }
113                }
114            }
115
116            if (result) {
117                throw new IllegalActionException(this, message.getExpression());
118            }
119        }
120    }
121
122    /** Read one token from each input channel that has a token,
123     *  and if any token is true, throw an exception.
124     *  @exception IllegalActionException If FIXME
125     *  @return Whatever the base class returns (probably true).
126     */
127    @Override
128    public boolean postfire() throws IllegalActionException {
129        if (((BooleanToken) throwInPostfire.getToken()).booleanValue()) {
130            boolean result = false;
131
132            // NOTE: We need to consume data on all channels that have data.
133            // If we don't then DE will go into an infinite loop.
134            for (int i = 0; i < input.getWidth(); i++) {
135                if (input.hasToken(i)) {
136                    if (((BooleanToken) input.get(i)).booleanValue()) {
137                        result = true;
138                    }
139                }
140            }
141
142            if (result) {
143                throw new IllegalActionException(this, message.getExpression());
144            }
145        }
146        return super.postfire();
147    }
148}