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

📄 smcjavagenerator.java

📁 SMC takes a state machine stored in a .sm file and generates a State pattern in twelve programming l
💻 JAVA
📖 第 1 页 / 共 4 页
字号:
//// The contents of this file are subject to the Mozilla Public// License Version 1.1 (the "License"); you may not use this file// except in compliance with the License. You may obtain a copy// of the License at http://www.mozilla.org/MPL///// Software distributed under the License is distributed on an// "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or// implied. See the License for the specific language governing// rights and limitations under the License.//// The Original Code is State Machine Compiler (SMC).//// The Initial Developer of the Original Code is Charles W. Rapp.// Portions created by Charles W. Rapp are// Copyright (C) 2005 - 2007. Charles W. Rapp.// All Rights Reserved.//// Contributor(s)://   Eitan Suez contributed examples/Ant.//   (Name withheld) contributed the C# code generation and//   examples/C#.//   Francois Perrad contributed the Python code generation and//   examples/Python.//   Chris Liscio contributed the Objective-C code generation//   and examples/ObjC.//// RCS ID// $Id: SmcJavaGenerator.java,v 1.11 2008/03/21 14:03:16 fperrad Exp $//// CHANGE LOG// (See the bottom of this file.)//package net.sf.smc;import java.io.PrintStream;import java.util.ArrayList;import java.util.Iterator;import java.util.List;/** * Visits the abstract syntax tree, emitting Java code. * @see SmcElement * @see SmcCodeGenerator * @see SmcVisitor * * @author <a href="mailto:rapp@acm.org">Charles Rapp</a> */public final class SmcJavaGenerator    extends SmcCodeGenerator{//---------------------------------------------------------------// Member methods//    public SmcJavaGenerator(String srcfileBase)    {        super (srcfileBase, "{0}{1}Context.{2}", "java");    } // end of SmcJavaGenerator(String)    public void visit(SmcFSM fsm)    {        String rawSource = fsm.getSource();        String packageName = fsm.getPackage();        String context = fsm.getContext();        String startState = fsm.getStartState();        String accessLevel = fsm.getAccessLevel();        List<SmcMap> maps = fsm.getMaps();        List<SmcTransition> transitions;        Iterator<SmcParameter> pit;        String transName;        String javaState;        String separator;        int index;        List<SmcParameter> params;        // If the access level has not been set, then the default        // is "public".        if (accessLevel == null || accessLevel.length() == 0)        {            accessLevel = "public";        }        // If the access level is package, change it to        // /* package */        else if (accessLevel.equals("package") == true)        {            accessLevel = "/* package */";        }        // Dump out the raw source code, if any.        if (rawSource != null && rawSource.length() > 0)        {            _source.println(rawSource);            _source.println();        }        // If a package has been specified, generate the        // package statement now.        if (packageName != null && packageName.length() > 0)        {            _source.print("package ");            _source.print(packageName);            _source.println(";");            _source.println();        }        // Do user-specified imports now.        for (String imp: fsm.getImports())        {            _source.print("import ");            _source.print(imp);            _source.println(";");        }        // If the -g option was specified, then import the        // PrintStream class.        if (Smc.isDebug() == true)        {            _source.println("import java.io.PrintStream;");        }        if (Smc.isReflection() == true)        {            _source.println("import java.util.HashMap;");            _source.println("import java.util.Map;");        }        _source.println();        // The context clas contains all the state classes as        // inner classes, so generate the context first rather        // than last.        _source.print(accessLevel);        _source.print(" final class ");        _source.print(context);        _source.println("Context");        _source.println("    extends statemap.FSMContext");        if (Smc.isSerial() == true)        {            _source.println(                "    implements java.io.Serializable");        }        _source.println("{");        _source.println(            "//---------------------------------------------------------------");        _source.println("// Member methods.");        _source.println("//");        _source.println();        // Generate the context class' constructor.        _source.print("    public ");        _source.print(context);        _source.print("Context(");        _source.print(context);        _source.println(" owner)");        _source.println("    {");        _source.println("        super();");        _source.println();        _source.println("        _owner = owner;");        // The state name "map::state" must be changed to        // "map.state".        if ((index = startState.indexOf("::")) >= 0)        {            javaState =                startState.substring(0, index) +                "." +                startState.substring(index + 2);        }        else        {            javaState = startState;        }        _source.print("        setState(");        _source.print(javaState);        _source.println(");");        // Execute the start state's entry actions.        _source.print("        ");        _source.print(javaState);        _source.println(".Entry(this);");        _source.println("    }");        _source.println();        // Generate the second constructor which allows the        // initial state to be dynamically set. Overrides the        // %start specifier.        _source.print("    public ");        _source.print(context);        _source.print("Context(");        _source.print(context);        _source.print(" owner, ");        _source.print(context);        _source.println("State initState)");        _source.println("    {");        _source.println("        super();");        _source.println("        _owner = owner;");        _source.println("        setState(initState);");        _source.println("        initState.Entry(this);");        _source.println("    }");        _source.println();        // Generate the default transition methods.        // Get the transition list for the entire FSM.        transitions = fsm.getTransitions();        // Generate the transition methods.        for (SmcTransition trans: transitions)        {            if (trans.getName().equals("Default") == false)            {                _source.print("    public ");                // If the -sync flag was specified, then output                // the "synchronized" keyword.                if (Smc.isSynchronized() == true)                {                    _source.print("synchronized ");                }                _source.print("void ");                _source.print(trans.getName());                _source.print("(");                params = trans.getParameters();                for (pit = params.iterator(), separator = "";                     pit.hasNext() == true;                     separator = ", ")                {                    _source.print(separator);                    (pit.next()).accept(this);                }                _source.println(")");                _source.println("    {");                // Save away the transition name in case it is                // need in an UndefinedTransitionException.                _source.print("        _transition = \"");                _source.print(trans.getName());                _source.println("\";");                _source.print("        getState().");                _source.print(trans.getName());                _source.print("(this");                for (pit = params.iterator();                     pit.hasNext() == true;                    )                {                    _source.print(", ");                    _source.print((pit.next()).getName());                }                _source.println(");");                _source.println("        _transition = \"\";");                _source.println("        return;");                _source.println("    }");                _source.println();            }        }        // If serialization is turned on, then generate a        // setOwner method which allows the application class        // to restore its ownership of the FSM.        if (Smc.isSerial() == true)        {            // Also output the valueOf method in case developers            // want to serialize manually.            _source.print("    public ");            _source.print(context);            _source.println("State valueOf(int stateId)");            _source.println(                "        throws ArrayIndexOutOfBoundsException");            _source.println("    {");            _source.println(                "        return (_States[stateId]);");            _source.println("    }");            _source.println();        }        // getState() method.        _source.print("    public ");        _source.print(context);        _source.println("State getState()");        _source.println(            "        throws statemap.StateUndefinedException");        _source.println("    {");        _source.println("        if (_state == null)");        _source.println("        {");        _source.println(            "            throw(");        _source.println(            "                new statemap.StateUndefinedException());");        _source.println("        }");        _source.println();        _source.print("        return ((");        _source.print(context);        _source.println("State) _state);");        _source.println("    }");        _source.println();        // getOwner() method.        _source.print("    protected ");        _source.print(context);        _source.println(" getOwner()");        _source.println("    {");        _source.println("        return (_owner);");        _source.println("    }");        _source.println();        // setOwner() method.        _source.print("    public void setOwner(");        _source.print(context);        _source.println(" owner)");        _source.println("    {");        _source.println("        if (owner == null)");        _source.println("        {");        _source.println("            throw (");        _source.println("                new NullPointerException(");        _source.println("                    \"null owner\"));");        _source.println("        }");        _source.println("        else");        _source.println("        {");        _source.println("            _owner = owner;");        _source.println("        }");        _source.println();        _source.println("        return;");        _source.println("    }");        _source.println();        // If serialization is turned on, then output the        // writeObject and readObject methods.        if (Smc.isSerial() == true)        {            _source.print(                "    private void writeObject(");            _source.println(                "java.io.ObjectOutputStream ostream)");            _source.println(                "        throws java.io.IOException");            _source.println("    {");            _source.println(                "        int size =");            _source.print("            ");            _source.println(                "(_stateStack == null ? 0 : _stateStack.size());");            _source.println("        int i;");            _source.println();            _source.println(                "        ostream.writeInt(size);");            _source.println();            _source.println(                "        for (i = 0; i < size; ++i)");            _source.println("        {");            _source.println("            ostream.writeInt(");            _source.print("                ((");            _source.print(context);            _source.println(                "State) _stateStack.get(i)).getId());");            _source.println("        }");            _source.println();            _source.println(                "        ostream.writeInt(_state.getId());");            _source.println();            _source.println("        return;");            _source.println("    }");            _source.println();            _source.print("    private void readObject(");            _source.println(                "java.io.ObjectInputStream istream)");            _source.println(                "        throws java.io.IOException");            _source.println("    {");            _source.println("        int size;");            _source.println();            _source.println("        size = istream.readInt();");            _source.println();            _source.println("        if (size == 0)");            _source.println("        {");            _source.println("            _stateStack = null;");            _source.println("        }");            _source.println("        else");            _source.println("        {");            _source.println("            int i;");            _source.println();            _source.println(                "            _stateStack = new java.util.Stack();");            _source.println();            _source.println(                "            for (i = 0; i < size; ++i)");            _source.println("            {");            _source.print(                "                _stateStack.add(i, _States[");            _source.println("istream.readInt()]);");            _source.println("            }");            _source.println("        }");            _source.println();            _source.println(                "        _state = _States[istream.readInt()];");            _source.println();            _source.println("        return;");            _source.println("    }");            _source.println();        }        // Declare member data.        _source.println(            "//---------------------------------------------------------------");        _source.println("// Member data.");        _source.println("//");        _source.println();        _source.print("    transient private ");        _source.print(context);        _source.println(" _owner;");        // If serialization support is on, then create the state        // array.        if (Smc.isSerial() == true)        {            Iterator<SmcMap> mit;            SmcMap map;            String mapName;            Iterator<SmcState> stateIt;            SmcState state;

⌨️ 快捷键说明

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