📄 netgenerator.java
字号:
Node n = list.item(i); if (n.getNodeName().toLowerCase().trim().equals("transition")) { final Element en = (Element) n; final String id = en.getAttribute("id").trim(); netTransitions.put(id, en); // lets process the transition annotations (guards/actions) final 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("action")) || (((Element) a.item(j)).getAttribute("type").trim().equals("expression"))) { final 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(); tranActions.put(id, text); } } else if (((Element) a.item(j)).getAttribute("type").trim().equals("guard")) { final NodeList children = ((Element) a.item(j)).getElementsByTagName("text"); //assume there is only one <text> tag; if (children.getLength() > 0 ) { final String text = children.item(0).getChildNodes().item(0).getNodeValue(); tranGuards.put(id, text); } } else if (((Element) a.item(j)).getAttribute("type").trim().equals("name")) { final NodeList children2 = ((Element) a.item(j)).getElementsByTagName("text"); //assume there is only one <text> tag; if (children2.getLength()>0) { final String text = children2.item(0).getChildNodes().item(0).getNodeValue(); transitionNodeName.put(id, text); } } } } } } private void processArcs(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("arc")){ Element en = (Element)n; String id = en.getAttribute("id").trim(); String source = en.getAttribute("source").trim(); String target = en.getAttribute("target").trim(); if(netPlaces.contains(source)) {//we have an input arc netInputArcs.put(id, new String[]{source, target}); // lets process the transition annotations (guards/actions) 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("guard")){ 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(); inputArcGuards.put(id, text); } } else if(((Element)a.item(j)).getAttribute("type").trim().equals("expression")){ 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(); inputArcExpressions.put(id, text); } } } } else { //we have an output arc netOutputArcs.put(id, new String[]{source, target}); // lets process the transition annotations (guards/actions) 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("expression")){ 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(); outputArcExpressions.put(id, text); } } } } } } } private void generateNetSource() { this.netSource.append("/* This is JFern generated file. Do not edit. */\n"); this.netSource.append("/* project page see http://sf.net/projects/jfern */\n\n"); // imports for (int i = 0; i < classImports.size(); i++) { this.netSource.append((String) classImports.get(i)).append("\n"); } this.netSource.append("import org.rakiura.cpn.Net;\n"); this.netSource.append("import org.rakiura.cpn.Place;\n"); this.netSource.append("import org.rakiura.cpn.Transition;\n"); this.netSource.append("import org.rakiura.cpn.InputArc;\n"); this.netSource.append("import org.rakiura.cpn.OutputArc;\n"); this.netSource.append("import org.rakiura.cpn.BasicNet;\n"); this.netSource.append("import org.rakiura.cpn.Multiset;\n"); this.netSource.append("import org.rakiura.cpn.FusionPlace;\n"); this.netSource.append("import org.rakiura.cpn.Marking;\n"); // name and the base class this.netSource.append("\n/** Net source file. */\n"); this.netSource.append("public class ").append(className).append(" extends BasicNet {\n\n"); // class declarations for(int i=0; i < classDeclarations.size(); i++){ this.netSource.append((String)classDeclarations.get(i)).append("\n"); } // places declaration this.netSource.append("\n /* Places declaration. */\n"); for (int i=0; i < netPlaces.size(); i++) { this.netSource.append(" private Place ").append(placePrefix) .append((String)netPlaces.get(i)) .append(" = new Place(\"") .append((String) this.netPlaces.get(i)) .append("\");\n"); } // transitions declaration this.netSource.append("\n /* Transitions declaration. */\n"); final Iterator tit = this.netTransitions.keySet().iterator(); while (tit.hasNext()) { final String id = (String) tit.next(); this.netSource.append(" private Transition ").append(transitionPrefix) .append(id) .append(" = new Transition(\"") .append(id) .append("\");\n"); } // input arcs declarations this.netSource.append("\n /* Input Arcs declaration. */\n"); final Iterator ait = this.netInputArcs.keySet().iterator(); while (ait.hasNext()) { final String id = (String) ait.next(); final String[] srcdst = (String[]) this.netInputArcs.get(id); this.netSource.append(" private InputArc ").append(arcPrefix) .append(id) .append(" = new InputArc(") .append(placePrefix).append(srcdst[0]) .append(", ") .append(transitionPrefix).append(srcdst[1]) .append(");\n"); } // output arcs declarations this.netSource.append("\n /* Output Arcs declaration. */\n"); final Iterator oit = this.netOutputArcs.keySet().iterator(); while (oit.hasNext()) { final String id = (String) oit.next(); final String[] srcdst = (String[]) this.netOutputArcs.get(id); this.netSource.append(" private OutputArc ").append(arcPrefix) .append(id) .append(" = new OutputArc(") .append(transitionPrefix).append(srcdst[0]) .append(", ") .append(placePrefix).append(srcdst[1]) .append(");\n"); } // net constructor, register all places and transitions this.netSource.append("\n /* The default " + this.className + " constructor. */\n"); this.netSource.append(" public ").append(this.className).append("() {\n"); for (int i = 0; i < this.netPlaces.size(); i++) { this.netSource.append(" add(").append(placePrefix) .append((String) this.netPlaces.get(i)) .append(");\n"); } final Iterator tit2 = this.netTransitions.keySet().iterator(); while (tit2.hasNext()) { final String id = (String) tit2.next(); this.netSource.append(" add(") .append(transitionPrefix) .append(id) .append(");\n"); } // transition actions this.netSource.append("\n /* Transition actions. */\n"); final Iterator trait = this.tranActions.keySet().iterator(); while (trait.hasNext()) { final String id = (String) trait.next(); final String body = (String) this.tranActions.get(id); this.netSource.append("\n ").append(transitionPrefix) .append(id) .append(".setAction(").append(transitionPrefix).append(id).append(".new Action() {\n") .append(" public void execute() {\n") .append(body) .append("\n }\n") .append(" });\n"); } // transition guards this.netSource.append("\n /* Transition guards. */\n"); final Iterator trgit = this.tranGuards.keySet().iterator(); while (trgit.hasNext()) { final String id = (String) trgit.next(); final String body = (String) this.tranGuards.get(id); this.netSource.append("\n ").append(transitionPrefix) .append(id) .append(".setGuard(").append(transitionPrefix).append(id).append(".new Guard() {\n") .append(" public boolean evaluate() {\n") .append(body) .append("\n }\n") .append(" });\n"); } // input arc guards this.netSource.append("\n /* Input Arcs guards. */\n"); final Iterator iagit = this.inputArcGuards.keySet().iterator(); while (iagit.hasNext()) { final String id = (String) iagit.next(); final String body = (String) this.inputArcGuards.get(id); this.netSource.append("\n ").append(arcPrefix) .append(id) .append(".setGuard(").append(arcPrefix).append(id).append(".new Guard() {\n") .append(" public boolean evaluate() {\n") .append(body) .append("\n }\n") .append(" });\n"); } // input arc expressions this.netSource.append("\n /* Input Arcs expressions. */\n"); final Iterator iaeit = this.inputArcExpressions.keySet().iterator(); while (iaeit.hasNext()) { final String id = (String) iaeit.next(); final String body = (String) this.inputArcExpressions.get(id); this.netSource.append("\n ").append(arcPrefix) .append(id) .append(".setExpression(").append(arcPrefix).append(id).append(".new Expression() {\n") .append(" public void evaluate() {\n") .append(body) .append("\n }\n") .append(" });\n"); } // output arc expressions this.netSource.append("\n /* Output Arcs expressions. */\n"); final Iterator oaeit = this.outputArcExpressions.keySet().iterator(); while (oaeit.hasNext()) { final String id = (String) oaeit.next(); final String body = (String) this.outputArcExpressions.get(id); this.netSource.append("\n ").append(arcPrefix) .append(id) .append(".setExpression(").append(arcPrefix).append(id).append(".new Expression() {\n") .append(" public Multiset evaluate() {\n") .append(body) .append("\n }\n") .append(" });\n"); } // node Names this.netSource.append("\n\n /* Place Node Names */\n"); final Iterator nnit = this.placeNodeName.keySet().iterator(); while (nnit.hasNext()) { final String id = (String) nnit.next(); final String body = (String) this.placeNodeName.get(id); this.netSource.append("\n ").append(placePrefix) .append(id) .append(".setName(\"") .append(body) .append("\");"); } this.netSource.append("\n\n /* Transition Node Names */\n"); final Iterator trit = this.transitionNodeName.keySet().iterator(); while (trit.hasNext()) { final String id = (String) trit.next(); final String body = (String) this.transitionNodeName.get(id); this.netSource.append("\n ").append(transitionPrefix) .append(id) .append(".setName(\"") .append(body) .append("\");"); } this.netSource.append(" }\n"); // class end this.netSource.append("\n}\n"); }} // NetGenerator//////////////////// end of file ////////////////////
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -