📄 smcvbgenerator.java
字号:
_source.println(); _source.print(_indent); _source.println( " '------------------------------------------------------------"); _source.print(_indent); _source.println(" ' Shared data"); _source.print(_indent); _source.println(" '"); _source.println(); _source.print(_indent); _source.print(" "); _source.println( "Private Shared _transitions As IDictionary"); _source.println(); _source.print(_indent); _source.println(" Shared Sub New()"); _source.println(); _source.print(_indent); _source.print(" "); _source.println("_transitions = New Hashtable()"); // Now place the transition names into the list. for (SmcTransition transition: allTransitions) { transName = transition.getName(); // If the transition is in this state, then its // value is 1. if (stateTransitions.contains( transition) == true) { transDefinition = 1; } // If the transition is defined in this map's // default state, then the value is 2. else if (defaultTransitions.contains( transition) == true) { transDefinition = 2; } // Otherwise the value is 0 - undefined. else { transDefinition = 0; } _source.print(_indent); _source.print(" "); _source.print("_transitions.Add(\""); _source.print(transName); _source.print("\", "); _source.print(transDefinition); _source.println(")"); } _source.println(); _source.print(_indent); _source.println(" End Sub"); } _source.print(_indent); _source.println("End Class"); return; } // end of visit(SmcState) public void visit(SmcTransition transition) { SmcState state = transition.getState(); SmcMap map = state.getMap(); String context = map.getFSM().getContext(); String mapName = map.getName(); String stateName = state.getClassName(); String transName = transition.getName(); List<SmcParameter> parameters = transition.getParameters(); List<SmcGuard> guards = transition.getGuards(); boolean nullCondition = false; Iterator<SmcParameter> pit; Iterator<SmcGuard> git; SmcGuard guard; _source.println(); _source.print(_indent); _source.print(" Public Overrides Sub "); // If this is the Default transition, then change its // name to "Default_" because Default is a VB keyword. if (transName.equals("Default") == true) { _source.print("Default_"); } else { _source.print(transName); } _source.print("(ByRef context As "); _source.print(context); _source.print("Context"); // Add user-defined parameters. for (SmcParameter param: parameters) { _source.print(", "); param.accept(this); } _source.println(")"); _source.println(); // Generate the ctxt local variable if needed. if (transition.hasCtxtReference() == true) { _source.print(_indent); _source.print(" Dim ctxt As "); _source.print(context); _source.println(" = context.Owner"); } // Generate the loopbackFlag for the Default state only // and only if at least one of the guards references a // non-nil end state. if (stateName.equals("Default") == true && transition.hasNonNilEndState() == true) { _source.print(_indent); _source.println( " Dim loopbackFlag As Boolean = False"); _source.println(); } // Output transition to debug stream. if (Smc.isDebug() == true) { String sep; _source.println("#If TRACE Then"); _source.print(_indent); _source.println(" Trace.WriteLine( _"); _source.print(_indent); _source.print(" \"TRANSITION : "); _source.print(mapName); _source.print("."); _source.print(stateName); _source.print("."); _source.print(transName); _source.print("("); for (pit = parameters.iterator(), sep = ""; pit.hasNext() == true; sep = ", ") { _source.print(sep); (pit.next()).accept(this); } _source.print(")"); _source.println("\")"); _source.println("#End If"); _source.println(); } // Loop through the guards and print each one. for (git = guards.iterator(), _guardIndex = 0, _guardCount = guards.size(); git.hasNext() == true; ++_guardIndex) { guard = git.next(); // Count up the guards with no condition. if (guard.getCondition().length() == 0) { nullCondition = true; } guard.accept(this); } // If all guards have a condition, then create a final // "else" clause which passes control to the default // transition. Pass all arguments into the default // transition. if (_guardIndex > 0 && nullCondition == false) { _source.println(); _source.print(_indent); _source.println(" Else"); _source.print(_indent); _source.print(" MyBase."); _source.print(transName); _source.print("(context"); for (SmcParameter param: parameters) { _source.print(", "); _source.print(param.getName()); } _source.println(")"); _source.print(_indent); _source.println(" End If"); } // Need to add a final newline after a multiguard block. else if (_guardCount > 1) { _source.print(_indent); _source.println(" End If"); _source.println(); } _source.print(_indent); _source.println(" End Sub"); 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 indent1; 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(); // 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); } 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) { indent1 = _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(" Then"); } 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(" ElseIf "); _source.print(condition); _source.println(" _"); _source.print(_indent); _source.println(" Then"); } else { // This is not the first transition and it has // no condition. _source.println(); _source.print(_indent); _source.println(" Else"); } } else { // There is only one guard. Does this guard have // a condition? if (condition.length() == 0) { // No. This is a plain, old. vanilla transition. indent1 = _indent + " "; } else { // Yes there is a condition. indent1 = _indent + " "; _source.print(_indent); _source.print(" If "); _source.print(condition); _source.println(" _"); _source.print(_indent); _source.println(" Then"); } } // 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 (actions.size() == 0) { fqEndStateName = endStateName; } // 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. else if (loopbackFlag == true) { fqEndStateName = "endState"; _source.print(_indent); _source.print("Dim "); _source.print(fqEndStateName); _source.print(" As "); _source.print(context); _source.println("State = context.State"); _source.println(); } else { fqEndStateName = endStateName; } // Decide if runtime loopback checking must be done. if (defaultFlag == true && transType != Smc.TRANS_POP && loopbackFlag == false) { _source.print(indent1); _source.print("If context.State.Name = "); _source.print(fqEndStateName); _source.println(".Name _"); _source.print(indent1); _source.println("Then"); _source.print(indent1); _source.println(" loopbackFlag = True");
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -