-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathCompress.java
executable file
·190 lines (180 loc) · 6.96 KB
/
Compress.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
import java.util.*;
import java.util.regex.*;
import java.io.*;
import java.nio.*;
import java.text.*;
public class Compress {
public static long fileId = 0;
public static final Pattern nodeRegex = Pattern.compile("([0-9]+\\.[0-9]+) \\[label=\"(.+)\"\\]");
public static final Pattern edgeRegex = Pattern.compile("([0-9]+\\.[0-9]+) -> ([0-9]+\\.[0-9]+) \\[label=\"R: (-?[0-9\\.]+) us\"\\]");
public static DecimalFormat df = new DecimalFormat("#.###");
public static void main(String args[]) throws IOException {
if (args.length < 2) {
System.out.println("Usage: java Compress <dot> <output_dir>");
System.exit(-1);
}
File dot = new File(args[0]);
if (!dot.exists() || !dot.isFile()) {
System.out.println("Cannot read DOT file");
System.exit(-2);
}
File outputDir = new File(args[1]);
if (outputDir.isFile()) {
System.out.println("Invalid directory");
System.exit(-3);
}
if (!outputDir.exists()) {
if (!outputDir.mkdir()) {
System.out.println("Could not make directory");
System.exit(-4);
}
}
df.setMinimumFractionDigits(3);
parseFile(dot, args[1]);
}
public static void parseFile(File dot, String outputDir) throws IOException {
Scanner sc = new Scanner(new FileInputStream(dot));
PrintStream out = new PrintStream(new FileOutputStream(outputDir + "/" + dot.getName()), true);
while(sc.hasNext()) {
String line = sc.nextLine();
out.println(line);
if (line.matches("\\# [0-9]+ R: [0-9\\.]+ usecs")) {
line = sc.nextLine();
out.println(line);
compress(sc, out, outputDir);
}
}
}
public static void compress(Scanner sc, PrintStream out, String outputDir) throws IOException {
String line = sc.nextLine();
Matcher m;
if ((line.indexOf("NEW_BLOCK") != -1 || line.indexOf("APPEND_BLOCK") != -1) && (m = nodeRegex.matcher(line)).matches()) {
Graph g = new Graph(m.group(1), m.group(2));
FrequencyGraph fg = new FrequencyGraph(m.group(2), g.root);
line = sc.nextLine();
while(line.indexOf("}") == -1) {
if (line.indexOf("->") == -1) {
m = nodeRegex.matcher(line);
if (!m.matches()) {
System.out.println("Doesn't match: " + line);
System.exit(-5);
}
g.addNode(m.group(1), m.group(2));
} else {
m = edgeRegex.matcher(line);
if (!m.matches()) {
System.out.println("Doesn't match: " + line);
System.exit(-6);
}
g.addEdge(m.group(1), m.group(2), Double.parseDouble(m.group(3)));
}
line = sc.nextLine();
}
fillFrequencyGraph(g, g.root, fg, new HashSet<String>());
printFrequencyGraphNodes(out, fg, fg.root, new HashSet<String>());
printFrequencyGraphEdges(out, fg, fg.root, new HashSet<String>(), outputDir);
out.println("}");
} else {
out.println(line);
line = sc.nextLine();
while(line.indexOf("}") == -1) {
out.println(line);
line = sc.nextLine();
}
out.println(line);
}
}
public static class Graph {
String root;
HashMap<String, String> name;
HashMap<String, ArrayList<Edge>> adjacencyList;
public Graph(String root, String rootName) {
this.root = root;
name = new HashMap<String, String>();
name.put(root, rootName);
adjacencyList = new HashMap<String, ArrayList<Edge>>();
adjacencyList.put(root, new ArrayList<Edge>());
}
public void addNode(String id, String name) {
this.name.put(id, name);
adjacencyList.put(id, new ArrayList<Edge>());
}
public void addEdge(String id, String id2, double latency) {
if (!adjacencyList.containsKey(id))
adjacencyList.put(id, new ArrayList<Edge>());
adjacencyList.get(id).add(new Edge(id2, latency));
}
}
public static class FrequencyGraph {
String root;
HashMap<String, HashMap<String, ArrayList<Double>>> adjacencyList;
HashMap<String, String> ids;
public FrequencyGraph(String root, String id) {
this.root = root;
adjacencyList = new HashMap<String, HashMap<String, ArrayList<Double>>>();
adjacencyList.put(root, new HashMap<String, ArrayList<Double>>());
ids = new HashMap<String, String>();
ids.put(root, id);
}
public void addNode(String node, String id) {
if (!ids.containsKey(node)) {
ids.put(node, id);
adjacencyList.put(node, new HashMap<String, ArrayList<Double>>());
}
}
public void addEdge(String from, String to, double latency) {
if (!adjacencyList.containsKey(from))
adjacencyList.put(from, new HashMap<String, ArrayList<Double>>());
if (!adjacencyList.get(from).containsKey(to))
adjacencyList.get(from).put(to, new ArrayList<Double>());
adjacencyList.get(from).get(to).add(latency);
}
}
public static class Edge {
String id;
double latency;
public Edge(String id, double latency) {
this.id = id;
this.latency = latency;
}
}
public static void fillFrequencyGraph (Graph g, String node, FrequencyGraph fg, HashSet<String> visited) {
if (visited.contains(node))
return;
visited.add(node);
fg.addNode(g.name.get(node), node);
for (Edge e : g.adjacencyList.get(node)) {
fg.addEdge(g.name.get(node), g.name.get(e.id), e.latency);
//System.out.println(g.name.get(node) + " -> " + g.name.get(e.id) + " " + e.latency);
fillFrequencyGraph(g, e.id, fg, visited);
}
}
public static void printFrequencyGraphNodes(PrintStream out, FrequencyGraph fg, String node, HashSet<String> visited) throws IOException {
if (visited.contains(node))
return;
visited.add(node);
out.println(fg.ids.get(node) + " [label=\"" + node + "\"]");
for (Map.Entry<String, ArrayList<Double>> entry : fg.adjacencyList.get(node).entrySet()) {
printFrequencyGraphNodes(out, fg, entry.getKey(), visited);
}
}
public static void printFrequencyGraphEdges(PrintStream out, FrequencyGraph fg, String node, HashSet<String> visited, String outputDir) throws IOException {
if (visited.contains(node))
return;
visited.add(node);
for (Map.Entry<String, ArrayList<Double>> entry : fg.adjacencyList.get(node).entrySet()) {
if (entry.getValue().size() == 1) {
out.println(fg.ids.get(node) + " -> " + fg.ids.get(entry.getKey()) + " [label=\"R: " + df.format(entry.getValue().get(0)) + " us\"]");
} else {
ArrayList<Double> latencies = entry.getValue();
String fileName = fileId++ + ".dat";
PrintStream datOut = new PrintStream(new FileOutputStream(outputDir + "/" + fileName), true);
for(int i = 0; i < latencies.size(); i++)
datOut.println(df.format(latencies.get(i)) + " us");
datOut.close();
out.println(fg.ids.get(node) + " -> " + fg.ids.get(entry.getKey()) + " [label=\"F: " + fileName + "\"]");
}
printFrequencyGraphEdges(out, fg, entry.getKey(), visited, outputDir);
}
}
}