changed a bit

This commit is contained in:
Walcher 2024-01-08 18:29:06 +01:00
parent e6ef99bb8b
commit 29e5431bc7
3 changed files with 84 additions and 111 deletions

View file

@ -2,12 +2,10 @@ package ab1.impl.GRUPPE;
import ab1.FinalizedStateException;
import ab1.NFA;
import ab1.NFAFactory;
import ab1.Transition;
import lombok.Getter;
import java.util.*;
import java.util.stream.Collectors;
public class NFAImpl implements NFA {
private Set<String> states;
@ -68,7 +66,7 @@ public class NFAImpl implements NFA {
this.transitions.add(transition);
}
public void addAllTransition(Set<Transition> transitions) throws FinalizedStateException {
public void addAllTransitions(Set<Transition> transitions) throws FinalizedStateException {
if(this.isFinalized){
throw new FinalizedStateException();
}
@ -96,6 +94,7 @@ public class NFAImpl implements NFA {
}
// #TODO
// handle case where the state is duplicated
@Override
public NFA union(NFA other) throws FinalizedStateException {
if (!this.isFinalized || !other.isFinalized()) {
@ -114,6 +113,7 @@ public class NFAImpl implements NFA {
}
// #TODO
// needs to work with a DFA not NFA
@Override
public NFA intersection(NFA other) throws FinalizedStateException {
if (!this.isFinalized || !other.isFinalized()) {
@ -162,21 +162,20 @@ public class NFAImpl implements NFA {
// copy, but without accepting states
nfa.states.addAll(this.states);
nfa.transitions.addAll(this.transitions);
nfa.addAllTransitions(this.transitions);
// adding the initial state as accepting state because we have to accept the empty string
nfa.acceptingStates.add(this.initialState);
// for each accepting state
for (String acceptingState : this.acceptingStates) {
for (String acceptingState : this.getAcceptingStates()) {
Transition loopBackTransition =
// creating an epsilon transition (null) for each accepting state
new Transition(acceptingState, null, this.initialState);
if (!nfa.transitions.contains(loopBackTransition)) {
nfa.transitions.add(loopBackTransition);
}
nfa.transitions.add(loopBackTransition);
}
return nfa;
}
@ -186,13 +185,22 @@ public class NFAImpl implements NFA {
throw new FinalizedStateException();
}
NFAImpl nfa = new NFAImpl(this.initialState);
String newInitialState = "newSTART";
NFAImpl nfa = new NFAImpl(newInitialState);
// simple copy
nfa.states.addAll(this.states);
nfa.transitions.addAll(this.transitions);
nfa.acceptingStates.addAll(this.acceptingStates);
nfa.transitions.add(new Transition(newInitialState, null, this.initialState));
for (String acceptingState : this.acceptingStates) {
nfa.transitions.add(new Transition(acceptingState, null, newInitialState));
}
/*
// for each accepting state
for (String acceptingState : this.acceptingStates) {
Transition loopBackTransition =
@ -202,6 +210,7 @@ public class NFAImpl implements NFA {
nfa.transitions.add(loopBackTransition);
}
}
*/
return nfa;
}
@ -213,9 +222,14 @@ public class NFAImpl implements NFA {
throw new FinalizedStateException();
}
NFAImpl complementNFA = new NFAImpl(this.initialState);
complementNFA.states.addAll(this.states);
complementNFA.transitions.addAll(this.transitions);
// create a copy of this NFA
NFAImpl nfa = new NFAImpl(this.initialState);
nfa.states.addAll(this.states);
nfa.transitions.addAll(this.transitions);
nfa.acceptingStates.addAll(this.acceptingStates);
//nfa = nfa.convertNFAtoDFA(nfa);
/*
for(String state : this.states){
@ -248,22 +262,40 @@ public class NFAImpl implements NFA {
// #TODO
@Override
public boolean acceptsWord(String word) throws FinalizedStateException {
//check if word is accepted
if (!this.isFinalized) {
throw new FinalizedStateException();
}
Set<String> currentStates = new HashSet<>();
currentStates.add(this.initialState);
currentStates = epsilonClosure(currentStates, this);
for (char letter : word.toCharArray()) {
Set<String> nextStates = new HashSet<>();
for (String state : currentStates) {
nextStates.addAll(getStateAfterTransition(currentStates, letter, this));
}
nextStates = epsilonClosure(nextStates, this);
currentStates = nextStates;
}
for (String state : currentStates) {
if (this.acceptingStates.contains(state)) {
return true;
}
}
return false;
}
public NFA convertNFAtoDFA(NFAImpl input) {
/*
What do we need to do?
Helper Methods:
- epsilonClosure
- getStateAfterTransition
- determineAcceptingStates
*/
// all accepting states of the DFA
Set<String> dfaAcceptingStates = new HashSet<>();
// all states of the DFA
List<Set<String>> dfaStates = new ArrayList<>();
@ -299,11 +331,9 @@ public class NFAImpl implements NFA {
}
}
// build new Transition
dfaTransitions.add(new Transition(getIndexOfSet(currentState, dfaStates), letter, getIndexOfSet(newState, dfaStates)));
// we got to change this to only a string is given
//dfaTransitions.add(new Transition(currentState, letter, newState));
}
}
@ -313,7 +343,7 @@ public class NFAImpl implements NFA {
newDFA.states.add(""+i);
}
newDFA.addAllTransition(dfaTransitions);
newDFA.addAllTransitions(dfaTransitions);
newDFA.addAllAcceptingStates(dfaAcceptingStates);
newDFA.finalizeAutomaton();
@ -322,35 +352,6 @@ public class NFAImpl implements NFA {
}
private Set<String> epsilonClosure(Set<String> states, NFA nfa) {
/*
Original:
boolean isAccepting = false;
int index = 0;
List<Set<String>> DFA_states = new ArrayList<>();
Set<Transition> DFA_transitions = new HashSet<>();
// start state alle verbindungen mit epsilon als set
for(String state : states) {
for (Transition transition : input.transitions) {
if (state.equals(transition.fromState()) && transition.readSymbol() == null) {
if(!isAccepting) {
for (String acceptingState : acceptingStates)
if (transition.toState().equals(acceptingState)) {
isAccepting = true;
break;
}
}
states.add(transition.toState());
// funktioniert immer noch, wenn w epsilon Übergänge hintereinander?
}
}
}
DFA_states.add(new HashSet<>(states));
*/
Set<String> closure = new HashSet<>(states);
Stack<String> stack = new Stack<>();
stack.addAll(states);