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

📄 smccsharpgenerator.java

📁 SMC takes a state machine stored in a .sm file and generates a State pattern in twelve programming l
💻 JAVA
📖 第 1 页 / 共 5 页
字号:
        // Declare the inner state class.        _source.println();        _source.print(_indent);        _source.print("        internal class ");        _source.print(mapName);        _source.print("_");        _source.print(stateName);        _source.println(" :");        _source.print(_indent);        _source.print("            ");        _source.print(mapName);        _source.println("_Default");        _source.print(_indent);        _source.println("        {");        // Generate the Transitions property if reflection is on.        if (Smc.isReflection() == true)        {            _source.print(_indent);            _source.println(                "        //-------------------------------------------------------");            _source.print(_indent);            _source.println("        // Properties.");            _source.print(_indent);            _source.println("        //");            _source.println();            _source.print(_indent);            _source.print("            ");            _source.println(                "public override IDictionary Transitions");            _source.print(_indent);            _source.println("            {");            _source.print(_indent);            _source.println("                get");            _source.print(_indent);            _source.println("                {");            _source.print(_indent);            _source.println(                "                    return (_transitions);");            _source.print(_indent);            _source.println("                }");            _source.print(_indent);            _source.println("            }");            _source.println();        }        // Add the constructor.        _source.print(_indent);        _source.println(            "        //-------------------------------------------------------");        _source.print(_indent);        _source.println("        // Member methods.");        _source.print(_indent);        _source.println("        //");        _source.println();        _source.print(_indent);        _source.print("            internal ");        _source.print(mapName);        _source.print("_");        _source.print(stateName);        _source.println("(string name, int id) :");        _source.print(_indent);        _source.println("                base (name, id)");        _source.print(_indent);        _source.println("            {}");        // Add the Entry() and Exit() methods if this state        // defines them.        actions = state.getEntryActions();        if (actions != null && actions.isEmpty() == false)        {            _source.println();            _source.print(_indent);            _source.print("            ");            _source.print(                "protected internal override void Entry(");            _source.print(context);            _source.println("Context context)");            _source.print(_indent);            _source.println("            {");            // Declare the "ctxt" local variable.            _source.print(_indent);            _source.print("                ");            _source.print(context);            _source.println(" ctxt = context.Owner;");            _source.println();            // Generate the actions associated with this code.            indent2 = _indent;            _indent = _indent + "                ";            for (SmcAction action: actions)            {                action.accept(this);            }            _indent = indent2;            // End of the Entry() method.            _source.print(_indent);            _source.println("                return;");            _source.print(_indent);            _source.println("            }");        }        actions = state.getExitActions();        if (actions != null && actions.isEmpty() == false)        {            _source.println();            _source.print(_indent);            _source.print("            ");            _source.print(                "protected internal override void Exit(");            _source.print(context);            _source.println("Context context)");            _source.print(_indent);            _source.println("            {");            // Declare the "ctxt" local variable.            _source.print(_indent);            _source.print("                ");            _source.print(context);            _source.println(" ctxt = context.Owner;");            _source.println();            // Generate the actions associated with this code.            indent2 = _indent;            _indent = _indent + "                ";            for (SmcAction action: actions)            {                action.accept(this);            }            // End of the Exit() method.            _source.print(_indent);            _source.println("                return;");            _source.print(_indent);            _source.println("            }");        }        // Have each transition generate its code.        indent2 = _indent;        _indent = _indent + "            ";        for (SmcTransition trans: state.getTransitions())        {            trans.accept(this);        }        _indent = indent2;        // If reflection is on, then generate the transitions        // map.        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>();            }            _source.println();            _source.print(_indent);            _source.println(                "        //-------------------------------------------------------");            _source.print(_indent);            _source.println("        // Member data.");            _source.print(_indent);            _source.println("        //");            _source.println();            _source.print(_indent);            _source.println(                "            //---------------------------------------------------");            _source.print(_indent);            _source.println("            // Statics.");            _source.print(_indent);            _source.println("            //");            _source.print(_indent);            _source.print("            ");            _source.println(                "new private static IDictionary _transitions;");            _source.println();            _source.print(_indent);            _source.print("            static ");            _source.print(mapName);            _source.print("_");            _source.print(stateName);            _source.println("()");            _source.print(_indent);            _source.println("            {");            _source.print(_indent);            _source.print("                ");            _source.println("_transitions = new Hashtable();");            // Now place the transition names into the list.            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("_transitions.Add(\"");                _source.print(transName);                _source.print("\", ");                _source.print(transDefinition);                _source.println(");");            }            _source.print(_indent);            _source.println("            }");        }        // End of state declaration.        _source.print(_indent);        _source.println("        }");        return;    } // end of visit(SmcState)    public void visit(SmcTransition transition)    {        SmcState state = transition.getState();        SmcMap map = state.getMap();        String context = map.getFSM().getContext();        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<SmcGuard> git;        SmcGuard guard;        _source.println();        _source.print(_indent);        _source.print("protected internal override void ");        _source.print(transName);        _source.print("(");        _source.print(context);        _source.print("Context context");        // Add user-defined parameters.        for (SmcParameter param: parameters)        {            _source.print(", ");            param.accept(this);        }        _source.println(")");        _source.print(_indent);        _source.println("{");        // Almost all transitions have a "ctxt" local variable.        if (transition.hasCtxtReference() == true)        {            _source.println();            _source.print(_indent);            _source.print("    ");            _source.print(context);            _source.println(" ctxt = context.Owner;");            _source.println();        }        // Output transition to debug stream.        if (Smc.isDebug() == true)        {            String sep;            _source.println();            _source.println("#if TRACE");            _source.print(_indent);            _source.println("    Trace.WriteLine(");            _source.print(_indent);            _source.print(                "        \"TRANSITION   : ");            _source.print(mapName);            _source.print(".");            _source.print(stateName);            _source.print(".");            _source.print(transName);            // Output the transition parameters.            _source.print("(");            for (SmcParameter param: parameters)            {                _source.print(", ");                param.accept(this);            }            _source.print(")");            _source.println("\");");            _source.println("#endif");            _source.println();        }        // Loop through the guards and print each one.        _guardIndex = 0;        _guardCount = guards.size();        for (git = guards.iterator();             git.hasNext() == true;             ++_guardIndex)        {            guard = git.next();            // Count up the 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. Pass all arguments into the default        // transition.        if (_guardIndex > 0 && nullCondition == false)        {            // If there was only one guard, then we need to close            // of its body.            if (_guardCount == 1)            {                _source.print(_indent);                _source.println("}");            }            _source.print(_indent);            _source.print("    else");            _source.print(_indent);            _source.println("    {");            // Call the super class' transition method using            // the "base" keyword and not the class name.            _source.print(_indent);            _source.print("        base.");            _source.print(transName);            _source.print("(context");            for (SmcParameter param: parameters)            {                _source.print(", ");                _source.print(param.getName());            }            _source.println(");");            _source.print(_indent);            _source.println("    }");            _source.println();        }        // Need to add a final newline after a multiguard block.        else if (_guardCount > 1)

⌨️ 快捷键说明

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