-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmealymachine.java
107 lines (84 loc) · 3.78 KB
/
mealymachine.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
import java.util.*;
class Main {
public static void main(String[] args) {
MealyMachineValidateString.main();
}
}
class Utility {
public static String get_input(String message) {
Scanner scanner = new Scanner(System.in);
String inputString;
do {
System.out.print(message);
inputString = scanner.nextLine();
if (inputString.length() == 0) {
System.out.println("Input can not be empty");
}
} while (inputString.length() == 0);
return inputString;
}
}
class MealyMachineValidateString {
public static void main() {
String inputString, message;
message = "Enter valid input symbols (comma-separated): ";
inputString = Utility.get_input(message);
Set<String> validInputs = new HashSet<>(Arrays.asList(inputString.split(",")));
message = "Enter states (Q) (comma-separated): ";
inputString = Utility.get_input(message);
Set<String> states = new HashSet<>(Arrays.asList(inputString.split(",")));
message = "Enter starting state: ";
String startingState = Utility.get_input(message);
Map<String, Map<String, String>> transitions = new HashMap<>();
for (String state : states) {
Map<String, String> transition = new HashMap<>();
for (String validInput : validInputs) {
message = String.format("Enter next state and output for transition '%s' with input '%s': ", state, validInput);
String next_state_and_output = Utility.get_input(message);
transition.put(validInput, next_state_and_output);
}
transitions.put(state, transition);
}
MealyMachine mealyMachine = new MealyMachine(startingState, transitions);
while (true) {
message = "Enter input sequence to be processed: ";
String input_sequence = Utility.get_input(message);
mealyMachine.process_input_sequence(input_sequence);
System.out.print("Do you want to process another input sequence? (y/n): ");
String choice = new Scanner(System.in).nextLine();
if (choice.equalsIgnoreCase("n")) {
break;
}
}
}
}
class MealyMachine {
private String startingState;
private Map<String, Map<String, String>> transitions;
public MealyMachine(String startingState, Map<String, Map<String, String>> transitions) {
this.startingState = startingState;
this.transitions = transitions;
}
public void process_input_sequence(String input_sequence) {
String current_state = startingState;
String output_sequence = "";
List<String> traversal_path = new ArrayList<>();
traversal_path.add(current_state);
for (int i = 0; i < input_sequence.length(); i++) {
String input_symbol = String.valueOf(input_sequence.charAt(i));
Map<String, String> current_transitions = transitions.get(current_state);
if (current_transitions.containsKey(input_symbol)) {
String next_state_and_output = current_transitions.get(input_symbol);
String[] next_state_and_output_arr = next_state_and_output.split(",");
String next_state = next_state_and_output_arr[0];
String output = next_state_and_output_arr[1];
output_sequence += output;
traversal_path.add(current_state + "(" + input_symbol + "," + output + ")->" + next_state);
current_state = next_state;
}
}
System.out.println("Output Sequence: " + output_sequence);
System.out.println();
System.out.println("Traversal Path: " + String.join(" -> ", traversal_path));
}
}