This commit is contained in:
Yorick Rommers
2017-05-24 14:08:42 +02:00
3 changed files with 86 additions and 5 deletions
+20
View File
@@ -65,4 +65,24 @@ public class DFA<T extends Comparable> extends Automata<T> {
}
return new Taal(addSymbols(n - 1, taal));
}
public DFA<T> Reverse()
{
DFA<T> reversed = new DFA<T>(this.symbols);
for(Transition<T> t : transistions)
{
reversed.addTransition(new Transition<T>(t.naarState, t.symbol, t.vanState));
}
for(T state : eindStates) {
reversed.defineAsEndState(state);
}
for(T state : beginStates) {
reversed.defineAsStartState(state);
}
return reversed;
}
}
+33
View File
@@ -0,0 +1,33 @@
package com.imegumii;
/**
* Created by kenny on 18-5-2017.
*/
public class Graph {
public static String generateGraphString(Automata<String> a)
{
String text = "digraph finite_state_machine {\nrankdir=LR;\nsize=\"8,5\";\nnode [shape = doublecircle];";
for(String s : a.eindStates){
text += s + " ";
}
text += ";\nnode [shape = circle];\n";
for(Transition<String> t : a.transistions)
{
String s = "";
if(t.symbol == Transition.EPSILON)
s = "$";
else
s = t.symbol + "";
text += t.vanState + " -> " + t.naarState + " [ label = \"" + s + "\"];\n";
}
text += "}";
return text;
}
}
+33 -5
View File
@@ -41,6 +41,7 @@ public class Main {
myAutomata.defineAsEndState("q7");
myAutomata.printTransitions();
final long[] startTime = {System.currentTimeMillis()};
myAutomata.geefTaalTotLengte(10).getSymbols().forEach((s) -> {
long before = System.currentTimeMillis();
@@ -105,13 +106,38 @@ public class Main {
RegExp expr3 = new RegExp("c");
RegExp expr4 = new RegExp("d");
RegExp expr5 = expr1.ster();
RegExp expr6 = expr3.plus();
RegExp expr5 = expr1.of(expr2);
RegExp expr6 = expr3.of(expr4);
RegExp expr7 = expr5.punt(expr6);
NDFA<String> test = ThompsonConverter.Convert(expr7);
test.printTransitions();
System.out.println(Graph.generateGraphString(test));
}
public static void ReverseAutomata()
{
Character [] characters = {'a', 'b'};
DFA<String> myAutomata = new DFA<String>(characters);
// AAB
myAutomata.addTransition(new Transition<String>("q0", 'a', "q1"));
//myAutomata.addTransition(new Transition<String>("q0", 'b', "q1"));
//myAutomata.addTransition(new Transition<String>("q1", 'a', "q0"));
myAutomata.addTransition(new Transition<String>("q1", 'b', "q0"));
myAutomata.defineAsEndState("q1");
myAutomata.defineAsStartState("q0");
System.out.println(Graph.generateGraphString(myAutomata));
System.out.println("\nREVERSE:");
DFA<String> reverseAutomata = myAutomata.Reverse();
System.out.println(Graph.generateGraphString(reverseAutomata));
}
public static void Practicum4() {
@@ -233,12 +259,14 @@ public class Main {
// testreg.testLanguage();
System.out.println("Zo moet het dus, nu onze beurt.");
// System.out.println("Zo moet het dus, nu onze beurt.");
// Practicum1();
// Practicum2();
Practicum4();
// Practicum4();
ReverseAutomata();
}
}