-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfsm.java
1449 lines (1203 loc) · 39 KB
/
fsm.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
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
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import java.util.*;
import java.util.regex.*;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
//alphatbet = abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789.,~!@$#%^&-+{}
public class FSM {
//Adam driving
public static boolean validAlpha(String x){
Pattern p = Pattern.compile("[a-zA-Z0-9\\.,~!@\\$#%\\^&\\-\\+\\{}]*");
Matcher m = p.matcher(x);
boolean b = m.matches();
return b;
}
//Adam
public static void invalidChars(String x, String inOut, String fsm, boolean isWarnings){
//System.out.println(x);
boolean breakIt = false;
String chars = "";
Pattern p = Pattern.compile("[a-zA-Z0-9\\.,~!@\\$#%\\^&\\-\\+\\{}]*");
for(int i = 0; i < x.length(); i++){
chars = chars + x.charAt(i);
Matcher m = p.matcher(chars);
boolean b = m.matches();
if(isWarnings && !b && (x.charAt(i) != '\n')){
System.out.println("FSM WARNING: " + fsm + ": UNSUPPORTED ALPHABET FOR " + x);
}
if(inOut.equalsIgnoreCase("MACHINE_TYPE")){
System.out.println("FSM FILE ERROR: "+ fsm + ": "+ inOut + " HAS INVALID VALUE '" + x +"'");
}
if(!b){
System.out.println("FSM FILE ERROR: "+ fsm + ": "+ inOut + " HAS INVALID VALUE '" + x.charAt(i) +"'");
}
chars = "";
}
}
//Adam
public static String removeWhite(String x) throws IOException{
FileInputStream inStream = new FileInputStream(x);
String wholeFile = "";
int lineCount = 1;
while(true){
char toTest = (char)inStream.read();
if(toTest == (char)-1){
break;
}
if(toTest == '\n'){
lineCount++;
}
//removing whitespaces
if(toTest != ' ' && toTest != '\t'){
wholeFile = wholeFile + toTest;
}
//System.out.println(toTest);
}
return wholeFile;
}
//Adam
public static List<String> inputName(String x) throws IOException{
String what = removeWhite(x);
what = what + ' ';
List<String> lines = new LinkedList<String>();
//FileInputStream inStream = new FileInputStream(x);
//char test1 = (char)inStream.read();
String lineForList = "";
for(int i = 0; i <= what.length(); i++){
if(what.charAt(i) == ' '){
break;
}
if(what.charAt(i) != '\n'){
lineForList = lineForList + what.charAt(i);
}
if(what.charAt(i) == '\n' || i + 2 == what.length()){
if(lineForList.length() != 0)
lineForList = lineForList.substring(0, lineForList.indexOf(':'));
lines.add(lineForList);
lineForList = "";
}
else{
}
}
}
return lines;
}
//Adam
public static List<String> getInputs(String x) throws IOException{
String what = removeWhite(x);
what = what + ' ';
List<String> lines = new LinkedList<String>();
//FileInputStream inStream = new FileInputStream(x);
//char test1 = (char)inStream.read();
String lineForList = "";
for(int i = 0; i <= what.length(); i++){
if(what.charAt(i) == ' '){
break;
}
if(what.charAt(i) != '\n'){
lineForList = lineForList + what.charAt(i);
}
if(what.charAt(i) == '\n' || i + 2 == what.length()){
if(lineForList.length() != 0){
lineForList = lineForList.substring(lineForList.indexOf(':') + 1, lineForList.length());
lines.add(lineForList);
lineForList = "";
}
else{
}
}
}
return lines;
}
//Adam
public static String getInputName(String x, String input, int index){
int delimiter = 0;
delimiter = x.indexOf(':');
String name = "";
if(delimiter == 0){
System.out.println("INPUT FILE ERROR: " + input + ": MISSING NAME ON LINE: " + (index + 1));
}
name = x.substring(0, delimiter);
return name;
}
//Adam
public static String getInputLine(String x, String input, int index){
int delimiter = 0;
int count = 0;
delimiter = x.indexOf(':');
for (int i = 0; i < x.length(); i++){
if(x.charAt(i) == ':')
{
count++;
}
}
String name = "";
System.out.println("COUNT = " + count);
if (count > 0){
System.out.println("INPUT FILE ERROR: " + input + ": MULTIPLE STRINGS ON LINE " + index);
}
if(delimiter+1 == x.length()){
System.out.println("INPUT FILE ERROR: " + input + ": MISSING INPUT STRING ON LINE " + (index + 1));
}
name = x.substring(delimiter + 1, x.length());
return name;
}
//Adam
//get the alphabet, taking into account the shortcut characters
public static String getAlpha(String x){
if(x.equals("|u")){
return "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
}
else if(x.equals("|l")){
return "abcdefghijklmnopqrstuvwxyz";
}
else if(x.equals("|a")){
return getAlpha("|l").concat(getAlpha("|u"));
}
else if(x.equals("|d")){
return "0123456789";
}
else if(x.equals("|n")){
return "123456789";
}
else if(x.equals("|s")){
return ".,~!@$#%^&-+{}";
}
else{
return x;
}
}
//Adam
//funtion to imbed commas into the alphabet strings if needed
public static String getAlphaCommas(String x){
if(x.equals("|u")){
return "A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z";
}
else if(x.equals("|l")){
return "a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z";
}
else if(x.equals("|a")){
return getAlpha("|l").concat(getAlpha("|u"));
}
else if(x.equals("|d")){
return "0,1,2,3,4,5,6,7,8,9";
}
else if(x.equals("|n")){
return "1,2,3,4,5,6,7,8,9";
}
else if(x.equals("|s")){
return ".,,,~,!,@,$,#,%,^,&,-,+,{,}";
}
else{
return x;
}
}
//Adam
//not used
//basically was going to blow up the piped input/output values
public static String blowUp(String x){
//System.out.println("Line X = " + x);
String fantastic = "";
String builder = "";
for(int i = 0; i < x.length(); i++){
if(x.charAt(i) == '|'){
builder+=x.charAt(i);
builder+=x.charAt(i+1);
builder = getAlphaCommas(builder);
fantastic = fantastic.concat(builder);
builder = "";
i++;
}
else{
fantastic+=x.charAt(i);
}
}
return fantastic;
}
//Adam
//grab a line number from the machine.fsm file
public static int getLine(String wholeFile, String toSearch){
String temp = "";
char temp2;
int line = 1;
for (int i = 0; i < wholeFile.length(); i++){
temp2 = wholeFile.charAt(i);
temp = temp + temp2;
if (wholeFile.charAt(i) == '\n'){
line++;
}
if (temp.endsWith(toSearch)){
break;
}
}
return line;
}
//Adam
//checks to see if the state is an accepting state
public static boolean isAccept(String x){
String stateName = "";
stateName = x.substring(0, x.indexOf(':'));
if(stateName.contains("$") || stateName.contains("$!") || stateName.contains("!$")){
return true;
}
else{
return false;
}
}
//Adam
//IT'S A TRAP! or is it?
public static boolean isTrap(String x){
String stateName = "";
stateName = x.substring(0, x.indexOf(':'));
if(stateName.contains("!") || stateName.contains("$!") || stateName.contains("!$")){
return true;
}
else{
return false;
}
}
//Adam
//building up the alphabet
public static String buildAlpha(String x){
String alphabet = "";
char charToCheck;
String shortcut;
for(int i = 0; i < x.length(); i++){
charToCheck = x.charAt(i);
Pattern p = Pattern.compile("[|]+[uladns]");
if(charToCheck == '|'){
shortcut = "|" + x.charAt(i+1);
Matcher m = p.matcher(shortcut);
boolean b = m.matches();
if (!b){
return "Invalid shortcut";
}
else{
String test = getAlpha(shortcut);
alphabet = alphabet.concat(getAlpha(shortcut));
i++;
}
}
else{
alphabet = alphabet + charToCheck;
}
}
return alphabet;
}
//Adam
//total length of the file -- could be handy
public static int fileLength(String inFile){
int length = inFile.length();
return length;
}
//Adam
//finding the start state
public static String startState(String wholeFile, int startIndex, String fsmName){
char build = wholeFile.charAt(startIndex);
String startState = "";
while(true){
if(build == '\n' || startIndex == fileLength(wholeFile)-1){
break;
}
startState = startState + build;
startIndex++;
build = wholeFile.charAt(startIndex);
}
invalidChars(startState, "START_STATE", fsmName, true);
return startState;
}
//Adam
//work-horse method, parses the machine.fsm file and gets all the goodies
public static ArrayList<States> parse(String inFile, String fsmName, boolean verbose, boolean warnings, boolean unspecified) throws IOException{
String name;
FileInputStream inStream = new FileInputStream(inFile);
ArrayList<States> returnVal = new ArrayList<States>();
int lineCount = 1;
String wholeFile = "";
//machinename missing?
//counting lines, removing whitespaces
while(true){
char toTest = (char)inStream.read();
if(toTest == (char)-1){
break;
}
if(toTest == '\n'){
lineCount++;
}
//removing whitespaces
if(toTest != ' '){
wholeFile = wholeFile + toTest;
}
}
wholeFile = wholeFile + ' ';
String rlRemove = "";
for(int r = 0; r < wholeFile.length(); r++){
char blah = wholeFile.charAt(r);
if(blah != '\n'){
rlRemove = rlRemove + blah;
}
}
//doesn't have an FSM name? ERROR!
if(rlRemove.startsWith("INPUT_ALPHABET") || rlRemove.startsWith("OUTPUT_ALPHABET") || rlRemove.startsWith("MACHINE_TYPE") || rlRemove.startsWith("STARTING_STATE")){
System.out.println("FSM FILE ERROR: " + fsmName + ": FSM NAME IS MISSING");
System.exit(0);
}
//doesn't have an machine type? ERROR!
if (!wholeFile.contains("MACHINE_TYPE")){
System.out.println("FSM FILE ERROR: " + fsmName + " : MACHINE_TYPE IS MISSING");
System.exit(0);
}
//doesn't have a starting state? ERROR!
if (!wholeFile.contains("STARTING_STATE")){
System.out.println("FSM FILE ERROR: " + fsmName + " : STARTING_STATE IS MISSING");
System.exit(0);
}
//getting the machine type from input file
//find line that the machine is on, cut it out of the file, check for match
//now to get the type of machine, this is stored in a boolean to be sent to the States class
boolean machine = false;
if(wholeFile.contains("MACHINE_TYPE")){
int machineIndex = wholeFile.indexOf("MACHINE_TYPE");
String machineType = "";
int indexOfNew = 0;
for(int i = machineIndex; wholeFile.charAt(i) != '\n';i++){
indexOfNew++;
}
String machineLine = wholeFile.substring(machineIndex, machineIndex + indexOfNew);
String type = machineLine.substring(machineLine.indexOf(':') + 1, machineLine.length());
if (type.equalsIgnoreCase("mealy") || type.equalsIgnoreCase("moore")){
if(type.equalsIgnoreCase("mealy")){
machine = true;
}
else{
machine = false;
}
//System.out.println("machine type = " + machine );
}
else{
invalidChars(machineType, "MACHINE_TYPE", fsmName, true);
//System.out.println("FSM ERROR: " + fsmName + " INVALID MACHINE TYPE SPECIFIED");
System.exit(0);
}
}
else{
System.out.println("FSM ERROR: " + fsmName + " INVALID MACHINE TYPE SPECIFIED");
System.exit(0);
}
//getting input line from the file, cut it out, and check it.
String pipe = "|";
//getting input alphabet from input file
String alphabetIn = "";
String alphabetOut = "";
if(wholeFile.contains("INPUT_ALPHABET")){
int inputIndex = wholeFile.indexOf("INPUT_ALPHABET");
int indexOfNew = 0;
for(int i = inputIndex; wholeFile.charAt(i) != '\n';i++){
indexOfNew++;
}
String inputLine = wholeFile.substring(inputIndex, inputIndex + indexOfNew);
String testInputLine = "";
for(int z = 0; z < inputLine.length(); z++){
if(inputLine.charAt(z) == '|'){
testInputLine = testInputLine + getAlpha(pipe + inputLine.charAt(z+1));
z++;
}
else{
testInputLine = testInputLine + inputLine.charAt(z);
}
}
int whereiscolon = 0;
for(int g = 0; g < testInputLine.length(); g++){
if(testInputLine.charAt(g) == ':' ){
whereiscolon = g;
}
}
String somuchwastedtime = testInputLine.substring(whereiscolon+1, testInputLine.length());
System.out.println("SOMUCHWASTED TIME = " + somuchwastedtime);
invalidChars(somuchwastedtime, "INPUT_ALPHABET", fsmName, warnings);
String inputFinal = getAlpha(inputLine.substring(inputLine.indexOf(':') + 1, inputLine.length()));
for(int b = 0; b < inputFinal.length(); b++){
if(inputFinal.charAt(b) == '|'){
alphabetIn = alphabetIn + getAlpha(pipe + inputFinal.charAt(b+1));
b++;
}
else{
alphabetIn = alphabetIn + inputFinal.charAt(b);
}
}
}
else{
alphabetIn = "A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z,a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z,0,1,2,3,4,5,6,7,8,9,.,,,~,!,@,$,#,%,^,&,-,+,{,}";
}
//make sure is valid and whatnot
boolean isValidAlpha = validAlpha(alphabetIn);
invalidChars(alphabetIn, "INPUT_ALPHABET", fsmName, true);
String inputAlpha = buildAlpha(alphabetIn);
//System.out.println("Input Alphabet = " + inputAlpha);
//now we have input alphabet, machine type, machine name
//still need --- state transitions, output alphabet
//getting output alphabet from input file
if(wholeFile.contains("OUTPUT_ALPHABET")){
int inputIndex = wholeFile.indexOf("OUTPUT_ALPHABET");
int indexOfNew = 0;
for(int i = inputIndex; wholeFile.charAt(i) != '\n';i++){
indexOfNew++;
}
;
String inputLine = wholeFile.substring(inputIndex, inputIndex + indexOfNew);
String outputFinal = getAlpha(inputLine.substring(inputLine.indexOf(':') + 1, inputLine.length()));
for(int b = 0; b < outputFinal.length(); b++){
if(outputFinal.charAt(b) == '|'){
alphabetOut = alphabetOut + getAlpha(pipe + outputFinal.charAt(b+1));
b++;
}
else{
alphabetOut = alphabetOut + outputFinal.charAt(b);
}
}
}
else{
alphabetOut = "A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z,a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z,0,1,2,3,4,5,6,7,8,9,.,,,~,!,@,$,#,%,^,&,-,+,{,}";
}
boolean isValidAlphaOut = validAlpha(alphabetOut);
invalidChars(alphabetOut, "OUTPUT_ALPHABET", fsmName, true);
if(!isValidAlphaOut){
System.out.println("FSM ERROR: " + fsmName + " OUTPUT_ALPHABET HAS INVALID VALUE");
// System.exit(0);
}
String outputAlpha = buildAlpha(alphabetOut);
//System.out.println("Output Alphabet = " + outputAlpha);
//still Adam
int startIndex = wholeFile.indexOf("STARTING_STATE");
String startLine = "";
int endofStart = 0;
for (int a = startIndex; wholeFile.charAt(a) != '\n';a++){
endofStart++;
}
startLine = wholeFile.substring(startIndex, startIndex + endofStart);
String sState = startLine.substring((startLine.indexOf(':')+1), startLine.length());
//now we need to get the individual states...
int statesIndex = startIndex;
char current = wholeFile.charAt(statesIndex);
while(true){
if(wholeFile.charAt(statesIndex) == '\n'){
break;
}
statesIndex++;
}
//getting just the lines of the file dealing with states...
String subWhole = wholeFile.substring(statesIndex, fileLength(wholeFile)) + ' ';
int filelength = fileLength(subWhole);
//get an individual line of states
//this is where we should send
int index = 0;
char toTest = subWhole.charAt(index);
String stateLine = "";
while(toTest == '\n'){
index++;
toTest = subWhole.charAt(index);
}
while(index < subWhole.length()+10){
while(true){
if (subWhole.charAt(index) == '\n' || index == fileLength(subWhole)-1){
break;
}
stateLine = stateLine + subWhole.charAt(index);
index++;
}
//various System.outs to make sure everything is correct
if(stateLine != ""){
int lineNum = getLine(wholeFile, stateLine);
boolean isAccept = isAccept(stateLine);
boolean isTrap = isTrap(stateLine);
int placeHolder = stateLine.indexOf(':');
if(placeHolder == -1){
System.out.println("FSM FILE ERROR: " + fsmName + " INVALID STATE TRANSITION");
}
else{
name = stateLine.substring(0, placeHolder);
if(name.equals(sState))
returnVal.add(0, new States(stateLine, lineNum, isTrap, isAccept, machine, verbose, warnings, unspecified, inputAlpha, outputAlpha,fsmName));
else
returnVal.add(new States(stateLine, lineNum, isTrap, isAccept, machine, verbose, warnings, unspecified, inputAlpha, outputAlpha,fsmName));
}
}
stateLine = "";
if(index == fileLength(subWhole)-1)
{
break;
}
index++;
}
//Neil driving
Set<String> allNextStates = new HashSet<String>();
boolean temp = false;
boolean used = true;
//Makes sure there is an Accepting state and get all states pointed to
for(int x = 0; x < returnVal.size(); x++)
{
allNextStates.addAll(returnVal.get(x).getAllNextSTates());
if(returnVal.get(x).hasAccepting())
{
temp = true;
x = returnVal.size();
}
}
//Checks to see if any state has nothing pointing to it
for(int x = 1; x < returnVal.size(); x++)
{
if(!allNextStates.contains(returnVal.get(x).getName()))
{
used = false;
x = returnVal.size();
}
}
//System.out.println(allNextStates.toString());
//Displays the warnings for Missing Accepting state and Unconnected components
if(warnings)
{
if(!temp)
{
System.out.println("FSM WARNING: " + fsmName + " MISSING ACCEPTING STATE.");
}
if(!used)
{
System.out.println("FSM WARNING: " + fsmName + " UNCONNECTED COMPONENTS.");
}
}
//Sends into a unspecified state if need be
if(unspecified){
System.out.println("UNLABELDDZXXX");
//returnVal.add(new States("UNLABELEDXXXXX:", 0, true, false, machine, verbose, warnings, unspecified, inputAlpha, outputAlpha, fsmName));
}
return returnVal;
//now we need to get the starting state, and the other states, must have a final state? check on this
}
//Adam
public static void main(String[] args) throws IOException {
//adam is driving
boolean isVerbose = false;
boolean isWarnings = false;
boolean isUnspecified = false;
String fsmName = "";
List<String> arguements = new LinkedList<String>();
List<String> machines = new LinkedList<String>();
String input = "";
for (String s: args) {
arguements.add(s);
}
int listSize = arguements.size();
int i = 0;
while(i < listSize){
//getting machines
if(arguements.get(i).equalsIgnoreCase("--fsm")){
//System.out.println("FSM INIT FOUND");
i++;
while(!arguements.get(i).startsWith("--")){
machines.add(arguements.get(i));
//System.out.println(arguements.get(i));
i++;
if(i == listSize){
break;
}
}
}
if(i == listSize){
break;
}
//getting input checking for tags
if(arguements.get(i).equalsIgnoreCase("--input")){
//System.out.println("INPUT INIT FOUND");
i++;
input = arguements.get(i);
}
if(arguements.get(i).equalsIgnoreCase("--verbose")){
//System.out.println("VERBOSE INIT FOUND");
isVerbose = true;
}
if(arguements.get(i).equalsIgnoreCase("--warnings")){
//System.out.println("WARNINGS INIT FOUND");
isWarnings = true;
}
if(arguements.get(i).equalsIgnoreCase("--unspecified-transitions-trap")){
//System.out.println("UNSPECIFIED INIT FOUND");
isUnspecified = true;
}
i++;
}
List<String> inputList = getInputs(input);
List<String> inputNames = inputName(input);
int numInputs = inputList.size();
int numMachines = machines.size();
ArrayList<States> test = new ArrayList<States>();
ArrayList<ArrayList<States>> allStates = new ArrayList<ArrayList<States>>();
allStates.add(test);
//adam still driving
//Now were are going to go through each machine and run each input from the input file on it
//using the array lists above and a double-for loop etc...
for(int count = 0; count < numMachines; count++){
test = parse(machines.get(count), machines.get(count), isVerbose, isWarnings, isUnspecified);
System.out.println();
System.out.println("<<<<<<"+ machines.get(count) + ">>>>>>");
for(int countInputs = 0; countInputs < numInputs; countInputs++){
String inputString = inputList.get(countInputs);
int v = 0;
if(test.get(v).isMealy()){
System.out.print(machines.get(count) + " : " + inputString + " / ");
}
for (int z = 0; z < inputString.length(); z++){
//System.out.println("BORAT: " + test.get(v).getAlphabet().indexOf(inputString.charAt(z)));
if(test.get(v).getAlphabet().indexOf(inputString.charAt(z)) == -1){
System.out.println("FSM FILE ERROR: " + machines.get(count) + " INVALID OUTPUT SYMBOL ON LINE " + (countInputs+1));
break;
}
String nextState = test.get(v).nextState(inputString.charAt(z));
if(isVerbose){
System.out.println((z+1) +" : " + z + " " + test.get(v).getName() + " -- " + inputString.charAt(z) + " --> " + nextState);
}
if(nextState.equals(":")){
System.out.println("ACCEPTED " + test.get(v).getAlphabet());
z = inputString.length();
}
else{
for(int p = 0; p < test.size(); p++){
if(test.get(p).getName().equals(nextState)){;
v = p;
p = test.size();
}
}
}
}
if(test.get(v).isMealy()){
System.out.println();
}
if(test.get(v).isFinal() && !test.get(v).isMealy()){
System.out.println(machines.get(count) + " : ACCEPTED " + inputString);
}
else if(!test.get(v).isFinal() && !test.get(v).isMealy()){
System.out.println(machines.get(count) + " : REJECTED " + inputString);
}
}
}
}
}
//Neil is driving
final class States {
//Strings need to store data
String name;
String preConections;
String nextStatesLine;
String inputAlphabet;
String outputAlphabet;
String FSMname;
//has a set of all next states
Set<String> nextStateList = new HashSet<String>();
String usedInputs = "";
//Pattern for Rejex
Pattern inAplha;
Pattern outAplha;
Pattern nameAlpha = Pattern.compile("a*b");
//ADD THIS
Pattern nameExp = Pattern.compile("[a-zA-Z0-9]*");
Pattern allSymbols = Pattern.compile("[a-zA-Z0-9\\.,~!@\\$#%\\^&\\-\\+\\{}]*");
Matcher validAlpha = allSymbols.matcher(" ");
int line;
//bolleans for needed things
boolean isFinal = false;
boolean isTrap = false;
boolean linksSet = false;
boolean isStart = false;
boolean isMealy;
boolean isVerbose;
boolean isWarnings;
boolean isUnspecified;
//holds all the nextstates according to values
HashMap<String, String> nextStates = new HashMap<String, String>();
//ArrayList<String> removedStates;
//Neil is driving
public States(String origianlLine, int line1, boolean trap, boolean finalState, boolean mealy,
boolean verbose, boolean warnings, boolean unspecified, String inputAlph, String outputAlph, String FsmName)
{
System.out.println("INPUTALPH = " + inputAlph);
preConections = origianlLine;
line = line1;
inputAlphabet = inputAlph;
outputAlphabet = outputAlph;
FSMname = FsmName;
//Gets the index of the colon
int placeHolder = origianlLine.indexOf(':');
name = origianlLine.substring(0, placeHolder);
//adam driving to fix case where a state is ! and $
//System.out.println("NAME " + name);
//gets final state and trap state
if(name.contains("$!") || name.contains("!$")){
name = name.substring(0, name.length()-1);
}
if(name.endsWith("$") || name.endsWith("!")){
name = name.substring(0, name.length()-1);
}
nextStatesLine = origianlLine.substring(1+placeHolder);
isTrap = trap;
isFinal = finalState;
isMealy = mealy;
isVerbose = verbose;
isWarnings = warnings;
isUnspecified = unspecified;
//makes the hashmaps if it is not a trap state
if(!isTrap){
nextStates.clear();
nextStates = newLinks(nextStatesLine);
}
//Outputs the warnings for transition state
if(warnings)
{
for(int x = 0; x < inputAlphabet.length(); x++)
{
if(usedInputs.indexOf(inputAlphabet.charAt(x)) == -1)
{
System.out.println("FSM WARNING: " + FSMname + " INCOMPLETE TRANSITION TABLE FOR STATE " + name);
x= inputAlphabet.length();
}
}
}
}
//You can make this a boolean so that true if worked false is not
//Neil is driving
public HashMap<String, String> newLinks(String connections)
{
//needed data storage for adding the values to the hashmap
int placeHolder = 0;
int placeHolderTwo = 0;
String newNextStateName;
String inputs;
String output = null;
String checker;
placeHolder = connections.indexOf(':');
//Check to make sure line is made properly
if(placeHolder == -1 || placeHolder == 0)
{
System.out.println("FSM FILE ERROR: " + FSMname + " STATE HAS NO TRANSITIONS OR NAME LISTED AT LINE " + line);
//System.exit(0);
}
while(placeHolder != -1)
{
//Gets the name of the
newNextStateName = connections.substring(0, placeHolder);
Matcher m = nameExp.matcher(newNextStateName);
//Error
if(!m.matches())
{
System.out.println("FSM FILE ERROR: " + FSMname + " INVALID STATE NAME AT LINE " + line);
//System.exit(0);
}
placeHolderTwo = connections.indexOf('{');
if(placeHolder+1 != placeHolderTwo)
{
System.out.println("FSM FILE ERROR: " + FSMname + " NEXTSTATE WITHOUT TRANSITION VALUES AT LINE " + line);
//System.exit(0);
}
connections = connections.substring(placeHolderTwo+1);
//gets the end of the inputs
placeHolderTwo = connections.indexOf("}");
if(placeHolderTwo == -1)
{
System.out.println("FSM FILE ERROR: " + FSMname + " UNCLOSED SET AT LINE " + line);
//System.exit(0);
}
placeHolder = connections.indexOf(':');
//System.out.println(placeHolder +" " + placeHolderTwo);
//checks to see if the colon is in the right place
if(placeHolder == -1)
checker = connections.substring(placeHolderTwo+1);
else
{
if(placeHolder < placeHolderTwo)
{
System.out.println("FSM FILE ERROR: " + FSMname + " UNCLOSED SET AT LINE " + line);
//System.exit(0);
}
checker = connections.substring(placeHolderTwo+1, placeHolder);
}
//gets the end of the inputs
if( checker.lastIndexOf('}') != -1)
placeHolderTwo = checker.lastIndexOf('}') + placeHolderTwo + 1;
//cuts the inputs off to the ramaining inputs
inputs = connections.substring(0, placeHolderTwo);
if(inputs.charAt(inputs.length()-1) == ',' && inputs.length() != 1)
{
System.out.println("FSM FILE ERROR: " + FSMname + " MISSING ELEMENT IN SET AT LINE " + line);
//System.exit(0);
}
int x = 2;
int trimer = inputs.length();
//Makes the moore states
if(!isMealy)
{
String input = inputs.charAt(0) + "";
//Expands shortcuts
if(input.equals("|"))
{
inputs = expand(inputs.charAt(1)) + inputs.substring(2);
input = inputs.charAt(0) + "";
}
if(nextStates.get(input) != null)
{
System.out.println("FSM FILE ERROR: " + FSMname + " MULTIPLE STATE TRANSITIONS FOR SINGLE INPUT ON LINE " + line);
//System.exit(0);