package generator;

import util.State;

public class BigFstGenerator {

    private StringBuffer strBuff;

    public BigFstGenerator() {
    }

    public StringBuffer compute(State initState, String className) {
        strBuff = new StringBuffer();
        System.out.println("[FstGenerator] Transforming Fst to a java class");
        append("package generated;");

        append("public class " + className + " {");

        appendWithTab("public static float compute(int[] token) {", 1);

        appendWithTab("int pos=0;", 2);
        appendWithTab("float result=0f;", 2);
        appendWithTab("return state_0(token, pos, result);", 2);

        appendWithTab("}", 1);

        generateCases(initState);

        append("}");
        System.out.println("[FstGenerator] Successfully transformed fst to " + className + ".java");
        return strBuff;
    }


    private void generateCases(State currentState) {
        append("\n\tprivate static float state_" + currentState.getId() +
                "(int[] token, int pos, float result) {");
       
        if( currentState.getNumArcs() > 0) { 
            generateTokenLengthTest(2);
            appendWithTab("switch(token[pos++]) {", 2);
            for (int i = 0; i < currentState.getNumArcs(); i++) {
                appendWithTab("case " + currentState.getArc(i).getIlabel()  + ":", 3); 
                if (currentState.getArc(i).getWeight() != 0f) {
                    appendWithTab("result+=" + currentState.getArc(i).getWeight() + "f;", 4);
                }
                generateCases(currentState.getArc(i).getNextState(), 4);
            }
            appendWithTab("default:", 3);
            appendWithTab("return -1;", 4);
            appendWithTab("}", 2);
        } else {
            appendWithTab("return (pos!=token.length) ? -1 : result;", 2);
        }
        appendWithTab("}", 1);
    }

    private void generateTokenLengthTest(int tab) {
        appendWithTab("if(pos>=token.length) {return -1;}", tab);
    }

    private void append(String strToAppend) {
        strBuff.append(strToAppend);
        strBuff.append("\n");
    }

    private void appendWithTab(String strToAppend, int numberOfTab) {
        for (int i = 0; i < numberOfTab; i++) {
            strBuff.append("\t");
        }
        strBuff.append(strToAppend);
        strBuff.append("\n");
    }

}

@@ -31,16 +31,44 @@
 l.State;
+%0Aimport java.util.ArrayList;
 %0A%0Apublic
@@ -939,24 +939,77 @@
 entState) %7B%0A
+        List%3CState%3E nextStates = new ArrayList%3C%3E();%0A%0A
         appe
@@ -1625,29 +1625,179 @@
         
-generateCases
+appendWithTab(%22return state_%22 + currentState.getArc(i).getNextState().getId() +%0A                        %22(token, pos, result);%22, 4);%0A                nextStates.add
 (current
@@ -1826,19 +1826,16 @@
 tState()
-, 4
 );%0A     
@@ -2058,32 +2058,32 @@
 , 2);%0A        %7D%0A
-
         appendWi
@@ -2096,16 +2096,99 @@
 %22%7D%22, 1);
+%0A%0A        for (State next: nextStates) %7B%0A            generateCases(next);%0A        %7D
 %0A    %7D%0A%0A

Create a list of next node to be written in the end