union working in progress

This commit is contained in:
Ghost_Element 2024-01-08 18:29:19 +01:00
parent e6ef99bb8b
commit 050f3a6d86
4 changed files with 61 additions and 36 deletions

View file

@ -101,6 +101,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);
@ -392,4 +425,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;
}
}