/** Generate the Huffman codebook for the given <i>pmf</i>, and
* encode the input into booleans and send them to the output port.
*/
public void fire() throws IllegalActionException {
super.fire();
ArrayToken alphabetArrayToken = (ArrayToken) alphabet.getToken();
if (_pmf.length != alphabetArrayToken.length()) {
throw new IllegalActionException(this,
"uncoded alphabet and pmf are required to be arrays"
+ "with same length.");
}
Token[] alphabetTokens = new Token[_pmf.length];
for (int i = 0; i < _pmf.length; i++) {
alphabetTokens[i] = alphabetArrayToken.getElement(i);
}
if (_parametersInvalid) {
_parametersInvalid = false;
_codeBook = generateCodeBook(_pmf);
// FIXME: only produce the code book if the parameters
// have been updated.
StringToken[] codeBookTokens = new StringToken[_pmf.length];
for (int i = 0; i < _pmf.length; i++) {
codeBookTokens[i] = new StringToken(_codeBook[i]);
}
huffmanCodeBook.send(0, new ArrayToken(BaseType.STRING,
codeBookTokens));
}
}