001/* An actor suppresses simultaneous events.
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 */
028package ptolemy.domains.de.lib;
029
030import ptolemy.actor.util.Time;
031import ptolemy.data.Token;
032import ptolemy.kernel.CompositeEntity;
033import ptolemy.kernel.util.IllegalActionException;
034import ptolemy.kernel.util.NameDuplicationException;
035import ptolemy.kernel.util.Workspace;
036
037///////////////////////////////////////////////////////////////////
038//// SuppressSimultaneousEvents
039
040/**
041Output the first token received on the input port, and suppress any other
042inputs received with the same physical time index.
043 @author Jeff C. Jensen
044 @version $Id$
045 @since Ptolemy II 8.0
046 */
047public class SuppressSimultaneousEvents extends DETransformer {
048    /** Construct an actor with the given container and name.
049     *  @param container The container.
050     *  @param name The name of this actor.
051     *  @exception IllegalActionException If the actor cannot be contained
052     *   by the proposed container.
053     *  @exception NameDuplicationException If the container already has an
054     *   actor with this name.
055     */
056    public SuppressSimultaneousEvents(CompositeEntity container, String name)
057            throws NameDuplicationException, IllegalActionException {
058        super(container, name);
059        output.setTypeEquals(input.getType());
060        output.setWidthEquals(input, false);
061    }
062
063    ///////////////////////////////////////////////////////////////////
064    ////                         public methods                    ////
065
066    /** Clone the actor into the specified workspace. This calls the
067     *  base class and then sets the ports.
068     *  @param workspace The workspace for the new object.
069     *  @return A new actor.
070     *  @exception CloneNotSupportedException If a derived class has
071     *   has an attribute that cannot be cloned.
072     */
073    @Override
074    public Object clone(Workspace workspace) throws CloneNotSupportedException {
075        SuppressSimultaneousEvents newObject = (SuppressSimultaneousEvents) super.clone(
076                workspace);
077
078        newObject.output.setTypeEquals(input.getType());
079        newObject.output.setWidthEquals(newObject.input, false);
080
081        // This is not strictly needed (since it is always recreated
082        // in preinitialize) but it is safer.
083        newObject._lastEventTime = null;
084
085        return newObject;
086    }
087
088    /** Consume one token from the input port, and output if it is the first
089     *  token seen at this point in physical time.
090     *  @exception IllegalActionException If thrown by the superclass,
091     *  while checking if the input has a token or while getting a token
092     *  from the input.
093     */
094    @Override
095    public void fire() throws IllegalActionException {
096        super.fire();
097        if (input.hasToken(0)) {
098            Time currentTime = getDirector().getModelTime();
099            Token currentToken = input.get(0);
100            if (_lastEventTime == null || !_lastEventTime.equals(currentTime)) {
101                output.broadcast(currentToken);
102            }
103            _lastEventTime = currentTime;
104        }
105    }
106
107    /** Reset to indicate that no input has yet been seen.
108     *  @exception IllegalActionException If the parent class throws it.
109     */
110    @Override
111    public void initialize() throws IllegalActionException {
112        super.initialize();
113        _lastEventTime = null;
114    }
115
116    ///////////////////////////////////////////////////////////////////
117    ////                         private members                   ////
118    private Time _lastEventTime;
119}