-
Notifications
You must be signed in to change notification settings - Fork 46
Expand file tree
/
Copy pathExternalSeqRecordParser.java
More file actions
44 lines (41 loc) · 1.73 KB
/
ExternalSeqRecordParser.java
File metadata and controls
44 lines (41 loc) · 1.73 KB
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
import tlc2.value.impl.*;
import util.UniqueString;
import java.io.*;
import java.util.*;
public class ExternalSeqRecordParser {
// @TLAPlusOperator(identifier = "ExSeqRcdParser", module = "ExternalSeqRecordParser")
public static Value ExSeqRcdParser(final StringValue absolutePath) throws IOException {
// read the log file at [absolutePath]
BufferedReader br = new BufferedReader(new FileReader(absolutePath.val.toString()));
// initialize array for all parsed records
List<RecordValue> rcdSeq = new ArrayList<>();
try {
String line = br.readLine();
while (line != null) {
// initialize arrays for field values [fields] and [values]
List<UniqueString> fields = new ArrayList<>();
List<Value> values = new ArrayList<>();
// split string on seperator into array of filed and value
String[] lnarr = line.split(", ");
// parse each entry of [lnarr] as a field-value pair
parsePair(lnarr[0].split(" : "), fields, values);
parsePair(lnarr[1].split(" : "), fields, values);
// add record to the sequence
rcdSeq.add(new RecordValue(fields.toArray(new UniqueString[0]), values.toArray(new Value[0]), true));
line = br.readLine();
}
} finally {
br.close();
}
// return the aggregated sequence of records
return new TupleValue(rcdSeq.toArray(new Value[0]));
}
private static void parsePair(String[] pair, List<UniqueString> fields, List<Value> values) {
fields.add(UniqueString.uniqueStringOf(pair[0]));
if (pair[1].equals("false") || pair[1].equals("true")) {
values.add(new BoolValue(Boolean.parseBoolean(pair[1])));
} else {
values.add(IntValue.gen(Integer.parseInt(pair[1])));
}
}
}