-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCGT2DOT.java
75 lines (61 loc) · 2.47 KB
/
CGT2DOT.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
package cgt2dot;
import java.io.*;
import java.util.*;
public class CGT2DOT {
public static void main(String[] args) throws IOException {
// open text file with cgtsuite information
Scanner sc = new Scanner(new BufferedReader( new FileReader(args[0]) ));
StringBuilder games = new StringBuilder(sc.next());
StringBuilder lattice = new StringBuilder(sc.next());
sc.close();
games.deleteCharAt(0); // remove [
games.deleteCharAt(games.length()-1); // remove ]
lattice.deleteCharAt(0); // remove [
lattice.deleteCharAt(lattice.length()-1); // remove ]
// make relevant commas into semicolons
int open = 0;
for(int i=0;i<games.length();i++) {
if (open==0 && games.charAt(i) == ',')
games.setCharAt(i, ';');
else if (games.charAt(i) == '{' || games.charAt(i) == '(')
open++;
else if (games.charAt(i) == '}' || games.charAt(i) == ')')
open--;
}
open = 0;
for(int i=0;i<lattice.length();i++) {
if (open==0 && lattice.charAt(i) == ',')
lattice.setCharAt(i, ';');
else if (lattice.charAt(i) == '{' || lattice.charAt(i) == '(')
open++;
else if (lattice.charAt(i) == '}' || lattice.charAt(i) == ')')
open--;
}
// create .dot from the cgtsuite text file
BufferedWriter outF = new BufferedWriter( new FileWriter(args[0]+"_closure.dot") );
outF.write("digraph G {\n\n");
outF.write(" rankdir=BT;");
// process string: first collect all different tokens
Scanner sg = new Scanner(games.toString()).useDelimiter("\\s*[.;]\\s*");
HashMap<String,Integer> labels = new HashMap<String,Integer>();
int node = 1;
while (sg.hasNext()) {
String game = sg.next();
labels.put(game, node);
outF.write(" node"+node+" [label = \""+game+"\"];\n");
node++;
}
sg.close();
// now, collect pairs of games (1st game < 2nd game)
sg = new Scanner(lattice.toString()).useDelimiter("\\s*[.;]\\s*");
while (sg.hasNext()) {
String game1 = sg.next();
String game2 = sg.next();
int index1 = labels.get(game1);
int index2 = labels.get(game2);
outF.write(" node" + index1 + " -> node" + index2 + ";\n");
}
outF.write("}");
outF.close();
}
}