eliminate ShipDescription.Constant class, use BitVector instead
[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 Fleet fleet;
10     private String name;
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>();
15
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();
22     }
23
24     public final LinkedList<PercolatedPort> percolatedPorts = new LinkedList<PercolatedPort>();
25
26     public ShipDescription(Fleet fleet, String name, BufferedReader r) throws IOException {
27         if (name.endsWith(".ship")) name = name.substring(0, name.length()-".ship".length());
28         this.name = name;
29         this.fleet = fleet;
30         String sectionName = null;
31         StringBuffer sb = new StringBuffer();
32         while(true) {
33             String s = r.readLine();
34             if (s==null || s.startsWith("==")) {
35                 if (sectionName != null) sections.put(sectionName, sb.toString());
36                 if (s==null) break;
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();
44                 continue;
45             }
46             sb.append(s+"\n");
47         }
48         for(String s : sections.keySet())
49             processSection(s);
50     }
51
52     public BitVector getConstant(String name) {
53         BitVector c = constants.get(name);
54         if (c==null)
55             throw new RuntimeException("unknown constant " + name);
56         return c;
57     }
58
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"))
67                     name = val.trim();
68             }
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)));
78                 }
79             }
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; }
86
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")) { 
97                     key = s;
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));
107                     continue;
108                 }
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)));
113                     continue;
114                 } else if (key.startsWith("shortcut to")) {
115                     continue;
116                 }
117                 else throw new RuntimeException("unknown port type: \""+key+"\"");
118
119                 p = null;
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);
124                 if (p==null) {
125                     p = new DockDescription(this, boxname, !rightSide, inbox, dockless);
126                     ports.put(boxname, p);
127                     if (!dockless) docks.put(boxname, p);
128                 }
129             }
130         }
131     }
132
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");
139         /*
140         for(DockDescription bbd : sd) {
141             pw.println("{\\bf "+(bbd.isInputDock() ? "Input: " : "Output: ")+"{\\tt "+bbd.getName()+"}}\n\n");
142         }
143         */
144         int boxGap = 5;
145         int boxHeight = 25;
146         int boxWidth = 75;
147
148         int leftSize = 0;
149         int rightSize = 0;
150         for(DockDescription bbd : sd)
151             if (bbd.isLeft()) leftSize += (boxHeight+boxGap);
152             else              rightSize += (boxHeight+boxGap);
153
154         int totalHeight = Math.max(leftSize, rightSize);
155         int shipWidth = (int)(boxWidth * 1.5);
156         int totalWidth = boxGap*2 + boxWidth*2 + shipWidth;
157
158         pw.println("");
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;");
164         pw.println("      draw "+
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+");");
170         int left = 0;
171         int right = 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()) {
178                 int p1x = p1;
179                 p1 = p3;
180                 p3 = p1x;
181             }
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);
186             if (goo) {
187                 pw.println("      label.rt(btex \\tt "+bbd.getName()+" etex, ("+(p1+3)+","+(ypos-boxHeight/2)+"));");
188             } else {
189                 pw.println("      label.lft(btex \\tt "+bbd.getName()+" etex, ("+(p1-3)+","+(ypos-boxHeight/2)+"));");
190             }
191             pw.println("      draw "+
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;
200         }
201         pw.println("  endfig;");
202         pw.println("\\end{emp}");
203         pw.println("\\end{empfile}");
204         pw.println("\\end{center}");
205         pw.println("");
206
207         if (tex!=null)
208             pw.println(tex);
209     }
210
211 }