/* Generate discrete events by sampling a CT signal. Copyright (c) 1998-2014 The Regents of the University of California. All rights reserved. Permission is hereby granted, without written agreement and without license or royalty fees, to use, copy, modify, and distribute this software and its documentation for any purpose, provided that the above copyright notice and the following two paragraphs appear in all copies of this software. IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. PT_COPYRIGHT_VERSION_2 COPYRIGHTENDKEY */ package ptolemy.actor.lib; import ptolemy.actor.TypedIOPort; import ptolemy.data.Token; import ptolemy.kernel.CompositeEntity; import ptolemy.kernel.util.IllegalActionException; import ptolemy.kernel.util.NameDuplicationException; import ptolemy.kernel.util.StringAttribute; import ptolemy.kernel.util.Workspace; /////////////////////////////////////////////////////////////////// //// Sampler /** This actor generates discrete events by sampling the input signal whenever the trigger input is present. This sampler will send to the output whatever input occurs at a sample time; if the input is absent, the output will be absent.

This actor has multiport inputs and outputs. Signals in each input channel are sampled and produced to corresponding output channel. When there are multiple inputs, the first non-absent input from each channel is read, and the output is produced at the first microstep after the last of the inputs became non-absent. @author Edward A. Lee @version $Id: Sampler.java 69607 2014-07-30 17:07:26Z cxh $ @since Ptolemy II 8.0 @Pt.ProposedRating Yellow (eal) @Pt.AcceptedRating Red (cxh) */ public class Sampler extends Transformer { /** Construct an actor in the specified container with the specified * name. The name must be unique within the container or an exception * is thrown. The container argument must not be null, or a * NullPointerException will be thrown. * The actor can be either dynamic, or not. It must be set at the * construction time and can't be changed thereafter. * A dynamic actor will produce a token at its initialization phase. * @param container The container of this actor. * @param name The actor's name * @exception IllegalActionException If the entity cannot be contained * by the proposed container. * @exception NameDuplicationException If name coincides with * an entity already in the container. */ public Sampler(CompositeEntity container, String name) throws IllegalActionException, NameDuplicationException { super(container, name); input.setMultiport(true); output.setMultiport(true); output.setWidthEquals(input, true); output.setTypeSameAs(input); _attachText("_iconDescription", "\n" + "\n" + "\n" + "\n" + "\n"); trigger = new TypedIOPort(this, "trigger", true, false); trigger.setMultiport(true); // Width constraint. Not bidirectional to not break any existing models. output.setWidthEquals(input, true); _attachText("_iconDescription", "\n" + "\n" + "\n" + "\n" + "\n" + "\n"); StringAttribute cardinality = new StringAttribute(trigger, "_cardinal"); cardinality.setExpression("SOUTH"); } /////////////////////////////////////////////////////////////////// //// ports and parameters //// /** The trigger port, which has undeclared type. If this port * receives a token, then the most recent token from the * input port will be emitted on the output port. */ public TypedIOPort trigger; /////////////////////////////////////////////////////////////////// //// public methods //// /** Clone this actor into the specified workspace. The new actor is * not added to the directory of that workspace (you must do this * yourself if you want it there). * The result is a new actor with the same ports as the original, but * no connections and no container. A container must be set before * much can be done with this actor. * * @param workspace The workspace for the cloned object. * @exception CloneNotSupportedException If cloned ports cannot have * as their container the cloned entity (this should not occur), or * if one of the attributes cannot be cloned. * @return A new ComponentEntity. */ @Override public Object clone(Workspace workspace) throws CloneNotSupportedException { Sampler newObject = (Sampler) super.clone(workspace); newObject.output.setTypeSameAs(newObject.input); newObject.output.setWidthEquals(newObject.input, true); return newObject; } /** Generate an output if the current time is one of the sampling * times. The value of the event is the value of the input signal at the * current time. * @exception IllegalActionException If the transfer of tokens failed. */ @Override public void fire() throws IllegalActionException { super.fire(); boolean hasTrigger = false; for (int i = 0; i < trigger.getWidth(); i++) { if (trigger.hasToken(i)) { trigger.get(i); hasTrigger = true; } } // Read the input and send it to the output if a trigger has arrived. int width = Math.min(input.getWidth(), output.getWidth()); for (int i = 0; i < input.getWidth(); i++) { if (input.hasToken(i)) { Token token = input.get(i); if (hasTrigger) { if (_debugging) { _debug("Sampled input value " + token + " at time " + getDirector().getModelTime()); } if (i < width) { output.send(i, token); } } } } } }