001/* A timed actor that outputs a const value at a given date.
002
003 Copyright (c) 1998-2015 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 */
028
029package ptolemy.domains.de.lib;
030
031import ptolemy.actor.CompositeActor;
032import ptolemy.actor.Director;
033import ptolemy.actor.Manager;
034import ptolemy.actor.lib.Transformer;
035import ptolemy.actor.util.Time;
036import ptolemy.data.BooleanToken;
037import ptolemy.data.DateToken;
038import ptolemy.data.DoubleToken;
039import ptolemy.data.type.BaseType;
040import ptolemy.domains.de.kernel.DEDirector;
041import ptolemy.kernel.CompositeEntity;
042import ptolemy.kernel.util.IllegalActionException;
043import ptolemy.kernel.util.NameDuplicationException;
044
045/** A timed actor that outputs the local clock value that corresponds to the date.
046 *  Such a correspondence is only given in models that synchronize to real time.
047 *  In such models, the real time (date) when the model starts is recorded. An input
048 *  date to this actor is compared to the model start date. The difference between
049 *  those dates (in millisecond resolution) is multiplied by the time resolution of
050 *  the local clock and then send to the output.
051 *
052 *  Currently, this actor only works in the DE domain.
053 * @author Patricia Derler
054@version $Id$
055@since Ptolemy II 10.0
056 * @version $Id$
057 * @Pt.ProposedRating Red (cxh)
058 * @Pt.AcceptedRating Red (cxh)
059 */
060public class DateToModelTime extends Transformer {
061
062    /** Create a new actor in the specified container with the specified
063     *  name.  The name must be unique within the container or an exception
064     *  is thrown. The container argument must not be null, or a
065     *  NullPointerException will be thrown.
066     *
067     *  @param container The container.
068     *  @param name The name of this actor within the container.
069     *  @exception IllegalActionException If this actor cannot be contained
070     *   by the proposed container (see the setContainer() method).
071     *  @exception NameDuplicationException If the name coincides with
072     *   an entity already in the container.
073     */
074    public DateToModelTime(CompositeEntity container, String name)
075            throws IllegalActionException, NameDuplicationException {
076        super(container, name);
077        input.setTypeEquals(BaseType.DATE);
078        output.setTypeEquals(BaseType.DOUBLE);
079    }
080
081    /** Check weather enclosing director is a DEDirector with
082     *  synchronizeToRealTime is enabled.
083     *  @exception IllegalActionException Thrown if the enclosing director is not a
084     *  DEDirector or the synchronizeToRealTime property is false.
085     */
086    @Override
087    public void initialize() throws IllegalActionException {
088        super.initialize();
089        Director director = getDirector();
090        if (director instanceof DEDirector) {
091            if (!((BooleanToken) ((DEDirector) director).synchronizeToRealTime
092                    .getToken()).booleanValue()) {
093                throw new IllegalActionException(this,
094                        "This actor can only be used when synchronizeToRealTime "
095                                + "in the director is enabled because a reference to real time is needed to compare "
096                                + "dates.");
097            }
098            _director = (DEDirector) director;
099        }
100    }
101
102    /** Read DateToken on input and output corresponding model time value.
103     *  @exception IllegalActionException If thrown in the parent class.
104     */
105    @Override
106    public void fire() throws IllegalActionException {
107        super.fire();
108        for (int channel = 0; channel < input.getWidth(); channel++) {
109            if (input.hasToken(channel)) {
110                DateToken token = (DateToken) input.get(channel);
111                if (_manager == null) {
112                    _manager = ((CompositeActor) getContainer()).getManager();
113                }
114                long realStartTime = _manager.getRealStartTime();
115
116                Time modelTime = new Time(_director,
117                        (double) (token.getCalendarInstance().getTimeInMillis()
118                                - realStartTime) / 1000); // The default unit of time is seconds.
119                output.send(channel,
120                        new DoubleToken(modelTime.getDoubleValue()));
121            }
122        }
123    }
124
125    private DEDirector _director;
126    private Manager _manager;
127
128}