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

📄 generator.java

📁 有限状态机程序自动生成器可以自动生成C、C++和Java程序。自动生成的程序很容易与程序员自己开发的程序结合。
💻 JAVA
字号:
package org.abeesoft.fsmgenerator.generator.java;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;

import org.abeesoft.fsmgenerator.api.java.IFSM;
import org.abeesoft.fsmgenerator.configuration.Configuration;
import org.abeesoft.fsmgenerator.database.Database;
import org.abeesoft.fsmgenerator.exception.Exception;
import org.abeesoft.fsmgenerator.specification.Event;
import org.abeesoft.fsmgenerator.specification.Specification;
import org.abeesoft.fsmgenerator.specification.State;
import org.abeesoft.fsmgenerator.specification.Transition;

public final class Generator extends org.abeesoft.fsmgenerator.generator.Generator
{
	private static final String	LANGUAGE	= "java";

	public Generator( Configuration configuration, Specification specification )
	{
		super( configuration, specification, LANGUAGE );
	}

	public void generate() throws Exception
	{
		if( configuration == null )
			throw new Exception();

		if( configuration.getGenerator() == null )
			throw new Exception();

		if( !configuration.getGenerator().getLanguage().equals( language ) )
			throw new Exception();

		if( configuration.getGenerator().getPackage() == null )
			throw new Exception();

		File pathDirectory = new File( configuration.getGenerator().getPath() );
		if( pathDirectory.exists() == false || pathDirectory.isDirectory() == false )
			throw new Exception();

		String packagePath = configuration.getGenerator().getPackage();
		packagePath = packagePath.replace( '.', File.separatorChar );
		packagePath = pathDirectory.getAbsolutePath() + File.separatorChar + packagePath;

		File packageDirectory = new File( packagePath );
		packageDirectory.mkdirs();

		String filePath = packageDirectory.getAbsolutePath() + File.separatorChar + specification.getMachine().getName() + ".java";

		File file = new File( filePath );

		try
		{
			FileWriter fw = new FileWriter( file );
			{
				fw.write( "package " + configuration.getGenerator().getPackage() + ";\n" );
				fw.write( "\n" );
				fw.write( "import java.util.HashMap;\n" );
				fw.write( "import java.util.List;\n" );
				fw.write( "import java.util.LinkedList;\n" );
				fw.write( "\n" );
				fw.write( "import org.abeesoft.fsmgenerator.api.java.IFSM;\n" );
				fw.write( "\n" );
				fw.write( "public final class " + specification.getMachine().getName() + " implements IFSM\n" );
				fw.write( "{\n" );
				fw.write( "\tprivate static final HashMap< String, Integer > mapStateNameId = new HashMap< String, Integer >();\n" );
				fw.write( "\tprivate static final HashMap< String, Integer > mapEventNameId = new HashMap< String, Integer >();\n" );
				fw.write( "\tprivate static final String[] mapStateIdName = new String[" + (specification.getMachine().getStates().getState().size() + 1) + "];\n" );
				fw.write( "\tprivate static final String[] mapEventIdName = new String[" + (specification.getMachine().getEvents().getEvent().size() + 1) + "];\n" );
				fw.write( "\tprivate static final int[][] mapTransition = new int[" + (specification.getMachine().getStates().getState().size() + 1) + "][" + (specification.getMachine().getEvents().getEvent().size() + 1) + "];\n" );
				fw.write( "\n" );
				fw.write( "\tprivate static final String	startState		= \"" + specification.getMachine().getStart().getName() + "\";\n" );
				fw.write( "\tstatic\n" );
				fw.write( "\t{\n" );
				fw.write( "\n" );
				for( State state : specification.getMachine().getStates().getState() )
				{
					fw.write( "\t\tmapStateNameId.put( \"" + state.getName() + "\" ," + state.getId() + " );\n" );
					fw.write( "\t\tmapStateIdName[" + state.getId() + "] = \"" + state.getName() + "\";\n" );
				}
				fw.write( "\n" );
				for( Event event : specification.getMachine().getEvents().getEvent() )
				{
					fw.write( "\t\tmapEventNameId.put( \"" + event.getName() + "\" ," + event.getId() + " );\n" );
					fw.write( "\t\tmapEventIdName[" + event.getId() + "] = \"" + event.getName() + "\";\n" );
				}
				fw.write( "\n" );
				for( Transition transition : specification.getMachine().getTransitions().getTransition() )
				{
					fw.write( "\t\tmapTransition[" + Database.mapStateNameId.get( transition.getFrom() ) + "][" + Database.mapEventNameId.get( transition.getOn() ) + "] = " + Database.mapStateNameId.get( transition.getTo() ) + ";\n" );

				}
				fw.write( "\n" );
				fw.write( "\t}\n" );
				fw.write( "\n" );
				fw.write( "\tprivate String				currentState	= null;\n" );
				fw.write( "\n" );
				fw.write( "\tpublic " + specification.getMachine().getName() + "() throws Exception\n" );
				fw.write( "\t{\n" );
				fw.write( "\t\trestart();\n" );
				fw.write( "\t}\n" );
				fw.write( "\n" );
				fw.write( "\tpublic void restart() throws Exception\n" );
				fw.write( "\t{\n" );
				fw.write( "\t\tcurrentState = startState;\n" );
				fw.write( "\t}\n" );
				fw.write( "\n" );
				fw.write( "\tpublic void process( String eventName ) throws Exception\n" );
				fw.write( "\t{\n" );
				fw.write( "\t\tcurrentState = mapStateIdName[mapTransition[mapStateNameId.get(currentState)][mapEventNameId.get(eventName)]];\n" );
				fw.write( "\t}\n" );
				fw.write( "\n" );
				fw.write( "\tpublic String getCurrentState()\n" );
				fw.write( "\t{\n" );
				fw.write( "\t\treturn currentState;\n" );
				fw.write( "\t}\n" );
				fw.write( "\n" );
				fw.write( "\tpublic void setCurrentState( String stateName )\n" );
				fw.write( "\t{\n" );
				fw.write( "\t\tcurrentState = stateName;\n" );
				fw.write( "\t}\n" );
				fw.write( "\n" );
				fw.write( "\tpublic List<String> getCurrentEventOut()\n" );
				fw.write( "\t{\n" );
				fw.write( "\t\tList<String> eventOut = new LinkedList<String>();\n" );
				fw.write( "\n" );
				fw.write( "\t\tint index = mapStateNameId.get(currentState);\n" );
				fw.write( "\t\tfor( int i = 1; i < mapTransition[index].length; i++ )\n" );
				fw.write( "\t\t{\n" );
				fw.write( "\t\tif( mapTransition[index][i] != 0 )\n" );
				fw.write( "\t\t\teventOut.add(mapEventIdName[mapTransition[index][i]]);\n" );
				fw.write( "\t\t}\n" );
				fw.write( "\n" );
				fw.write( "\t\treturn eventOut;\n" );
				fw.write( "\t}\n" );
				fw.write( "\n" );
				fw.write( "\tpublic String getCurrentTransitionOut( String eventName )\n" );
				fw.write( "\t{\n" );
				fw.write( "\t\treturn mapStateIdName[mapTransition[mapStateNameId.get(currentState)][mapEventNameId.get(eventName)]];\n" );
				fw.write( "\t}\n" );
				fw.write( "}\n" );
			}
			fw.flush();
			fw.close();
		}
		catch ( IOException e )
		{
			throw new Exception( e );
		}
	}
}

⌨️ 快捷键说明

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