From 050f3a6d86bffaa1b019b33941e8934694090c4f Mon Sep 17 00:00:00 2001 From: Ghost_Element Date: Mon, 8 Jan 2024 18:29:19 +0100 Subject: [PATCH] union working in progress --- .idea/misc.xml | 2 +- .idea/theorethische-informatik-gruppe-10.iml | 9 ++++ .idea/workspace.xml | 46 +++++--------------- src/main/java/ab1/impl/GRUPPE/NFAImpl.java | 40 +++++++++++++++++ 4 files changed, 61 insertions(+), 36 deletions(-) create mode 100644 .idea/theorethische-informatik-gruppe-10.iml diff --git a/.idea/misc.xml b/.idea/misc.xml index ecfa09c..9902dc5 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -8,7 +8,7 @@ - + \ No newline at end of file diff --git a/.idea/theorethische-informatik-gruppe-10.iml b/.idea/theorethische-informatik-gruppe-10.iml new file mode 100644 index 0000000..d6ebd48 --- /dev/null +++ b/.idea/theorethische-informatik-gruppe-10.iml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/.idea/workspace.xml b/.idea/workspace.xml index 4fde15a..8ee1354 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -5,14 +5,13 @@ - - + + + - - - + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - @@ -134,6 +103,13 @@ + + + + + + diff --git a/src/main/java/ab1/impl/GRUPPE/NFAImpl.java b/src/main/java/ab1/impl/GRUPPE/NFAImpl.java index 8fd9b1e..69ac215 100644 --- a/src/main/java/ab1/impl/GRUPPE/NFAImpl.java +++ b/src/main/java/ab1/impl/GRUPPE/NFAImpl.java @@ -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 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 toCheck){ + while(toCheck.contains(str)){ + str = str+"|"; + } + return str; + } + }