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