1 package edu.berkeley.fleet.two;
2 import edu.berkeley.fleet.api.*;
6 /** NOT YET FINALIZED: A description (specification) of a ship */
7 public class ShipDescription implements Iterable<DockDescription> {
11 private LinkedHashMap<String,DockDescription> docks = new LinkedHashMap<String,DockDescription>();
12 private LinkedHashMap<String,DockDescription> ports = new LinkedHashMap<String,DockDescription>();
13 private HashMap<String,String> sections = new HashMap<String,String>();
14 private HashMap<String,BitVector> constants = new HashMap<String,BitVector>();
16 public String getName() { return name; }
17 public String getSection(String sectionName) { return sections.get(sectionName); }
18 public DockDescription getDockDescription(String name) { return docks.get(name); }
19 public Iterator<DockDescription> iterator() { return docks.values().iterator(); }
20 public Iterable<DockDescription> ports() {
21 return ports.values();
24 public final LinkedList<PercolatedPort> percolatedPorts = new LinkedList<PercolatedPort>();
26 public ShipDescription(Fleet fleet, String name, BufferedReader r) throws IOException {
27 if (name.endsWith(".ship")) name = name.substring(0, name.length()-".ship".length());
30 String sectionName = null;
31 StringBuffer sb = new StringBuffer();
33 String s = r.readLine();
34 if (s==null || s.startsWith("==")) {
35 if (sectionName != null) sections.put(sectionName, sb.toString());
37 sb = new StringBuffer();
38 sectionName = s.trim();
39 while(sectionName.startsWith("="))
40 sectionName = sectionName.substring(1);
41 while(sectionName.endsWith("="))
42 sectionName = sectionName.substring(0, sectionName.length()-1);
43 sectionName = sectionName.trim().toLowerCase();
48 for(String s : sections.keySet())
52 public BitVector getConstant(String name) {
53 BitVector c = constants.get(name);
54 if (c==null) throw new RuntimeException("unknown constant " + name);
58 private void processSection(String section) throws IOException {
59 if (section.equals("")) {
60 BufferedReader br = new BufferedReader(new StringReader(sections.get(section)));
61 for(String s = br.readLine(); s != null; s = br.readLine()) {
62 if (s.trim().length()==0) continue;
63 String key = s.substring(0, s.indexOf(':')).trim();
64 String val = s.substring(s.indexOf(':')+1).trim();
65 if (key.toLowerCase().equals("ship"))
68 } else if (section.equals("constants")) {
69 BufferedReader br = new BufferedReader(new StringReader(sections.get(section)));
70 for(String s = br.readLine(); s != null; s = br.readLine()) {
71 if (s.indexOf(':')==-1) continue;
72 String key = s.substring(0, s.indexOf(':')).trim();
73 if (key.startsWith("constant")) {
74 String constname = key.substring("constant".length()+1).trim();
75 String val = s.substring(s.indexOf(':')+1).trim();
76 constants.put(constname, new BitVector(fleet.getWordWidth()).set(Integer.parseInt(val)));
79 } else if (section.equals("ports")) {
80 BufferedReader br = new BufferedReader(new StringReader(sections.get(section)));
81 boolean rightSide = false;
82 DockDescription p = null;
83 for(String s = br.readLine(); s != null; s = br.readLine()) {
84 if (s.trim().length()==0) { rightSide = true; continue; }
86 String key = s.substring(0, s.indexOf(':')).trim();
87 boolean inbox = false;
88 boolean dockless = false;
89 key = key.replaceAll(" +", " ");
90 if (key.equals("data in")) { inbox = true; }
91 else if (key.equals("data out")) { inbox = false; }
92 else if (key.equals("in")) { inbox = true; }
93 else if (key.equals("dockless out")) { inbox = false; dockless = true; }
94 else if (key.equals("out")) { inbox = false; }
95 else if (key.startsWith("percolate")) {
97 key = key.substring("percolate".length()+1).trim();
98 PercolatedPort.PortType type = null;
99 if (key.startsWith("up")) type = PercolatedPort.PortType.UP;
100 if (key.startsWith("down")) type = PercolatedPort.PortType.DOWN;
101 if (key.startsWith("inout")) type = PercolatedPort.PortType.INOUT;
102 key = key.substring(key.indexOf(':')+1).trim();
103 String name = key.substring(0, key.indexOf(' '));
104 int width = Integer.parseInt(key.substring(key.indexOf(' ')).trim());
105 percolatedPorts.add(new PercolatedPort(name, width, type));
108 else if (key.startsWith("constant")) {
109 String constname = key.substring("constant".length()+1).trim();
110 String val = s.substring(s.indexOf(':')+1).trim();
111 p.addConstant(constname, new BitVector(fleet.getWordWidth()).set(Integer.parseInt(val)));
113 } else if (key.startsWith("shortcut to")) {
116 else throw new RuntimeException("unknown port type: \""+key+"\"");
119 String val = s.substring(s.indexOf(':')+1).trim();
120 String boxname = val.indexOf('.') != -1 ? val.substring(0, val.indexOf('.')) : val;
121 String dest = val.indexOf('.') != -1 ? val.substring(val.indexOf('.')+1) : "";
122 p = docks.get(boxname);
124 p = new DockDescription(this, boxname, !rightSide, inbox, dockless);
125 ports.put(boxname, p);
126 if (!dockless) docks.put(boxname, p);
132 public void printTeX(PrintWriter pw) throws Exception {
133 ShipDescription sd = this;
134 pw.println("\\pagebreak");
135 pw.println("\\section*{The {\\tt "+sd.getName()+"} Ship}");
136 pw.println("\\addcontentsline{toc}{subsection}{"+sd.getName()+"}");
137 String tex = sd.getSection("tex");
139 for(DockDescription bbd : sd) {
140 pw.println("{\\bf "+(bbd.isInputDock() ? "Input: " : "Output: ")+"{\\tt "+bbd.getName()+"}}\n\n");
149 for(DockDescription bbd : sd)
150 if (bbd.isLeft()) leftSize += (boxHeight+boxGap);
151 else rightSize += (boxHeight+boxGap);
153 int totalHeight = Math.max(leftSize, rightSize);
154 int shipWidth = (int)(boxWidth * 1.5);
155 int totalWidth = boxGap*2 + boxWidth*2 + shipWidth;
158 pw.println("\\begin{center}");
159 pw.println("\\begin{empfile}["+sd.getName()+"]");
160 pw.println("\\begin{emp}["+sd.getName()+"]("+(totalWidth+10)+","+(totalHeight+10)+")");
161 pw.println(" beginfig(1)");
162 pw.println(" pickup pencircle scaled 1pt;");
164 "("+((totalWidth-shipWidth)/2)+","+0+")--"+
165 "("+((totalWidth-shipWidth)/2)+","+totalHeight+")--"+
166 "("+(totalWidth-((totalWidth-shipWidth)/2))+","+totalHeight+")--"+
167 "("+(totalWidth-((totalWidth-shipWidth)/2))+","+0+")--"+
168 "("+((totalWidth-shipWidth)/2)+","+0+");");
171 for(DockDescription bbd : sd) {
172 int ypos = (totalHeight - (boxGap/2) - (bbd.isLeft() ? left : right));
173 int half = (totalWidth-shipWidth)/2;
174 int p1 = bbd.isLeft() ? (half-5) : ((totalWidth-half)+5);
175 int p3 = bbd.isLeft() ? (p1 - boxWidth) : (p1 + boxWidth);
176 if (bbd.isInputDock()) {
181 boolean goo = ((bbd.isLeft() && bbd.isInputDock()) || (!bbd.isLeft() && bbd.isOutputDock()));
182 int p2 = goo ? (p3 - (boxHeight/2)) : (p3 + (boxHeight/2));
183 if (bbd.isLeft()) left += (boxHeight+boxGap);
184 else right += (boxHeight+boxGap);
186 pw.println(" label.rt(btex \\tt "+bbd.getName()+" etex, ("+(p1+3)+","+(ypos-boxHeight/2)+"));");
188 pw.println(" label.lft(btex \\tt "+bbd.getName()+" etex, ("+(p1-3)+","+(ypos-boxHeight/2)+"));");
191 " ("+p1+","+ypos+")--"+
192 " ("+p2+","+ypos+")--"+
193 " ("+p3+","+(ypos-(boxHeight/2))+")--"+
194 " ("+p2+","+(ypos-boxHeight)+")--"+
195 " ("+p1+","+(ypos-boxHeight)+")--"+
196 " ("+p1+","+ypos+");");
197 if (bbd.isLeft()) leftSize += boxHeight;
198 else rightSize += boxHeight;
200 pw.println(" endfig;");
201 pw.println("\\end{emp}");
202 pw.println("\\end{empfile}");
203 pw.println("\\end{center}");