📄 smccgenerator.java
字号:
// There is only one guard. Does this guard have a // condition. if (condition.length() == 0) { // Actually, this is a plain, old, vaniila // transition. indent2 = _indent + " "; } else { // Yes, there is a condition. _source.print(_indent); _source.print(" if ("); _source.print(condition); _source.println(") {"); indent2 = _indent + " "; } } // Now that the necessary conditions are in place, it's // time to dump out the transitions 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 && endStateName.length() > 0) { fqEndStateName = endStateName; } else if (actions.size() > 0) { // Save away the current state if this is a loopback // transition. Storing current state allows the // current 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 so. if (loopbackFlag == true) { fqEndStateName = "EndStateName"; // Generate this declaration only if this // transition has multiple guards. If this // is the only guard, then this local variable // declaration will appear after the first // control statement - which is an ANSI C // syntax error. // If there is only one guard, then this code // is generated by visit(SmcTransition) before // the debug code is generated. // If there are multiple guards, then this code // appears at the start of an if, else if or else // code block which is acceptable ANSI C. if (_guardCount > 1) { _source.print(indent2); _source.print("const struct "); _source.print(context); _source.print("State* "); _source.print(fqEndStateName); _source.println(" = getState(fsm);"); _source.println(); } } else { fqEndStateName = endStateName; } } // Decide if runtime loopback checking must be done. if (defaultFlag == true && transType == Smc.TRANS_SET && loopbackFlag == false) { _source.print(_indent); _source.print( " if (strcmp(getName(getState(fsm)), "); _source.print("getName("); _source.print(endStateName); _source.println(")) == 0) {"); _source.print(_indent); _source.println(" loopbackFlag = 1;"); _source.print(_indent); _source.println(" }"); } // Before doing anything else, perform the current // state's exit actions. // v. 1.0, beta 3: Not any more. The exit actions are // executed only if 1) this is a standard, non-loopback // transition or 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 == 0) {"); } _source.print(indent4); _source.println( "EXIT_STATE(getState(fsm));"); if (transType == Smc.TRANS_SET && defaultFlag == true) { _source.print(indent2); _source.println("}"); } } if (actions.isEmpty() == false) { // Now that we are in the transition, clear the // current state. _source.print(indent2); _source.println("clearState(fsm);"); } // Dump out this transition's actions. if (actions.isEmpty() == true) { if (condition.length() > 0) { _source.print(indent2); _source.println("/* No actions. */"); } } else { indent4 = _indent; _indent = indent2; for (SmcAction action: actions) { action.accept(this); } _indent = indent4; } indent3 = indent2; // Print the setState() call, if necessary. Do NOT // generate the set state 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 && (actions.isEmpty() == false || loopbackFlag == false)) { _source.print(indent3); _source.print("setState(fsm, "); _source.print(fqEndStateName); _source.println(");"); } else if (transType == Smc.TRANS_PUSH) { // Set the end state so that it can be pushed // onto the state stack. But only do so if a clear // state was done. if (loopbackFlag == false || actions.isEmpty() == false) { _source.print(indent3); _source.print("setState(fsm, "); _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 == 0) {"); } else { indent4 = indent3; } _source.print(indent4); _source.println( "ENTRY_STATE(getState(fsm));"); if (defaultFlag == true) { _source.print(indent3); _source.println("}"); } } _source.print(indent3); _source.print("pushState(fsm, "); _source.print(pushStateName); _source.println(");"); } else if (transType == Smc.TRANS_POP) { _source.print(indent3); _source.println("popState(fsm);"); } // Perform the new state's entry actions. // v. 1.0, beta 3: Not any more. The entry actions are // executed only if 1) this is a standard, non-loopback // transition or a push transition. if ((transType == Smc.TRANS_SET && loopbackFlag == false) || transType == Smc.TRANS_PUSH) { // 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 == 0) {"); } else { indent4 = indent2; } _source.print(indent4); _source.println( "ENTRY_STATE(getState(fsm));"); if (transType == Smc.TRANS_SET && defaultFlag == true) { _source.print(indent2); _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 && endStateName.length() > 0) { String popArgs = guard.getPopArgs(); _source.print(indent2); _source.print(context); _source.print("Context_"); _source.print(endStateName); _source.print("(fsm"); // Output any and all pop arguments. if (popArgs.length() > 0) { _source.print(", "); _source.print(popArgs); } _source.println(");"); } // If this is a guarded transition, it will be necessary // to close off the "if" body. DON'T PRINT A NEW LINE! // Why? Because an "else" or "else if" may follow and we // won't know until we go back to the transition source // generator whether all clauses have been done. if (_guardCount > 1) { _source.print(_indent); _source.print(" }"); } return; } // end of visit(SmcGuard) public void visit(SmcAction action) { String name = action.getName(); // Need to distinguish between FSMContext actions and // application class actions. If the action is // "emptyStateStack", then pass it to the context. // Otherwise, let the application class handle it. _source.print(_indent); if (name.equals("emptyStateStack") == true) { _source.print(name); _source.print("(fsm"); } else { _source.print(_context); _source.print("_"); _source.print(name); _source.print("(ctxt"); } for (String arg: action.getArguments()) { if (arg.equals("") == false) { _source.print(", "); _source.print(arg); } } _source.println(");"); return; } // end of visit(SmcAction) public void visit(SmcParameter parameter) { _source.print(parameter.getType()); _source.print(" "); _source.print(parameter.getName()); return; } // end of visit(SmcParameter) // Scope the state name. If the state is unscoped, then // return "<mapName>.<stateName>". If the state named // contains the scope string "::", replace that with a "_". protected String scopeStateName(String stateName, String mapName) { int index; StringWriter retval = new StringWriter(); index = stateName.indexOf("::"); if (index < 0) { retval.write(mapName); retval.write("_"); retval.write(stateName); } else { retval.write(stateName.substring(0, index)); retval.write('_'); retval.write(stateName.substring(index + 2)); } return (retval.toString()); } // end of scopeStateName(String, String)//---------------------------------------------------------------// Member data// String _context;} // end of class SmcCGenerator//// CHANGE LOG// $Log: SmcCGenerator.java,v $// Revision 1.15 2008/03/21 14:03:16 fperrad// refactor : move from the main file Smc.java to each language generator the following data :// - the default file name suffix,// - the file name format for the generated SMC files//// Revision 1.14 2008/02/08 08:46:02 fperrad// C : optimize footprint when no Entry action or no Exit action//// Revision 1.13 2008/02/04 10:26:51 fperrad// Don't generate cuddled else//// Revision 1.12 2007/12/28 12:34:41 cwrapp// Version 5.0.1 check-in.//// Revision 1.11 2007/11/19 18:48:54 fperrad// fix C generation :// default state must be always 'DefaultState' (previously 'Default')//// Revision 1.10 2007/08/05 14:50:54 cwrapp// Version 5.0.1 check-in. See net/sf/smc/CODE_README.txt for more information.//// Revision 1.9 2007/08/05 14:26:25 cwrapp// Version 5.0.1 check-in. See net/sf/smc/CODE_README.txt for more informaiton.//// Revision 1.8 2007/07/16 06:13:21 fperrad// + Added the generation of a DO NOT EDIT comment//// Revision 1.7 2007/02/21 13:53:57 cwrapp// Moved Java code to release 1.5.0//// Revision 1.6 2007/01/15 00:23:50 cwrapp// Release 4.4.0 initial commit.//// Revision 1.5 2006/09/16 15:04:28 cwrapp// Initial v. 4.3.3 check-in.//// Revision 1.4 2006/07/11 18:11:41 cwrapp// Added support for new -headerd command line option.//// Revision 1.3 2006/04/22 12:45:26 cwrapp// Version 4.3.1//// Revision 1.2 2005/11/07 19:34:54 cwrapp// Changes in release 4.3.0:// New features://// + Added -reflect option for Java, C#, VB.Net and Tcl code// generation. When used, allows applications to query a state// about its supported transitions. Returns a list of transition// names. This feature is useful to GUI developers who want to// enable/disable features based on the current state. See// Programmer's Manual section 11: On Reflection for more// information.//// + Updated LICENSE.txt with a missing final paragraph which allows// MPL 1.1 covered code to work with the GNU GPL.//// + Added a Maven plug-in and an ant task to a new tools directory.// Added Eiten Suez's SMC tutorial (in PDF) to a new docs// directory.//// Fixed the following bugs://// + (GraphViz) DOT file generation did not properly escape// double quotes appearing in transition guards. This has been// corrected.//// + A note: the SMC FAQ incorrectly stated that C/C++ generated// code is thread safe. This is wrong. C/C++ generated is// certainly *not* thread safe. Multi-threaded C/C++ applications// are required to synchronize access to the FSM to allow for// correct performance.//// + (Java) The generated getState() method is now public.//// Revision 1.1 2005/06/16 18:11:01 fperrad// Added C, Perl & Ruby generators.////
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -