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

📄 smcgroovygenerator.java

📁 SMC takes a state machine stored in a .sm file and generates a State pattern in twelve programming l
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
//// 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.//// Port to Groovy by Francois Perrad, francois.perrad@gadz.org// Copyright 2007, Francois Perrad.// 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, Perl code generation and examples/Perl,//   Ruby code generation and examples/Ruby, Lua code generation//   and examples/Lua.//   Chris Liscio contributed the Objective-C code generation//   and examples/ObjC.//// RCS ID// $Id: SmcGroovyGenerator.java,v 1.2 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 Groovy code. * @see SmcElement * @see SmcCodeGenerator * @see SmcVisitor * * @author Francois Perrad */public final class SmcGroovyGenerator    extends SmcCodeGenerator{//---------------------------------------------------------------// Member methods//    public SmcGroovyGenerator(String srcfileBase)    {        super (srcfileBase, "{0}{1}Context.{2}", "groovy");    } // end of SmcGroovyGenerator(String)    public void visit(SmcFSM fsm)    {        String rawSource = fsm.getSource();        String packageName = fsm.getPackage();        String context = fsm.getContext();        String startState = fsm.getStartState();        List<SmcMap> maps = fsm.getMaps();        List<SmcTransition> transitions;        Iterator<SmcParameter> pit;        String transName;        String javaState;        String separator;        int index;        List<SmcParameter> params;        _source.println("// DO NOT EDIT.");        _source.println(            "// generated by smc (http://smc.sourceforge.net/)");        _source.print("// from file : ");        _source.print(_srcfileBase);        _source.println(".sm");        // Dump out the raw source code, if any.        if (rawSource != null && rawSource.length() > 0)        {            _source.println();            _source.println(rawSource);        }        // If a package has been specified, generate the        // package statement now.        if (packageName != null && packageName.length() > 0)        {            _source.println();            _source.print("package ");            _source.println(packageName);        }        // Do user-specified imports now.        _source.println();        for (String imp: fsm.getImports())        {            _source.print("import ");            _source.println(imp);        }        _source.println();        // The context clas contains all the state classes as        // inner classes, so generate the context first rather        // than last.        _source.print("class ");        _source.print(context);        _source.print("Context extends statemap.FSMContext");        if (Smc.isSerial() == true)        {            _source.print(" implements Serializable");        }        _source.println(" {");        // Declare member data.        _source.println();        _source.println("    def owner");        _source.println();        // Generate the context class' constructor.        _source.print("    def ");        _source.print(context);        _source.println("Context (owner) {");        _source.println("        super()");        _source.println();        _source.println("        this.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("    def ");        _source.print(context);        _source.println("Context (owner, initState) {");        _source.println("        super()");        _source.println();        _source.println("        this.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)            {                // If the -sync flag was specified, then output                // the "synchronized" keyword.                if (Smc.isSynchronized() == true)                {                    _source.print("    synchronized void ");                }                else                {                    _source.print("    def ");                }                _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(") {");                // Save away the transition name in case it is                // need in an UndefinedTransitionException.                _source.print("        transition = '");                _source.print(trans.getName());                _source.println("'");                _source.print("        state.");                _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("    }");                _source.println();            }        }        // End of context class.        _source.println("}");        // Declare the base state class.        _source.println();        _source.print("private class ");        _source.print(context);        _source.println("State extends statemap.State {");        _source.println();        // Constructor.        _source.println("    def Entry (context) {}");        _source.println("    def Exit (context) {}");        _source.println();        // Generate the default transition definitions.        for (SmcTransition trans: transitions)        {            transName = trans.getName();            // Don't generate the Default transition here.            if (transName.equals("Default") == false)            {                _source.print("    def ");                _source.print(transName);                _source.print(" (context");                for (SmcParameter param: trans.getParameters())                {                    _source.print(", ");                    param.accept(this);                }                _source.println(") {");                // If this method is reached, that means that                // this transition was passed to a state which                // does not define the transition. Call the                // state's default transition method.                _source.println("        Default(context)");                _source.println("    }");                _source.println();            }        }        // Generate the overall Default transition for all maps.        _source.println("    def Default (context) {");        if (Smc.isDebug() == true)        {            _source.println(                "        if (context.debugFlag)");            _source.println(                "            context.debugStream.println 'TRANSITION   : Default'");            _source.println();        }        _source.println(            "        throw new statemap.TransitionUndefinedException(");        _source.println(            "                'State: ' + context.state.name +");        _source.println(            "                ', Transition: ' + context.transition)");        _source.println("    }");        // End of state class.        _source.println();        _source.println("}");        _source.println();        // Have each map print out its source code now.        for (SmcMap map: maps)        {            map.accept(this);        }        return;    } // end of visit(SmcFSM)    public void visit(SmcMap map)    {        List<SmcTransition> definedDefaultTransitions;        SmcState defaultState = map.getDefaultState();        String context = map.getFSM().getContext();        String mapName = map.getName();        List<SmcState> states = map.getStates();        // Initialize the default transition list to all the        // default state's transitions.        if (defaultState != null)        {            definedDefaultTransitions =                defaultState.getTransitions();        }        else        {            definedDefaultTransitions =                new ArrayList<SmcTransition>();        }        // Declare the map default state class.        _source.print("private class ");        _source.print(mapName);        _source.print("_Default extends ");        _source.print(context);        _source.println("State {");        // Declare the user-defined default transitions first.        _indent = "    ";        for (SmcTransition trans: definedDefaultTransitions)        {            trans.accept(this);        }        // If -reflect was specified, then generate the        // _transitions map.        if (Smc.isReflection() == true)        {            List<SmcTransition> allTransitions =                map.getFSM().getTransitions();            String transName;            int transDefinition;            // Now output the transition collection's            // initialization.            _source.println();            _source.print("    def transitions = [");            // Now place all transition names and states into the            // map.            String sep = "";            for (SmcTransition transition: allTransitions)            {                transName = transition.getName();                // If the transition is defined in this map's                // default state, then the value is 2.                if (definedDefaultTransitions.contains(                        transition) == true)                {                    transDefinition = 2;                }                // Otherwise the value is 0 - undefined.                else                {                    transDefinition = 0;                }                _source.println(sep);                _source.print("        ");                _source.print(transName);                _source.print(":");

⌨️ 快捷键说明

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