001/* An ExecutionListener that copies events to a given stream.
002
003 Copyright (c) 1998-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;
029
030import java.io.OutputStream;
031import java.io.PrintStream;
032
033///////////////////////////////////////////////////////////////////
034//// StreamExecutionListener
035
036/**
037 A default implementation of the ExecutionListener interface.
038 This implementation prints information about each event to a stream.
039
040 @author Steve Neuendorffer, Lukito Muliadi, Edward A. Lee
041 @version $Id$
042 @since Ptolemy II 0.4
043 @Pt.ProposedRating Green (eal)
044 @Pt.AcceptedRating Green (bart)
045 */
046public class StreamExecutionListener implements ExecutionListener {
047    /** Create a listener that sends messages to the standard output.
048     */
049    public StreamExecutionListener() {
050        _output = System.out;
051    }
052
053    /** Create a listener that sends messages to the given output stream.
054     *  @param out The output stream to send the messages to.
055     */
056    public StreamExecutionListener(OutputStream out) {
057        _output = new PrintStream(out);
058    }
059
060    ///////////////////////////////////////////////////////////////////
061    ////                         public methods                    ////
062
063    /** Report an execution failure by printing a message to output
064     *  stream specified to the constructor.
065     */
066    @Override
067    public void executionError(Manager manager, Throwable throwable) {
068        _output.println("Execution error.");
069        throwable.printStackTrace(_output);
070    }
071
072    /** Report that the current execution finished  by printing a
073     *  message to output stream specified to the constructor.
074     *  @param manager The manager controlling the execution.
075     */
076    @Override
077    public void executionFinished(Manager manager) {
078        _output.println("Completed execution with "
079                + manager.getIterationCount() + " iterations");
080    }
081
082    /** Report that the manager has changed state by printing a
083     *  message to output stream specified to the constructor.
084     *  @param manager The manager controlling the execution.
085     */
086    @Override
087    public void managerStateChanged(Manager manager) {
088        Manager.State state = manager.getState();
089        String message;
090
091        if (state == Manager.ITERATING) {
092            message = state.getDescription() + " number "
093                    + manager.getIterationCount();
094        } else {
095            message = state.getDescription();
096        }
097
098        _output.println(message);
099    }
100
101    ///////////////////////////////////////////////////////////////////
102    ////                         private variables                 ////
103    private PrintStream _output;
104}