-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDriver.java
More file actions
684 lines (580 loc) · 30.4 KB
/
Driver.java
File metadata and controls
684 lines (580 loc) · 30.4 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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
package UserProgram;
import java.io.PrintWriter;
import java.io.IOException;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;
import java.util.*;
/* The driver program interacts with user,
* asking for inputs of #of variables in each state,
* names and initial values as well as all possible transitions
*/
public class Driver {
private static Scanner keyboard = new Scanner(System.in);
private static int numVariablesInEachState;
private static String[] arrayOfVariableNames;
private static UserInputParser userInputParser;
private static Queue<State> q;
private static PrintWriter log;
private static boolean isReadingFromLog;
public static void main(String[] args) throws IOException, FileNotFoundException {
System.out.println("Loading previous inputs from the log? enter 1 for yes, enter 0 for no");
int answer = keyboard.nextInt();
keyboard.nextLine();
if (answer == 1) {
keyboard = new Scanner(new File("InputLog.txt"));
isReadingFromLog = true;
log = new PrintWriter(new FileWriter("InputLog.txt", true));
} else {
isReadingFromLog = false;
log = new PrintWriter("InputLog.txt");
}
userInputParser = new UserInputParser();
q = new LinkedList<State>();
String outFileName = askOutFileName();
takeInput();
printInfo(outFileName) ;
}
public static String askOutFileName() throws FileNotFoundException {
System.out.println("======== Input File name ========");
System.out.println("what's the output file name?");
// Switch scanner if the log reaches its end.
if (isReadingFromLog && !keyboard.hasNextLine()) {
log.println();
keyboard = new Scanner(System.in);
System.out.println("Log file reaches the end, please input the answer to the previous question");
isReadingFromLog = false;
}
String outFileName = keyboard.nextLine();
if (!isReadingFromLog) {
log.println(outFileName);
} else {
System.out.println("Loaded answer:" + outFileName);
}
return outFileName;
}
/* For each state:
* Step 1: ask user how many variables are in each state
* Step 2: ask user to input each name of the variable
* Step 3: looping to ask user input state names and
* values of variables that the state contains
*
* First ask user information of the first state and add the first state to the queue
* Then recursively pop out the head of the queue, asking for details of the transitions
* for the state:
* 1: input/output pair of each transition
* 2: destnation state of each correcponding transition
* a. if the destnation state does not exist, create one (ask info for the state in
* the order of Step1 - Step3) and add it to the queue + unique set of states.
* then add the transition between current state and destination state
* b. if the destnation state already exists in the unique set of states, just add
* the transition between current state and destination state
*/
public static void takeInput() {
System.out.println("========Ask whether a fully specified FSM========");
System.out.println("Is this a fully specified FSM? (y/n)");
// Switch scanner if the log reaches its end.
if (isReadingFromLog && !keyboard.hasNextLine()) {
log.println();
keyboard = new Scanner(System.in);
System.out.println("Log file reaches the end, please input the answer to the previous question");
isReadingFromLog = false;
}
String response = keyboard.nextLine();
if (!isReadingFromLog) {
log.println(response);
} else {
System.out.println("Loaded answer:" + response);
}
System.out.println("the response is: "+ response);
if (response.equals("y")) {
System.out.println(" this is a fully specified FSM");
try {
fullySpecifiedFSM();
} catch (Exception e) {
log.close();
}
} else {
System.out.println(" this is NOT a fully specified FSM");
try {
notFullyFSM();
} catch (Exception e) {
log.close();
}
}
}
private static void fullySpecifiedFSM (){
// ask user how many variables are in each state
System.out.println("========Now asking for information on variables========");
System.out.println("How many variables are in each state?");
// Switch scanner if the log reaches its end.
if (isReadingFromLog && !keyboard.hasNextLine()) {
log.println();
keyboard = new Scanner(System.in);
System.out.println("Log file reaches the end, please input the answer to the previous question");
isReadingFromLog = false;
}
numVariablesInEachState = keyboard.nextInt();
keyboard.nextLine();
if (!isReadingFromLog) {
log.println(numVariablesInEachState);
} else {
System.out.println("Loaded answer:" + numVariablesInEachState);
}
arrayOfVariableNames = new String[numVariablesInEachState];
// ask user to input names of variables one by one
for (int i = 0; i < numVariablesInEachState; i++) {
System.out.println("Please enter the name of variable #" + (i+1) + ":");
// Switch scanner if the log reaches its end.
if (isReadingFromLog && !keyboard.hasNextLine()) {
log.println();
keyboard = new Scanner(System.in);
System.out.println("Log file reaches the end, please input the answer to the previous question");
isReadingFromLog = false;
}
arrayOfVariableNames[i] = keyboard.nextLine();
if (!isReadingFromLog) {
log.println(arrayOfVariableNames[i]);
} else {
System.out.println("Loaded answer:" + arrayOfVariableNames[i]);
}
}
// ask user how many inputs are there
System.out.println("========Now asking for inputs ========");
System.out.println("How many inputs are there?");
// Switch scanner if the log reaches its end.
if (isReadingFromLog && !keyboard.hasNextLine()) {
log.println();
keyboard = new Scanner(System.in);
System.out.println("Log file reaches the end, please input the answer to the previous question");
isReadingFromLog = false;
}
int numInputs = keyboard.nextInt();
keyboard.nextLine();
if (!isReadingFromLog) {
log.println(numInputs);
} else {
System.out.println("Loaded answer:" + numInputs);
}
String[] arrayOfInputs = new String[numInputs];
// ask for inputs names and possible input values
for (int i = 0; i < numInputs; i++) {
System.out.println("Please enter the value of input #" + (i+1) + ":");
// Switch scanner if the log reaches its end.
if (isReadingFromLog && !keyboard.hasNextLine()) {
log.println();
keyboard = new Scanner(System.in);
System.out.println("Log file reaches the end, please input the answer to the previous question");
isReadingFromLog = false;
}
arrayOfInputs[i] = keyboard.nextLine();
if (!isReadingFromLog) {
log.println(arrayOfInputs[i]);
} else {
System.out.println("Loaded answer:" + arrayOfInputs[i]);
}
}
// ask for information on states and transitions
System.out.println("========Now asking for information on states and transitions========");
System.out.println("(Please notice that state #1 is the initial state)");
/* Get the info for the first state */
// Get the state name for the first state
System.out.println("Please enter the name of state #1:");
// Switch scanner if the log reaches its end.
if (isReadingFromLog && !keyboard.hasNextLine()) {
log.println();
keyboard = new Scanner(System.in);
System.out.println("Log file reaches the end, please input the answer to the previous question");
isReadingFromLog = false;
}
String stateName = keyboard.nextLine();
if (!isReadingFromLog) {
log.println(stateName);
} else {
System.out.println("Loaded answer:" + stateName);
}
State newState = new State(numVariablesInEachState, stateName);
// Get the initial values of variables in the first state
for (int i = 0; i < numVariablesInEachState; i++) {
System.out.println("Please enter initial value for variable " + arrayOfVariableNames[i] + " in state " + stateName + ":");
// Switch scanner if the log reaches its end.
if (isReadingFromLog && !keyboard.hasNextLine()) {
log.println();
keyboard = new Scanner(System.in);
System.out.println("Log file reaches the end, please input the answer to the previous question");
isReadingFromLog = false;
}
int initialValue = keyboard.nextInt();
if (!isReadingFromLog) {
log.println(initialValue);
} else {
System.out.println("Loaded answer:" + initialValue);
}
Variable newVariable = new Variable(arrayOfVariableNames[i], initialValue);
newState.addVariableToArray(newVariable);
}
// Add the first state to the queue and set
q.add(newState);
userInputParser.addStateToMap(newState.encodeToString(), newState);
userInputParser.addState(newState);
int stateIndex = 2;
int answer = 1;
keyboard.nextLine();
// Main loop
while (!q.isEmpty()) {
// Pop out the first state in the waiting queue
State currState = q.poll();
// Ask for transitions regarding the current state
for (int transitionIndex = 0; transitionIndex < numInputs; transitionIndex++) {
// Get input
System.out.println("The input name for transition #" + (transitionIndex+1) + " in state " + currState.getStateName() + "is: " + arrayOfInputs[transitionIndex]);
String inputName = arrayOfInputs[transitionIndex];
// Skip this transition if such transition does not exist
// System.out.println("Does such transition corresponding to input " + arrayOfInputs[transitionIndex] + " exist for state " + currState.getStateName() + "? enter 1 for yes, enter 0 for no");
// answer = keyboard.nextInt();
// keyboard.nextLine();
// if (answer == 0) {
// continue;
// }
// No need to check duplicate inputs here
// Get output
System.out.println("Given input " + arrayOfInputs[transitionIndex] +", please enter its output name for transition #" + (transitionIndex+1) + " in state " + currState.getStateName()
+ ":");
// Switch scanner if the log reaches its end.
if (isReadingFromLog && !keyboard.hasNextLine()) {
log.println();
keyboard = new Scanner(System.in);
System.out.println("Log file reaches the end, please input the answer to the previous question");
isReadingFromLog = false;
}
String outputName = keyboard.nextLine();
if (!isReadingFromLog) {
log.println(outputName);
} else {
System.out.println("Loaded answer:" + outputName);
}
// Get encoded destination
System.out.println("Now asking for internal memory of destination state");
Variable[] newArrayOfVariables = new Variable[numVariablesInEachState];
for (int i = 0; i < numVariablesInEachState; i++) {
System.out.println("Please enter initial value for variable " + arrayOfVariableNames[i] + " in the destination state:");
// Switch scanner if the log reaches its end.
if (isReadingFromLog && !keyboard.hasNextLine()) {
log.println();
keyboard = new Scanner(System.in);
System.out.println("Log file reaches the end, please input the answer to the previous question");
isReadingFromLog = false;
}
int initialValue = keyboard.nextInt();
if (!isReadingFromLog) {
log.println(initialValue);
} else {
System.out.println("Loaded answer:" + initialValue);
}
Variable newVariable = new Variable(arrayOfVariableNames[i], initialValue);
newArrayOfVariables[i] = newVariable;
}
String encodedDest = State.encodeArrayToString(newArrayOfVariables, numVariablesInEachState);
keyboard.nextLine();
// Get destination
State destState = userInputParser.getStateByName(encodedDest);
// if input & destination state the same?
// If we have not seen such destination state in the set
if (destState == null) {
// Create such new state
System.out.println("Destination state does not exist, creating a new state object");
// Get the state name
System.out.println("Please enter destination state name for such transition #" + transitionIndex + ": <" + inputName + "/" + outputName + "> in state encoded as: " + encodedDest);
// Switch scanner if the log reaches its end.
if (isReadingFromLog && !keyboard.hasNextLine()) {
log.println();
keyboard = new Scanner(System.in);
System.out.println("Log file reaches the end, please input the answer to the previous question");
isReadingFromLog = false;
}
String destStateName = keyboard.nextLine();
if (!isReadingFromLog) {
log.println(destStateName);
} else {
System.out.println("Loaded answer:" + destStateName);
}
// check duplicate state name!!
Set<String> allStateNames = userInputParser.getListOfAllStateName();
while(allStateNames.contains(destStateName)) {
System.out.println("!!!Warning!!! Duplicate state name, please enter again for such transition #" + transitionIndex + ": <" + inputName + "/" + outputName + "> in state encoded as: " + encodedDest);
// Switch scanner if the log reaches its end.
if (isReadingFromLog && !keyboard.hasNextLine()) {
log.println();
keyboard = new Scanner(System.in);
System.out.println("Log file reaches the end, please input the answer to the previous question");
isReadingFromLog = false;
}
destStateName = keyboard.nextLine();
if (!isReadingFromLog) {
log.println(destStateName);
} else {
System.out.println("Loaded answer:" + destStateName);
}
}
State newDestState = new State(numVariablesInEachState, destStateName);
// Assign the array to the destination state
newDestState.setArrayOfVariables(newArrayOfVariables);
// Add it to the queue as well as the set
q.add(newDestState);
userInputParser.addStateToMap(encodedDest, newDestState);
userInputParser.addState(newDestState);
// Assign destState again
destState = newDestState;
}
// Adding transition
userInputParser.addInput(inputName);
userInputParser.addOutput(outputName);
currState.addTransition(inputName, outputName, destState);
System.out.println("Transition <" + inputName + "/" + outputName + "---> " + destState.encodeToString() +"> added to state" + currState.getStateName());
}
}
}
private static void notFullyFSM (){
// ask user how many variables are in each state
System.out.println("========Now asking for information on variables========");
System.out.println("How many variables are in each state?");
// Switch scanner if the log reaches its end.
if (isReadingFromLog && !keyboard.hasNextLine()) {
log.println();
keyboard = new Scanner(System.in);
System.out.println("Log file reaches the end, please input the answer to the previous question");
isReadingFromLog = false;
}
numVariablesInEachState = keyboard.nextInt();
keyboard.nextLine();
if (!isReadingFromLog) {
log.println(numVariablesInEachState);
} else {
System.out.println("Loaded answer:" + numVariablesInEachState);
}
arrayOfVariableNames = new String[numVariablesInEachState];
// ask user to input names of variables one by one
for (int i = 0; i < numVariablesInEachState; i++) {
System.out.println("Please enter the name of variable #" + (i+1) + ":");
// Switch scanner if the log reaches its end.
if (isReadingFromLog && !keyboard.hasNextLine()) {
log.println();
keyboard = new Scanner(System.in);
System.out.println("Log file reaches the end, please input the answer to the previous question");
isReadingFromLog = false;
}
arrayOfVariableNames[i] = keyboard.nextLine();
if (!isReadingFromLog) {
log.println(arrayOfVariableNames[i]);
} else {
System.out.println("Loaded answer:" + arrayOfVariableNames[i]);
}
}
// ask for information on states and transitions
System.out.println("========Now asking for information on states and transitions========");
System.out.println("(Please notice that state #1 is the initial state)");
/* Get the info for the first state */
// Get the state name for the first state
System.out.println("Please enter the name of state #1:");
// Switch scanner if the log reaches its end.
if (isReadingFromLog && !keyboard.hasNextLine()) {
log.println();
keyboard = new Scanner(System.in);
System.out.println("Log file reaches the end, please input the answer to the previous question");
isReadingFromLog = false;
}
String stateName = keyboard.nextLine();
if (!isReadingFromLog) {
log.println(stateName);
} else {
System.out.println("Loaded answer:" + stateName);
}
State newState = new State(numVariablesInEachState, stateName);
// Get the initial values of variables in the first state
for (int i = 0; i < numVariablesInEachState; i++) {
System.out.println("Please enter initial value for variable " + arrayOfVariableNames[i] + " in state " + stateName + ":");
// Switch scanner if the log reaches its end.
if (isReadingFromLog && !keyboard.hasNextLine()) {
log.println();
keyboard = new Scanner(System.in);
System.out.println("Log file reaches the end, please input the answer to the previous question");
isReadingFromLog = false;
}
int initialValue = keyboard.nextInt();
if (!isReadingFromLog) {
log.println(initialValue);
} else {
System.out.println("Loaded answer:" + initialValue);
}
Variable newVariable = new Variable(arrayOfVariableNames[i], initialValue);
newState.addVariableToArray(newVariable);
}
// Add the first state to the queue and set
q.add(newState);
userInputParser.addStateToMap(newState.encodeToString(), newState);
userInputParser.addState(newState);
int stateIndex = 2;
int answer = 1;
keyboard.nextLine();
// Main loop
while (!q.isEmpty()) {
// Pop out the first state in the waiting queue
State currState = q.poll();
int transitionIndex = 1;
// Ask for transitions regarding the current state
do {
// Get input
System.out.println("Please enter input name for transition #" + transitionIndex + " in state " + currState.getStateName() + ":");
// Switch scanner if the log reaches its end.
if (isReadingFromLog && !keyboard.hasNextLine()) {
log.println();
keyboard = new Scanner(System.in);
System.out.println("Log file reaches the end, please input the answer to the previous question");
isReadingFromLog = false;
}
String inputName = keyboard.nextLine();
if (!isReadingFromLog) {
log.println(inputName);
} else {
System.out.println("Loaded answer:" + inputName);
}
// Check duplicate inputs
Set<String> allInput = currState.getAllInputNames();
// while (currState.transitions.KeysSet().getkey().contains(inputName)) {
while (allInput.contains(inputName)) {
System.out.println("!!!Warning!!! duplicate input, enter new input name");
// Switch scanner if the log reaches its end.
if (isReadingFromLog && !keyboard.hasNextLine()) {
log.println();
keyboard = new Scanner(System.in);
System.out.println("Log file reaches the end, please input the answer to the previous question");
isReadingFromLog = false;
}
inputName = keyboard.nextLine();
if (!isReadingFromLog) {
log.println(inputName);
} else {
System.out.println("Loaded answer:" + inputName);
}
}
// Get output
System.out.println("Please enter output name for transition #" + transitionIndex + " in state " + currState.getStateName() + ":");
// Switch scanner if the log reaches its end.
if (isReadingFromLog && !keyboard.hasNextLine()) {
log.println();
keyboard = new Scanner(System.in);
System.out.println("Log file reaches the end, please input the answer to the previous question");
isReadingFromLog = false;
}
String outputName = keyboard.nextLine();
if (!isReadingFromLog) {
log.println(outputName);
} else {
System.out.println("Loaded answer:" + outputName);
}
// Get encoded destination
System.out.println("Now asking for internal memory of destination state");
Variable[] newArrayOfVariables = new Variable[numVariablesInEachState];
for (int i = 0; i < numVariablesInEachState; i++) {
System.out.println("Please enter initial value for variable " + arrayOfVariableNames[i] + " in the destination state:");
// Switch scanner if the log reaches its end.
if (isReadingFromLog && !keyboard.hasNextLine()) {
log.println();
keyboard = new Scanner(System.in);
System.out.println("Log file reaches the end, please input the answer to the previous question");
isReadingFromLog = false;
}
int initialValue = keyboard.nextInt();
if (!isReadingFromLog) {
log.println(initialValue);
} else {
System.out.println("Loaded answer:" + initialValue);
}
Variable newVariable = new Variable(arrayOfVariableNames[i], initialValue);
newArrayOfVariables[i] = newVariable;
}
String encodedDest = State.encodeArrayToString(newArrayOfVariables, numVariablesInEachState);
keyboard.nextLine();
// Get destination
State destState = userInputParser.getStateByName(encodedDest);
// if input & destination state the same?
// If we have not seen such destination state in the set
if (destState == null) {
// Create such new state
System.out.println("Destination state does not exist, creating a new state object");
// Get the state name
System.out.println("Please enter destination state name for such transition #" + transitionIndex + ": <" + inputName + "/" + outputName + "> in state encoded as: " + encodedDest);
// Switch scanner if the log reaches its end.
if (isReadingFromLog && !keyboard.hasNextLine()) {
log.println();
keyboard = new Scanner(System.in);
System.out.println("Log file reaches the end, please input the answer to the previous question");
isReadingFromLog = false;
}
String destStateName = keyboard.nextLine();
if (!isReadingFromLog) {
log.println(destStateName);
} else {
System.out.println("Loaded answer:" + destStateName);
}
// check duplicate state name!!
Set<String> allStateNames = userInputParser.getListOfAllStateName();
while(allStateNames.contains(destStateName)) {
System.out.println("!!!Warning!!! Duplicate state name, please enter again for such transition #" + transitionIndex + ": <" + inputName + "/" + outputName + "> in state encoded as: " + encodedDest);
// Switch scanner if the log reaches its end.
if (isReadingFromLog && !keyboard.hasNextLine()) {
log.println();
keyboard = new Scanner(System.in);
System.out.println("Log file reaches the end, please input the answer to the previous question");
isReadingFromLog = false;
}
destStateName = keyboard.nextLine();
if (!isReadingFromLog) {
log.println(destStateName);
} else {
System.out.println("Loaded answer:" + destStateName);
}
}
State newDestState = new State(numVariablesInEachState, destStateName);
// Assign the array to the destination state
newDestState.setArrayOfVariables(newArrayOfVariables);
// Add it to the queue as well as the set
q.add(newDestState);
userInputParser.addStateToMap(encodedDest, newDestState);
userInputParser.addState(newDestState);
// Assign destState again
destState = newDestState;
}
// Adding transition
userInputParser.addInput(inputName);
userInputParser.addOutput(outputName);
currState.addTransition(inputName, outputName, destState);
System.out.println("Transition <" + inputName + "/" + outputName + "---> " + destState.encodeToString() +"> added to state" + currState.getStateName());
// Recursively ask for transitions
System.out.println("Enter another transition for state " + currState.getStateName() + "? enter 1 for yes, enter 0 for no");
// Switch scanner if the log reaches its end.
if (isReadingFromLog && !keyboard.hasNextLine()) {
log.println();
keyboard = new Scanner(System.in);
System.out.println("Log file reaches the end, please input the answer to the previous question");
isReadingFromLog = false;
}
answer = keyboard.nextInt();
if (!isReadingFromLog) {
log.println(answer);
} else {
System.out.println("Loaded answer:" + answer);
}
keyboard.nextLine();
transitionIndex++;
} while (answer != 0);
}
}
public static void printInfo(String outputFile) throws IOException {
System.out.println("========Printing all the states========");
userInputParser.printListOfStates(outputFile);
}
}