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

📄 smccppgenerator.java

📁 SMC takes a state machine stored in a .sm file and generates a State pattern in twelve programming l
💻 JAVA
📖 第 1 页 / 共 4 页
字号:
                for (pit = transition.getParameters().iterator(),                         sep = "";                     pit.hasNext() == true;                     sep = ", ")                {                    _source.print(sep);                    (pit.next()).accept(this);                }                _source.println(")\"");                _source.print(_indent);                _source.println("            << std::endl;");            }            _source.print(_indent);            _source.println("    }");            _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 number of 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.        if (_guardIndex > 0 && nullCondition == false)        {            // If there is only one transition definition, then            // close off the guard.            if (_guardCount == 1)            {                _source.print(_indent);                _source.println("    }");            }            _source.print(_indent);            _source.println("    else");            _source.print(_indent);            _source.println("    {");            _source.print(_indent);            _source.print("         ");            _source.print(mapName);            _source.print("_Default::");            _source.print(transName);            _source.print("(context");            // Output user-defined parameters.            for (SmcParameter param: transition.getParameters())            {                _source.print(", ");                _source.print(param.getName());            }            _source.println(");");            _source.print(_indent);            _source.println("    }");        }        else if (_guardCount > 1)        {            _source.println();        }        _source.println();        _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();        // 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") == false &&            endStateName.indexOf("::") < 0)        {            endStateName = mapName + "::" + endStateName;        }        // Qualify the state name as well.        if (stateName.indexOf("::") < 0)        {            stateName = mapName + "::" + stateName;        }        loopbackFlag =            isLoopback(transType, stateName, endStateName);        // v. 2.0.2: If the push state is not fully-qualified,        // then prepend the current map's name and make if        // fully-qualified.        if (pushStateName != null &&            pushStateName.length() > 0 &&            pushStateName.indexOf("::") < 0)        {            pushStateName = mapName + "::" + pushStateName;        }        // 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 + "        ";            // More than one guard. Is this the first guard?            if (_guardIndex == 0 && condition.length() > 0)            {                // Yes, this is the first. This means an                // "if" should be used for this condition.                _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" for                // the condition.                _source.println();                _source.print(_indent);                _source.print("    else if (");                _source.print(condition);                _source.println(")");                _source.println(_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("    {");            }        }        else        {            // 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(")");                _source.print(_indent);                _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";                _source.print(indent2);                _source.print(context);                _source.print("State& ");                _source.print(fqEndStateName);                _source.println(" = context.getState();");                _source.println();            }            else            {                fqEndStateName = endStateName;            }        }        // Decide if runtime loopback checking must be done.        if (defaultFlag == true &&            transType == Smc.TRANS_SET &&            loopbackFlag == false)        {            _source.print(indent2);            _source.println(                "if (strcmp(context.getState().getName(), ");            _source.print(indent2);            _source.print("           ");            _source.print(fqEndStateName);            _source.println(".getName())");            _source.print(indent2);            _source.println("       == 0)");            _source.print(indent2);            _source.println("{");            _source.print(indent2);            _source.println("    loopbackFlag = true;");            _source.print(indent2);            _source.println("}");            _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 == false)");                _source.print(indent2);                _source.println("{");            }            _source.print(indent4);            _source.println(                "(context.getState()).Exit(context);");            if (transType == Smc.TRANS_SET &&                defaultFlag == true)            {                _source.print(indent2);                _source.println("}");                _source.println();            }        }        if (actions.size() > 0)        {            // Now that we are in the transition, clear the            // current state.            _source.print(indent2);            _source.println("context.clearState();");        }        // Dump out this transition's actions.        if (actions.size() == 0)        {            if (condition.length() > 0)            {                _source.print(indent2);                _source.println("// No actions.");            }            indent3 = indent2;        }        else        {            // v. 2.0.2: Place the actions inside a try/catch            // 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");                _source.print(indent2);                _source.println("{");                indent3 = indent2 + "    ";            }            else            {                indent3 = indent2;            }            indent4 = _indent;            _indent = indent3;            for (SmcAction action: actions)            {                action.accept(this);            }            _indent = indent4;        }        // 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.

⌨️ 快捷键说明

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