total overhaul: fleetcode-1.0 api finished
[fleet.git] / src / edu / berkeley / fleet / two / ShipDescription.java
1 package edu.berkeley.fleet.two;
2 import edu.berkeley.fleet.api.*;
3 import java.io.*;
4 import java.util.*;
5
6 /** NOT YET FINALIZED: A description (specification) of a ship */
7 public class ShipDescription implements Iterable<DockDescription> {
8
9     private String name;
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>();
13
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(); }
18
19     public ShipDescription(String name, BufferedReader r) throws IOException {
20         this.name = name;
21         String sectionName = null;
22         StringBuffer sb = new StringBuffer();
23         while(true) {
24             String s = r.readLine();
25             if (s==null || s.startsWith("==")) {
26                 if (sectionName != null) sections.put(sectionName, sb.toString());
27                 if (s==null) break;
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();
35                 continue;
36             }
37             sb.append(s+"\n");
38         }
39         for(String s : sections.keySet())
40             processSection(s);
41     }
42
43     public Constant getConstant(String name) {
44         return constants.get(name);
45     }
46
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"))
55                     name = val.trim();
56             }
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));
66                 }
67             }
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; }
74
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));
86                     continue;
87                 } else if (key.startsWith("shortcut to")) {
88                     continue;
89                 }
90                 else throw new RuntimeException("unknown port type: \""+key+"\"");
91
92                 p = null;
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);
97                 if (p==null) {
98                     p = new DockDescription(this, boxname, !rightSide, inbox);
99                     docks.put(boxname, p);
100                 }
101             }
102         }
103     }
104
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");
111         /*
112         for(DockDescription bbd : sd) {
113             pw.println("{\\bf "+(bbd.isInputDock() ? "Input: " : "Output: ")+"{\\tt "+bbd.getName()+"}}\n\n");
114         }
115         */
116         int boxGap = 5;
117         int boxHeight = 25;
118         int boxWidth = 75;
119
120         int leftSize = 0;
121         int rightSize = 0;
122         for(DockDescription bbd : sd)
123             if (bbd.isLeft()) leftSize += (boxHeight+boxGap);
124             else              rightSize += (boxHeight+boxGap);
125
126         int totalHeight = Math.max(leftSize, rightSize);
127         int shipWidth = (int)(boxWidth * 1.5);
128         int totalWidth = boxGap*2 + boxWidth*2 + shipWidth;
129
130         pw.println("");
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;");
136         pw.println("      draw "+
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+");");
142         int left = 0;
143         int right = 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()) {
150                 int p1x = p1;
151                 p1 = p3;
152                 p3 = p1x;
153             }
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);
158             if (goo) {
159                 pw.println("      label.rt(btex \\tt "+bbd.getName()+" etex, ("+(p1+3)+","+(ypos-boxHeight/2)+"));");
160             } else {
161                 pw.println("      label.lft(btex \\tt "+bbd.getName()+" etex, ("+(p1-3)+","+(ypos-boxHeight/2)+"));");
162             }
163             pw.println("      draw "+
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;
172         }
173         pw.println("  endfig;");
174         pw.println("\\end{emp}");
175         pw.println("\\end{empfile}");
176         pw.println("\\end{center}");
177         pw.println("");
178
179         if (tex!=null)
180             pw.println(tex);
181     }
182
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);
196                     switch(c) {
197                         case '0': clearbits |= (1<<i); break;
198                         case '1': setbits   |= (1<<i); break;
199                         case '.': break;
200                         case 's': signExtend = true;  numberOffset = i; numberWidth++; break;
201                         case 'u': signExtend = false; numberOffset = i; numberWidth++; break;
202                     }
203                 }
204             } else {
205                 setbits = Long.parseLong(s);
206                 clearbits = ~setbits;
207             }
208         }
209     }
210
211 }