documentation. cleanup. your turn daniel

This commit is contained in:
Walcher 2024-01-10 20:46:35 +01:00
parent f3e1a1a16c
commit e2687635cb
8 changed files with 129 additions and 98 deletions

40
.idea/workspace.xml generated
View file

@ -4,23 +4,14 @@
<option name="autoReloadType" value="SELECTIVE" /> <option name="autoReloadType" value="SELECTIVE" />
</component> </component>
<component name="ChangeListManager"> <component name="ChangeListManager">
<list default="true" id="f5e08808-a5af-4c89-a1ee-21b3bcb9ae3e" name="Changes" comment="isFinite methode recursiv..."> <list default="true" id="f5e08808-a5af-4c89-a1ee-21b3bcb9ae3e" name="Changes" comment=".">
<change afterPath="$PROJECT_DIR$/.idea/uiDesigner.xml" afterDir="false" />
<change afterPath="$PROJECT_DIR$/src/main/java/ab1/impl/gruppe10_aigensberger_dworski_walcher/AutomatonManager.java" afterDir="false" />
<change afterPath="$PROJECT_DIR$/src/main/java/ab1/impl/gruppe10_aigensberger_dworski_walcher/DFAConverter.java" afterDir="false" />
<change afterPath="$PROJECT_DIR$/src/main/java/ab1/impl/gruppe10_aigensberger_dworski_walcher/FiniteAnalysis.java" afterDir="false" />
<change afterPath="$PROJECT_DIR$/src/main/java/ab1/impl/gruppe10_aigensberger_dworski_walcher/StateManager.java" afterDir="false" />
<change afterPath="$PROJECT_DIR$/src/main/java/ab1/impl/gruppe10_aigensberger_dworski_walcher/TransitionManager.java" afterDir="false" />
<change afterPath="$PROJECT_DIR$/src/main/java/ab1/impl/gruppe10_aigensberger_dworski_walcher/WordAcceptor.java" afterDir="false" />
<change afterPath="$PROJECT_DIR$/src/test/java/ab1/tests/GRUPPE/betterSimpleTests.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/.idea/misc.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/misc.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" /> <change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/main/java/ab1/impl/GRUPPE/NFAFactoryImpl.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/java/ab1/impl/gruppe10_aigensberger_dworski_walcher/NFAFactoryImpl.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/main/java/ab1/impl/GRUPPE/NFAImpl.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/java/ab1/impl/gruppe10_aigensberger_dworski_walcher/NFAImpl.java" afterDir="false" /> <change beforePath="$PROJECT_DIR$/src/main/java/ab1/impl/GRUPPE/NFAImpl.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/java/ab1/impl/gruppe10_aigensberger_dworski_walcher/NFAImpl.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/main/java/ab1/impl/GRUPPE/Tuple.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/java/ab1/impl/gruppe10_aigensberger_dworski_walcher/Tuple.java" afterDir="false" /> <change beforePath="$PROJECT_DIR$/target/classes/ab1/Transition$TransitionBuilder.class" beforeDir="false" afterPath="$PROJECT_DIR$/target/classes/ab1/Transition$TransitionBuilder.class" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/test/java/ab1/NFAProvider.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/test/java/ab1/NFAProvider.java" afterDir="false" /> <change beforePath="$PROJECT_DIR$/target/classes/ab1/Transition.class" beforeDir="false" afterPath="$PROJECT_DIR$/target/classes/ab1/Transition.class" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/test/java/ab1/tests/myTests.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/test/java/ab1/tests/GRUPPE/myTests.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/target/classes/ab1/impl/GRUPPE/NFAFactoryImpl.class" beforeDir="false" /> <change beforePath="$PROJECT_DIR$/target/classes/ab1/impl/GRUPPE/NFAFactoryImpl.class" beforeDir="false" />
<change beforePath="$PROJECT_DIR$/target/test-classes/ab1/NFAProvider.class" beforeDir="false" afterPath="$PROJECT_DIR$/target/test-classes/ab1/NFAProvider.class" afterDir="false" />
<change beforePath="$PROJECT_DIR$/target/test-classes/ab1/tests/FinalizeTests.class" beforeDir="false" afterPath="$PROJECT_DIR$/target/test-classes/ab1/tests/FinalizeTests.class" afterDir="false" />
</list> </list>
<list id="39d4ccb3-eae9-4ed4-996a-5a13f44678fa" name="Changes by carol" comment="" /> <list id="39d4ccb3-eae9-4ed4-996a-5a13f44678fa" name="Changes by carol" comment="" />
<option name="SHOW_DIALOG" value="false" /> <option name="SHOW_DIALOG" value="false" />
@ -282,7 +273,23 @@
<option name="project" value="LOCAL" /> <option name="project" value="LOCAL" />
<updated>1704888180655</updated> <updated>1704888180655</updated>
</task> </task>
<option name="localTasksCounter" value="10" /> <task id="LOCAL-00010" summary=".">
<option name="closed" value="true" />
<created>1704914901826</created>
<option name="number" value="00010" />
<option name="presentableId" value="LOCAL-00010" />
<option name="project" value="LOCAL" />
<updated>1704914901826</updated>
</task>
<task id="LOCAL-00011" summary=".">
<option name="closed" value="true" />
<created>1704914909876</created>
<option name="number" value="00011" />
<option name="presentableId" value="LOCAL-00011" />
<option name="project" value="LOCAL" />
<updated>1704914909876</updated>
</task>
<option name="localTasksCounter" value="12" />
<servers /> <servers />
</component> </component>
<component name="TypeScriptGeneratedFilesManager"> <component name="TypeScriptGeneratedFilesManager">
@ -307,7 +314,8 @@
<MESSAGE value="concat GEHT" /> <MESSAGE value="concat GEHT" />
<MESSAGE value="changed nfaAddTrap, bei dem Fall, dass Transition.readSymbol() einen nullwert zurückgibt. (Thx ChatGBT)&#10;-&gt; intersectionTests funktionieren" /> <MESSAGE value="changed nfaAddTrap, bei dem Fall, dass Transition.readSymbol() einen nullwert zurückgibt. (Thx ChatGBT)&#10;-&gt; intersectionTests funktionieren" />
<MESSAGE value="isFinite methode recursiv..." /> <MESSAGE value="isFinite methode recursiv..." />
<option name="LAST_COMMIT_MESSAGE" value="isFinite methode recursiv..." /> <MESSAGE value="." />
<option name="LAST_COMMIT_MESSAGE" value="." />
</component> </component>
<component name="XDebuggerManager"> <component name="XDebuggerManager">
<breakpoint-manager> <breakpoint-manager>

View file

@ -1,4 +1,4 @@
package ab1.impl.GRUPPE; package ab1.impl.gruppe10_aigensberger_dworski_walcher;
import ab1.FinalizedStateException; import ab1.FinalizedStateException;
import ab1.NFA; import ab1.NFA;
@ -7,6 +7,9 @@ import ab1.Transition;
import java.util.*; import java.util.*;
import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicInteger;
/**
* Implementation of the NFA interface.
*/
public class NFAImpl implements NFA { public class NFAImpl implements NFA {
private final Set<String> states; private final Set<String> states;
private final Set<Transition> transitions; private final Set<Transition> transitions;
@ -39,14 +42,6 @@ public class NFAImpl implements NFA {
return this.states; return this.states;
} }
/*
public void safeAddStates(Set<String> states, Set<String> toCheck, NFAImpl nfa) {
for (String state : states) {
nfa.states.add(changeIfNecessary(state, toCheck));
}
}
*/
@Override @Override
public Collection<Transition> getTransitions() { public Collection<Transition> getTransitions() {
return this.transitions; return this.transitions;
@ -66,24 +61,35 @@ public class NFAImpl implements NFA {
return this.alphabet; return this.alphabet;
} }
/**
* Adds a new transition to the automaton. If the automaton is already finalized, a FinalizedStateException is thrown.
* @param transition The transition to add
* @throws FinalizedStateException
*/
@Override @Override
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();
} }
if (!states.contains(transition.fromState())) { if (transition == null || transition.fromState() == null || transition.toState() == null) {
this.states.add(transition.fromState()); throw new IllegalArgumentException();
} }
if (!states.contains(transition.toState())) {
this.states.add(transition.toState()); states.add(transition.fromState());
states.add(transition.toState());
if (transition.readSymbol()!=null) {
alphabet.add(transition.readSymbol());
} }
// add symbol to alphabet. If it is already in the alphabet, nothing happens transitions.add(transition);
if (transition.readSymbol() != null) {
this.alphabet.add(transition.readSymbol());
}
this.transitions.add(transition);
} }
/**
* Adds all transitions to the automaton. If the automaton is already finalized, a FinalizedStateException is thrown.
* @param transitions The Set of transitions to add
* @throws FinalizedStateException
*/
public void addAllTransitions(Set<Transition> transitions) throws FinalizedStateException { public void addAllTransitions(Set<Transition> transitions) throws FinalizedStateException {
if (this.isFinalized) { if (this.isFinalized) {
throw new FinalizedStateException(); throw new FinalizedStateException();
@ -93,49 +99,79 @@ public class NFAImpl implements NFA {
} }
} }
/**
* Adds a new accepting state to the automaton. If the automaton is already finalized, a FinalizedStateException is thrown.
* @param state The state to add as accepting state
* @throws FinalizedStateException
*/
@Override @Override
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();
} }
if (state == null) {
throw new IllegalArgumentException();
}
this.states.add(state); this.states.add(state);
this.acceptingStates.add(state); this.acceptingStates.add(state);
} }
/**
* Adds all accepting states to the automaton. If the automaton is already finalized, a FinalizedStateException is thrown.
* @param states The Set of states to add as accepting states
* @throws FinalizedStateException
*/
public void addAllAcceptingStates(Set<String> states) throws FinalizedStateException { public void addAllAcceptingStates(Set<String> states) throws FinalizedStateException {
if (this.isFinalized) { if (this.isFinalized) {
throw new FinalizedStateException(); throw new FinalizedStateException();
} }
if (states == null) {
throw new IllegalArgumentException();
}
for (String state : states) { for (String state : states) {
this.addAcceptingState(state); this.addAcceptingState(state);
} }
} }
/**
* Adds a new state to the automaton. If the automaton is already finalized, a FinalizedStateException is thrown.
* If the state already exists, the state is renamed to state1, state11, state111, etc. until a unique name is found.
* @param states The Set of states to add
* @param toCheck The Set of states to check for duplicates
* @param nfa The NFA to add the states to
*/
public void safeAddAcceptingStates(Set<String> states, Set<String> toCheck, NFAImpl nfa) { public void safeAddAcceptingStates(Set<String> states, Set<String> toCheck, NFAImpl nfa) {
if (this.isFinalized) {
throw new FinalizedStateException();
}
if(states == null || toCheck == null || nfa == null){
throw new IllegalArgumentException();
}
for (String state : states) { for (String state : states) {
nfa.addAcceptingState(changeIfNecessary(state, toCheck)); nfa.addAcceptingState(changeIfNecessary(state, toCheck));
} }
} }
// #TODO /**
// write more testcases * Creates a new NFA that is the union of this NFA and the other NFA. If either of the NFAs is not finalized, a FinalizedStateException is thrown.
* @param other The other NFA to union with
* @return The union of this NFA and the other NFA
* @throws FinalizedStateException
*/
@Override @Override
public NFA union(NFA other) throws FinalizedStateException { public NFA union(NFA other) throws FinalizedStateException {
if (!this.isFinalized || !other.isFinalized()) { if (!this.isFinalized || !other.isFinalized()) {
throw new FinalizedStateException(); 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?
NFAImpl unionNFA = new NFAImpl(this.getInitialState()); NFAImpl unionNFA = new NFAImpl(this.getInitialState());
unionNFA.states.addAll(this.states); unionNFA.states.addAll(this.states);
unionNFA.addAllAcceptingStates(this.acceptingStates); unionNFA.addAllAcceptingStates(this.acceptingStates);
Set<String> otherAcceptingStates = new HashSet<>(other.getAcceptingStates()); Set<String> otherAcceptingStates = new HashSet<>(other.getAcceptingStates());
unionNFA.safeAddAcceptingStates(this.acceptingStates, otherAcceptingStates, unionNFA); unionNFA.safeAddAcceptingStates(this.acceptingStates, otherAcceptingStates, unionNFA);
Set<Transition> newTransitions = new HashSet<>(); Set<Transition> newTransitions = new HashSet<>();
for (Transition transition : other.getTransitions()) { for (Transition transition : other.getTransitions()) {
Transition newTransition = Transition newTransition =
@ -152,14 +188,9 @@ public class NFAImpl implements NFA {
unionNFA.acceptingStates.add(state); unionNFA.acceptingStates.add(state);
} }
} else { } else {
// a state of other has the same name as one in this.
// change name of state and every Transition it is a part of in other
String newState = changeIfNecessary(state, unionNFA.states); String newState = changeIfNecessary(state, unionNFA.states);
// wieder a neues set
Set<Transition> tempTransitions = new HashSet<>(); Set<Transition> tempTransitions = new HashSet<>();
// check each transition of other if start or to state was state
for (Transition transition : newTransitions) { for (Transition transition : newTransitions) {
if (transition.fromState().equals(state)) { if (transition.fromState().equals(state)) {
transition = new Transition(newState, transition.readSymbol(), transition.toState()); transition = new Transition(newState, transition.readSymbol(), transition.toState());
@ -169,16 +200,15 @@ public class NFAImpl implements NFA {
} }
tempTransitions.add(transition); tempTransitions.add(transition);
} }
for (String accState : other.getAcceptingStates()) { for (String accState : other.getAcceptingStates()) {
if (accState.equals(state)) { if (accState.equals(state)) {
unionNFA.acceptingStates.add(newState); unionNFA.acceptingStates.add(newState);
} }
} }
newTransitions = tempTransitions; // Replace the original set with the updated set newTransitions = tempTransitions;
} }
} }
// add, alphabet
Iterator<Transition> iterator = newTransitions.iterator(); Iterator<Transition> iterator = newTransitions.iterator();
while (iterator.hasNext()){ while (iterator.hasNext()){
Transition transition = iterator.next(); Transition transition = iterator.next();
@ -191,17 +221,18 @@ public class NFAImpl implements NFA {
} }
unionNFA.addAllTransitions(newTransitions); unionNFA.addAllTransitions(newTransitions);
unionNFA.addAllTransitions(this.transitions); unionNFA.addAllTransitions(this.transitions);
unionNFA.finalizeAutomaton(); unionNFA.finalizeAutomaton();
return unionNFA; return unionNFA;
} }
// #TODO /**
// needs to work with a DFA not NFA * Creates a new NFA that is the intersection of this NFA and the other NFA. If either of the NFAs is not finalized, a FinalizedStateException is thrown.
* @param other The other NFA to intersect with
* @return The intersection of this NFA and the other NFA
* @throws FinalizedStateException
*/
@Override @Override
public NFA intersection(NFA other) throws FinalizedStateException { public NFA intersection(NFA other) throws FinalizedStateException {
if (!this.isFinalized || !other.isFinalized()) { if (!this.isFinalized || !other.isFinalized()) {
@ -213,6 +244,12 @@ public class NFAImpl implements NFA {
return unionNFA.complement(); return unionNFA.complement();
} }
/**
* Creates a new NFA that is the concatenation of this NFA and the other NFA. If either of the NFAs is not finalized, a FinalizedStateException is thrown.
* @param other The other NFA to concatenate with
* @return The concatenation of this NFA and the other NFA
* @throws FinalizedStateException
*/
@Override @Override
public NFA concatenation(NFA other) throws FinalizedStateException { public NFA concatenation(NFA other) throws FinalizedStateException {
if (!this.isFinalized || !other.isFinalized()) { if (!this.isFinalized || !other.isFinalized()) {
@ -221,10 +258,8 @@ public class NFAImpl implements NFA {
NFAImpl concatenationNFA = new NFAImpl(this.initialState); NFAImpl concatenationNFA = new NFAImpl(this.initialState);
// Add states from 'this'
concatenationNFA.states.addAll(this.states); concatenationNFA.states.addAll(this.states);
// Add states from 'other', with renaming if necessary
Map<String, String> renamedStates = new HashMap<>(); Map<String, String> renamedStates = new HashMap<>();
for (String state : other.getStates()) { for (String state : other.getStates()) {
String newState = changeIfNecessary(state, concatenationNFA.states); String newState = changeIfNecessary(state, concatenationNFA.states);
@ -232,10 +267,8 @@ public class NFAImpl implements NFA {
concatenationNFA.states.add(newState); concatenationNFA.states.add(newState);
} }
// Add transitions from 'this'
concatenationNFA.transitions.addAll(this.transitions); concatenationNFA.transitions.addAll(this.transitions);
// Add transitions from 'other', updating state names
for (Transition transition : other.getTransitions()) { for (Transition transition : other.getTransitions()) {
String newFromState = renamedStates.getOrDefault(transition.fromState(), transition.fromState()); String newFromState = renamedStates.getOrDefault(transition.fromState(), transition.fromState());
String newToState = renamedStates.getOrDefault(transition.toState(), transition.toState()); String newToState = renamedStates.getOrDefault(transition.toState(), transition.toState());
@ -243,14 +276,12 @@ public class NFAImpl implements NFA {
concatenationNFA.transitions.add(newTransition); concatenationNFA.transitions.add(newTransition);
} }
// Connect accepting states of 'this' to the initial state of 'other' with epsilon transitions
String newOtherInitialState = renamedStates.getOrDefault(other.getInitialState(), other.getInitialState()); String newOtherInitialState = renamedStates.getOrDefault(other.getInitialState(), other.getInitialState());
for (String acceptingState : this.acceptingStates) { for (String acceptingState : this.acceptingStates) {
Transition epsilon = new Transition(acceptingState, null, newOtherInitialState); Transition epsilon = new Transition(acceptingState, null, newOtherInitialState);
concatenationNFA.transitions.add(epsilon); concatenationNFA.transitions.add(epsilon);
} }
// Set accepting states of the concatenated NFA to be the accepting states of 'other'
concatenationNFA.acceptingStates.clear(); concatenationNFA.acceptingStates.clear();
for (String acceptingState : other.getAcceptingStates()) { for (String acceptingState : other.getAcceptingStates()) {
concatenationNFA.acceptingStates.add(renamedStates.getOrDefault(acceptingState, acceptingState)); concatenationNFA.acceptingStates.add(renamedStates.getOrDefault(acceptingState, acceptingState));
@ -261,7 +292,11 @@ public class NFAImpl implements NFA {
return concatenationNFA; return concatenationNFA;
} }
/**
* Creates a new NFA that is the Kleene star of this NFA. If the NFA is not finalized, a FinalizedStateException is thrown.
* @return The Kleene star of this NFA
* @throws FinalizedStateException
*/
@Override @Override
public NFA kleeneStar() throws FinalizedStateException { public NFA kleeneStar() throws FinalizedStateException {
if (!this.isFinalized) { if (!this.isFinalized) {
@ -270,17 +305,13 @@ public class NFAImpl implements NFA {
NFAImpl nfa = new NFAImpl(this.initialState); NFAImpl nfa = new NFAImpl(this.initialState);
// copy, but without accepting states
nfa.states.addAll(this.states); nfa.states.addAll(this.states);
nfa.addAllTransitions(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); nfa.acceptingStates.add(this.initialState);
// for each accepting state
for (String acceptingState : this.getAcceptingStates()) { for (String acceptingState : this.getAcceptingStates()) {
Transition loopBackTransition = Transition loopBackTransition =
// creating an epsilon transition (null) for each accepting state
new Transition(acceptingState, null, this.initialState); new Transition(acceptingState, null, this.initialState);
nfa.transitions.add(loopBackTransition); nfa.transitions.add(loopBackTransition);
} }
@ -290,6 +321,11 @@ public class NFAImpl implements NFA {
return nfa; return nfa;
} }
/**
* Creates a new NFA that is the plus operator of this NFA. If the NFA is not finalized, a FinalizedStateException is thrown.
* @return The plus operator of this NFA
* @throws FinalizedStateException
*/
@Override @Override
public NFA plusOperator() throws FinalizedStateException { public NFA plusOperator() throws FinalizedStateException {
if (!this.isFinalized) { if (!this.isFinalized) {
@ -299,55 +335,41 @@ public class NFAImpl implements NFA {
String newInitialState = "newSTART"; String newInitialState = "newSTART";
NFAImpl nfa = new NFAImpl(newInitialState); NFAImpl nfa = new NFAImpl(newInitialState);
// 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);
nfa.transitions.add(new Transition(newInitialState, null, this.initialState)); nfa.transitions.add(new Transition(newInitialState, null, this.initialState));
for (String acceptingState : this.acceptingStates) { for (String acceptingState : this.acceptingStates) {
nfa.transitions.add(new Transition(acceptingState, null, newInitialState)); nfa.transitions.add(new Transition(acceptingState, null, newInitialState));
} }
/*
// for each accepting state
for (String acceptingState : this.acceptingStates) {
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.finalizeAutomaton(); nfa.finalizeAutomaton();
return nfa; return nfa;
} }
// #TODO /**
* Creates a new NFA that is the complement of this NFA. If the NFA is not finalized, a FinalizedStateException is thrown.
* @return The complement of this NFA
* @throws FinalizedStateException
*/
@Override @Override
public NFA complement() throws FinalizedStateException { public NFA complement() throws FinalizedStateException {
if (!this.isFinalized) { if (!this.isFinalized) {
throw new FinalizedStateException(); throw new FinalizedStateException();
} }
// create a copy of this NFA
NFAImpl nfa = new NFAImpl(this.initialState); NFAImpl nfa = new NFAImpl(this.initialState);
nfa.states.addAll(this.states); nfa.states.addAll(this.states);
nfa.addAllTransitions(this.transitions); nfa.addAllTransitions(this.transitions);
nfa.addAllAcceptingStates(this.acceptingStates); nfa.addAllAcceptingStates(this.acceptingStates);
nfaAddTrap(nfa); //error on last occasion nfaAddTrap(nfa);
NFAImpl fakeNFA = convertNFAtoDFA(nfa); NFAImpl fakeNFA = convertNFAtoDFA(nfa);
// state -> accepting
// accepting -> state
Set<String> newAcceptingState = new HashSet<>(); Set<String> newAcceptingState = new HashSet<>();
for (String state : fakeNFA.getStates()) { for (String state : fakeNFA.getStates()) {
@ -355,38 +377,42 @@ public class NFAImpl implements NFA {
newAcceptingState.add(state); newAcceptingState.add(state);
} }
} }
fakeNFA.acceptingStates.clear(); fakeNFA.acceptingStates.clear();
fakeNFA.addAllAcceptingStates(newAcceptingState); fakeNFA.addAllAcceptingStates(newAcceptingState);
fakeNFA.finalizeAutomaton(); fakeNFA.finalizeAutomaton();
return fakeNFA; return fakeNFA;
} }
/**
* Returns whether the automaton is finalized.
* @return Whether the automaton is finalized
*/
@Override @Override
public boolean isFinalized() { public boolean isFinalized() {
return isFinalized; return isFinalized;
} }
/**
* Finalizes the automaton. After finalization, no further states or transitions can be added.
*/
@Override @Override
public void finalizeAutomaton() { public void finalizeAutomaton() {
this.isFinalized = true; this.isFinalized = true;
} }
// #TODO /**
* Returns whether the automaton is finite. An automaton is finite if it has a finite number of states and no loops, that have a path to an accepting state.
* @return Whether the automaton is finite
*/
@Override @Override
public boolean isFinite() { public boolean isFinite() {
//transitions={fromState==toState}
//states={A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z}
//obergrenze = anzahl der states (26)
//how do we get an infinite language?
//we need to have a loop, how can we check:
List<Tuple> path = new ArrayList<>(); List<Tuple> path = new ArrayList<>();
List<Transition> transitions= new ArrayList<>(this.getTransitions()); List<Transition> transitions= new ArrayList<>(this.getTransitions());
AtomicInteger finished = new AtomicInteger(0); //0=unfinished, 1=finite, 2 = infinite AtomicInteger finished = new AtomicInteger(0);
List<Character> letters = new ArrayList<>(this.alphabet); // letters is alphabet with epsilon (null) List<Character> letters = new ArrayList<>(this.alphabet);
if(this.alphabet.isEmpty()){ if(this.alphabet.isEmpty()){
return true; return true;
} }
@ -394,7 +420,6 @@ public class NFAImpl implements NFA {
letters.add(null); letters.add(null);
} }
// EndTuple hast firstElement = a //doesn´t matter at all aktually
path.add(new Tuple('a', this.initialState)); path.add(new Tuple('a', this.initialState));
if(fillToEnd(path, transitions, letters, finished)){ if(fillToEnd(path, transitions, letters, finished)){
return false; return false;
@ -409,7 +434,6 @@ public class NFAImpl implements NFA {
} }
System.out.println("ERROR-isFinite method isn´t running right!"); System.out.println("ERROR-isFinite method isn´t running right!");
return true; return true;
//return continueOnLastTuple(path, transitions, letters);
} }
private void continueOnLastTuple(List<Tuple> path, List<Transition> transitions, List<Character> letters, AtomicInteger finished) { private void continueOnLastTuple(List<Tuple> path, List<Transition> transitions, List<Character> letters, AtomicInteger finished) {

View file

@ -18,8 +18,7 @@ public class betterSimpleTests {
@Test @Test
public void invalidAcceptingState() { public void invalidAcceptingState() {
var instance = factory.buildNFA("START"); var instance = factory.buildNFA("START");
instance.addAcceptingState(null); assertThrows(IllegalArgumentException.class, () -> instance.addAcceptingState(null));
assertEquals(0, instance.getAcceptingStates().size());
} }
@Test @Test

Binary file not shown.