-
Notifications
You must be signed in to change notification settings - Fork 1
/
MathematicaAdapter.java
113 lines (84 loc) · 2.58 KB
/
MathematicaAdapter.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
import java.io.*;
import java.util.*;
/**
* <h1>Using KnotTheory` from within java!</h1>
* The MathematicaAdapter class makes avaliable functionality from
* the KnotTheory` Mathematica library.
* <p>
* <b>Note:</b> since the methods in this class call Mathematica scripts
* they can take quite some time to run in some cases.
*
* @author Craig Reilly
* @version 0.1
* @since 2015-08-08
*/
public class MathematicaAdapter
{
private LinkedList<Integer> gaussList = new LinkedList<Integer>();
private PrintWriter writer = null;
/**
* This method takes a Gauss code representation of a knot
* and draws the corresponding planar diagram and saves it as a jpg.
* The diagram is saved to a file.
* @param gaussCode is Gauss code representation of a knot in the form "-1, 2, -3, 1, -2, 3"
* @param destName is the name of the file where the image of the planar diagram is to be saved
* @return void
* @exception IOException on jpg file save
*/
public void drawPlanarDiagram(String gaussCode) throws IOException, InterruptedException
{
gaussList = new LinkedList<Integer>();
for (String s: gaussCode.split("[, ]+"))
{
int num = Integer.parseInt(s);
gaussList.addLast(num);
}
int size = gaussList.size();
writer = new PrintWriter("tempPlanar.txt");
String s;
for (int i = 0; i < size; i++)
{
s = "" + gaussList.get(i);
System.out.println(s);
writer.write(s + " ");
}
writer.close();
Runtime r = Runtime.getRuntime();
Process p = r.exec("./planarDraw.m tempPlanar.txt");
p.waitFor();
BufferedReader b = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line = "";
while ((line = b.readLine()) != null) {
System.out.println(line);
}
b.close();
}
public void drawArcPresentation(String gaussCode) throws IOException, InterruptedException
{
gaussList = new LinkedList<Integer>();
for (String s: gaussCode.split("[, ]+"))
{
int num = Integer.parseInt(s);
gaussList.addLast(num);
}
int size = gaussList.size();
writer = new PrintWriter("tempArc.txt");
String s;
for (int i = 0; i < size; i++)
{
s = "" + gaussList.get(i);
System.out.println(s);
writer.write(s + " ");
}
writer.close();
Runtime r = Runtime.getRuntime();
Process p = r.exec("./arcTest.m tempArc.txt");
p.waitFor();
BufferedReader b = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line = "";
while ((line = b.readLine()) != null) {
System.out.println(line);
}
b.close();
}
}