001/* A timed actor that outputs the simulated physical time .
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.lib;
029
030import ptolemy.actor.SuperdenseTimeDirector;
031import ptolemy.data.IntToken;
032import ptolemy.data.type.BaseType;
033import ptolemy.kernel.CompositeEntity;
034import ptolemy.kernel.util.IllegalActionException;
035import ptolemy.kernel.util.NameDuplicationException;
036
037///////////////////////////////////////////////////////////////////
038//// GetCurrentMicrostep
039
040/**
041Produce an output token on each firing with a value that is
042the current microstep, also called the current index,
043of superdense time. This can only be used with
044directors that implement SuperdenseTimeDirector.
045
046@author Jia Zou
047@version $Id$
048@since Ptolemy II 10.0
049@see SuperdenseTimeDirector
050@Pt.ProposedRating Yellow (jiazou)
051@Pt.AcceptedRating Red
052 */
053public class CurrentMicrostep extends TimedSource {
054    /** Construct an actor with the given container and name.
055     *
056     *  @param container The container.
057     *  @param name The name of this actor.
058     *  @exception IllegalActionException If the actor cannot be contained
059     *   by the proposed container.
060     *  @exception NameDuplicationException If the container already has an
061     *   actor with this name.
062     */
063    public CurrentMicrostep(CompositeEntity container, String name)
064            throws NameDuplicationException, IllegalActionException {
065        super(container, name);
066
067        // set the type constraints.
068        output.setTypeEquals(BaseType.INT);
069
070        // Override the clock to make it look a bit
071        // different from the DiscreteClock and PoissonClock.
072        _attachText("_iconDescription", "<svg>\n" + "<rect x=\"-20\" y=\"-20\" "
073                + "width=\"40\" height=\"40\" " + "style=\"fill:lightGrey\"/>\n"
074                + "<circle cx=\"0\" cy=\"0\" r=\"17\""
075                + "style=\"fill:black\"/>\n"
076                + "<line x1=\"0\" y1=\"-15\" x2=\"0\" y2=\"-13\" style=\"stroke:white\"/>\n"
077                + "<line x1=\"0\" y1=\"14\" x2=\"0\" y2=\"16\" style=\"stroke:white\"/>\n"
078                + "<line x1=\"-15\" y1=\"0\" x2=\"-13\" y2=\"0\" style=\"stroke:white\"/>\n"
079                + "<line x1=\"14\" y1=\"0\" x2=\"16\" y2=\"0\" style=\"stroke:white\"/>\n"
080                + "<line x1=\"0\" y1=\"-8\" x2=\"0\" y2=\"0\" style=\"stroke:white\"/>\n"
081                + "<line x1=\"0\" y1=\"0\" x2=\"11.26\" y2=\"-6.5\" style=\"stroke:white\"/>\n"
082                + "</svg>\n");
083    }
084
085    ///////////////////////////////////////////////////////////////////
086    ////                         public methods                    ////
087
088    /** Send the simulated physical time to the output, which is the
089     *  currentTime of the enclosing DE director.
090     *  @exception IllegalActionException If send() throws it.
091     */
092    @Override
093    public void fire() throws IllegalActionException {
094        if (!(getDirector() instanceof SuperdenseTimeDirector)) {
095            throw new IllegalActionException(this,
096                    "Actor can only work with directors that implement SuperdenseTimeDirector.");
097        }
098        SuperdenseTimeDirector director = (SuperdenseTimeDirector) getDirector();
099
100        output.send(0, new IntToken(director.getIndex()));
101
102        super.fire();
103    }
104
105}