如何让我的案例状态机在当前案例中执行代码

问题描述

我正在尝试构建状态机的多种解释,以便我可以找到最适合我的方法以及我认为最容易使用的方法,以便我最终可以制作它的模板。代码最终将在 Java 控制的 PLC 上运行。

目前我正在尝试像下面的代码那样做,但问题是一个案例只执行一次,而我需要它继续执行直到满足条件并设置新状态。

public class testClass(){

int timesExecuted = 0;
boolean triggerBoolean; 
Enum currentState; 
enum State {
    s10_firstState,s20_secondState,s30_thirthState
}


public void main() throws Exception {
    
    switch (StateMachine()) {
    
        case s10_firstState:
            executeState(State.s10_firstState); //cases only executed once i need this to keep executing the code in the current case
            break;

        case s20_secondState:
            executeState(State.s20_secondState);
            break;
            
        case s30_thirthState:
            executeState(State.s30_thirthState);
            break;
    }
}

//Determines logic between states
public State StateMachine() {
    
    //Goto State 10 on start
    if (currentState == null){currentState = State.s10_firstState;}
    
    //Goto state 20 
    if(currentState.equals(s10_firstState)&& triggerBoolean==true){
    currentState= State.s20_secondState}

 return (State) currentState;   
 }


//Executes logic in states
public void executeState(State stateToExecute) {
   
   //while in state 10 keep doing this       
    if (stateToExecute.equals(State.s10_StandBy)) {
        if(timesExecuted>1000){
        triggerBoolean=true
        } 
     timesExecuted++;
    }
        
    //while in state 20 keep doing this
    if (stateToExecute.equals(State.s20_secondState)){
    //do something
        }
    }
}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)