-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathState.java
More file actions
133 lines (101 loc) · 4.12 KB
/
State.java
File metadata and controls
133 lines (101 loc) · 4.12 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
129
130
131
132
133
package UserProgram;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;
import java.io.*;
import java.util.*;
/* Each state object holds its state name
* and a list of variables, which are
* <variableName, variableValue> pairs.
*/
public class State {
private String stateName;
public Variable[] arrayOfVariables;
private int currArrayIndex;
private int arraySize;
private HashMap<Map.Entry<String, String>, State> transitions;
// private Entry<String, String> inOutPair;
public State(int arrayLength, String name) {
this.stateName = name;
this.arrayOfVariables = new Variable[arrayLength];
this.arraySize = arrayLength;
this.currArrayIndex = 0;
//this.transitions = new HashMap< String, State>();
this.transitions = new HashMap<Map.Entry<String, String>, State>();
}
public String getStateName() {
return this.stateName;
}
public Set<String> getAllInputNames() {
Set<String> allNames = new HashSet<String>();
for (Map.Entry<String, String> oneTrans : transitions.keySet()) {
String input = oneTrans.getKey();
allNames.add(input);
}
return allNames;
}
public void addVariableToArray(Variable variable) {
if (currArrayIndex >= arraySize) {
System.out.println("The array of variable is full!");
return;
}
this.arrayOfVariables[currArrayIndex] = variable;
currArrayIndex++;
}
public void setArrayOfVariables(Variable[] newArrayOfVariables) {
this.arrayOfVariables = newArrayOfVariables;
}
public void addTransition(String input, String output, State destination) {
Map.Entry<String,String> entry = new AbstractMap.SimpleEntry<String, String>(input,output);
this.transitions.put(entry,destination);
}
public void printState(PrintWriter p, int flag) {
int i = 0;
// p.println("the size of keyset is: "+ this.transitions.keySet().size());
if (flag == 1) {
p.print(this.stateName);
//for (String input : this.transitions.keySet().getKey()) {
for (Map.Entry<String,String> oneTrans : this.transitions.keySet()) {
String input = oneTrans.getKey();
String output = oneTrans.getValue();
i += 1;
// p.println("i is " + i);
State destState = this.transitions.get(oneTrans);
p.print(" : " + destState.getStateName() + " ");
if (i != this.transitions.keySet().size()) {
p.println("WHEN (" + input + "," + output + ")" + " COST 1");
} else if (i == this.transitions.keySet().size()){
p.println("WHEN (" + input + "," + output + ")" + " COST 1" + ",");
}
}
} else {
p.print(this.stateName);
// for (String input : this.transitions.keySet()) {
for (Map.Entry<String,String> oneTrans : this.transitions.keySet()) {
String input = oneTrans.getKey();
String output = oneTrans.getValue();
i += 1;
// p.println("i is " + i);
State destState = this.transitions.get(oneTrans);
p.print(" : " + destState.getStateName() + " ");
p.println("WHEN (" + input + "," + output + ")" + " COST 1");
}
}
}
public String encodeToString() {
String s = "";
for (int i = 0; i < arraySize; i++) {
s = s + arrayOfVariables[i].getVariableName() + arrayOfVariables[i].getVariableValue();
}
System.out.println("encodeToString::stateName: " + stateName + " encodedValue: " + s);
return s;
}
public static String encodeArrayToString(Variable[] arrayOfVariables, int arraySize) {
String s = "";
for (int i = 0; i < arraySize; i++) {
s = s + arrayOfVariables[i].getVariableName() + arrayOfVariables[i].getVariableValue();
}
System.out.println("encodeArrayToString::encodedValue: " + s);
return s;
}
}