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

📄 smctablegenerator.java

📁 SMC takes a state machine stored in a .sm file and generates a State pattern in twelve programming l
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
//// 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. 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: SmcTableGenerator.java,v 1.6 2008/03/21 14:03:17 fperrad Exp $//// CHANGE LOG// (See the bottom of this file.)//package net.sf.smc;import java.io.PrintStream;import java.util.Iterator;import java.util.List;/** * Visits the abstract syntax tree, emitting an HTML table. * @see SmcElement * @see SmcCodeGenerator * @see SmcVisitor * * @author <a href="mailto:rapp@acm.org">Charles Rapp</a> */public final class SmcTableGenerator    extends SmcCodeGenerator{//---------------------------------------------------------------// Member methods//    public SmcTableGenerator(String srcfileBase)    {        super (srcfileBase, "{0}{1}_sm.{2}", "html");    } // end of SmcTableGenerator(String)    public void visit(SmcFSM fsm)    {        Iterator<SmcMap> mit;        String separator;        // Output the top-of-page HTML.        _source.println("<html>");        _source.println("  <head>");        _source.print("    <title>");        _source.print(_srcfileBase);        _source.println("</title>");        _source.println("  </head>");        _source.println();        _source.println("  <body>");        // Have each map generate its HTML table.        for (mit = fsm.getMaps().iterator(), separator = "";             mit.hasNext();             separator = "    <p>\n")        {            _source.print(separator);            (mit.next()).accept(this);        }        // Output the end-of-page HTML.        _source.println("  </body>");        _source.println("</html>");        return;    } // end of visit(SmcFSM)    public void visit(SmcMap map)    {        String mapName = map.getName();        List<SmcTransition> transitions = map.getTransitions();        List<SmcParameter> params;        int transitionCount = transitions.size() + 1;        Iterator<SmcParameter> it;        SmcTransition defaultTransition = null;        String transName;        boolean firstFlag;        // Output start of this map's table.        _source.println(            "    <table align=center border=3 cellspacing=2 cellpadding=2>");        _source.println("      <caption align=\"top\">");        _source.print("        ");        _source.print(mapName);        _source.println(" Finite State Machine");        _source.println("      </caption>");        // Output the table's header.        _source.println("      <tr>");        _source.println("        <th rowspan=2>");        _source.println("          State");        _source.println("        </th>");        _source.println("        <th colspan=2>");        _source.println("          Actions");        _source.println("        </th>");        _source.print("        <th colspan=");        _source.print(transitionCount);        _source.println(">");        _source.println("          Transition");        _source.println("        </th>");        _source.println("      </tr>");        _source.println("      <tr>");        _source.println("        <th>");        _source.println("          Entry");        _source.println("        </th>");        _source.println("        <th>");        _source.println("         Exit");        _source.println("        </th>");        // Place each transition name into the header.        for (SmcTransition transition: transitions)        {            transName = transition.getName();            params = transition.getParameters();            // Since we are placing the default transition at the            // right-most column, don't output it here if it            // should locally defined.            if (transName.equals("Default") == false)            {                _source.println("        <th>");                _source.print("          ");                _source.println(transName);                // If the transition has parameters, output                // them now.                if (params.isEmpty() == false)                {                    _source.println("          <br>");                    _source.print("          (");                    for (it = params.iterator(),                             firstFlag = true;                         it.hasNext() == true;                         firstFlag = false)                    {                        if (firstFlag == false)                        {                            _source.println(',');                            _source.println("          <br>");                            _source.print("          ");                        }                        (it.next()).accept(this);                    }                    _source.println(")");                }                _source.println("        </th>");            }        }        // Also output the default transition.        _source.println("        <th>");        _source.println("          <b>Default</b>");        _source.println("        </th>");        _source.println("      </tr>");        // The table header is finished. Now have each state        // output its row.        for (SmcState state: map.getStates())        {            // Output the row start.            _source.println("      <tr>");            // Note: the state outputs only its name and            // entry/exit actions. It does not output its            // transitions (see below).            state.accept(this);            // We need to generate transitions in the exact same            // order as in the header. But the state object            // does not store its transitions in any particular            // order. Therefore we must output the state's            // transitions for it.            for (SmcTransition transition: transitions)            {                transName = transition.getName();                params = transition.getParameters();                // Since we are placing the default transition                // at the right-most column, don't output it                // here if it should locally defined.                if (transName.equals("Default") == true)                {                    // If this state has a false transition,                    // get it now and store it away for later.                    defaultTransition =                        state.findTransition(transName, params);                }                else                {                    _source.println("        <td>");                    // We have the default transition definition                    // in hand. We need the state's transition.                    transition =                        state.findTransition(transName, params);                    if (transition != null)                    {                        // Place the transitions in preformatted                        // sections. Don't add a new line - the                        // transition will do that.                        _source.print("          <pre>");                        transition.accept(this);                        _source.println("          </pre>");                    }                    _source.println("        </td>");                }            }            // Now add the Default transition to the last column.            _source.println("        <td>");            if (defaultTransition != null)            {                // Place the transitions in preformatted                // sections. Don't add a new line - the                // transition will do that.                _source.print("          <pre>");                defaultTransition.accept(this);                _source.println("          </pre>");            }            _source.println("        </td>");            // Output the row end.            _source.println("      </tr>");        }        // Output end of this map's table.        _source.println("    </table>");

⌨️ 快捷键说明

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