001/* Output true if the input is present, false otherwise. 002 003 Copyright (c) 1997-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.logic; 029 030import ptolemy.actor.lib.Transformer; 031import ptolemy.data.BooleanToken; 032import ptolemy.data.type.BaseType; 033import ptolemy.kernel.CompositeEntity; 034import ptolemy.kernel.util.IllegalActionException; 035import ptolemy.kernel.util.NameDuplicationException; 036 037/////////////////////////////////////////////////////////////////// 038//// IsPresent 039 040/** 041 On each firing, output true if the input is present and false otherwise. 042 The type of the output port is boolean, and the input is general. 043 The width of the input is expected to match the width of the output. 044 Note that the utility of this actor varies by domain. In PN, for 045 example, the input is always present (by definition). In SDF, it is 046 expected to be always present, but may not be. In DE, the actor is 047 only triggered if one of the input channels has data. The actor is 048 probably most useful in synchronous domains like SR and Giotto. 049 050 @author Edward A. Lee 051 @version $Id$ 052 @since Ptolemy II 2.0 053 @Pt.ProposedRating Yellow (eal) 054 @Pt.AcceptedRating Red (eal) 055 */ 056public class IsPresent extends Transformer { 057 /** Construct an actor in the specified container with the specified 058 * name. 059 * @param container The container. 060 * @param name The name of this actor within the container. 061 * @exception IllegalActionException If the actor cannot be contained 062 * by the proposed container. 063 * @exception NameDuplicationException If the name coincides with 064 * an actor already in the container. 065 */ 066 public IsPresent(CompositeEntity container, String name) 067 throws IllegalActionException, NameDuplicationException { 068 super(container, name); 069 input.setTypeEquals(BaseType.GENERAL); 070 input.setMultiport(true); 071 output.setTypeEquals(BaseType.BOOLEAN); 072 output.setMultiport(true); 073 074 input.setWidthEquals(output, true); 075 076 _attachText("_iconDescription", 077 "<svg>\n" + "<rect x=\"-15\" y=\"-15\" " 078 + "width=\"30\" height=\"30\" " 079 + "style=\"fill:white\"/>\n" + "<text x=\"-4\" y=\"8\"" 080 + "style=\"font-size:24\">?</text>\n" + "</svg>\n"); 081 } 082 083 /////////////////////////////////////////////////////////////////// 084 //// public methods //// 085 086 /** Consume at most one token from each input channel, and output 087 * a boolean on the corresponding output channel (if there is one). 088 * The value of the boolean is true if the input is present and 089 * false otherwise. 090 */ 091 @Override 092 public void fire() throws IllegalActionException { 093 super.fire(); 094 int outputWidth = output.getWidth(); 095 096 for (int i = 0; i < input.getWidth(); i++) { 097 if (input.hasToken(i)) { 098 // Consume the token. 099 input.get(i); 100 101 if (i < outputWidth) { 102 output.send(i, BooleanToken.TRUE); 103 } 104 } else { 105 if (i < outputWidth) { 106 output.send(i, BooleanToken.FALSE); 107 } 108 } 109 } 110 } 111}