001/* An actor that converts 32 consecutive BooleanTokens to an IntToken 002 003 Copyright (c) 1998-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.domains.sdf.lib; 029 030import ptolemy.data.BooleanToken; 031import ptolemy.data.IntToken; 032import ptolemy.data.Token; 033import ptolemy.data.expr.Parameter; 034import ptolemy.data.type.BaseType; 035import ptolemy.kernel.CompositeEntity; 036import ptolemy.kernel.util.Attribute; 037import ptolemy.kernel.util.IllegalActionException; 038import ptolemy.kernel.util.NameDuplicationException; 039 040/////////////////////////////////////////////////////////////////// 041/// BitsToInt 042 043/** 044 This actor converts a sequence of BooleanTokens into a single IntToken. 045 The number of Boolean tokens is specified by the <i>numberOfBits</i> 046 parameter and should be a positive integer not larger than 32. Let <i>k</i> 047 denotes the value of the <i>numberOfBits</i> parameter. The output 048 integer is ranged from -2<sup><i>k</i></sup> to 2<sup><i>k</i></sup> - 1. 049 050 The first boolean token received indicates the sign of the integer. If 051 it is "false", the output integer is a non-negative number. If it is "true", 052 the output integer is a negative number. The least significant bit is 053 the last boolean token received. 054 055 @author Michael Leung 056 @version $Id$ 057 @since Ptolemy II 0.4 058 @Pt.ProposedRating Green (neuendor) 059 @Pt.AcceptedRating Yellow (neuendor) 060 */ 061public class BitsToInt extends SDFConverter { 062 /** Construct an actor with the given container and name. 063 * @param container The container. 064 * @param name The name of this actor. 065 * @exception IllegalActionException If the actor cannot be contained 066 * by the proposed container. 067 * @exception NameDuplicationException If the container already has an 068 * actor with this name. 069 */ 070 public BitsToInt(CompositeEntity container, String name) 071 throws NameDuplicationException, IllegalActionException { 072 super(container, name); 073 074 numberOfBits = new Parameter(this, "numberOfBits"); 075 numberOfBits.setExpression("32"); 076 077 input_tokenConsumptionRate.setExpression("numberOfBits"); 078 079 input.setTypeEquals(BaseType.BOOLEAN); 080 081 output.setTypeEquals(BaseType.INT); 082 } 083 084 /////////////////////////////////////////////////////////////////// 085 //// parameters //// 086 087 /** The number of bits that is converted to the output integer. 088 * It should be a positive integer no more than 32. 089 */ 090 public Parameter numberOfBits; 091 092 /////////////////////////////////////////////////////////////////// 093 //// public methods //// 094 095 /** If the argument is the <i>numberOfBits</i> parameter, then 096 * set the production rate of the output port. 097 * @param attribute The attribute that has changed. 098 * @exception IllegalActionException If the parameter is out of range. 099 */ 100 @Override 101 public void attributeChanged(Attribute attribute) 102 throws IllegalActionException { 103 if (attribute == numberOfBits) { 104 int rate = ((IntToken) numberOfBits.getToken()).intValue(); 105 106 if (rate < 1 || rate > 32) { 107 throw new IllegalActionException(this, 108 "Invalid number of bits: " + rate); 109 } 110 } else { 111 super.attributeChanged(attribute); 112 } 113 } 114 115 /** Consume <i>numberOfBits</i> BooleanTokens on the input. 116 * Output a single IntToken which is representing by the 117 * BooleanTokens. 118 * The first token consumed is the most significant bit (The sign bit). 119 * The last token consumed is the least significant bit 120 * @exception IllegalActionException If there is no director. 121 */ 122 @Override 123 public final void fire() throws IllegalActionException { 124 super.fire(); 125 126 int rate = ((IntToken) numberOfBits.getToken()).intValue(); 127 Token[] bits /* Dead Store: = new BooleanToken[rate]*/; 128 bits = input.get(0, rate); 129 130 int integer = 0; 131 132 for (int i = 1; i < rate; i++) { 133 integer = integer << 1; 134 135 if (((BooleanToken) bits[i]).booleanValue()) { 136 integer += 1; 137 } 138 } 139 140 if (((BooleanToken) bits[0]).booleanValue()) { 141 //convert integer to negative value. 142 integer = integer - (1 << rate - 1); 143 } 144 145 IntToken value = new IntToken(integer); 146 output.send(0, value); 147 } 148}