001/* Plot input data as a function of elapsed real time.
002
003 @Copyright (c) 1998-2014 The Regents of the University of California.
004 All rights reserved.
005
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
009 above copyright notice and the following two paragraphs appear in all
010 copies 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 */
028package ptolemy.actor.lib.gui;
029
030import ptolemy.actor.TypedIOPort;
031import ptolemy.data.DoubleToken;
032import ptolemy.data.IntToken;
033import ptolemy.data.type.BaseType;
034import ptolemy.kernel.CompositeEntity;
035import ptolemy.kernel.util.IllegalActionException;
036import ptolemy.kernel.util.NameDuplicationException;
037import ptolemy.plot.Plot;
038
039///////////////////////////////////////////////////////////////////
040//// RealTimePlotter
041
042/**
043 This plotter plots input data as a function of elapsed real time in seconds.
044 Elapsed time is set to zero when the initialize() method is invoked.
045 The resolution of time depends on the implementation of the Java
046 virtual machine, but with Sun's JDK 1.3 under Windows 2000, it is
047 10 milliseconds.
048
049 <p>This plotter contains an instance of the Plot class from the
050 Ptolemy plot package as a public member.
051
052 @author  Edward A. Lee
053 @version $Id$
054 @since Ptolemy II 2.0
055 @Pt.ProposedRating Red (eal)
056 @Pt.AcceptedRating Red (cxh)
057 */
058public class RealTimePlotter extends Plotter {
059    /** Construct an actor with the given container and name.
060     *  @param container The container.
061     *  @param name The name of this actor.
062     *  @exception IllegalActionException If the actor cannot be contained
063     *   by the proposed container.
064     *  @exception NameDuplicationException If the container already has an
065     *   actor with this name.
066     */
067    public RealTimePlotter(CompositeEntity container, String name)
068            throws IllegalActionException, NameDuplicationException {
069        super(container, name);
070
071        // Create the input port and make it a multiport.
072        input = new TypedIOPort(this, "input", true, false);
073        input.setMultiport(true);
074        input.setTypeEquals(BaseType.DOUBLE);
075    }
076
077    ///////////////////////////////////////////////////////////////////
078    ////                     ports and parameters                  ////
079
080    /** Input port, which has type DoubleToken. */
081    public TypedIOPort input;
082
083    ///////////////////////////////////////////////////////////////////
084    ////                         public methods                    ////
085
086    /** Record the start time.
087     *  @exception IllegalActionException If the base class throws it.
088     */
089    @Override
090    public void initialize() throws IllegalActionException {
091        super.initialize();
092        _startTime = System.currentTimeMillis();
093    }
094
095    /** Read at most one input from each channel and plot it as a
096     *  function of time.
097     *  This is done in postfire to ensure that data has settled.
098     *  @exception IllegalActionException If there is no director, or
099     *   if the base class throws it.
100     *  @return True if it is OK to continue.
101     */
102    @Override
103    public boolean postfire() throws IllegalActionException {
104        long elapsedTime = System.currentTimeMillis() - _startTime;
105        double currentTime = elapsedTime / 1000.0;
106        int width = input.getWidth();
107        int offset = ((IntToken) startingDataset.getToken()).intValue();
108
109        for (int i = width - 1; i >= 0; i--) {
110            if (input.hasToken(i)) {
111                DoubleToken currentToken = (DoubleToken) input.get(i);
112                double currentValue = currentToken.doubleValue();
113
114                // NOTE: We assume the superclass ensures this cast is safe.
115                ((Plot) plot).addPoint(i + offset, currentTime, currentValue,
116                        true);
117            }
118        }
119
120        return super.postfire();
121    }
122
123    ///////////////////////////////////////////////////////////////////
124    ////                         private variables                 ////
125
126    /** The start time. */
127    private long _startTime;
128}