documentation. cleanup. your turn daniel

This commit is contained in:
Walcher 2024-01-10 20:53:17 +01:00
parent e2687635cb
commit 31badb9da5
3 changed files with 66 additions and 30 deletions

24
.idea/workspace.xml generated
View file

@ -4,14 +4,9 @@
<option name="autoReloadType" value="SELECTIVE" />
</component>
<component name="ChangeListManager">
<list default="true" id="f5e08808-a5af-4c89-a1ee-21b3bcb9ae3e" name="Changes" comment=".">
<change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/main/java/ab1/impl/GRUPPE/NFAImpl.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/java/ab1/impl/gruppe10_aigensberger_dworski_walcher/NFAImpl.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/target/classes/ab1/Transition$TransitionBuilder.class" beforeDir="false" afterPath="$PROJECT_DIR$/target/classes/ab1/Transition$TransitionBuilder.class" afterDir="false" />
<change beforePath="$PROJECT_DIR$/target/classes/ab1/Transition.class" beforeDir="false" afterPath="$PROJECT_DIR$/target/classes/ab1/Transition.class" afterDir="false" />
<change beforePath="$PROJECT_DIR$/target/classes/ab1/impl/GRUPPE/NFAFactoryImpl.class" beforeDir="false" />
<change beforePath="$PROJECT_DIR$/target/test-classes/ab1/NFAProvider.class" beforeDir="false" afterPath="$PROJECT_DIR$/target/test-classes/ab1/NFAProvider.class" afterDir="false" />
<change beforePath="$PROJECT_DIR$/target/test-classes/ab1/tests/FinalizeTests.class" beforeDir="false" afterPath="$PROJECT_DIR$/target/test-classes/ab1/tests/FinalizeTests.class" afterDir="false" />
<list default="true" id="f5e08808-a5af-4c89-a1ee-21b3bcb9ae3e" name="Changes" comment="documentation. cleanup. your turn daniel">
<change beforePath="$PROJECT_DIR$/src/main/java/ab1/impl/gruppe10_aigensberger_dworski_walcher/NFAImpl.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/java/ab1/impl/gruppe10_aigensberger_dworski_walcher/NFAImpl.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/test/java/ab1/tests/GRUPPE/betterSimpleTests.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/test/java/ab1/tests/GRUPPE/betterSimpleTests.java" afterDir="false" />
</list>
<list id="39d4ccb3-eae9-4ed4-996a-5a13f44678fa" name="Changes by carol" comment="" />
<option name="SHOW_DIALOG" value="false" />
@ -289,7 +284,15 @@
<option name="project" value="LOCAL" />
<updated>1704914909876</updated>
</task>
<option name="localTasksCounter" value="12" />
<task id="LOCAL-00012" summary="documentation. cleanup. your turn daniel">
<option name="closed" value="true" />
<created>1704915995807</created>
<option name="number" value="00012" />
<option name="presentableId" value="LOCAL-00012" />
<option name="project" value="LOCAL" />
<updated>1704915995807</updated>
</task>
<option name="localTasksCounter" value="13" />
<servers />
</component>
<component name="TypeScriptGeneratedFilesManager">
@ -315,7 +318,8 @@
<MESSAGE value="changed nfaAddTrap, bei dem Fall, dass Transition.readSymbol() einen nullwert zurückgibt. (Thx ChatGBT)&#10;-&gt; intersectionTests funktionieren" />
<MESSAGE value="isFinite methode recursiv..." />
<MESSAGE value="." />
<option name="LAST_COMMIT_MESSAGE" value="." />
<MESSAGE value="documentation. cleanup. your turn daniel" />
<option name="LAST_COMMIT_MESSAGE" value="documentation. cleanup. your turn daniel" />
</component>
<component name="XDebuggerManager">
<breakpoint-manager>

View file

@ -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<String> dfaAcceptingStates = new HashSet<>();
// all states of the DFA
List<Set<String>> dfaStates = new ArrayList<>();
// all transitions of the DFA
Set<Transition> dfaTransitions = new HashSet<>();
// all states of the NFA
Set<String> 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<Set<String>> queue = new LinkedList<>();
queue.add(startState);
// iterate each Letter
while (!queue.isEmpty()) {
Set<String> currentState = queue.poll();
// for each possible Letter
for (char letter : input.getAlphabet()) {
// get the new state after the transition
Set<String> 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<String> epsilonClosure(Set<String> states, NFA nfa) {
Set<String> closure = new HashSet<>(states);
Stack<String> 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<String> getStateAfterTransition(Set<String> states, char symbol, NFA nfa) {
Set<String> 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<String> 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<String> set, List<Set<String>> 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<String> 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);
}
}

View file

@ -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"));
}
}