- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUserInputParser.java
More file actions
Latest commit
128 lines (104 loc) · 2.93 KB
/
Copy pathUserInputParser.java
File metadata and controls
128 lines (104 loc) · 2.93 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
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
packageUserProgram;
importjava.io.PrintWriter;
importjava.io.IOException;
importjava.util.ArrayList;
importjava.util.HashMap;
importjava.util.List;
importjava.util.Map;
importjava.util.Set;
importjava.util.TreeSet;
importjava.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.
*/
publicclassUserInputParser {
privateList<State> listOfStates;
privateHashMap<String, State> stateNameToState;
privateTreeSet<String> inputs;
privateTreeSet<String> outputs;
publicUserInputParser() {
this.listOfStates = newArrayList<State>();
this.stateNameToState = newHashMap<String, State>();
this.inputs = newTreeSet<String>();
this.outputs = newTreeSet<String>();
}
publicvoidaddState(StatenewState) {
this.listOfStates.add(newState);
}
publicvoidaddInput(Stringinput) {
this.inputs.add(input);
}
publicvoidaddOutput(Stringoutput) {
this.outputs.add(output);
}
publicList<State> getListOfStates() {
returnthis.listOfStates;
}
publicSet<String> getListOfAllStateName() {
Set<String> allNames = newHashSet<String>();
for (inti = 0; i < listOfStates.size(); i++) {
allNames.add(listOfStates.get(i).getStateName());
// System.out.println(listOfStates.get(i));
}
returnallNames;
}
publicvoidaddStateToMap(StringstateName, Statestate) {
this.stateNameToState.put(stateName, state);
}
publicStategetStateByName(StringstateName) {
returnthis.stateNameToState.get(stateName);
}
publicvoidprintListOfStates(StringoutFileName) throwsIOException {
PrintWriterp = newPrintWriter(outFileName);
// INPUTS
p.print("INPUTS ");
for (Strings : inputs) {
p.print(s);
if (s != inputs.last()) {
p.print(", ");
}
}
p.println(";");
// OUTPUTS
p.print("OUTPUTS ");
for (Strings : 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 (States : 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 ");
intnumOfStates = 0;
for (States : listOfStates) {
numOfStates += 1;
if (numOfStates != listOfStates.size()) {
s.printState(p,1);
} else {
s.printState(p,0);
}
}
// CLOSE FILE
p.println("EOF");
p.close();
}
}