001/* Lempel-Ziv decoder. 002 003 Copyright (c) 2004-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.comm; 029 030import java.util.LinkedList; 031 032import ptolemy.actor.lib.Transformer; 033import ptolemy.data.BooleanToken; 034import ptolemy.data.IntToken; 035import ptolemy.data.Token; 036import ptolemy.data.expr.Parameter; 037import ptolemy.data.type.BaseType; 038import ptolemy.kernel.CompositeEntity; 039import ptolemy.kernel.util.IllegalActionException; 040import ptolemy.kernel.util.NameDuplicationException; 041 042/////////////////////////////////////////////////////////////////// 043//// Lempel-Ziv decoder. 044 045/** 046 Lempel-Ziv decoder. 047 048 @see LempelZivCoder 049 @author Ye Zhou 050 @version $Id$ 051 @since Ptolemy II 4.1 052 @Pt.ProposedRating Red (zhouye) 053 @Pt.AcceptedRating Red (cxh) 054 */ 055public class LempelZivDecoder extends Transformer { 056 /** Construct an actor with the given container and name. 057 * The output and trigger ports are also constructed. 058 * @param container The container. 059 * @param name The name of this actor. 060 * @exception IllegalActionException If the entity cannot be contained 061 * by the proposed container. 062 * @exception NameDuplicationException If the container already has an 063 * actor with this name. 064 */ 065 public LempelZivDecoder(CompositeEntity container, String name) 066 throws NameDuplicationException, IllegalActionException { 067 super(container, name); 068 069 // Declare port types. 070 input.setTypeEquals(BaseType.INT); 071 new Parameter(input, "tokenConsumptionRate", new IntToken(2)); 072 output.setTypeEquals(BaseType.BOOLEAN); 073 } 074 075 /** Decode the Lempel-Ziv code while generating the decode book. 076 * The decode book should be same as the code book of the 077 * corresponding Lempel-Ziv encoder. 078 * @exception IllegalActionException if the input is not a decodable 079 * Lempel-Ziv code. 080 */ 081 @Override 082 public void fire() throws IllegalActionException { 083 super.fire(); 084 Token[] inputToken = input.get(0, 2); 085 int oldPhase = ((IntToken) inputToken[0]).intValue(); 086 int bit = ((IntToken) inputToken[1]).intValue(); 087 String current = (String) _decodeBook.get(oldPhase); 088 089 if (bit == 0) { 090 current = current + "0"; 091 } else if (bit == 1) { 092 current = current + "1"; 093 } else { 094 throw new IllegalActionException(this, 095 "This is not a valid Lempel-Ziv code."); 096 } 097 098 _decodeBook.add(current); 099 100 for (int i = 0; i < current.length(); i++) { 101 if (current.charAt(i) == '0') { 102 output.send(0, new BooleanToken(false)); 103 } else { 104 output.send(0, new BooleanToken(true)); 105 } 106 } 107 } 108 109 /** initialize the actor by creating a decode book that only 110 * contains one empty string "". 111 * @exception IllegalActionException If thrown by a super class. 112 */ 113 @Override 114 public void initialize() throws IllegalActionException { 115 super.initialize(); 116 _decodeBook = new LinkedList(); 117 _decodeBook.add(""); 118 } 119 120 /////////////////////////////////////////////////////////////////// 121 //// private variables //// 122 // The Lempel-Ziv decode book. 123 private LinkedList _decodeBook; 124}