diff --git a/.idea/workspace.xml b/.idea/workspace.xml
index b8993e6..c56bcf8 100644
--- a/.idea/workspace.xml
+++ b/.idea/workspace.xml
@@ -4,14 +4,9 @@
-
-
-
-
-
-
-
-
+
+
+
@@ -289,7 +284,15 @@
1704914909876
-
+
+
+ 1704915995807
+
+
+
+ 1704915995807
+
+
@@ -315,7 +318,8 @@
-
+
+
diff --git a/src/main/java/ab1/impl/gruppe10_aigensberger_dworski_walcher/NFAImpl.java b/src/main/java/ab1/impl/gruppe10_aigensberger_dworski_walcher/NFAImpl.java
index 08cfa8c..884a50e 100644
--- a/src/main/java/ab1/impl/gruppe10_aigensberger_dworski_walcher/NFAImpl.java
+++ b/src/main/java/ab1/impl/gruppe10_aigensberger_dworski_walcher/NFAImpl.java
@@ -562,6 +562,7 @@ public class NFAImpl implements NFA {
}
return false;
}
+
private boolean stateReachesAcceptingstates(String state){
if(this.acceptingStates.contains(state)){
return true;
@@ -592,7 +593,12 @@ public class NFAImpl implements NFA {
return false;
}
- // #TODO
+ /**
+ * Returns whether the automaton accepts the given word. If the automaton is not finalized, a FinalizedStateException is thrown.
+ * @param word word to check, can be empty
+ * @return Whether the automaton accepts the given word
+ * @throws FinalizedStateException
+ */
@Override
public boolean acceptsWord(String word) throws FinalizedStateException {
if (!this.isFinalized) {
@@ -606,46 +612,41 @@ public class NFAImpl implements NFA {
currentStates = (epsilonClosure(currentStates, this));
for (char letter : wordArray) {
- // schritt a
currentStates = getStateAfterTransition(currentStates, letter, this);
- // schritt b
currentStates = epsilonClosure(currentStates, this);
}
-
-
return isAcceptingState(currentStates, this);
}
+
+ /**
+ * Converts the given NFA to a DFA. If the NFA is not finalized, a FinalizedStateException is thrown.
+ * @param input The NFA to convert
+ * @return The converted DFA
+ */
public NFAImpl convertNFAtoDFA(NFAImpl input) {
- // all accepting states of the DFA
Set dfaAcceptingStates = new HashSet<>();
- // all states of the DFA
List> dfaStates = new ArrayList<>();
- // all transitions of the DFA
Set dfaTransitions = new HashSet<>();
-
- // all states of the NFA
Set startState = new HashSet<>();
startState.add(input.getInitialState());
- // getting the epsilon closure of the start state
startState = epsilonClosure(startState, input);
dfaStates.add(startState);
if (isAcceptingState(startState, input)) {
dfaAcceptingStates.add(getIndexOfSet(startState, dfaStates));
}
- // do the above with each DFA state
Queue> queue = new LinkedList<>();
queue.add(startState);
- // iterate each Letter
+
while (!queue.isEmpty()) {
Set currentState = queue.poll();
- // for each possible Letter
+
for (char letter : input.getAlphabet()) {
- // get the new state after the transition
+
Set newState = epsilonClosure(getStateAfterTransition(currentState, letter, input), input);
if (!dfaStates.contains(newState)) {
queue.add(newState);
@@ -656,7 +657,6 @@ public class NFAImpl implements NFA {
}
- // build new Transition
dfaTransitions.add(new Transition(getIndexOfSet(currentState, dfaStates), letter, getIndexOfSet(newState, dfaStates)));
}
}
@@ -673,6 +673,12 @@ public class NFAImpl implements NFA {
return newDFA;
}
+ /**
+ * Returns the epsilon closure of the given states.
+ * @param states The states to get the epsilon closure of
+ * @param nfa The NFA to get the epsilon closure from
+ * @return The epsilon closure of the given states
+ */
private Set epsilonClosure(Set states, NFA nfa) {
Set closure = new HashSet<>(states);
Stack stack = new Stack<>();
@@ -690,6 +696,13 @@ public class NFAImpl implements NFA {
return closure;
}
+ /**
+ * Returns the states that are reached by the given states after reading the given symbol.
+ * @param states The states to get the states after reading the symbol of
+ * @param symbol The symbol to read
+ * @param nfa The NFA to get the states from
+ * @return The states that are reached by the given states after reading the given symbol
+ */
private Set getStateAfterTransition(Set states, char symbol, NFA nfa) {
Set result = new HashSet<>();
for (String state : states) {
@@ -702,6 +715,12 @@ public class NFAImpl implements NFA {
return result;
}
+ /**
+ * Returns whether the given states contain an accepting state.
+ * @param states The states to check
+ * @param input The NFA to check the states for
+ * @return Whether the given states contain an accepting state
+ */
private boolean isAcceptingState(Set states, NFA input) {
for (String accState : input.getAcceptingStates()) {
if (states.contains(accState)) {
@@ -711,10 +730,22 @@ public class NFAImpl implements NFA {
return false;
}
+ /**
+ * Returns the index of the given set in the given list.
+ * @param set The set to get the index of
+ * @param dfaStates The list to get the index from
+ * @return The index of the given set in the given list
+ */
private String getIndexOfSet(Set set, List> dfaStates) {
return Integer.toString(dfaStates.indexOf(set));
}
+ /**
+ * Returns the given string with a number appended to it, if the string already exists in the given set.
+ * @param str The string to check
+ * @param toCheck The set to check for duplicates
+ * @return The given string with a number appended to it, if the string already exists in the given set
+ */
private String changeIfNecessary(String str, Set toCheck) {
while (toCheck.contains(str)) {
str = str + "1";
@@ -722,6 +753,10 @@ public class NFAImpl implements NFA {
return str;
}
+ /**
+ * Adds a trap state to the given NFA.
+ * @param input The NFA to add the trap state to
+ */
private void nfaAddTrap(NFAImpl input){
String trap = changeIfNecessary("TRAP", input.getStates());
input.states.add(trap);
@@ -742,7 +777,6 @@ public class NFAImpl implements NFA {
}
}
}
- input.addAllTransitions(newTransitions); //input.transitions.addAll(newTransitions);
+ input.addAllTransitions(newTransitions);
}
-
}
diff --git a/src/test/java/ab1/tests/GRUPPE/betterSimpleTests.java b/src/test/java/ab1/tests/GRUPPE/betterSimpleTests.java
index ab93e24..99c9f71 100644
--- a/src/test/java/ab1/tests/GRUPPE/betterSimpleTests.java
+++ b/src/test/java/ab1/tests/GRUPPE/betterSimpleTests.java
@@ -38,7 +38,6 @@ public class betterSimpleTests {
assertThrows(IllegalArgumentException.class, () -> instance.addTransition(transition));
}
-
@Test
public void transitionWithoutExistingToState() {
var instance = factory.buildNFA("START");
@@ -70,6 +69,5 @@ public class betterSimpleTests {
assertFalse(instance.acceptsWord("b"));
assertTrue(instance.acceptsWord("a"));
assertFalse(instance.acceptsWord("ab"));
-
}
}