-
Notifications
You must be signed in to change notification settings - Fork 1
/
AppToCallFromPython.java
88 lines (65 loc) · 3.31 KB
/
AppToCallFromPython.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
import java.io.*;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
public class AppToCallFromPython {
private static List<String> oniList;
private static List<String> pniList;
//For norms involving a maximum of two events (e.g. 'ab' being prohibted). If we would also want to know about 'abc' then change this value to 3.
private static int noOfEventsBeforeSanction = 2;
public static void main(String[] args) {
// Code adapted from https://www.caveofprogramming.com/java/java-file-reading-and-writing-files-in-java.html
//For a unix machine the path has to be changed to "/observations.txt";
String inputFileName = Paths.get(".").toAbsolutePath().normalize().toString() + "/observations.txt";
String oniOutputFileName = "oni_out.txt";
String pniOutputFileName = "pni_out.txt";
String line = null;
try {
FileReader fileReader =
new FileReader(inputFileName);
BufferedReader bufferedReader =
new BufferedReader(fileReader);
FileWriter oniFileWriter =
new FileWriter(oniOutputFileName);
FileWriter pniFileWriter =
new FileWriter(pniOutputFileName);
BufferedWriter oniBufferedWriter =
new BufferedWriter(oniFileWriter);
BufferedWriter pniBufferedWriter =
new BufferedWriter(pniFileWriter);
//The list below stores event sequences as Strings
List<String> sequenceList = new ArrayList<String>();
while((line = bufferedReader.readLine()) != null) {
//System.out.println("Observation read: " + line);
sequenceList.add(line);
}
//Call the PNI algorithm
NormIdentificationAlgorithm pni = new NormIdentificationAlgorithm(sequenceList.toArray(new String[sequenceList.size()]));
pniList = pni.computeProhibitionNorms(0, noOfEventsBeforeSanction, pniOutputFileName);
//Write PNI output to file
for (int i = 0; i < pniList.size(); i++) {
pniBufferedWriter.write(pniList.get(i));
pniBufferedWriter.newLine();
}
//Call the ONI algorithm
NormIdentificationAlgorithm oni = new NormIdentificationAlgorithm(sequenceList.toArray(new String[sequenceList.size()]));
oniList = oni.computeObligationNorms(0, noOfEventsBeforeSanction-1, oniOutputFileName);
//Write oni output to file
for (int i = 0; i < oniList.size(); i++) {
oniBufferedWriter.write(oniList.get(i));
oniBufferedWriter.newLine();
}
bufferedReader.close();
oniBufferedWriter.close();
pniBufferedWriter.close();
}
catch(FileNotFoundException ex) {
System.out.println("Unable to open file '" +
inputFileName + "'");
ex.printStackTrace();
}
catch(IOException ex) {
ex.printStackTrace();
}
}
}