001/* A logical equals operator. 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.Token; 033import ptolemy.data.type.BaseType; 034import ptolemy.kernel.CompositeEntity; 035import ptolemy.kernel.util.IllegalActionException; 036import ptolemy.kernel.util.NameDuplicationException; 037 038/////////////////////////////////////////////////////////////////// 039//// Equals 040 041/** 042 A logical equals operator. This operator has one input 043 multiport and one output port that is not a multiport. It will consume 044 at most one token from each input channel, and compare the tokens 045 using the isEqualTo() method of the Token class. If all observed 046 input tokens are equal, then the output will be a true-valued boolean 047 token. If there is not at least one token on the input channels, 048 then no output is produced. 049 The type of the input port is undeclared and will be resolved by the type 050 resolution mechanism. Note that all input channels must resolve to the 051 same type. The type of the output port is boolean. 052 053 @see Token#isEqualTo(Token) 054 @author John Li and Edward A. Lee 055 @version $Id$ 056 @since Ptolemy II 0.4 057 @Pt.ProposedRating Green (eal) 058 @Pt.AcceptedRating Green (johnli) 059 */ 060public class Equals extends Transformer { 061 /** Construct an actor in the specified container with the specified 062 * name. 063 * @param container The container. 064 * @param name The name of this actor within the container. 065 * @exception IllegalActionException If the actor cannot be contained 066 * by the proposed container. 067 * @exception NameDuplicationException If the name coincides with 068 * an actor already in the container. 069 */ 070 public Equals(CompositeEntity container, String name) 071 throws IllegalActionException, NameDuplicationException { 072 super(container, name); 073 input.setMultiport(true); 074 output.setTypeEquals(BaseType.BOOLEAN); 075 076 _attachText("_iconDescription", 077 "<svg>\n" + "<rect x=\"-30\" y=\"-15\" " 078 + "width=\"60\" height=\"30\" " 079 + "style=\"fill:white\"/>\n" + "<text x=\"-14\" 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 * the result of comparing these tokens using the isEqualTo() method 088 * of the Token class. If the input has width 1, then the output 089 * is always true. If the input has width 0, or there are no 090 * no input tokens available, then no output is produced. 091 * @exception IllegalActionException If there is no director. 092 */ 093 @Override 094 public void fire() throws IllegalActionException { 095 super.fire(); 096 BooleanToken result = BooleanToken.TRUE; 097 Token reference = null; 098 boolean foundOne = false; 099 100 for (int i = 0; i < input.getWidth(); i++) { 101 if (!input.hasToken(i)) { 102 continue; 103 } 104 105 foundOne = true; 106 107 Token next = input.get(i); 108 109 if (reference == null) { 110 reference = next; 111 } else if (!next.isEqualTo(reference).booleanValue()) { 112 result = BooleanToken.FALSE; 113 } 114 } 115 116 if (foundOne) { 117 output.send(0, result); 118 } 119 } 120}