📄 smccsharpgenerator.java
字号:
{ _source.println(); _source.println(); } // End of transition. _source.print(_indent); _source.println(" return;"); _source.print(_indent); _source.println("}"); return; } // end of visit(SmcTransition) public void visit(SmcGuard guard) { SmcTransition transition = guard.getTransition(); SmcState state = transition.getState(); SmcMap map = state.getMap(); String context = map.getFSM().getContext(); String mapName = map.getName(); String stateName = state.getClassName(); int transType = guard.getTransType(); boolean defaultFlag = stateName.equalsIgnoreCase("Default"); boolean loopbackFlag = false; String indent2; String indent3; String indent4; String endStateName = guard.getEndState(); String fqEndStateName = ""; String pushStateName = guard.getPushState(); String condition = guard.getCondition(); List<SmcAction> actions = guard.getActions(); boolean hasActions = !(actions.isEmpty()); // If this guard's end state is not of the form // "map::state", then prepend the map name to the // state name. // DON'T DO THIS IF THIS IS A POP TRANSITION! // The "state" is actually a transition name. if (transType != Smc.TRANS_POP && endStateName.length () > 0 && endStateName.equals(NIL_STATE) == false) { endStateName = scopeStateName(endStateName, mapName); } // Qualify the state and push state names as well. stateName = scopeStateName(stateName, mapName); pushStateName = scopeStateName(pushStateName, mapName); loopbackFlag = isLoopback(transType, stateName, endStateName); // The guard code generation is a bit tricky. The first // question is how many guards are there? If there are // more than one, then we will need to generate the // proper "if-then-else" code. if (_guardCount > 1) { indent2 = _indent + " "; // There are multiple guards. // Is this the first guard? if (_guardIndex == 0 && condition.length() > 0) { // Yes, this is the first. This means an "if" // should be used. _source.print(_indent); _source.print(" if ("); _source.print(condition); _source.println(")"); _source.print(_indent); _source.println(" {"); } else if (condition.length() > 0) { // No, this is not the first transition but it // does have a condition. Use an "else if". _source.println(); _source.print(_indent); _source.print(" else if ("); _source.print(condition); _source.println(")"); _source.print(_indent); _source.println(" {"); } else { // This is not the first transition and it has // no condition. _source.println(); _source.print(_indent); _source.println(" else"); _source.print(_indent); _source.println(" {"); } } // There is only one guard. Does this guard have // a condition? else if (condition.length() == 0) { // No. This is a plain, old. vanilla transition. indent2 = _indent + " "; } else { // Yes there is a condition. indent2 = _indent + " "; _source.print(_indent); _source.print(" if ("); _source.print(condition); _source.println(")"); _source.print(_indent); _source.println(" {"); } // Now that the necessary conditions are in place, it's // time to dump out the transition's actions. First, do // the proper handling of the state change. If this // transition has no actions, then set the end state // immediately. Otherwise, unset the current state so // that if an action tries to issue a transition, it will // fail. if (hasActions == false && endStateName.length() != 0) { fqEndStateName = endStateName; } else if (hasActions == true) { // Save away the current state if this is a loopback // transition. Storing current state allows the // current state to be cleared before any actions are // executed. Remember: actions are not allowed to // issue transitions and clearing the current state // prevents them from doing do. if (loopbackFlag == true) { fqEndStateName = "endState"; _source.print(indent2); _source.print(context); _source.print("State "); _source.print(fqEndStateName); _source.println(" = context.State;"); } else { fqEndStateName = endStateName; } } // Decide if runtime loopback checking must be done. if (defaultFlag == true && transType != Smc.TRANS_POP && loopbackFlag == false) { _source.print(indent2); _source.println("bool loopbackFlag ="); _source.print(indent2); _source.print( " (context.State.Name == "); _source.print(fqEndStateName); _source.println(".Name);"); } _source.println(); // Dump out the exit actions if // 1) this is a standard, non-loopback transition or // 2) a pop transition. if (transType == Smc.TRANS_POP || loopbackFlag == false) { indent4 = indent2; // If this is a non-loopback, generic transition, // do runtime loopback checking. if (transType == Smc.TRANS_SET && defaultFlag == true) { indent4 = indent2 + " "; _source.print(indent2); _source.println("if (loopbackFlag == false)"); _source.print(indent2); _source.println("{"); } _source.print(indent4); _source.println("context.State.Exit(context);"); if (transType == Smc.TRANS_SET && defaultFlag == true) { _source.print(indent2); _source.println("}"); _source.println(); } } // Dump out this transition's actions. if (hasActions == false) { if (condition.length() > 0) { _source.print(indent2); _source.println("// No actions."); } indent3 = indent2; } else { // Now that we are in the transition, clear the // current state. _source.print(indent2); _source.println("context.ClearState();"); // v. 2.0.0: Place the actions inside a try/finally // block. This way the state will be set before an // exception leaves the transition method. // v. 2.2.0: Check if the user has turned off this // feature first. if (Smc.isNoCatch() == false) { _source.println(); _source.print(indent2); _source.println("try"); _source.print(indent2); _source.println("{"); indent3 = indent2 + " "; } else { indent3 = indent2; } indent4 = _indent; _indent = indent3; for (SmcAction action: actions) { action.accept(this); } _indent = indent4; // v. 2.2.0: Check if the user has turned off this // feature first. if (Smc.isNoCatch() == false) { _source.print(indent2); _source.println("}"); _source.print(indent2); _source.println("finally"); _source.print(indent2); _source.println("{"); } } // Print the state assignment if necessary. Do NOT // generate the state assignment if: // 1. The transition has no actions AND is a loopback OR // 2. This is a push or pop transition. if (transType == Smc.TRANS_SET && (hasActions == true || loopbackFlag == false)) { _source.print(indent3); _source.print("context.State = "); _source.print(fqEndStateName); _source.println(";"); } else if (transType == Smc.TRANS_PUSH) { // Set the next state so this it can be pushed // onto the state stack. But only do so if a clear // state was done. // v. 4.3.0: If the full-qualified end state is // "nil", then don't need to do anything. if ((loopbackFlag == false || hasActions == true) && fqEndStateName.equals(NIL_STATE) == false) { _source.print(indent3); _source.print("context.State = "); _source.print(fqEndStateName); _source.println(";"); } // Before doing the push, execute the end state's // entry actions (if any) if this is not a loopback. if (loopbackFlag == false) { if (defaultFlag == true) { indent4 = indent3 + " "; _source.println(); _source.print(indent3); _source.println( "if (loopbackFlag == false)"); _source.print(indent3); _source.println("{"); } else { indent4 = indent3; } _source.print(indent4); _source.println("context.State.Entry(context);"); if (defaultFlag == true) { _source.print(indent3); _source.println("}"); } } _source.print(indent3); _source.print("context.PushState("); _source.print(pushStateName); _source.println(");"); } else if (transType == Smc.TRANS_POP) { _source.print(indent3); _source.println("context.PopState();"); } // Perform the new state's enty actions if: // 1) this is a standard, non-loopback transition or // 2) a push transition. if ((transType == Smc.TRANS_SET && endStateName.equals(NIL_STATE) == false && endStateName.equals(stateName) == false) || transType == Smc.TRANS_PUSH) { indent4 = indent3; // If this is a non-loopback, generic transition, // do runtime loopback checking. if (transType == Smc.TRANS_SET && defaultFlag == true) { indent4 = indent3 + " "; _source.println(); _source.print(indent3); _source.println("if (loopbackFlag == false)"); _source.print(indent3); _source.println("{"); } _source.print(indent4); _source.println("context.State.Entry(context);"); if (transType == Smc.TRANS_SET && defaultFlag == true) { _source.print(indent3); _source.println("}"); _source.println(); } } // If there was a try/finally, then put the closing // brace on the finally block. // v. 2.2.0: Check if the user has turned off this // feature first. if (hasActions == true && Smc.isNoCatch() == false) { _source.print(indent2); _source.println("}"); _source.println(); } // If there is a transition associated with the pop, then // issue that transition here. if (transType == Smc.TRANS_POP && endStateName.equals(NIL_STATE) == false &&
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -