📄 smcgraphgenerator.java
字号:
//// 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: SmcGraphGenerator.java,v 1.8 2008/03/21 14:03:16 fperrad Exp $//// CHANGE LOG// (See the bottom of this file.)//package net.sf.smc;import java.io.ByteArrayOutputStream;import java.io.PrintStream;import java.util.Iterator;import java.util.List;/** * Visits the abstract syntax tree, emitting a Graphviz diagram. * @see SmcElement * @see SmcCodeGenerator * @see SmcVisitor * * @author <a href="mailto:rapp@acm.org">Charles Rapp</a> */public final class SmcGraphGenerator extends SmcCodeGenerator{//---------------------------------------------------------------// Member methods// public SmcGraphGenerator(String srcfileBase) { super (srcfileBase, "{0}{1}_sm.{2}", "dot"); _parameters = null; } // end of SmcGraphGenerator(String) public void visit(SmcFSM fsm) { // Create one overall graph and place each map in a // subgraph. _source.println("digraph "); _source.print(_srcfileBase); _source.println(" {"); _source.println(); _source.println(" node"); _source.println(" [shape=record];"); _source.println(); // Have each map generate its subgraph. for (SmcMap map: fsm.getMaps()) { _source.print(" subgraph cluster_"); _source.print(map.getName()); _source.println(" {"); map.accept(this); // Output the subgraph's closing brace. _source.println(" }"); _source.println(); } // Output the digraph's closing brace. _source.println("}"); return; } // end of visit(SmcFSM) public void visit(SmcMap map) { String mapName = map.getName(); int graphLevel = Smc.graphLevel(); SmcState defaultState = map.getDefaultState(); List<SmcTransition> transitions = map.getTransitions(); String popArgs; _source.println(); _source.print(" label=\""); _source.print(mapName); _source.println("\";"); _source.println(); _source.print(" //"); _source.println( "-------------------------------------------------------"); _source.println(" // States (Nodes)"); _source.println(" //"); _source.println(); // Output the state names first. for (SmcState state: map.getStates()) { state.accept(this); } // Output the default state - if there is one and it // has some transitions. if (defaultState != null && defaultState.getTransitions().isEmpty() == false) { defaultState.accept(this); } // Now output the pop transitions as "nodes". for (SmcTransition transition: transitions) { for (SmcGuard guard: transition.getGuards()) { if (guard.getTransType() == Smc.TRANS_POP) { // Graph Level 0, 1: Output the pop // transition. // Graph Level 2: Output the pop arguments. _source.print(" \""); _source.print(mapName); _source.print("::pop("); _source.print(guard.getEndState()); if (graphLevel == Smc.GRAPH_LEVEL_2 && (popArgs = guard.getPopArgs()) != null && popArgs.length() > 0) { _source.print(", "); _source.print(Smc.escape(popArgs)); } _source.println(")\""); _source.println( " [shape=plaintext];"); _source.println(); } } } _source.print(" //"); _source.println( "-------------------------------------------------------"); _source.println(" // Transitions (Edges)"); _source.println(" //"); // For each state, output its transitions. for (SmcState state: map.getStates()) { for (SmcTransition transition: state.getTransitions()) { transition.accept(this); _source.println(); } } // Have the default state output its transitions last. if (defaultState != null) { for (SmcTransition transition: defaultState.getTransitions()) { transition.accept(this); _source.println(); } } return; } // end of visit(SmcMap) public void visit(SmcState state) { int graphLevel = Smc.graphLevel(); String mapName = state.getMap().getName(); String instanceName = state.getInstanceName(); // The state name must be fully-qualified because // Graphviz does not allow subgraphs to share node names. // Place the node name in quotes. _source.print(" \""); _source.print(mapName); _source.print("::"); if (instanceName.equals("DefaultState") == true) { _source.print("Default"); } else { _source.print(instanceName); } _source.println("\""); // For graph levels 0 and 1, output just the state name. if (graphLevel < Smc.GRAPH_LEVEL_2) { _source.println(" [label=\"\\N\"];"); } // For graph level 2, output the state name, entry and // exit actions. else { List<SmcAction> actions; Iterator<SmcAction> it; String sep; _source.print(" [label=\"{\\N| Entry:"); // Output the entry actions, one per line. actions = state.getEntryActions(); if (actions != null && actions.isEmpty() == false) { for (it = actions.iterator(), sep = " "; it.hasNext() == true; sep = "\\l") { _source.print(sep); (it.next()).accept(this); } } _source.print("| Exit:"); // Output the exit actions, one per line. actions = state.getExitActions(); if (actions != null && actions.isEmpty() == false) { for (it = actions.iterator(), sep = " "; it.hasNext() == true; sep = "\\l") { _source.print(sep); (it.next()).accept(this); } } _source.println("}\"];"); } _source.println(); return; } // end of visit(SmcState) public void visit(SmcTransition transition) { // Graph level 2: add parameters to transition name. // Generate the parameters once and pass the string to // the guards in the "package" argument. if (Smc.graphLevel() == Smc.GRAPH_LEVEL_2) { List<SmcParameter> parameters = transition.getParameters(); Iterator<SmcParameter> pit; if (parameters.isEmpty() == true) { _parameters = null; }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -