/**
 * Java WordNet Library (JWNL)
 * See the documentation for copyright information.
 */
package net.didion.jwnl.data;

import net.didion.jwnl.JWNL;
import net.didion.jwnl.util.Resolvable;

import java.io.Serializable;
import java.util.BitSet;

/** A <code>VerbFrame</code> is the frame of a sentence in which it is proper to use a given verb. */
public final class VerbFrame implements Serializable {
	static final long serialVersionUID = 1450633678809744269L;

	private static VerbFrame[] _verbFrames;
	private static boolean _initalized = false;

	public static void initialize() {
		if (!_initalized) {
			int framesSize = Integer.parseInt(JWNL.resolveMessage("NUMBER_OF_VERB_FRAMES"));
			_verbFrames = new VerbFrame[framesSize];
			for (int i = 1; i <= framesSize; i++)
				_verbFrames[i - 1] = new VerbFrame(getKeyString(i), i);
			_initalized = true;
		}
	}

	public static final String getKeyString(int i) {
		StringBuffer buf = new StringBuffer();
		buf.append("VERB_FRAME_");
		int numZerosToAppend = 3 - String.valueOf(i).length();
		for (int j = 0; j < numZerosToAppend; j++)
			buf.append(0);
		buf.append(i);
		return buf.toString();
	}

	public static int getVerbFramesSize() {
		return _verbFrames.length;
	}

	/** Get frame at index <var>index</var>.*/
	public static String getFrame(int index) {
		return _verbFrames[index - 1].getFrame();
	}

	/**
	 * Get the frames at the indexes encoded in <var>l</var>.
	 * Verb Frames are encoded within <code>Word</code>s as a long. Each bit represents
	 * the frame at its corresponding index. If the bit is set, that verb
	 * frame is valid for the word.
	 */
	public static String[] getFrames(BitSet bits) {
		int[] indicies = getVerbFrameIndicies(bits);
		String[] frames = new String[indicies.length];
		for (int i = 0; i < indicies.length; i++)
			frames[i] = _verbFrames[i].getFrame();
		return frames;
	}

	public static int[] getVerbFrameIndicies(BitSet bits) {
		int[] indicies = new int[bits.cardinality()];
		for (int i = 0; i < indicies.length; i++) {
			indicies[i] = bits.nextSetBit(i == 0 ? 0 : indicies[i - 1]);
		}
		return indicies;
	}

	private Resolvable _frame;
	private int _index;

	private VerbFrame(String frame, int index) {
		_frame = new Resolvable(frame);
		_index = index;
	}

	public String getFrame() {
		return _frame.toString();
	}

	public int getIndex() {
		return _index;
	}

	private String _cachedToString = null;

	public String toString() {
		if (_cachedToString == null)
			_cachedToString = JWNL.resolveMessage("DATA_TOSTRING_007", getFrame());
		return _cachedToString;
	}

	public int hashCode() {
		return getIndex();
	}
}
@@ -1891,16 +1891,30 @@
 Frames%5Bi
+ndicies%5Bi%5D - 1
 %5D.getFra
@@ -1936,32 +1936,396 @@
  frames;%0D%0A%09%7D%0D%0A%0D%0A
+%09/**%0D%0A%09 * Gets the verb frame indices for a synset. This is the collection%0D%0A%09 * of f_num values for a synset definition. In the case of a synset, this%0D%0A%09 * is only the values that are true for all words with the synset. In other%0D%0A%09 * words, only the sentence frames that belong to all words. %0D%0A%09 * @param bits the bit set%0D%0A%09 * @return an integer collection%0D%0A%09 */%0D%0A
 %09public static i
@@ -2375,25 +2375,24 @@
 %09int%5B%5D indic
-i
 es = new int
@@ -2420,67 +2420,67 @@
 %0D%0A%09%09
-for (
 int i
+ndex
  = 0;
- i %3C indicies.length; i++) %7B%0D%0A%09%09%09indicies%5Bi%5D
+%0D%0A%09%09for (int i = bits.nextSetBit(0); i %3E=0; i
  = b
@@ -2499,44 +2499,47 @@
 it(i
- == 0 ? 0 : 
++1)) %7B%0D%0A%09%09%09
 indic
-i
 es%5Bi
- - 1%5D)
+ndex++%5D = i
 ;%0D%0A%09%09%7D%0D%0A
 %09%09re
@@ -2534,16 +2534,20 @@
 ;%0D%0A%09%09%7D%0D%0A
+%09%09%0D%0A
 %09%09return
@@ -2556,14 +2556,15 @@
 ndic
-i
 es;%0D%0A
+%0D%0A
 %09%7D%0D%0A

Fix for bug 1375238, extracting sentence frames improperly. This was caused by improper translation of bit sets. Thanks to juanjof for the patch way back in 2005!