001/* A timed actor that outputs the current 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.data.DateToken;
031import ptolemy.data.type.BaseType;
032import ptolemy.kernel.CompositeEntity;
033import ptolemy.kernel.util.IllegalActionException;
034import ptolemy.kernel.util.NameDuplicationException;
035
036///////////////////////////////////////////////////////////////////
037//// CurrentTime
038
039/**
040 Produce an output token on each firing with a value that is
041 the current date.
042
043 @author Patricia Derler
044 @version $Id$
045 @since Ptolemy II 10.0
046 @Pt.ProposedRating Red (pd)
047 @Pt.AcceptedRating Red (pd)
048 */
049public class CurrentDate extends TimedSource {
050
051    /** Construct an actor with the given container and name.
052     *
053     *  @param container The container.
054     *  @param name The name of this actor.
055     *  @exception IllegalActionException If the actor cannot be contained
056     *   by the proposed container.
057     *  @exception NameDuplicationException If the container already has an
058     *   actor with this name.
059     */
060    public CurrentDate(CompositeEntity container, String name)
061            throws NameDuplicationException, IllegalActionException {
062        super(container, name);
063
064        // set the type constraints.
065        output.setTypeEquals(BaseType.DATE);
066    }
067
068    ///////////////////////////////////////////////////////////////////
069    ////                         public methods                    ////
070
071    /** Send the current time to the output.  If there are trigger inputs,
072     *  then the current time is the minimum of the times of each of the
073     *  input tokens (currently, these can be different only in the DT
074     *  domain).  Otherwise, current time is that reported by the director.
075     *  @exception IllegalActionException If send() throws it.
076     */
077    @Override
078    public void fire() throws IllegalActionException {
079
080        if (trigger.isOutsideConnected()) {
081            // Trigger port is connected.
082            // If there is a token in a channel of the trigger port,
083            // output the current time (that is associated with the token).
084            for (int i = 0; i < trigger.getWidth(); i++) {
085                if (trigger.hasToken(i)) {
086                    // Do not consume the token... It will be consumed
087                    // in the superclass fire().
088                    // trigger.get(i);
089                    output.send(0, new DateToken());
090                }
091            }
092        } else {
093            output.send(0, new DateToken());
094        }
095
096        super.fire();
097    }
098}