forked from MaxRobinson/Alt-AIResearch
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTransitionTable.java
More file actions
299 lines (269 loc) · 7.55 KB
/
TransitionTable.java
File metadata and controls
299 lines (269 loc) · 7.55 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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
package randomPackage;
import java.util.ArrayList;
/**
* class TransitionTable
*
* contains a transition table representing a FSM as created by an episodic
* memory agent
*/
public class TransitionTable {
// CONSTANTS
public static final int UNKNOWN_TRANSITION = -1;
/*
* The table itself is an ArrayList of array since each row in the table
* will always ways be the same length as the alphabet.
*/
protected ArrayList<StateID[]> table;
/* the alphabet of allowed commands used by the FSM */
protected char[] alphabet;
/**
* TransitionTable()
*
* constructor.
*
* @param void - Default Constructor
*
*/
public TransitionTable() {
table = new ArrayList<StateID[]>();
table.add(null);
alphabet = null;
}
/**
* TransitionTable()
*
* constructor.
*
* @param alphabet
* - A given set of chars to use as a transition table's alphabet
*
*
*/
public TransitionTable(char[] alphabet) {
table = new ArrayList<StateID[]>();
table.add(null);
this.alphabet = alphabet;
}
/**
* addNullRow()
*
* A null row is added to the table
*
* @param void
* @return void
*/
public void addNullRow() {
table.add(null);
}
/**
* addEmptyRow()
*
* Add a row of unknown transitions to the table
*
* @param void
* @return void
*/
public void addEmptyRow() {
StateID[] emptyRow = createRow(UNKNOWN_TRANSITION, alphabet.length);
table.add(emptyRow);
}
/**
* addRow()
*
* Given an array of StateID's, add the row to the table
*
* @param row
* - row to add
*
* @return void
*/
public void addRow(StateID[] row) {
// ensure the row is not null, there is a separate method for that
if (row != null) {
table.add(row);
}
}
/**
* setRow()
*
* Given an array of StateID's, and an index, add the row to the table at
* the index
*
* @param row
* - row to add
* @param idx
* - where to insert the row
*
* IMPORTANT: ROW MUST EXIST PRIOR TO THIS METHOD BEING CALLED!!
*
* TODO: Must create test for this method
*
* @return void
*/
public void setRow(StateID[] row, int idx) {
table.set(idx, row);
}
/**
* updateSingleTransition()
*
* Given a source episode and a target episode this method will go to the
* row of the source stateID value and update, at the command that was taken
* to get to the target, the value with the stateID of Target
*
* @param source
* @param target
*
* @return void
*/
public void updateSingleTransition(Episode source, Episode target) {
int rowIndex = source.stateID.get(); // index of row in transitionTable
// to update;
System.out.println("" + rowIndex);
char command = target.command;
int indexOfCommand = findIndexOfChar(command);
StateID targetID = target.stateID;
// check if valid row
if (rowIndex >= table.size()) {
System.err
.println("ERROR: corrupted episode passed to updateSingleTransition");
System.exit(-1);
}
// modify the transition table at the place
table.get(rowIndex)[indexOfCommand] = targetID;
}
/**
* addPath
*
* given a list of Episode objects, this method adds associated transitions.
* If an episode contains a state Id for which there is no row in the table,
* rows are added to accommodate it.
*
* @param eps
* the list of episodes
*/
public void addPath(ArrayList<Episode> eps) {
Episode prev = eps.get(0);
for (int i = 1; i < eps.size(); ++i) { // start at '1' because we're
// processing pairs of Episodes
Episode curr = eps.get(i);
// Make sure there is an entry in the table for this episode
while (this.size() <= prev.stateID.get()) {
this.addEmptyRow();
}
updateSingleTransition(prev, curr);
prev = curr;
}// for
}// addPath
/**
* print()
*
* This method prints an ASCII representation of the table to stdout
*
*/
public void print() {
System.out.print(" ");
for (int i = 0; i < alphabet.length; ++i) {
System.out.printf("%3c", alphabet[i]);
}
System.out.println();
for (int i = 0; i < table.size(); i++) {
System.out.printf("%3d: ", i);
if (table.get(i) != null) {
for (int j = 0; j < alphabet.length; j++) {
System.out.printf("%3d", table.get(i)[j].get());
}
System.out.println();
} else {
System.out.println(" null");
}
}// for
System.out.print(" ");
for (int i = 0; i < alphabet.length; ++i) {
System.out.printf("%3c", alphabet[i]);
}
System.out.println();
}//print
/**
* containsUnknownTransitions()
*
* Given a TransitionTable, checks to see if the table contains any values
* equal to '-1'
*
* @param table
* - The transition table in question
*
* @return boolean - True if the table contains '-1' --OR-- False if the
* table has only 'other' entries
*/
public boolean containsUnknownTransitions() {
for (StateID[] row : table) {
for (int i = 0; row != null && i < row.length; ++i) {
if (row[i].get() == -1) {
return true;
}
}
}
return false;
}
// ---------------------------------HELPER FUNCTIONS-------------------------------------//
/**
* createRow()
*
* create a non empty array of StateID's
*
* @param value
* - The Value to be placed in each slot of the array
*
*
* @param length
* - The length of the array to be created
*
* @return StateID[] - An array of StateID's with specified values
*/
public StateID[] createRow(int value, int length) {
StateID[] row = new StateID[length];
for (int i = 0; i < row.length; i++) {
row[i] = new StateID(value);
}
return row;
}
/**
* size()
*
* gets the numbers of rows in the table.
*
* @return int - size of the table
*/
public int size() {
return table.size();
}
/**
* findIndexOfChar()
*
* Maps char in alphabet to an index
*
* @param command
* - Letter from the alphabet
* @return int - Index of the letter given --OR-- '-1' if the letter is not
* in the alphabet
*
*/
public int findIndexOfChar(char command) {
int index = UNKNOWN_TRANSITION;
for (int j = 0; j < alphabet.length; ++j) {
if (alphabet[j] == command) {
index = j;
return j;
}
}
return index;
}
/**
* get
*
* retrieves a single row from the table
*/
public StateID[] get(int i) {
return table.get(i);
}
}