Skip to content

Commit a0da05a

Browse files
committed
Refactor transitions in automata implementations
- Removed the Transition class from NondeterministicFiniteAutomaton and TuringMachine packages. - Introduced BaseTransition class to provide common functionality for all transition types. - Created FSATransition class for finite state automata transitions, extending BaseTransition. - Updated PDATransition to extend BaseTransition, simplifying its constructor and methods. - Implemented TMTransition class for Turing Machine transitions, including read/write operations and movement direction. - Modified TM and TMParser classes to utilize TMTransition instead of the removed Transition class. - Updated tests in DFAExecuteTest, DFATest, ExecuteTest, and others to use FSATransition and ensure compatibility with new transition structure. - Removed Direction enum from TuringMachine package, replacing it with TMTransition.Direction.
1 parent cccb947 commit a0da05a

File tree

17 files changed

+716
-398
lines changed

17 files changed

+716
-398
lines changed

src/main/java/DeterministicFiniteAutomaton/DFA.java

Lines changed: 35 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
import common.Automaton;
1919
import common.ExecutionResult;
20+
import common.FSATransition;
2021
import common.InputNormalizer;
2122
import common.MachineType;
2223
import common.ParseResult;
@@ -32,7 +33,7 @@
3233
public class DFA extends Automaton {
3334
private Set<State> states;
3435
private Set<Symbol> alphabet;
35-
private Set<Transition> transitions;
36+
private Set<FSATransition> transitions;
3637

3738
private State startState;
3839
private Set<State> finalStates;
@@ -60,8 +61,8 @@ public DFA() {
6061
public DFA(Set<State> states,
6162
Set<Symbol> alphabet,
6263
Set<State> finalStates,
63-
State startState,
64-
Set<Transition> transitions) {
64+
State startState,
65+
Set<FSATransition> transitions) {
6566
super(MachineType.DFA);
6667
this.states = states;
6768
this.alphabet = alphabet;
@@ -93,7 +94,7 @@ public Set<Symbol> getAlphabet() {
9394
*
9495
* @return An unmodifiable set of all transitions
9596
*/
96-
public Set<Transition> getTransitions() {
97+
public Set<FSATransition> getTransitions() {
9798
return Collections.unmodifiableSet(transitions);
9899
}
99100

@@ -222,23 +223,23 @@ public ExecutionResult execute(String inputText) {
222223
}
223224

224225
// Find transition
225-
Transition validTransition = null;
226-
for (Transition transition : transitions) {
227-
if (transition.getFrom().getName().equals(currentState.getName()) &&
228-
transition.getSymbol().equals(inputSymbol)) {
226+
FSATransition validTransition = null;
227+
for (FSATransition transition : transitions) {
228+
if (transition.getFromState().getName().equals(currentState.getName()) &&
229+
transition.getInputSymbol().equals(inputSymbol)) {
229230
validTransition = transition;
230231
break;
231232
}
232233
}
233-
234+
234235
if (validTransition == null) {
235236
trace.append("No transition from state ").append(currentState.getName())
236237
.append(" on symbol '").append(inputChar).append("'\n");
237238
runtimeMessages.add(new ValidationMessage("No transition defined", i, ValidationMessage.ValidationMessageType.ERROR));
238239
return new ExecutionResult(false, runtimeMessages, trace.toString());
239240
}
240-
241-
currentState = validTransition.getTo();
241+
242+
currentState = validTransition.getToState();
242243
trace.append("Read '").append(inputChar).append("' -> state ").append(currentState.getName()).append("\n");
243244
}
244245

@@ -293,31 +294,31 @@ public List<ValidationMessage> validate() {
293294
/**
294295
* Checks if all states have transitions for every symbol in the alphabet.
295296
* Adds error messages for any missing transitions.
296-
*
297+
*
297298
* @param states All states in the DFA
298299
* @param alphabet The alphabet of the DFA
299300
* @param transitions The set of transitions
300301
* @param messages List to collect validation messages
301302
*/
302-
private void checkMissingTransitions(Set<State> states,
303+
private void checkMissingTransitions(Set<State> states,
303304
Set<Symbol> alphabet,
304-
Set<Transition> transitions,
305+
Set<FSATransition> transitions,
305306
List<ValidationMessage> messages) {
306307
if (states == null || alphabet == null || transitions == null) {
307308
return;
308309
}
309310

310311
// Create a map of state -> symbol -> target state
311312
Map<State, Map<Symbol, State>> transitionMap = new HashMap<>();
312-
313+
313314
// Initialize the transition map
314315
for (State state : states) {
315316
transitionMap.put(state, new HashMap<>());
316317
}
317-
318+
318319
// Populate the transition map with existing transitions
319-
for (Transition t : transitions) {
320-
transitionMap.get(t.getFrom()).put(t.getSymbol(), t.getTo());
320+
for (FSATransition t : transitions) {
321+
transitionMap.get(t.getFromState()).put(t.getInputSymbol(), t.getToState());
321322
}
322323

323324
// Check for missing transitions
@@ -452,12 +453,12 @@ private Set<State> processFinalStates(List<String> lines, int lineNum,
452453
* @param startLine The line number where the transitions section starts
453454
* @param stateMap Map of state names to State objects
454455
* @param messages List to collect any validation messages
455-
* @return A set of Transition objects representing the DFA's transitions
456+
* @return A set of FSATransition objects representing the DFA's transitions
456457
*/
457-
private Set<Transition> processTransitions(List<String> lines, int startLine,
458+
private Set<FSATransition> processTransitions(List<String> lines, int startLine,
458459
Map<String, State> stateMap,
459460
List<ValidationMessage> messages) {
460-
Set<Transition> transitionSet = new HashSet<>();
461+
Set<FSATransition> transitionSet = new HashSet<>();
461462
if (lines == null) return transitionSet;
462463

463464
// Map to track transitions by fromState and symbol
@@ -512,7 +513,7 @@ private Set<Transition> processTransitions(List<String> lines, int startLine,
512513
transitionMap.computeIfAbsent(from, k -> new HashMap<>()).put(sym, to);
513514

514515
// Add to the final transition set
515-
transitionSet.add(new Transition(from, sym, to));
516+
transitionSet.add(new FSATransition(from, sym, to));
516517
}
517518
} else {
518519
messages.add(new ValidationMessage("Invalid transition format.", currentLine, ValidationMessage.ValidationMessageType.ERROR));
@@ -576,9 +577,9 @@ private Symbol validateSymbol(Symbol symbol, Set<Symbol> alphabet,
576577
* @param transitions Set of transitions in the DFA
577578
* @param messages List to collect validation messages
578579
*/
579-
private void checkForUnreachableStates(Set<State> allStates,
580+
private void checkForUnreachableStates(Set<State> allStates,
580581
State startState,
581-
Set<Transition> transitions,
582+
Set<FSATransition> transitions,
582583
List<ValidationMessage> messages) {
583584
if (startState == null || allStates.isEmpty()) return;
584585

@@ -589,8 +590,8 @@ private void checkForUnreachableStates(Set<State> allStates,
589590
queue.add(startState);
590591

591592
Map<State, Set<State>> transitionMap = new HashMap<>();
592-
for (Transition t : transitions) {
593-
transitionMap.computeIfAbsent(t.getFrom(), k -> new HashSet<>()).add(t.getTo());
593+
for (FSATransition t : transitions) {
594+
transitionMap.computeIfAbsent(t.getFromState(), k -> new HashSet<>()).add(t.getToState());
594595
}
595596

596597
while (!queue.isEmpty()) {
@@ -625,11 +626,11 @@ private void checkForUnreachableStates(Set<State> allStates,
625626
*/
626627
private void checkForDeadEndStates(Set<State> allStates,
627628
Set<State> finalStates,
628-
Set<Transition> transitions,
629+
Set<FSATransition> transitions,
629630
List<ValidationMessage> messages) {
630631

631632
Set<State> statesWithOutgoing = transitions.stream()
632-
.map(Transition::getFrom)
633+
.map(FSATransition::getFromState)
633634
.collect(Collectors.toSet());
634635

635636
for (State state : allStates) {
@@ -653,8 +654,8 @@ private boolean hasAllTransitions() {
653654
// Check if any state is missing a transition for any symbol
654655
for (State state : states) {
655656
Set<Symbol> symbolsForState = transitions.stream()
656-
.filter(t -> t.getFrom().equals(state))
657-
.map(Transition::getSymbol)
657+
.filter(t -> t.getFromState().equals(state))
658+
.map(FSATransition::getInputSymbol)
658659
.collect(Collectors.toSet());
659660

660661
if (!symbolsForState.containsAll(alphabet)) {
@@ -711,11 +712,11 @@ public String toDotCode(String inputText) {
711712

712713
// Group transitions by from-state and symbol to create a single edge with multiple labels
713714
Map<State, Map<State, Set<Symbol>>> transitionMap = new HashMap<>();
714-
for (Transition t : transitions) {
715+
for (FSATransition t : transitions) {
715716
transitionMap
716-
.computeIfAbsent(t.getFrom(), k -> new HashMap<>())
717-
.computeIfAbsent(t.getTo(), k -> new TreeSet<>(Comparator.comparing(Symbol::toString)))
718-
.add(t.getSymbol());
717+
.computeIfAbsent(t.getFromState(), k -> new HashMap<>())
718+
.computeIfAbsent(t.getToState(), k -> new TreeSet<>(Comparator.comparing(Symbol::toString)))
719+
.add(t.getInputSymbol());
719720
}
720721

721722
// Add edges

src/main/java/DeterministicFiniteAutomaton/Transition.java

Lines changed: 0 additions & 99 deletions
This file was deleted.

0 commit comments

Comments
 (0)