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

📄 smccgenerator.java

📁 SMC takes a state machine stored in a .sm file and generates a State pattern in twelve programming l
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
                    _source.print(stateName);                    _source.print("_");                    _source.print(transName);                    _source.print(" ");                    _source.print(mapName);                    _source.print("_DefaultState_");                    _source.println(transName);                }                _source.print("#undef ");                _source.print(mapName);                _source.print("_DefaultState_");                _source.println(transName);                trans.accept(this);            }        }        // Have each state now generate its code.        for (SmcState state: map.getStates())        {            stateName = state.getInstanceName();            state.accept(this);            _source.println();            _source.print("const struct ");            _source.print(context);            _source.print("State ");            _source.print(mapName);            _source.print("_");            _source.print(stateName);            _source.print(" = { POPULATE_STATE(");            _source.print(mapName);            _source.print("_");            _source.print(stateName);            _source.print("), \"");            _source.print(mapName);            _source.print("_");            _source.print(stateName);            _source.print("\", ");            _source.print(                Integer.toString(map.getNextStateId()));            _source.println(" };");        }        return;    } // end of visit(SmcMap)    public void visit(SmcState state)    {        SmcMap map = state.getMap();        String packageName = map.getFSM().getPackage();        String context = map.getFSM().getContext();        String mapName = map.getName();        String instanceName = state.getInstanceName();        String indent2;        List<SmcAction> actions;        // If a package has been specified,        if (packageName != null && packageName.length() > 0)        {            context = packageName + "_" + context;            mapName = packageName + "_" + mapName;        }        _context = context;        // Add the Entry() and Exit() methods if this state        // defines them.        actions = state.getEntryActions();        if (actions != null && actions.isEmpty() == false)        {            _source.println();            _source.print("#undef ");            _source.print(mapName);            _source.print("_");            _source.print(instanceName);            _source.println("_Entry");            _source.print("void ");            _source.print(mapName);            _source.print("_");            _source.print(instanceName);            _source.print("_Entry(struct ");            _source.print(context);            _source.println("Context *fsm)");            _source.println("{");            // Declare the "ctxt" local variable.            _source.print("    struct ");            _source.print(context);            _source.println(" *ctxt = getOwner(fsm);");            _source.println();            // Generate the actions associated with this code.            indent2 = _indent;            _indent = _indent + "    ";            for (SmcAction action: actions)            {                action.accept(this);            }            _indent = indent2;            // End the Entry() method.            _source.println("}");        }        actions = state.getExitActions();        if (actions != null && actions.isEmpty() == false)        {            _source.println();            _source.print("#undef ");            _source.print(mapName);            _source.print("_");            _source.print(instanceName);            _source.println("_Exit");            _source.print("void ");            _source.print(mapName);            _source.print("_");            _source.print(instanceName);            _source.print("_Exit(struct ");            _source.print(context);            _source.println("Context *fsm)");            _source.println("{");            // Declare the "ctxt" local variable.            _source.print("    struct ");            _source.print(context);            _source.println(" *ctxt = getOwner(fsm);");            _source.println();            // Generate the actions associated with this code.            indent2 = _indent;            _indent = _indent + "    ";            for (SmcAction action: actions)            {                action.accept(this);            }            _indent = indent2;            // End the Entry() method.            _source.println("}");        }        // Have the transitions generate their code.        for (SmcTransition transition: state.getTransitions())        {            transition.accept(this);        }        return;    } // end of visit(SmcState)    public void visit(SmcTransition transition)    {        SmcState state = transition.getState();        SmcMap map = state.getMap();        String packageName = map.getFSM().getPackage();        String context = map.getFSM().getContext();        String mapName = map.getName();        String stateName = state.getInstanceName();        String transName = transition.getName();        boolean defaultFlag = false;        boolean nullCondition = false;        List<SmcGuard> guards = transition.getGuards();        Iterator<SmcGuard> git;        SmcGuard guard;        // If a package has been specified,        if (packageName != null && packageName.length() > 0)        {            context = packageName + "_" + context;            mapName = packageName + "_" + mapName;        }        // Set a flag to denote if this is a Default state        // transition.        if (stateName.equals("DefaultState") == true)        {            // stateName = state.getClassName();            defaultFlag = true;        }        if (defaultFlag == false)        {            _source.println();            _source.print("#undef ");            _source.print(mapName);            _source.print("_");            _source.print(stateName);            _source.print("_");            _source.println(transName);        }        _source.print("static void ");        _source.print(mapName);        _source.print("_");        _source.print(stateName);        _source.print("_");        _source.print(transName);        _source.print("(struct ");        _source.print(context);        _source.print("Context *fsm");        // Add user-defined parameters.        for (SmcParameter parameter: transition.getParameters())        {            _source.print(", ");            parameter.accept(this);        }        _source.println(")");        _source.println("{");        // All transitions have a "ctxt" local variable.        // 8/14/2003:        // Do this only if there are any transition actions or        // guard conditions which reference it.        if (transition.hasCtxtReference() == true)        {            _source.print("    struct ");            _source.print(context);            _source.println("* ctxt = getOwner(fsm);");        }        if (defaultFlag == true)        {            _source.println("    int loopbackFlag = 0;");        }        // ANSI C requires all local variables be declared        // at the code block's start before any control        // statements. If this transition appears only once        // in the state, has at least one action and it is a        // loopback and debugging is on, then visit(SmcGuard)        // will generate a local variable declaration after the        // debug if clause - an ANSI syntax error.        // So we need to check if this transition meets that        // condition and generate the local variable declaration        // here rather than in visit(SmcGuard).        //        // Note: when guard count is > 1, then the guard code        // is placed into an if or else block - and so the        // end state variable will appear at the start of that        // block, nullifying the debug if clauses affect.        _guardCount = guards.size();        if (_guardCount == 1)        {            guard = (SmcGuard) guards.get(0);            if (guard.getActions().isEmpty() == false &&                isLoopback(guard.getTransType(),                           stateName,                           guard.getEndState()) == true)            {                _source.print("    const struct ");                _source.print(context);                _source.println(                    "State* EndStateName = getState(fsm);");            }        }        _source.println();        // Print the transition to the verbose log.        if (Smc.isDebug() == true)        {            Iterator<SmcParameter> pit;            SmcParameter param;            String sep;            _source.println("    if (getDebugFlag(fsm) != 0) {");            _source.print("        TRACE(\"TRANSITION   : ");            _source.print(mapName);            _source.print("_");            _source.print(stateName);            _source.print(".");            _source.print(transName);            _source.print("(");            for (pit = transition.getParameters().iterator(),                     sep = "";                 pit.hasNext() == true;                 sep = ", ")            {                param = pit.next();                _source.print(sep);                _source.print(param.getName());            }            _source.println(")\\n\\r\");");            _source.println("    }");        }        // Loop through the guards and print each one.        for (git = guards.iterator(), _guardIndex = 0;             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.println("    }");            }            _source.println("    else {");            _source.print("        ");            _source.print(mapName);            _source.print("_DefaultState_");            _source.print(transName);            _source.print("(fsm");            // Output user-defined parameters.            for (SmcParameter param: transition.getParameters())            {                _source.print(", ");                _source.print(param.getName());            }            _source.println(");");            _source.println("    }");        }        else if (_guardCount > 1)        {            _source.println();        }        _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 packageName = map.getFSM().getPackage();        String context = map.getFSM().getContext();        String mapName = map.getName();        String stateName = state.getInstanceName();        int transType = guard.getTransType();        boolean defaultFlag =            stateName.equalsIgnoreCase("DefaultState");        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 a package has been specified,        if (packageName != null && packageName.length() > 0)        {            context = packageName + "_" + context;            mapName = packageName + "_" + mapName;        }        _context = context;        // 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);        if (packageName != null && packageName.length() > 0)        {            pushStateName =                "&" + packageName + "_" + pushStateName;        }        else        {            pushStateName = "&" + pushStateName;        }        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 + "        ";            // 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(") {");            }            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(") {");            }            else            {                // This is not the first transition and it has                // no condition.                _source.println();                _source.print(_indent);                _source.println("    else {");            }        }        else        {

⌨️ 快捷键说明

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