hope this works...

This commit is contained in:
Tocuro 2024-01-08 15:13:00 +01:00
parent ac579644f5
commit 8a6ea12b68
8 changed files with 262 additions and 77 deletions

View file

@ -7,6 +7,7 @@ import ab1.Transition;
import lombok.Getter;
import java.util.*;
import java.util.stream.Collectors;
public class NFAImpl implements NFA {
private Set<String> states;
@ -35,6 +36,7 @@ public class NFAImpl implements NFA {
public Set<String> getStates() {
return this.states;
}
@Override
public Collection<Transition> getTransitions() {
return this.transitions;
@ -54,10 +56,24 @@ public class NFAImpl implements NFA {
public void addTransition(Transition transition) throws FinalizedStateException {
if (this.isFinalized) {
throw new FinalizedStateException();
} else if (!this.transitions.contains(transition)) {
// add symbol to alphabet. If it is already in the alphabet, nothing happens
this.alphabet.add(transition.readSymbol());
this.transitions.add(transition);
}
if (!states.contains(transition.fromState())) {
this.states.add(transition.fromState());
}
if (!states.contains(transition.toState())) {
this.states.add(transition.toState());
}
// add symbol to alphabet. If it is already in the alphabet, nothing happens
this.alphabet.add(transition.readSymbol());
this.transitions.add(transition);
}
public void addAllTransition(Set<Transition> transitions) throws FinalizedStateException {
if(this.isFinalized){
throw new FinalizedStateException();
}
for (Transition transition : transitions) {
this.addTransition(transition);
}
}
@ -65,16 +81,24 @@ public class NFAImpl implements NFA {
public void addAcceptingState(String state) throws FinalizedStateException {
if (this.isFinalized) {
throw new FinalizedStateException();
} else if (!this.states.contains(state)) {
this.states.add(state);
this.acceptingStates.add(state);
}
this.states.add(state);
this.acceptingStates.add(state);
}
public void addAllAcceptingStates(Set<String> states) throws FinalizedStateException {
if(this.isFinalized){
throw new FinalizedStateException();
}
for (String state : states) {
this.addAcceptingState(state);
}
}
// #TODO
@Override
public NFA union(NFA other) throws FinalizedStateException {
if(!this.isFinalized || !other.isFinalized()){
if (!this.isFinalized || !other.isFinalized()) {
throw new FinalizedStateException();
}
NFAImpl unionNFA = new NFAImpl(this.initialState);
@ -92,7 +116,7 @@ public class NFAImpl implements NFA {
// #TODO
@Override
public NFA intersection(NFA other) throws FinalizedStateException {
if(!this.isFinalized || !other.isFinalized()){
if (!this.isFinalized || !other.isFinalized()) {
throw new FinalizedStateException();
}
NFAImpl intersectionNFA = new NFAImpl(this.initialState);
@ -109,7 +133,7 @@ public class NFAImpl implements NFA {
// #TODO
@Override
public NFA concatenation(NFA other) throws FinalizedStateException {
if(!this.isFinalized || !other.isFinalized()){
if (!this.isFinalized || !other.isFinalized()) {
throw new FinalizedStateException();
}
NFAImpl concatenationNFA = new NFAImpl(this.initialState);
@ -120,7 +144,7 @@ public class NFAImpl implements NFA {
concatenationNFA.transitions.addAll(this.transitions);
concatenationNFA.transitions.addAll(other.getTransitions());
for(String accceptingState : this.acceptingStates){
for (String accceptingState : this.acceptingStates) {
Transition epsilon = new Transition(accceptingState, null, other.getInitialState());
concatenationNFA.transitions.add(epsilon);
}
@ -185,7 +209,7 @@ public class NFAImpl implements NFA {
// #TODO
@Override
public NFA complement() throws FinalizedStateException {
if(!this.isFinalized){
if (!this.isFinalized) {
throw new FinalizedStateException();
}
@ -228,7 +252,7 @@ public class NFAImpl implements NFA {
return false;
}
public NFA convertNFAtoDFA (NFAImpl input){
public NFA convertNFAtoDFA(NFAImpl input) {
/*
What do we need to do?
@ -240,6 +264,7 @@ public class NFAImpl implements NFA {
*/
Set<String> dfaAcceptingStates = new HashSet<>();
// all states of the DFA
List<Set<String>> dfaStates = new ArrayList<>();
// all transitions of the DFA
@ -252,31 +277,15 @@ public class NFAImpl implements NFA {
// getting the epsilon closure of the start state
startState = epsilonClosure(startState, input);
dfaStates.add(startState);
// find all letters in transitions
// obsolete, added alphabet to NFA which stores all read symbols
/*
Set<Character> possibleLetters = new HashSet<>();
Set<Transition> possibleTransitions = new HashSet<>();
for(Transition transition : input.transitions){
for(String state : states) {
if (state.equals(transition.fromState())){
if(transition.readSymbol() != null){
possibleLetters.add(transition.readSymbol());
possibleTransitions.add(transition);
}
break;
}
}
if (isAcceptingState(startState, input)){
dfaAcceptingStates.add(getIndexOfSet(startState, dfaStates));
}
*/
// do the above with each DFA state
Queue<Set<String>> queue = new LinkedList<>();
queue.add(startState);
// iterate each Letter
while (!queue.isEmpty()){
while (!queue.isEmpty()) {
Set<String> currentState = queue.poll();
// for each possible Letter
for (char letter : input.getAlphabet()) {
@ -285,36 +294,31 @@ public class NFAImpl implements NFA {
if (!dfaStates.contains(newState)) {
queue.add(newState);
dfaStates.add(newState);
// build new Transition
if (isAcceptingState(newState, input)) {
dfaAcceptingStates.add(getIndexOfSet(newState, dfaStates));
}
}
// build new Transition
dfaTransitions.add(new Transition(Integer.toString(dfaStates.indexOf(currentState)), letter, Integer.toString(dfaStates.indexOf(newState))));
// we gotta change this to only a string is given
// we got to change this to only a string is given
//dfaTransitions.add(new Transition(currentState, letter, newState));
}
}
/*
for(Character letter : possibleLetters){
Set<String> newState = new HashSet<>();
for(Transition trans : possibleTransitions){
if(trans.readSymbol().equals(letter)){
// is every transition fromState = state from states? Hoffentlich ja :')
newState.add(trans.toState());
}
}
//deal with newState + new Transitions
if(DFA_states.contains(newState)){
Transition newtrans = new Transition(DFA_states.get(index), letter, DFA_states.get(DFA_states.indexOf(newState)));
}
NFAImpl newDFA = new NFAImpl("0");
for(int i=1;i<dfaStates.size();i++){
newDFA.states.add(""+i);
}
*/
newDFA.addAllTransition(dfaTransitions);
newDFA.addAllAcceptingStates(dfaAcceptingStates);
// what we gonna do now?
return null;
// return new DFA(dfaStates, dfaTransitions, startState, determineAcceptingStates(dfaStates, input));
newDFA.finalizeAutomaton();
return newDFA;
}
private Set<String> epsilonClosure(Set<String> states, NFA nfa) {
@ -375,31 +379,13 @@ public class NFAImpl implements NFA {
return result;
}
private Set<Set<String>> determineAcceptingStates(Set<Set<String>> dfaStates, NFA nfa) {
Set<Set<String>> acceptingStates = new HashSet<>();
for (Set<String> dfaState : dfaStates) {
for (String nfaState : dfaState) {
if (nfa.getAcceptingStates().contains(nfaState)) {
acceptingStates.add(dfaState);
break;
}
private boolean isAcceptingState(Set<String> states, NFA input) {
for (String accState : input.getAcceptingStates()) {
if (states.contains(accState)) {
return true;
}
}
return acceptingStates;
return false;
}
public void test(){
final NFAFactory factory = new NFAFactoryImpl();
var instance = factory.buildNFA("START");
instance.addTransition(
Transition.builder()
.fromState("START")
.readSymbol('a')
.toState("ACCEPT")
.build()
);
instance.addAcceptingState("ACCEPT");
instance.finalizeAutomaton();
}
}