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

📄 smcluagenerator.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(" = ");                _source.print(transDefinition);                _source.println(",");            }            _source.println("}");        }        // Have each state now generate its code. Each state        // class is an inner class.        for (SmcState state: states)        {            state.accept(this);        }        return;    } // end of visit(SmcMap)    public void visit(SmcState state)    {        SmcMap map = state.getMap();        String mapName = map.getName();        String stateName = state.getClassName();        List<SmcAction> actions;        String indent2;        // Declare the inner state class.        _source.println();        _source.print(mapName);        _source.print('.');        _source.print(stateName);        _source.print(" = ");        _source.print(mapName);        _source.print(".Default:new('");        _source.print(mapName);        _source.print('.');        _source.print(stateName);        _source.print("', ");        _source.print(map.getNextStateId());        _source.println(")");        // Add the Entry() and Exit() member functions if this        // state defines them.        actions = state.getEntryActions();        if (actions != null && actions.size() > 0)        {            _source.println();            _source.print("function ");            _source.print(mapName);            _source.print('.');            _source.print(stateName);            _source.println(":Entry (fsm)");            // Declare the "ctxt" local variable.            _source.println("    local ctxt = fsm:getOwner()");            // Generate the actions associated with this code.            indent2 = _indent;            _indent = _indent + "    ";            for (SmcAction action: actions)            {                action.accept(this);            }            _indent = indent2;            // End the Entry() member function with a return.            _source.println("end");        }        actions = state.getExitActions();        if (actions != null && actions.size() > 0)        {            _source.println();            _source.print("function ");            _source.print(mapName);            _source.print('.');            _source.print(stateName);            _source.println(":Exit (fsm)");            // Declare the "ctxt" local variable.            _source.print(_indent);            _source.println("    local ctxt = fsm:getOwner()");            // Generate the actions associated with this code.            indent2 = _indent;            _indent = _indent + "    ";            for (SmcAction action: actions)            {                action.accept(this);            }            _indent = indent2;            // End the Exit() member function with a return.            _source.print(_indent);            _source.println("end");        }        // Have each transition generate its code.        for (SmcTransition transition: state.getTransitions())        {            transition.accept(this);        }        // If -reflect was specified, then generate the        // _transitions table.        if (Smc.isReflection() == true)        {            List<SmcTransition> allTransitions =                map.getFSM().getTransitions();            List<SmcTransition> stateTransitions =                state.getTransitions();            SmcState defaultState = map.getDefaultState();            List<SmcTransition> defaultTransitions;            String transName;            int transDefinition;            // Initialize the default transition list to all the            // default state's transitions.            if (defaultState != null)            {                defaultTransitions =                    defaultState.getTransitions();            }            else            {                defaultTransitions =                    new ArrayList<SmcTransition>();            }            // Now output the transition collection's            // initialization.            _source.println();            _source.print(mapName);            _source.print(".");            _source.print(stateName);            _source.println("._transitions = {");            // Now place all transition names and states into the            // map.            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("    ");                _source.print(transName);                _source.print(" = ");                _source.print(transDefinition);                _source.println(",");            }            _source.println("}");        }        return;    } // end of visit(SmcState)    public void visit(SmcTransition transition)    {        SmcState state = transition.getState();        SmcMap map = state.getMap();        String packageName = map.getFSM().getPackage();        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;        String indent2;        _source.println();        _source.print("function ");        _source.print(mapName);        _source.print('.');        _source.print(stateName);        _source.print(':');        _source.print(transName);        _source.print(" (fsm");        // Add user-defined parameters.        for (SmcParameter param: parameters)        {            _source.print(", ");            _source.print(param.getName());        }        _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.println("    local ctxt = fsm:getOwner()");        }        // Output transition to debug stream.        if (Smc.isDebug() == true)        {            String sep;            _source.println(                "    if fsm:getDebugFlag() then");            _source.print(                "        fsm:getDebugStream():write(\"TRANSITION   : ");            if (packageName != null && packageName.length() > 0)            {                _source.print(packageName);                _source.print(".");            }            _source.print(mapName);            _source.print(".");            _source.print(stateName);            _source.print(":");            _source.print(transName);            _source.print("(");            for (pit = parameters.iterator(), sep = "";                 pit.hasNext() == true;                 sep = ", ")            {                String trArgName =(pit.next()).getName();                _source.print(sep);                _source.print(trArgName);                _source.print("=\" .. tostring(");                _source.print(trArgName);                _source.print(") .. \"");            }            _source.print(")");            _source.println("\\n\")");            _source.println("    end");        }        // Loop through the guards and print each one.        indent2 = _indent;        _indent = _indent + "    ";        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);        }        _indent = indent2;        // 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.print(_indent);            _source.println("    else");            // Call the super class' transition method            _source.print(_indent);            _source.print("        ");            _source.print(mapName);            _source.print(".Default:");            _source.print(transName);            _source.print("(fsm");            // Add user-defined parameters.            for (SmcParameter param: parameters)            {                _source.print(", ");                _source.print(param.getName());            }            _source.println(")");            _source.print(_indent);            _source.println("    end");        }        // Need to add a final newline after a multiguard block.        else if (_guardCount > 1)        {            _source.print(_indent);            _source.println("    end");        }        _source.println("end");        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.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_STATE) == false)        {            endStateName = scopeStateName(endStateName, mapName);        }        // Qualify the state and push state names as well.        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)        {            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);

⌨️ 快捷键说明

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