fixed kleeneStar and plusOperator, unit tests now work

This commit is contained in:
Walcher 2024-01-07 13:02:07 +01:00
parent 34cf8f6207
commit 0c3480e590

View file

@ -47,7 +47,7 @@ public class NFAImpl implements NFA {
public void addTransition(Transition transition) throws FinalizedStateException {
if (this.isFinalized) {
throw new FinalizedStateException();
} else {
} else if (!this.transitions.contains(transition)) {
this.transitions.add(transition);
}
}
@ -56,7 +56,7 @@ public class NFAImpl implements NFA {
public void addAcceptingState(String state) throws FinalizedStateException {
if (this.isFinalized) {
throw new FinalizedStateException();
} else {
} else if (!this.states.contains(state)) {
this.acceptingStates.add(state);
}
}
@ -80,7 +80,6 @@ public class NFAImpl implements NFA {
}
*/
// #TODO
@Override
public NFA union(NFA other) throws FinalizedStateException {
@ -99,16 +98,17 @@ public class NFAImpl implements NFA {
return null;
}
// #TODO
@Override
public NFA kleeneStar() throws FinalizedStateException {
if (this.isFinalized) {
if (!this.isFinalized) {
throw new FinalizedStateException();
}
NFAImpl nfa = new NFAImpl(this.initialState);
// deep copy but without accepting states
// copy, but without accepting states
nfa.states.addAll(this.states);
nfa.transitions.addAll(this.transitions);
@ -119,24 +119,22 @@ public class NFAImpl implements NFA {
nfa.transitions.add(loopBackTransition);
}
// adding the initial state as accepting state
// adding the initial state as accepting state because we have to accept the empty string
nfa.acceptingStates.add(this.initialState);
nfa.finalizeAutomaton();
return nfa;
}
// #TODO
@Override
public NFA plusOperator() throws FinalizedStateException {
if (this.isFinalized) {
if (!this.isFinalized) {
throw new FinalizedStateException();
}
NFAImpl nfa = new NFAImpl(this.initialState);
// simple deep copy
// simple copy
nfa.states.addAll(this.states);
nfa.transitions.addAll(this.transitions);
nfa.acceptingStates.addAll(this.acceptingStates);
@ -158,13 +156,11 @@ public class NFAImpl implements NFA {
return null;
}
// #TODO
@Override
public boolean isFinalized() {
return isFinalized;
}
// #TODO
@Override
public void finalizeAutomaton() {
this.isFinalized = true;
@ -179,7 +175,8 @@ public class NFAImpl implements NFA {
// #TODO
@Override
public boolean acceptsWord(String word) {
public boolean acceptsWord(String word) throws FinalizedStateException {
//check if word is accepted
return false;
}
}