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);
55 throw new RuntimeException("unknown constant " + name);
59 private void processSection(String section) throws IOException {
60 if (section.equals("")) {
61 BufferedReader br = new BufferedReader(new StringReader(sections.get(section)));
62 for(String s = br.readLine(); s != null; s = br.readLine()) {
63 if (s.trim().length()==0) continue;
64 String key = s.substring(0, s.indexOf(':')).trim();
65 String val = s.substring(s.indexOf(':')+1).trim();
66 if (key.toLowerCase().equals("ship"))
69 } else if (section.equals("constants")) {
70 BufferedReader br = new BufferedReader(new StringReader(sections.get(section)));
71 for(String s = br.readLine(); s != null; s = br.readLine()) {
72 if (s.indexOf(':')==-1) continue;
73 String key = s.substring(0, s.indexOf(':')).trim();
74 if (key.startsWith("constant")) {
75 String constname = key.substring("constant".length()+1).trim();
76 String val = s.substring(s.indexOf(':')+1).trim();
77 constants.put(constname, new BitVector(fleet.getWordWidth()).set(Integer.parseInt(val)));
80 } else if (section.equals("ports")) {
81 BufferedReader br = new BufferedReader(new StringReader(sections.get(section)));
82 boolean rightSide = false;
83 DockDescription p = null;
84 for(String s = br.readLine(); s != null; s = br.readLine()) {
85 if (s.trim().length()==0) { rightSide = true; continue; }
87 String key = s.substring(0, s.indexOf(':')).trim();
88 boolean inbox = false;
89 boolean dockless = false;
90 key = key.replaceAll(" +", " ");
91 if (key.equals("data in")) { inbox = true; }
92 else if (key.equals("data out")) { inbox = false; }
93 else if (key.equals("in")) { inbox = true; }
94 else if (key.equals("dockless out")) { inbox = false; dockless = true; }
95 else if (key.equals("out")) { inbox = false; }
96 else if (key.startsWith("percolate")) {
98 key = key.substring("percolate".length()+1).trim();
99 PercolatedPort.PortType type = null;
100 if (key.startsWith("up")) type = PercolatedPort.PortType.UP;
101 if (key.startsWith("down")) type = PercolatedPort.PortType.DOWN;
102 if (key.startsWith("inout")) type = PercolatedPort.PortType.INOUT;
103 key = key.substring(key.indexOf(':')+1).trim();
104 String name = key.substring(0, key.indexOf(' '));
105 int width = Integer.parseInt(key.substring(key.indexOf(' ')).trim());
106 percolatedPorts.add(new PercolatedPort(name, width, type));
109 else if (key.startsWith("constant")) {
110 String constname = key.substring("constant".length()+1).trim();
111 String val = s.substring(s.indexOf(':')+1).trim();
112 p.addConstant(constname, new BitVector(fleet.getWordWidth()).set(Integer.parseInt(val)));
114 } else if (key.startsWith("shortcut to")) {
117 else throw new RuntimeException("unknown port type: \""+key+"\"");
120 String val = s.substring(s.indexOf(':')+1).trim();
121 String boxname = val.indexOf('.') != -1 ? val.substring(0, val.indexOf('.')) : val;
122 String dest = val.indexOf('.') != -1 ? val.substring(val.indexOf('.')+1) : "";
123 p = docks.get(boxname);
125 p = new DockDescription(this, boxname, !rightSide, inbox, dockless);
126 ports.put(boxname, p);
127 if (!dockless) docks.put(boxname, p);
133 public void printTeX(PrintWriter pw) throws Exception {
134 ShipDescription sd = this;
135 pw.println("\\pagebreak");
136 pw.println("\\section*{The {\\tt "+sd.getName()+"} Ship}");
137 pw.println("\\addcontentsline{toc}{subsection}{"+sd.getName()+"}");
138 String tex = sd.getSection("tex");
140 for(DockDescription bbd : sd) {
141 pw.println("{\\bf "+(bbd.isInputDock() ? "Input: " : "Output: ")+"{\\tt "+bbd.getName()+"}}\n\n");
150 for(DockDescription bbd : sd)
151 if (bbd.isLeft()) leftSize += (boxHeight+boxGap);
152 else rightSize += (boxHeight+boxGap);
154 int totalHeight = Math.max(leftSize, rightSize);
155 int shipWidth = (int)(boxWidth * 1.5);
156 int totalWidth = boxGap*2 + boxWidth*2 + shipWidth;
159 pw.println("\\begin{center}");
160 pw.println("\\begin{empfile}["+sd.getName()+"]");
161 pw.println("\\begin{emp}["+sd.getName()+"]("+(totalWidth+10)+","+(totalHeight+10)+")");
162 pw.println(" beginfig(1)");
163 pw.println(" pickup pencircle scaled 1pt;");
165 "("+((totalWidth-shipWidth)/2)+","+0+")--"+
166 "("+((totalWidth-shipWidth)/2)+","+totalHeight+")--"+
167 "("+(totalWidth-((totalWidth-shipWidth)/2))+","+totalHeight+")--"+
168 "("+(totalWidth-((totalWidth-shipWidth)/2))+","+0+")--"+
169 "("+((totalWidth-shipWidth)/2)+","+0+");");
172 for(DockDescription bbd : sd) {
173 int ypos = (totalHeight - (boxGap/2) - (bbd.isLeft() ? left : right));
174 int half = (totalWidth-shipWidth)/2;
175 int p1 = bbd.isLeft() ? (half-5) : ((totalWidth-half)+5);
176 int p3 = bbd.isLeft() ? (p1 - boxWidth) : (p1 + boxWidth);
177 if (bbd.isInputDock()) {
182 boolean goo = ((bbd.isLeft() && bbd.isInputDock()) || (!bbd.isLeft() && bbd.isOutputDock()));
183 int p2 = goo ? (p3 - (boxHeight/2)) : (p3 + (boxHeight/2));
184 if (bbd.isLeft()) left += (boxHeight+boxGap);
185 else right += (boxHeight+boxGap);
187 pw.println(" label.rt(btex \\tt "+bbd.getName()+" etex, ("+(p1+3)+","+(ypos-boxHeight/2)+"));");
189 pw.println(" label.lft(btex \\tt "+bbd.getName()+" etex, ("+(p1-3)+","+(ypos-boxHeight/2)+"));");
192 " ("+p1+","+ypos+")--"+
193 " ("+p2+","+ypos+")--"+
194 " ("+p3+","+(ypos-(boxHeight/2))+")--"+
195 " ("+p2+","+(ypos-boxHeight)+")--"+
196 " ("+p1+","+(ypos-boxHeight)+")--"+
197 " ("+p1+","+ypos+");");
198 if (bbd.isLeft()) leftSize += boxHeight;
199 else rightSize += boxHeight;
201 pw.println(" endfig;");
202 pw.println("\\end{emp}");
203 pw.println("\\end{empfile}");
204 pw.println("\\end{center}");