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.data.DateToken; 036import ptolemy.data.type.BaseType; 037import ptolemy.domains.de.kernel.DEDirector; 038import ptolemy.kernel.CompositeEntity; 039import ptolemy.kernel.util.IllegalActionException; 040import ptolemy.kernel.util.NameDuplicationException; 041 042/** A timed actor that outputs a date token that corresponds to the current 043 * model time (maintained by the local clock in the director). 044 * Such a correspondence is only given in models that synchronize to real time. 045 * In such models, the real time (date) when the model starts is recorded. An input 046 * to this actor is compared to the model start time (in model time). The difference between 047 * those dates (in millisecond resolution) is divided by the time resolution of 048 * the local clock, added to the start date of the model and then sent to the output. 049 * @author Patricia Derler 050 * @version $Id$ 051 * @since Ptolemy II 10.0 052 * @Pt.ProposedRating Red (cxh) 053 * @Pt.AcceptedRating Red (cxh) 054 */ 055public class EventToDate extends Transformer { 056 057 /** Create a new actor in the specified container with the specified 058 * name. The name must be unique within the container or an exception 059 * is thrown. The container argument must not be null, or a 060 * NullPointerException will be thrown. 061 * 062 * @param container The container. 063 * @param name The name of this actor within the container. 064 * @exception IllegalActionException If this actor cannot be contained 065 * by the proposed container (see the setContainer() method). 066 * @exception NameDuplicationException If the name coincides with 067 * an entity already in the container. 068 */ 069 public EventToDate(CompositeEntity container, String name) 070 throws IllegalActionException, NameDuplicationException { 071 super(container, name); 072 output.setTypeEquals(BaseType.DATE); 073 } 074 075 /** Check weather enclosing director is a DEDirector with 076 * synchronizeToRealTime is enabled. 077 * @exception IllegalActionException Thrown if the enclosing director is not a 078 * DEDirector or the synchronizeToRealTime property is false. 079 */ 080 @Override 081 public void initialize() throws IllegalActionException { 082 super.initialize(); 083 Director director = getDirector(); 084 if (director instanceof DEDirector) { 085 _director = (DEDirector) director; 086 } 087 } 088 089 /** Output a DateToken with a date that corresponds to the current model 090 * time. 091 * @exception IllegalActionException Not thrown here. 092 */ 093 @Override 094 public void fire() throws IllegalActionException { 095 super.fire(); 096 for (int channel = 0; channel < input.getWidth(); channel++) { 097 if (input.hasToken(channel)) { 098 input.get(channel); 099 } 100 double modelTimeSinceStart = _director.getModelTime() 101 .getDoubleValue() 102 - _director.getModelStartTime().getDoubleValue(); 103 if (_manager == null) { 104 _manager = ((CompositeActor) getContainer()).getManager(); 105 } 106 long time = (long) (modelTimeSinceStart * 1000) // The default unit of time is seconds. 107 + _manager.getRealStartTime(); 108 output.send(channel, new DateToken(time)); 109 } 110 } 111 112 private DEDirector _director; 113 114 private Manager _manager; 115 116}