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> {
10 private LinkedHashMap<String,DockDescription> docks = new LinkedHashMap<String,DockDescription>();
11 private HashMap<String,String> sections = new HashMap<String,String>();
12 private HashMap<String,Constant> constants = new HashMap<String,Constant>();
14 public String getName() { return name; }
15 public String getSection(String sectionName) { return sections.get(sectionName); }
16 public DockDescription getDockDescription(String name) { return docks.get(name); }
17 public Iterator<DockDescription> iterator() { return docks.values().iterator(); }
19 public ShipDescription(String name, BufferedReader r) throws IOException {
21 String sectionName = null;
22 StringBuffer sb = new StringBuffer();
24 String s = r.readLine();
25 if (s==null || s.startsWith("==")) {
26 if (sectionName != null) sections.put(sectionName, sb.toString());
28 sb = new StringBuffer();
29 sectionName = s.trim();
30 while(sectionName.startsWith("="))
31 sectionName = sectionName.substring(1);
32 while(sectionName.endsWith("="))
33 sectionName = sectionName.substring(0, sectionName.length()-1);
34 sectionName = sectionName.trim().toLowerCase();
39 for(String s : sections.keySet())
43 public Constant getConstant(String name) {
44 return constants.get(name);
47 private void processSection(String section) throws IOException {
48 if (section.equals("")) {
49 BufferedReader br = new BufferedReader(new StringReader(sections.get(section)));
50 for(String s = br.readLine(); s != null; s = br.readLine()) {
51 if (s.trim().length()==0) continue;
52 String key = s.substring(0, s.indexOf(':')).trim();
53 String val = s.substring(s.indexOf(':')+1).trim();
54 if (key.toLowerCase().equals("ship"))
57 } else if (section.equals("constants")) {
58 BufferedReader br = new BufferedReader(new StringReader(sections.get(section)));
59 for(String s = br.readLine(); s != null; s = br.readLine()) {
60 if (s.indexOf(':')==-1) continue;
61 String key = s.substring(0, s.indexOf(':')).trim();
62 if (key.startsWith("constant")) {
63 String constname = key.substring("constant".length()+1).trim();
64 String val = s.substring(s.indexOf(':')+1).trim();
65 constants.put(constname, new Constant(val));
68 } else if (section.equals("ports")) {
69 BufferedReader br = new BufferedReader(new StringReader(sections.get(section)));
70 boolean rightSide = false;
71 DockDescription p = null;
72 for(String s = br.readLine(); s != null; s = br.readLine()) {
73 if (s.trim().length()==0) { rightSide = true; continue; }
75 String key = s.substring(0, s.indexOf(':')).trim();
76 boolean inbox = false;
77 key = key.replaceAll(" +", " ");
78 if (key.equals("data in")) { inbox = true; }
79 else if (key.equals("data out")) { inbox = false; }
80 else if (key.equals("in")) { inbox = true; }
81 else if (key.equals("out")) { inbox = false; }
82 else if (key.startsWith("constant")) {
83 String constname = key.substring("constant".length()+1).trim();
84 String val = s.substring(s.indexOf(':')+1).trim();
85 p.addConstant(constname, new Constant(val));
87 } else if (key.startsWith("shortcut to")) {
90 else throw new RuntimeException("unknown port type: \""+key+"\"");
93 String val = s.substring(s.indexOf(':')+1).trim();
94 String boxname = val.indexOf('.') != -1 ? val.substring(0, val.indexOf('.')) : val;
95 String dest = val.indexOf('.') != -1 ? val.substring(val.indexOf('.')+1) : "";
96 p = docks.get(boxname);
98 p = new DockDescription(this, boxname, !rightSide, inbox);
99 docks.put(boxname, p);
105 public void printTeX(PrintWriter pw) throws Exception {
106 ShipDescription sd = this;
107 pw.println("\\pagebreak");
108 pw.println("\\section*{The {\\tt "+sd.getName()+"} Ship}");
109 pw.println("\\addcontentsline{toc}{subsection}{"+sd.getName()+"}");
110 String tex = sd.getSection("tex");
112 for(DockDescription bbd : sd) {
113 pw.println("{\\bf "+(bbd.isInputDock() ? "Input: " : "Output: ")+"{\\tt "+bbd.getName()+"}}\n\n");
122 for(DockDescription bbd : sd)
123 if (bbd.isLeft()) leftSize += (boxHeight+boxGap);
124 else rightSize += (boxHeight+boxGap);
126 int totalHeight = Math.max(leftSize, rightSize);
127 int shipWidth = (int)(boxWidth * 1.5);
128 int totalWidth = boxGap*2 + boxWidth*2 + shipWidth;
131 pw.println("\\begin{center}");
132 pw.println("\\begin{empfile}["+sd.getName()+"]");
133 pw.println("\\begin{emp}["+sd.getName()+"]("+(totalWidth+10)+","+(totalHeight+10)+")");
134 pw.println(" beginfig(1)");
135 pw.println(" pickup pencircle scaled 1pt;");
137 "("+((totalWidth-shipWidth)/2)+","+0+")--"+
138 "("+((totalWidth-shipWidth)/2)+","+totalHeight+")--"+
139 "("+(totalWidth-((totalWidth-shipWidth)/2))+","+totalHeight+")--"+
140 "("+(totalWidth-((totalWidth-shipWidth)/2))+","+0+")--"+
141 "("+((totalWidth-shipWidth)/2)+","+0+");");
144 for(DockDescription bbd : sd) {
145 int ypos = (totalHeight - (boxGap/2) - (bbd.isLeft() ? left : right));
146 int half = (totalWidth-shipWidth)/2;
147 int p1 = bbd.isLeft() ? (half-5) : ((totalWidth-half)+5);
148 int p3 = bbd.isLeft() ? (p1 - boxWidth) : (p1 + boxWidth);
149 if (bbd.isInputDock()) {
154 boolean goo = ((bbd.isLeft() && bbd.isInputDock()) || (!bbd.isLeft() && bbd.isOutputDock()));
155 int p2 = goo ? (p3 - (boxHeight/2)) : (p3 + (boxHeight/2));
156 if (bbd.isLeft()) left += (boxHeight+boxGap);
157 else right += (boxHeight+boxGap);
159 pw.println(" label.rt(btex \\tt "+bbd.getName()+" etex, ("+(p1+3)+","+(ypos-boxHeight/2)+"));");
161 pw.println(" label.lft(btex \\tt "+bbd.getName()+" etex, ("+(p1-3)+","+(ypos-boxHeight/2)+"));");
164 " ("+p1+","+ypos+")--"+
165 " ("+p2+","+ypos+")--"+
166 " ("+p3+","+(ypos-(boxHeight/2))+")--"+
167 " ("+p2+","+(ypos-boxHeight)+")--"+
168 " ("+p1+","+(ypos-boxHeight)+")--"+
169 " ("+p1+","+ypos+");");
170 if (bbd.isLeft()) leftSize += boxHeight;
171 else rightSize += boxHeight;
173 pw.println(" endfig;");
174 pw.println("\\end{emp}");
175 pw.println("\\end{empfile}");
176 pw.println("\\end{center}");
183 public class Constant {
184 public long setbits = 0;
185 public long clearbits = 0;
186 public boolean signExtend = false;
187 public int numberOffset = 0;
188 public int numberWidth = 0;
189 public Constant(String s) {
190 if (s.startsWith("0x")) {
191 setbits = Long.parseLong(s.substring(2), 16);
192 clearbits = ~setbits;
193 } else if (s.length() == 37) {
194 for(int i=36; i>=0; i--) {
195 char c = s.charAt(36-i);
197 case '0': clearbits |= (1<<i); break;
198 case '1': setbits |= (1<<i); break;
200 case 's': signExtend = true; numberOffset = i; numberWidth++; break;
201 case 'u': signExtend = false; numberOffset = i; numberWidth++; break;
205 setbits = Long.parseLong(s);
206 clearbits = ~setbits;