diff --git a/.idea/workspace.xml b/.idea/workspace.xml
index 4fde15a..8cbd20a 100644
--- a/.idea/workspace.xml
+++ b/.idea/workspace.xml
@@ -4,15 +4,13 @@
-
-
-
-
-
-
-
+
+
+
+
+
@@ -42,36 +40,6 @@
"second": "e0c4ebfe-e254-4a34-93d3-a6ee2cb18f94"
}
}
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
@@ -83,25 +51,25 @@
- {
- "keyToString": {
- "RunOnceActivity.OpenProjectViewOnStart": "true",
- "RunOnceActivity.ShowReadmeOnStart": "true",
- "WebServerToolWindowFactoryState": "false",
- "git-widget-placeholder": "main",
- "ignore.virus.scanning.warn.message": "true",
- "node.js.detected.package.eslint": "true",
- "node.js.detected.package.tslint": "true",
- "node.js.selected.package.eslint": "(autodetect)",
- "node.js.selected.package.tslint": "(autodetect)",
- "nodejs_package_manager_path": "npm",
- "project.structure.last.edited": "Project",
- "project.structure.proportion": "0.0",
- "project.structure.side.proportion": "0.0",
- "settings.editor.selected.configurable": "preferences.pluginManager",
- "vue.rearranger.settings.migration": "true"
+
+}]]>
@@ -151,6 +119,10 @@
+
+
+
+
diff --git a/src/main/java/ab1/impl/GRUPPE/NFAImpl.java b/src/main/java/ab1/impl/GRUPPE/NFAImpl.java
index 8fd9b1e..69ae9a4 100644
--- a/src/main/java/ab1/impl/GRUPPE/NFAImpl.java
+++ b/src/main/java/ab1/impl/GRUPPE/NFAImpl.java
@@ -2,12 +2,10 @@ package ab1.impl.GRUPPE;
import ab1.FinalizedStateException;
import ab1.NFA;
-import ab1.NFAFactory;
import ab1.Transition;
import lombok.Getter;
import java.util.*;
-import java.util.stream.Collectors;
public class NFAImpl implements NFA {
private Set states;
@@ -68,7 +66,7 @@ public class NFAImpl implements NFA {
this.transitions.add(transition);
}
- public void addAllTransition(Set transitions) throws FinalizedStateException {
+ public void addAllTransitions(Set transitions) throws FinalizedStateException {
if(this.isFinalized){
throw new FinalizedStateException();
}
@@ -96,6 +94,7 @@ public class NFAImpl implements NFA {
}
// #TODO
+ // handle case where the state is duplicated
@Override
public NFA union(NFA other) throws FinalizedStateException {
if (!this.isFinalized || !other.isFinalized()) {
@@ -114,6 +113,7 @@ public class NFAImpl implements NFA {
}
// #TODO
+ // needs to work with a DFA not NFA
@Override
public NFA intersection(NFA other) throws FinalizedStateException {
if (!this.isFinalized || !other.isFinalized()) {
@@ -162,21 +162,20 @@ public class NFAImpl implements NFA {
// copy, but without accepting states
nfa.states.addAll(this.states);
- nfa.transitions.addAll(this.transitions);
+ nfa.addAllTransitions(this.transitions);
// adding the initial state as accepting state because we have to accept the empty string
nfa.acceptingStates.add(this.initialState);
// for each accepting state
- for (String acceptingState : this.acceptingStates) {
+ for (String acceptingState : this.getAcceptingStates()) {
Transition loopBackTransition =
// creating an epsilon transition (null) for each accepting state
new Transition(acceptingState, null, this.initialState);
- if (!nfa.transitions.contains(loopBackTransition)) {
- nfa.transitions.add(loopBackTransition);
- }
+ nfa.transitions.add(loopBackTransition);
}
+
return nfa;
}
@@ -186,13 +185,22 @@ public class NFAImpl implements NFA {
throw new FinalizedStateException();
}
- NFAImpl nfa = new NFAImpl(this.initialState);
+ String newInitialState = "newSTART";
+ NFAImpl nfa = new NFAImpl(newInitialState);
// simple copy
nfa.states.addAll(this.states);
nfa.transitions.addAll(this.transitions);
nfa.acceptingStates.addAll(this.acceptingStates);
+ nfa.transitions.add(new Transition(newInitialState, null, this.initialState));
+
+
+ for (String acceptingState : this.acceptingStates) {
+ nfa.transitions.add(new Transition(acceptingState, null, newInitialState));
+ }
+
+ /*
// for each accepting state
for (String acceptingState : this.acceptingStates) {
Transition loopBackTransition =
@@ -202,6 +210,7 @@ public class NFAImpl implements NFA {
nfa.transitions.add(loopBackTransition);
}
}
+ */
return nfa;
}
@@ -213,9 +222,14 @@ public class NFAImpl implements NFA {
throw new FinalizedStateException();
}
- NFAImpl complementNFA = new NFAImpl(this.initialState);
- complementNFA.states.addAll(this.states);
- complementNFA.transitions.addAll(this.transitions);
+ // create a copy of this NFA
+ NFAImpl nfa = new NFAImpl(this.initialState);
+ nfa.states.addAll(this.states);
+ nfa.transitions.addAll(this.transitions);
+ nfa.acceptingStates.addAll(this.acceptingStates);
+
+ //nfa = nfa.convertNFAtoDFA(nfa);
+
/*
for(String state : this.states){
@@ -248,22 +262,40 @@ public class NFAImpl implements NFA {
// #TODO
@Override
public boolean acceptsWord(String word) throws FinalizedStateException {
- //check if word is accepted
+ if (!this.isFinalized) {
+ throw new FinalizedStateException();
+ }
+
+ Set currentStates = new HashSet<>();
+ currentStates.add(this.initialState);
+ currentStates = epsilonClosure(currentStates, this);
+
+
+ for (char letter : word.toCharArray()) {
+ Set nextStates = new HashSet<>();
+
+
+ for (String state : currentStates) {
+ nextStates.addAll(getStateAfterTransition(currentStates, letter, this));
+ }
+
+ nextStates = epsilonClosure(nextStates, this);
+
+ currentStates = nextStates;
+ }
+
+ for (String state : currentStates) {
+ if (this.acceptingStates.contains(state)) {
+ return true;
+ }
+ }
+
return false;
}
public NFA convertNFAtoDFA(NFAImpl input) {
- /*
- What do we need to do?
-
- Helper Methods:
- - epsilonClosure
- - getStateAfterTransition
- - determineAcceptingStates
-
- */
-
+ // all accepting states of the DFA
Set dfaAcceptingStates = new HashSet<>();
// all states of the DFA
List> dfaStates = new ArrayList<>();
@@ -299,11 +331,9 @@ public class NFAImpl implements NFA {
}
}
+
// build new Transition
dfaTransitions.add(new Transition(getIndexOfSet(currentState, dfaStates), letter, getIndexOfSet(newState, dfaStates)));
-
- // we got to change this to only a string is given
- //dfaTransitions.add(new Transition(currentState, letter, newState));
}
}
@@ -313,7 +343,7 @@ public class NFAImpl implements NFA {
newDFA.states.add(""+i);
}
- newDFA.addAllTransition(dfaTransitions);
+ newDFA.addAllTransitions(dfaTransitions);
newDFA.addAllAcceptingStates(dfaAcceptingStates);
newDFA.finalizeAutomaton();
@@ -322,35 +352,6 @@ public class NFAImpl implements NFA {
}
private Set epsilonClosure(Set states, NFA nfa) {
- /*
- Original:
-
- boolean isAccepting = false;
-
- int index = 0;
-
- List> DFA_states = new ArrayList<>();
- Set DFA_transitions = new HashSet<>();
-
- // start state alle verbindungen mit epsilon als set
- for(String state : states) {
- for (Transition transition : input.transitions) {
- if (state.equals(transition.fromState()) && transition.readSymbol() == null) {
- if(!isAccepting) {
- for (String acceptingState : acceptingStates)
- if (transition.toState().equals(acceptingState)) {
- isAccepting = true;
- break;
- }
- }
- states.add(transition.toState());
- // funktioniert immer noch, wenn w epsilon Übergänge hintereinander?
- }
- }
- }
- DFA_states.add(new HashSet<>(states));
- */
-
Set closure = new HashSet<>(states);
Stack stack = new Stack<>();
stack.addAll(states);
diff --git a/target/classes/ab1/impl/GRUPPE/NFAImpl.class b/target/classes/ab1/impl/GRUPPE/NFAImpl.class
index 1ec6e11..a3c18e6 100644
Binary files a/target/classes/ab1/impl/GRUPPE/NFAImpl.class and b/target/classes/ab1/impl/GRUPPE/NFAImpl.class differ