complement works now
This commit is contained in:
parent
6c47b25ed2
commit
0d47de5c44
3 changed files with 46 additions and 22 deletions
|
|
@ -15,6 +15,7 @@ public class NFAImpl implements NFA {
|
|||
|
||||
private Set<Character> alphabet;
|
||||
|
||||
private Set<Character> completeAlphabet;
|
||||
private boolean isFinalized;
|
||||
|
||||
public NFAImpl(String startState) {
|
||||
|
|
@ -24,9 +25,9 @@ public class NFAImpl implements NFA {
|
|||
this.transitions = new HashSet<>();
|
||||
this.acceptingStates = new HashSet<>();
|
||||
this.alphabet = new HashSet<>();
|
||||
Set<Character> completeAlphabet = new HashSet<>();
|
||||
for (char ch = 'a'; ch <= 'z'; ch++) {
|
||||
completeAlphabet.add(ch);
|
||||
this.completeAlphabet = new HashSet<>();
|
||||
for(int i='a';i<='z';i++){
|
||||
completeAlphabet.add((char)i);
|
||||
}
|
||||
this.isFinalized = false;
|
||||
|
||||
|
|
@ -344,6 +345,8 @@ public class NFAImpl implements NFA {
|
|||
nfa.addAllTransitions(this.transitions);
|
||||
nfa.addAllAcceptingStates(this.acceptingStates);
|
||||
|
||||
nfaAddTrap(nfa);
|
||||
|
||||
NFAImpl fakeNFA = convertNFAtoDFA(nfa);
|
||||
|
||||
// state -> accepting
|
||||
|
|
@ -533,4 +536,25 @@ public class NFAImpl implements NFA {
|
|||
return str;
|
||||
}
|
||||
|
||||
private void nfaAddTrap(NFAImpl input){
|
||||
String trap = changeIfNecessary("TRAP", input.getStates());
|
||||
input.states.add(trap);
|
||||
Set<Transition> newTransitions = new HashSet<>();
|
||||
for(String state : input.getStates()){
|
||||
for(char letter : completeAlphabet){
|
||||
boolean hasTransition = false;
|
||||
for(Transition transition : input.getTransitions()){
|
||||
if(transition.fromState().equals(state)&&transition.readSymbol().equals(letter)){
|
||||
hasTransition = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if(!hasTransition){
|
||||
newTransitions.add(new Transition(state, letter, trap));
|
||||
}
|
||||
}
|
||||
}
|
||||
input.addAllTransitions(newTransitions); //input.transitions.addAll(newTransitions);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
Reference in a new issue