Merge branch 'main' into '2-playing-around'

# Conflicts:
#   .idea/workspace.xml
This commit is contained in:
rawalcher 2024-01-08 17:34:39 +00:00
commit b37348b9c3
4 changed files with 64 additions and 2 deletions

View file

@ -100,6 +100,39 @@ public class NFAImpl implements NFA {
if (!this.isFinalized || !other.isFinalized()) {
throw new FinalizedStateException();
}
// new initialState with epsilon to initialState of this and other.
// Problem: what if states are called the same in this and other + what do we use as initialState?
Set<String> unionStates = new HashSet<>();
unionStates.addAll(this.states);
//do the union:
String start = changeIfNecessary(changeIfNecessary("START", unionStates), other.getStates());
for(String state : other.getStates()){
if(!unionStates.contains(state)){
unionStates.add(state);
}else{
// a state of other has the same name as one in this.
//change name of state and every Transitions it is a part of in other
String newstate = state;
while (unionStates.contains(newstate)){
//change the state slightly and save the different version
newstate = newstate+"|";
}
for(Transition transition : other.getTransitions()){
if(transition.fromState().equals(state)){
Transition changedTransition = new Transition(newstate, transition.readSymbol(), transition.toState());
}
if(transition.toState().equals(state)){
Transition changedTransition = new Transition(transition.fromState(), transition.readSymbol(), newstate);
}
}
unionStates.add(newstate);
}
}
NFAImpl unionNFA = new NFAImpl(this.initialState);
unionNFA.states.addAll(this.states);
@ -393,4 +426,11 @@ public class NFAImpl implements NFA {
return Integer.toString(dfaStates.indexOf(set));
}
private String changeIfNecessary(String str, Set<String> toCheck){
while(toCheck.contains(str)){
str = str+"|";
}
return str;
}
}