1 | package ai.d.ai18;
|
2 |
|
3 | import drjava.util.Log;
|
4 | import drjava.util.StringUtil;
|
5 | import net.luaos.tb.tb15.CentralDatabase;
|
6 | import net.luaos.tb.tb20.Database;
|
7 | import net.luaos.tb.tb20.DatabaseAPI;
|
8 | import net.luaos.tb.tb20.FloraUtil;
|
9 | import net.luaos.tb.tb20.ListEntry;
|
10 |
|
11 | import java.util.ArrayList;
|
12 | import java.util.List;
|
13 | import java.util.regex.Matcher;
|
14 | import java.util.regex.Pattern;
|
15 |
|
16 | public class GetDiskStats2 {
|
17 | public static void main(String[] args) {
|
18 | DatabaseAPI db = CentralDatabase.copyToMemory("df", "GetDiskStats2");
|
19 | String dfOutputID1 = "#t1-kokjwjhpwuas-qbtsjoyahagl";
|
20 | ListEntry dfOutput1 = db.get(dfOutputID1);
|
21 | ListEntry rowMarking = db.oneOfType_flex("RowMarking");
|
22 | List<ListEntry> colMarkings = db.allOfType("ColMarking");
|
23 |
|
24 | DatabaseAPI db2 = CentralDatabase.copyToMemory("df2", "GetDiskStats2");
|
25 | ListEntry dfOutput2 = db2.oneOfType_flex("DFOutput");
|
26 |
|
27 | /*
|
28 | (adapts to differing widths in a table. assumes spaces are used as field
|
29 | separator. looks only at one row.)
|
30 |
|
31 | function adaptColMarkings(text1, text2, rowMarking, colMarkings)
|
32 | row1 = text1.getRow(rowMarking)
|
33 | row2 = text2.getRow(rowMarking)
|
34 | cols1 = findColumns(row1)
|
35 | cols2 = findColumns(row2)
|
36 | newColMarkings = []
|
37 | for colMarking in colMarkings:
|
38 | idx = cols1.indexOf(colMarking)
|
39 | col2 = cols2.get(idx)
|
40 | add col2 to newColMarkings with label of colMarking
|
41 | end
|
42 | return newColMarkings
|
43 | end */
|
44 |
|
45 | String row1 = StringUtil.toLines(dfOutput1.desc).get(rowMarking.getInt("textRow")-1);
|
46 | String row2 = StringUtil.toLines(dfOutput2.desc).get(rowMarking.getInt("textRow")-1);
|
47 | List<String> cols1 = findColumns(row1);
|
48 | List<String> cols2 = findColumns(row2);
|
49 | Database dbOut = new Database();
|
50 | for (ListEntry colMarking : colMarkings) {
|
51 | int idx = findColumn(cols1, colMarking.getPointer("textCol"));
|
52 | if (idx < 0)
|
53 | Log.surprise("Column not found: " + colMarking.desc);
|
54 | else
|
55 | dbOut.emit("ColMarking", colMarking.desc, "onText", dfOutput2.id, "textCol", cols2.get(idx));
|
56 | }
|
57 |
|
58 | FloraUtil.copyNoRewriting(db2, dbOut);
|
59 | System.out.println("\nResult DB:");
|
60 | FloraUtil.copyNoRewriting(db, dbOut, rowMarking.id);
|
61 | FloraUtil.printDatabase(dbOut);
|
62 |
|
63 | GetDiskStats.doIt(dbOut, dfOutput2.id);
|
64 | }
|
65 |
|
66 | private static int findColumn(List<String> cols, String textCol) {
|
67 | System.out.println("findColumn " + cols + " " + textCol);
|
68 | String start = textCol.split("\\-")[0];
|
69 | for (int i = 0; i < cols.size(); i++) {
|
70 | String[] fromTo = cols.get(i).split("\\-");
|
71 | if (rangeContains(fromTo, start))
|
72 | return i;
|
73 | }
|
74 | return -1;
|
75 | }
|
76 |
|
77 | private static boolean rangeContains(String[] fromTo, String start) {
|
78 | if (Integer.parseInt(start) < Integer.parseInt(fromTo[0])) return false;
|
79 | if (fromTo[1].equals("end"))
|
80 | return true;
|
81 | else if (fromTo[1].endsWith(" (inc)"))
|
82 | return Integer.parseInt(start) <= Integer.parseInt(fromTo[1].substring(0, fromTo[1].length() - " (inc)".length()));
|
83 | else
|
84 | throw new RuntimeException("Unparsable range end: " + fromTo[1]);
|
85 | }
|
86 |
|
87 | private static List<String> findColumns(String row) {
|
88 | int i = 0;
|
89 | System.out.println("findColumns: " + row);
|
90 | Matcher matcher = Pattern.compile("[^ ]+").matcher(row);
|
91 | List<String> list = new ArrayList<String>();
|
92 | while (matcher.find()) {
|
93 | int start = matcher.start(), end = matcher.end();
|
94 | list.add((start+1) + "-" + end + " (inc)");
|
95 | }
|
96 | return list;
|
97 | }
|
98 | }
|