0

I use if() sentences to control some transition, but it does not work. How can I solve these problems? I use the EasyFSM library to implement the simulation based on Finite State Machine.

I attach my own code. This is only the part of my code that has the problem I want to solve.

I think that there is no syntax error, but when I run the program there is no transition (the f.ProcessFSM(~) does not work) and there are only IDLE states (it is the first state of the FSM.)

How can I solve this problem?

        FSM f = new FSM("C://Users//losin//Desktop//file//PMC.xml", new FSMAction() {
            @Override
            public boolean action(String curState, String message, String nextState, Object args) {
                return true;
            }
        });

        for(int j=0; j <10 ; j++) {
            list.add(f.getCurrentState().toString());
            if(f.getCurrentState().toString()=="IDLE") {
                double rnd1 = Math.random();
                if(list.contains("PROCESS_COMPLIETED")==true) {
                    if(rnd1 < 0.05) {
                        f.ProcessFSM("FAIL");
                    } else if(rnd1 < 0.15) {
                        f.ProcessFSM("CLEAN");
                    } else {
                        f.ProcessFSM("LOAD");
                    }
                } else {
                    if(rnd1 < 0.05) {
                        f.ProcessFSM("FAIL");
                    } else {
                        f.ProcessFSM("LOAD");
                    }
                }
            } else if(f.getCurrentState().toString()=="PREPARE") {
                f.ProcessFSM("PROCESS_START");
            } else if(f.getCurrentState().toString()=="PROCESS") {
                double rnd2 = Math.random();
                if(rnd2 < 0.05) {
                    f.ProcessFSM("FAIL");
                } else {
                    f.ProcessFSM("COMPLETE");
                }
            } else if(f.getCurrentState().toString()=="PROCESS_COMPLETED") {
                f.ProcessFSM("UNLOAD");
            } else if(f.getCurrentState().toString()=="FAILURE") {
                f.ProcessFSM("FIX");
            } else {
                f.ProcessFSM("CLEAN_END");
            }
        }
        CSVUtils.writeLine(writer, list);               

        writer.flush();
        writer.close(); 
walen
  • 7,103
  • 2
  • 37
  • 58

2 Answers2

4
if(f.getCurrentState().toString()=="IDLE") //is wrong use

if(f.getCurrentState().toString().equals("IDLE"))
Youcef LAIDANI
  • 55,661
  • 15
  • 90
  • 140
Ashutosh Jha
  • 1,465
  • 1
  • 17
  • 28
1

This condition is never correct

if(f.getCurrentState().toString()=="IDLE") {

you are comparing references and not the content of the object

String objects must be compared using the equals method

 if("IDLE".equals(f.getCurrentState().toString()))
ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97