001/* Lempel-Ziv encoder.
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.type.BaseType;
036import ptolemy.kernel.CompositeEntity;
037import ptolemy.kernel.util.IllegalActionException;
038import ptolemy.kernel.util.NameDuplicationException;
039
040///////////////////////////////////////////////////////////////////
041//// LempelZivCoder
042
043/**
044 Lempel-Ziv encoder.
045
046 @author Ye Zhou
047 @version $Id$
048 @since Ptolemy II 4.1
049 @Pt.ProposedRating Red (zhouye)
050 @Pt.AcceptedRating Red (cxh)
051 */
052public class LempelZivCoder extends Transformer {
053    /** Construct an actor with the given container and name.
054     *  The output and trigger ports are also constructed.
055     *  @param container The container.
056     *  @param name The name of this actor.
057     *  @exception IllegalActionException If the entity cannot be contained
058     *   by the proposed container.
059     *  @exception NameDuplicationException If the container already has an
060     *   actor with this name.
061     */
062    public LempelZivCoder(CompositeEntity container, String name)
063            throws NameDuplicationException, IllegalActionException {
064        super(container, name);
065
066        // Declare port types.
067        input.setTypeEquals(BaseType.BOOLEAN);
068        output.setTypeEquals(BaseType.INT);
069    }
070
071    /** Encode the input into Lempel-Ziv code while generating the
072     *  code book.
073     *  @exception IllegalActionException if the super class throws it.
074     */
075    @Override
076    public void fire() throws IllegalActionException {
077        super.fire();
078        if (input.hasToken(0)) {
079            boolean inputValue = ((BooleanToken) input.get(0)).booleanValue();
080            _current = _current + (inputValue ? "1" : "0");
081
082            int index = _codeBook.indexOf(_current);
083
084            if (index == -1) {
085                output.send(0, new IntToken(_previousIndex));
086                output.send(0, new IntToken(inputValue ? 1 : 0));
087                _codeBook.add(_current);
088                _current = "";
089                _previousIndex = 0;
090            } else {
091                _previousIndex = index;
092            }
093        } /*else if (_current != "") {
094                              // FIXME: This part of output can not be produced.
095                              // The last few input booleans can not be encoded and produced.
096                              // The input probably should be a string or frame of booleans.
097                              // I.e., the actor knows whether the current input is the last
098                              // input token, probably by giving a parameter to specify the
099                              // length of the input.
100                              int length = _current.length();
101                              _previousIndex =
102                              _codeBook.indexOf(_current.substring(0, length - 2));
103                              output.send(0, new IntToken(_previousIndex));
104                              if (_current.endsWith("1")) {
105                              output.send(0, new IntToken(1));
106                              } else {
107                              output.send(0, new IntToken(0));
108                              }
109                              }*/
110    }
111
112    /** Initialize the actor by creating the code book containing
113     *  only one empty string "".
114     *  @exception IllegalActionException If thrown by a super class.
115     */
116    @Override
117    public void initialize() throws IllegalActionException {
118        super.initialize();
119        _codeBook = new LinkedList();
120        _codeBook.add("");
121        _current = "";
122        _previousIndex = 0;
123    }
124
125    ///////////////////////////////////////////////////////////////////
126    ////                         private variables                 ////
127    // The Lempel-Ziv code book.
128    private LinkedList _codeBook;
129
130    // The current string, concatenated by the inputs.
131    private String _current;
132
133    // The index of the previous string in the code book.
134    // current string is previous string appended by "1" or "0",
135    // depending on whether the current input is true or false.
136    private int _previousIndex = 0;
137}