I tried
This commit is contained in:
@@ -10,11 +10,11 @@ import java.util.TreeSet;
|
||||
*/
|
||||
public class Automata <T extends Comparable> {
|
||||
|
||||
private Set<Transition<T>> transistions;
|
||||
protected Set<Transition<T>> transistions;
|
||||
|
||||
private SortedSet<T> states;
|
||||
private SortedSet<T> beginStates;
|
||||
private SortedSet<T> eindStates;
|
||||
protected SortedSet<T> states;
|
||||
protected SortedSet<T> beginStates;
|
||||
protected SortedSet<T> eindStates;
|
||||
|
||||
private SortedSet<Character> symbols;
|
||||
|
||||
@@ -50,21 +50,7 @@ public class Automata <T extends Comparable> {
|
||||
|
||||
public void printTransitions () {
|
||||
transistions.forEach(System.out::println);
|
||||
|
||||
}
|
||||
|
||||
public boolean accept(String stringToSearch) {
|
||||
T currentState;
|
||||
states.forEach((s) -> {
|
||||
System.out.println("State is " + s);
|
||||
});
|
||||
beginStates.forEach(System.out::println);
|
||||
eindStates.forEach(System.out::println);
|
||||
|
||||
for (T beginState : beginStates) {
|
||||
currentState = beginState;
|
||||
// process currentState.
|
||||
System.out.println(beginState);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,46 @@
|
||||
package com.imegumii;
|
||||
|
||||
import java.util.Iterator;
|
||||
import java.util.SortedSet;
|
||||
|
||||
/**
|
||||
* Created by imegumii on 01/05/2017.
|
||||
*/
|
||||
public class DFA<T extends Comparable> extends Automata<T> {
|
||||
|
||||
public DFA(Character[] symbols) {
|
||||
super(symbols);
|
||||
}
|
||||
|
||||
public DFA(SortedSet symbols) {
|
||||
super(symbols);
|
||||
}
|
||||
|
||||
boolean accepteer(String s) {
|
||||
// because this is a DFA, assume first is always beginstate and endstate.
|
||||
T beginState = this.beginStates.first();
|
||||
T eindState = this.eindStates.first();
|
||||
T currentState = this.beginStates.first();
|
||||
for (int i = 0; i < s.length(); i++) {
|
||||
System.out.println(s.charAt(i));
|
||||
|
||||
for (Transition<T> tr : this.transistions) {
|
||||
System.out.println(tr);
|
||||
System.out.println("Current state is : " +currentState);
|
||||
if (currentState.equals(tr.vanState)) { // this is our current state
|
||||
System.out.println("This is our current state, checking if symbols match and advancing");
|
||||
if (s.charAt(i) == tr.symbol) {
|
||||
System.out.println("Theyre equal, currentState becomes naarState");
|
||||
currentState = tr.naarState;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return (currentState == eindState);
|
||||
}
|
||||
|
||||
void geefTaalTotLengte(int n) {
|
||||
|
||||
}
|
||||
}
|
||||
@@ -1,7 +1,6 @@
|
||||
package com.imegumii;
|
||||
|
||||
import com.imegumii.codevandocent.*;
|
||||
import com.sun.source.util.Trees;
|
||||
|
||||
import java.util.TreeSet;
|
||||
|
||||
@@ -12,22 +11,25 @@ public class Main {
|
||||
stringsToParse.add("abbaa");
|
||||
stringsToParse.add("babaa");
|
||||
stringsToParse.add("abbaaabaab");
|
||||
stringsToParse.add("ababababababaab");
|
||||
|
||||
Character [] characters = {'a', 'b'};
|
||||
Automata<String> myAutomata = new Automata<String>(characters);
|
||||
DFA<String> myAutomata = new DFA<String>(characters);
|
||||
myAutomata.addTransition(new Transition<String>("q0", 'a', "q1"));
|
||||
|
||||
myAutomata.addTransition(new Transition<String>("q1", 'b', "q2"));
|
||||
|
||||
myAutomata.addTransition(new Transition<String>("q2", 'b', "q0"));
|
||||
myAutomata.addTransition(new Transition<String>("q2", 'b', "q3"));
|
||||
|
||||
myAutomata.addTransition(new Transition<String>("q3", "q0"));
|
||||
|
||||
myAutomata.defineAsStartState("q0");
|
||||
myAutomata.defineAsEndState("q2");
|
||||
myAutomata.defineAsEndState("q3");
|
||||
|
||||
myAutomata.printTransitions();
|
||||
|
||||
stringsToParse.forEach((s) -> {
|
||||
boolean result = myAutomata.accept(s);
|
||||
boolean result = myAutomata.accepteer(s);
|
||||
System.out.println("string " + s + " fully parsed, result: " + result);
|
||||
});
|
||||
}
|
||||
|
||||
@@ -0,0 +1,16 @@
|
||||
package com.imegumii;
|
||||
|
||||
import java.util.SortedSet;
|
||||
|
||||
/**
|
||||
* Created by imegumii on 01/05/2017.
|
||||
*/
|
||||
public class NDFA<T extends Comparable> extends Automata<T> {
|
||||
public NDFA(Character[] symbols) {
|
||||
super(symbols);
|
||||
}
|
||||
|
||||
public NDFA(SortedSet<Character> symbols) {
|
||||
super(symbols);
|
||||
}
|
||||
}
|
||||
@@ -43,5 +43,6 @@ public class Transition <T extends Comparable> implements Comparable<Transition<
|
||||
@Override
|
||||
public String toString() {
|
||||
return "(" + this.vanState + ", " + this.symbol + ")-->" + this.naarState;
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user