6752e70f835c8c90b93747cf179446dfdbdff202
[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) throw new RuntimeException("unknown constant " + name);
55         return c;
56     }
57
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"))
66                     name = val.trim();
67             }
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)));
77                 }
78             }
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; }
85
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")) { 
96                     key = s;
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));
106                     continue;
107                 }
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)));
112                     continue;
113                 } else if (key.startsWith("shortcut to")) {
114                     continue;
115                 }
116                 else throw new RuntimeException("unknown port type: \""+key+"\"");
117
118                 p = null;
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);
123                 if (p==null) {
124                     p = new DockDescription(this, boxname, !rightSide, inbox, dockless);
125                     ports.put(boxname, p);
126                     if (!dockless) docks.put(boxname, p);
127                 }
128             }
129         }
130     }
131
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");
138         /*
139         for(DockDescription bbd : sd) {
140             pw.println("{\\bf "+(bbd.isInputDock() ? "Input: " : "Output: ")+"{\\tt "+bbd.getName()+"}}\n\n");
141         }
142         */
143         int boxGap = 5;
144         int boxHeight = 25;
145         int boxWidth = 75;
146
147         int leftSize = 0;
148         int rightSize = 0;
149         for(DockDescription bbd : sd)
150             if (bbd.isLeft()) leftSize += (boxHeight+boxGap);
151             else              rightSize += (boxHeight+boxGap);
152
153         int totalHeight = Math.max(leftSize, rightSize);
154         int shipWidth = (int)(boxWidth * 1.5);
155         int totalWidth = boxGap*2 + boxWidth*2 + shipWidth;
156
157         pw.println("");
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;");
163         pw.println("      draw "+
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+");");
169         int left = 0;
170         int right = 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()) {
177                 int p1x = p1;
178                 p1 = p3;
179                 p3 = p1x;
180             }
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);
185             if (goo) {
186                 pw.println("      label.rt(btex \\tt "+bbd.getName()+" etex, ("+(p1+3)+","+(ypos-boxHeight/2)+"));");
187             } else {
188                 pw.println("      label.lft(btex \\tt "+bbd.getName()+" etex, ("+(p1-3)+","+(ypos-boxHeight/2)+"));");
189             }
190             pw.println("      draw "+
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;
199         }
200         pw.println("  endfig;");
201         pw.println("\\end{emp}");
202         pw.println("\\end{empfile}");
203         pw.println("\\end{center}");
204         pw.println("");
205
206         if (tex!=null)
207             pw.println(tex);
208     }
209
210 }