⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 smcscalagenerator.java

📁 SMC takes a state machine stored in a .sm file and generates a State pattern in twelve programming l
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
        {            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(") {");            }            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(") {");            }            else            {                // This is not the first transition and it has                // no condition.                _source.println();                _source.print(_indent);                _source.println("    else {");            }        }        // 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(") {");        }        // 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)        {            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(indent2);            _source.print("val ");            _source.print(fqEndStateName);            _source.println(" = context.getState()");        }        else        {            fqEndStateName = endStateName;        }        // Decide if runtime loopback checking must be done.        if (defaultFlag == true &&            transType != Smc.TRANS_POP &&            loopbackFlag == false)        {            _source.print(indent2);            _source.print("val loopbackFlag: Boolean = ");            _source.print("context.getState().getName() == ");            _source.print(fqEndStateName);            _source.println(".getName()");        }        _source.println();        // Dump out the exit actions - but only for the first        // guard.        // 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) {");            }            _source.print(indent4);            _source.println("context.getState().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.print(indent2);                _source.println("try {");                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 {");            }        }        // Print the setState() call, if necessary. Do NOT        // generate the set state it:        // 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.setState(");            _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.            if (loopbackFlag == false || hasActions == true)            {                _source.print(indent3);                _source.print("context.setState(");                _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) {");                }                else                {                    indent4 = indent3;                }                _source.print(indent4);                _source.println("context.getState().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.        // 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)        {            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) {");            }            _source.print(indent4);            _source.println("context.getState().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('}');        }        // 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.println();            _source.print(indent2);            _source.print("context.");            _source.print(endStateName);            _source.print("(");            // Output any and all pop arguments.            if (popArgs.length() > 0)            {                _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();        Iterator<String> it;        String sep;        // 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("context.");        }        else        {            _source.print("ctxt.");        }        _source.print(name);        _source.print("(");        for (it = action.getArguments().iterator(), sep = "";             it.hasNext() == true;             sep = ", ")        {            _source.print(sep);            _source.print(it.next());        }        _source.println(")");        return;    } // end of visit(SmcAction)    public void visit(SmcParameter parameter)    {        _source.print(parameter.getName());        _source.print(": ");        _source.print(parameter.getType());        return;    } // end of visit(SmcParameter)//---------------------------------------------------------------// Member data//} // end of class SmcScalaGenerator//// CHANGE LOG// $Log: SmcScalaGenerator.java,v $// Revision 1.4  2008/03/21 14:03:17  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.3  2008/02/06 09:34:24  fperrad// add getName()//// Revision 1.2  2008/02/06 09:07:03  fperrad// Scala 2.3 -> 2.6//// Revision 1.1  2008/02/04 10:32:49  fperrad// + Added Scala language generation.////

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -