-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUserInputParser.java
128 lines (104 loc) · 2.93 KB
/
UserInputParser.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
package UserProgram;
import java.io.PrintWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;
import java.util.*;
/* States with initial values are stored in a list
* Each state contains an array of variables
* Each variable contains a name and value
* So the data structure is a list of states
* of array of variables.
*/
public class UserInputParser {
private List<State> listOfStates;
private HashMap<String, State> stateNameToState;
private TreeSet<String> inputs;
private TreeSet<String> outputs;
public UserInputParser() {
this.listOfStates = new ArrayList<State>();
this.stateNameToState = new HashMap<String, State>();
this.inputs = new TreeSet<String>();
this.outputs = new TreeSet<String>();
}
public void addState(State newState) {
this.listOfStates.add(newState);
}
public void addInput(String input) {
this.inputs.add(input);
}
public void addOutput(String output) {
this.outputs.add(output);
}
public List<State> getListOfStates() {
return this.listOfStates;
}
public Set<String> getListOfAllStateName() {
Set<String> allNames = new HashSet<String>();
for (int i = 0; i < listOfStates.size(); i++) {
allNames.add(listOfStates.get(i).getStateName());
// System.out.println(listOfStates.get(i));
}
return allNames;
}
public void addStateToMap(String stateName, State state) {
this.stateNameToState.put(stateName, state);
}
public State getStateByName(String stateName) {
return this.stateNameToState.get(stateName);
}
public void printListOfStates(String outFileName) throws IOException {
PrintWriter p = new PrintWriter(outFileName);
// INPUTS
p.print("INPUTS ");
for (String s : inputs) {
p.print(s);
if (s != inputs.last()) {
p.print(", ");
}
}
p.println(";");
// OUTPUTS
p.print("OUTPUTS ");
for (String s : outputs) {
p.print(s);
if (s != outputs.last()) {
p.print(", ");
}
}
p.println(";");
// INITIAL STATE and OTHER STATES
p.println("INITIAL_STATE " + listOfStates.get(0).getStateName() + ";");
// OTHER STATES
p.print("OTHER_STATES ");
for (State s : listOfStates) {
//s.printState(p);
if (s == listOfStates.get(0)) {
continue;
}
p.print(s.getStateName());
if (s != listOfStates.get(listOfStates.size() - 1)) {
p.print(", ");
}
}
p.println(";");
// TRANSITIONS
p.print("TRANSITIONS ");
int numOfStates = 0;
for (State s : listOfStates) {
numOfStates += 1;
if (numOfStates != listOfStates.size()) {
s.printState(p,1);
} else {
s.printState(p,0);
}
}
// CLOSE FILE
p.println("EOF");
p.close();
}
}