?? netgenerator.java
字號:
// This is copyrighted source file, part of Rakiura JFern package. // See the file LICENSE for copyright information and the terms and conditions// for copying, distributing and modifications of Rakiura JFern package.// Copyright (C) 1999-2002 by Mariusz Nowostawski and others [http://www.rakiura.org]package org.rakiura.cpn;/**/import java.util.ArrayList;import java.util.HashMap;import java.util.Iterator;import java.util.List;import java.util.Map;import java.io.Reader;import javax.xml.parsers.ParserConfigurationException;import org.w3c.dom.Document;import org.w3c.dom.Element;import org.w3c.dom.Node;import org.w3c.dom.NodeList;import org.xml.sax.SAXException;import compiler.DynamicCompiler;/** * Utility class to read XML net description and generates * the JFern Java source file. The generated source file needs to be * compiled, and can be executed/manipulated as any ordinary * JFern Petri Net (see {@link Net Net} and {@link BasicNet BasicNet}). * *<br><br> * NetGenerator.java<br> * Created: Tue Oct 30 22:31:11 2001<br> * *@author <a href="mariusz@rakiura.org">Mariusz Nowostawski</a> *@version 2.1.0 $Revision: 1.14 $ $Date: 2002/05/31 05:34:07 $ *@since 1.2 */public class NetGenerator { /** The generated Java source. */ private StringBuffer netSource = new StringBuffer(); /** The name of the net. */ private String className; /** Imports as strings. */ private List classImports = new ArrayList(); /** Declarations as strings. */ private List classDeclarations = new ArrayList(); /**/ private List netPlaces = new ArrayList(); // strings /**/ private Map netTransitions = new HashMap(); /**/ private Map netInputArcs = new HashMap(); /**/ private Map netOutputArcs = new HashMap(); /** transition ID -> action body string. */ private Map tranActions = new HashMap(); /** transition ID -> guard body string */ private Map tranGuards = new HashMap(); /** arc ID -> guard body string */ private Map inputArcGuards = new HashMap(); /** arc ID -> expression body string */ private Map inputArcExpressions = new HashMap(); /** Place node ID -> Name String */ /** arc ID -> expression body string */ private Map outputArcExpressions = new HashMap(); /**/ private Map placeNodeName = new HashMap(); /** transition node ID -> Name String */ private Map transitionNodeName = new HashMap(); /**/ static final String placePrefix = "_place_"; /**/ static final String transitionPrefix = "_transition_"; /**/ static final String arcPrefix = "_arc_"; /** * For tests and debugging only. * @param args a <code>String[]</code> value * @exception Exception if an error occurs */ public static void main(String[] args) throws Exception { System.out.println("JFern XML-based net generator."); System.out.println("Copyright 2000-2002 by Mariusz Nowostawski."); if (args.length != 1) { System.out.println(" usage: java org.rakiura.cpn.NetGenerator input.xml"); } NetGenerator generator = new NetGenerator(); generator.generateNetSourceFromFile(args[0]); java.io.PrintWriter writer = new java.io.PrintWriter( new java.io.FileOutputStream(generator.className + ".java")); writer.println(generator.netSource.toString()); writer.flush(); writer.close(); } /** * Takes Petri net XML data and returns a Net object reference. *@param aReader the input reader with the XML data to be parsed *@return Net, a newly instantiated Net object */ public Net generateNet(final Reader aReader){ String javaNet = null; Class classNet = null; Net aNet = null; try { javaNet = generateNetSource(aReader); classNet = new DynamicCompiler().compileClass(javaNet); aNet = (Net) classNet.newInstance(); } catch (Exception ce) { System.out.println("ERROR: Compiling the net from XML failed! "); ce.printStackTrace(); } return aNet; } /** * Takes Petri net XML data and returns a Net object reference. *@param aData String input containing the XML data to be parsed *@return Net, a newly instantiated Net object */ public Net generateNet(final String aData){ String javaNet = null; Class classNet = null; Net aNet = null; try { javaNet = generateNetSource(aData); classNet = new DynamicCompiler().compileClass(javaNet); aNet = (Net) classNet.newInstance(); } catch (Exception ce) { System.out.println("ERROR: Compiling the net from XML failed! "); ce.printStackTrace(); } return aNet; } /** * Reads an input stream from the filename, and generates the net Java source. * @param aFileName a <code>String</code> value * @return String with the generated Java source * @exception ParserConfigurationException if an error occurs * @exception SAXException if an error occurs */ public String generateNetSourceFromFile(final String aFileName) throws ParserConfigurationException, SAXException { final Document doc = XmlUtil.parseFile(aFileName); parseXML(doc); generateNetSource(); return this.netSource.toString(); } /** * Reads an input stream from the given reader, and generates the net Java source. * @param aReader the input reader with the XML data to be parsed * @return String with the generated Java source * @exception ParserConfigurationException if an error occurs * @exception SAXException if an error occurs */ public String generateNetSource(final Reader aReader) throws ParserConfigurationException, SAXException { final Document doc = XmlUtil.parse(aReader); parseXML(doc); generateNetSource(); return this.netSource.toString(); } /** * Reads an input stream from the filename, and generates the net Java source. * @param anInput String input containing the XML data to be parsed * @return String with the generated Java source * @exception ParserConfigurationException if an error occurs * @exception SAXException if an error occurs */ public String generateNetSource(final String anInput) throws ParserConfigurationException, SAXException { final Document doc = XmlUtil.parse(anInput); parseXML(doc); generateNetSource(); return this.netSource.toString(); } /**/ private void parseXML(final Document aDoc) { final Element e = aDoc.getDocumentElement(); aDoc.normalize(); //to preserve the oroginal formatting lets assume simple TEXT elements. //thus, no need to e.normalize(); processNetAnnotations(e); processPlaces(e); processTransitions(e); processArcs(e); } private void processNetAnnotations(Element e){ Element name = null; final List imports = new ArrayList(); final List declarations = new ArrayList(); final NodeList list = e.getChildNodes(); for (int i = 0; i < list.getLength(); i++) { Node n = list.item(i); if (n.getNodeName().toLowerCase().trim().equals("annotation")) { Element en = (Element) n; String type = en.getAttribute("type"); if (type.toLowerCase().trim().equals("import")){ imports.add(en); } else if (type.toLowerCase().trim().equals("declaration")) { declarations.add(en); } else if (type.toLowerCase().trim().equals("name")) { name = en; } } } processImports(imports); processName(name); processDeclarations(declarations); } private void processName(Element e) { this.className = "NoNamedNet"; if (e != null){ NodeList children = e.getElementsByTagName("text"); if (children.getLength() > 0) { this.className = children.item(0).getChildNodes().item(0).getNodeValue().trim(); } } } private void processImports(List list) { for (int i = 0; i < list.size(); i++) { NodeList children = ((Element)list.get(i)).getElementsByTagName("text"); //assume there is only one <text> tag; if (children.getLength() > 0) { String text = children.item(0).getChildNodes().item(0).getNodeValue().trim(); this.classImports.add(text); } } } private void processDeclarations(final List list) { for (int i = 0; i < list.size(); i++) { NodeList children = ((Element) list.get(i)).getElementsByTagName("text"); if (children.getLength() > 0) { String text = children.item(0).getChildNodes().item(0).getNodeValue(); classDeclarations.add(text); } } } private void processPlaces(Element e){ final NodeList list = e.getChildNodes(); for (int i = 0; i < list.getLength(); i++) { Node n = list.item(i); if (n.getNodeName().toLowerCase().trim().equals("place")) { Element en = (Element)n; String id = en.getAttribute("id").trim(); netPlaces.add(id); NodeList a = en.getElementsByTagName("annotation"); for (int j = 0; j < a.getLength(); j++){ if (a.item(j) instanceof Element) { if (((Element) a.item(j)).getAttribute("type").trim().equals("name")) { NodeList children = ((Element) a.item(j)).getElementsByTagName("text"); //assume there is only one <text> tag; if (children.getLength() > 0) { String text = children.item(0).getChildNodes().item(0).getNodeValue(); this.placeNodeName.put(id, text); } } } } } } } private void processTransitions(final Element e) { final NodeList list = e.getChildNodes(); for (int i = 0; i < list.getLength(); i++) {
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -