guess what works now, fker:)

This commit is contained in:
Ghost_Element 2024-01-10 15:27:21 +01:00
parent 2e466eea83
commit b6ab5bdf72
3 changed files with 141 additions and 68 deletions

View file

@ -5,6 +5,7 @@ import ab1.NFA;
import ab1.Transition;
import java.util.*;
import java.util.concurrent.atomic.AtomicInteger;
public class NFAImpl implements NFA {
private final Set<String> states;
@ -385,49 +386,61 @@ public class NFAImpl implements NFA {
List<Tuple> path = new ArrayList<>();
List<Transition> transitions= new ArrayList<>(this.getTransitions());
//int finished = 0; //0=unfinished, 1=finite, 2 = infinite
AtomicInteger finished = new AtomicInteger(0); //0=unfinished, 1=finite, 2 = infinite
List<Character> letters = new ArrayList<>(this.alphabet); // letters is alphabet with epsilon (null)
if(!letters.contains(null)) {
letters.add(null);
}
if(this.alphabet.isEmpty()){
return true;
}
// EndTuple hast firstElement = a
path.add(new Tuple('a', this.initialState, true));
if(fillToEnd(path, transitions, letters)){
return true;
if(!letters.contains(null)) {
letters.add(null);
}
return continueOnLastTuple(path, transitions, letters);
// EndTuple hast firstElement = a //doesn´t matter at all aktually
path.add(new Tuple('a', this.initialState));
if(fillToEnd(path, transitions, letters, finished)){
return false;
}
while(finished.intValue()==0){
continueOnLastTuple(path, transitions, letters, finished);
}
if (finished.intValue() == 1) {
return true;
} else if (finished.intValue() == 2) {
return false;
}
System.out.println("ERROR-isFinite method isn´t running right!");
return true;
//return continueOnLastTuple(path, transitions, letters);
}
private boolean continueOnLastTuple(List<Tuple> path, List<Transition> transitions, List<Character> letters) {
private void continueOnLastTuple(List<Tuple> path, List<Transition> transitions, List<Character> letters, AtomicInteger finished) {
// path is already filled to end -> check next available state for the transition.
// case we reached the limit (last state)
if(path.size()==1){
return false;
finished.set(1);
return;
}
String fromState = path.get(path.size()-2).getElement_2();
Character readSymbol = path.get(path.size()-1).getElement_1();
Transition currentTransition = new Transition(fromState, readSymbol, path.get(path.size()-1).getElement_2());
for(int i=transitions.indexOf(currentTransition);i<transitions.size();i++){
for(int i=transitions.indexOf(currentTransition)+1;i<transitions.size();i++){
if(transitions.get(i).fromState().equals(fromState) && transitions.get(i).readSymbol().equals(readSymbol)){
String newState = transitions.get(i).toState();
if (stateInPath(path, newState)) {
//loop!
//loop ends in acceptingstate?
if(stateReachesAcceptingstates(newState)) {
//true:
finished.set(2);
return;
}
} else {
path.remove(path.size()-1);
path. add(new Tuple(readSymbol, newState));
if(fillToEnd(path, transitions, letters)){
return true;
if(fillToEnd(path, transitions, letters, finished)){
return;
}
return continueOnLastTuple(path, transitions, letters);
return;// continueOnLastTuple(path, transitions, letters);
}
}
}
@ -435,18 +448,28 @@ public class NFAImpl implements NFA {
for(int i=letters.indexOf(readSymbol)+1;i<letters.size();i++){
Character newLetter = letters.get(i);
String newState = checkForNewLetter(path, transitions, letters, newLetter);
//more than 1 transition for new letter?
if(newState!=null){
//check given state
if(stateInPath(path, newState)){
//loop!
//loop ends in acceptingstate?
if(stateReachesAcceptingstates(newState)) {
//true:
finished.set(2);
return;
}
//else:
// save !duplicate! and return:
path.add(new Tuple(newLetter, newState));
return;
}else {
//deletion already in checkForNewLetter.
path.add(new Tuple(newLetter, newState));
if(fillToEnd(path, transitions, letters)){
return true;
if(fillToEnd(path, transitions, letters, finished)){
return;
}
return continueOnLastTuple(path, transitions, letters);
return;// continueOnLastTuple(path, transitions, letters);
}
}
//else go to next letter
@ -455,7 +478,7 @@ public class NFAImpl implements NFA {
//remove last Tuple
path.remove(path.size()-1);
//goto next thingy
return continueOnLastTuple(path, transitions, letters);
//return;// continueOnLastTuple(path, transitions, letters);
}
/**
@ -481,7 +504,7 @@ public class NFAImpl implements NFA {
* after method need to
* @return true if a valid loop has been found while filling it up
*/
private boolean fillToEnd(List<Tuple> path, List<Transition> transitions, List<Character> letters){
private boolean fillToEnd(List<Tuple> path, List<Transition> transitions, List<Character> letters, AtomicInteger finished){
String fromState = path.get(path.size()-1).getElement_2();
for(Character letter : letters) {
for (Transition transition : transitions) {
@ -490,9 +513,15 @@ public class NFAImpl implements NFA {
if(stateInPath(path, transition.toState())) {
//loop!
//loop reaches a acceptingstate? then return true, else delete some shit
if(stateReachesAcceptingstates(transition.toState())) {
//true:
finished.set(2);
return true;
}
//else do nothing and take next transition if possible
} else {
path.add(new Tuple(letter, transition.toState()));
return fillToEnd(path, transitions, letters);
return fillToEnd(path, transitions, letters, finished);
}
}
}
@ -510,6 +539,35 @@ public class NFAImpl implements NFA {
}
return false;
}
private boolean stateReachesAcceptingstates(String state){
if(this.acceptingStates.contains(state)){
return true;
}
Set<String> currentStates = new HashSet<>();
Set<String> newStates = new HashSet<>();
currentStates.add(state);
boolean containsNewStates;
do{
containsNewStates = false;
for(String currentstate : currentStates){
for(Transition transition : this.transitions){
if(transition.fromState().equals(currentstate)){
newStates.add(transition.toState());
}
}
}
for(String newState : newStates){
if(this.acceptingStates.contains(newState)){
return true;
}
if(!currentStates.contains(newState)){
containsNewStates=true;
currentStates.add(newState);
}
}
}while(containsNewStates);
return false;
}
// #TODO
@Override

View file

@ -21,20 +21,10 @@ public class Tuple {
private String element_2;
public boolean isEnd() {
return isEnd;
}
private boolean isEnd;
public Tuple(Character firstElement, String secondElement){
this.element_1=firstElement;
this.element_2=secondElement;
this.isEnd = false;
}
public Tuple(Character firstElement, String secondElement, boolean isend){
this.element_1=firstElement;
this.element_2=secondElement;
this.isEnd = isend;
}
}