Removed characters, Hopcroft now supports all characters
This commit is contained in:
@@ -18,11 +18,7 @@ public class Automata <T extends Comparable> extends Importable {
|
|||||||
|
|
||||||
protected SortedSet<Character> symbols;
|
protected SortedSet<Character> symbols;
|
||||||
|
|
||||||
public Automata(Character [] symbols) {
|
public Automata() {
|
||||||
this(new TreeSet<Character>(Arrays.asList(symbols)));
|
|
||||||
}
|
|
||||||
|
|
||||||
public Automata(SortedSet<Character> symbols) {
|
|
||||||
super(Type.ERROR);
|
super(Type.ERROR);
|
||||||
states = new TreeSet<T>();
|
states = new TreeSet<T>();
|
||||||
beginStates = new TreeSet<T>();
|
beginStates = new TreeSet<T>();
|
||||||
@@ -30,10 +26,13 @@ public class Automata <T extends Comparable> extends Importable {
|
|||||||
|
|
||||||
transistions = new TreeSet<>();
|
transistions = new TreeSet<>();
|
||||||
|
|
||||||
this.symbols = symbols;
|
this.symbols = new TreeSet<>();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void addTransition (Transition<T> t) {
|
public void addTransition (Transition<T> t) {
|
||||||
|
if(t.symbol != Transition.EPSILON)
|
||||||
|
symbols.add(t.symbol);
|
||||||
|
|
||||||
transistions.add(t);
|
transistions.add(t);
|
||||||
states.add(t.vanState);
|
states.add(t.vanState);
|
||||||
states.add(t.naarState);
|
states.add(t.naarState);
|
||||||
|
|||||||
@@ -10,13 +10,8 @@ import javafx.util.Pair;
|
|||||||
*/
|
*/
|
||||||
public class DFA<T extends Comparable> extends Automata<T> {
|
public class DFA<T extends Comparable> extends Automata<T> {
|
||||||
|
|
||||||
public DFA(Character[] symbols) {
|
public DFA() {
|
||||||
super(symbols);
|
super();
|
||||||
this.type = Type.DFA;
|
|
||||||
}
|
|
||||||
|
|
||||||
public DFA(SortedSet<Character> symbols) {
|
|
||||||
super(symbols);
|
|
||||||
this.type = Type.DFA;
|
this.type = Type.DFA;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -73,7 +68,7 @@ public class DFA<T extends Comparable> extends Automata<T> {
|
|||||||
|
|
||||||
public NDFA<String> reverse()
|
public NDFA<String> reverse()
|
||||||
{
|
{
|
||||||
NDFA<String> reversed = new NDFA<String>(this.symbols);
|
NDFA<String> reversed = new NDFA<String>();
|
||||||
|
|
||||||
for(Transition<T> t : transistions)
|
for(Transition<T> t : transistions)
|
||||||
{
|
{
|
||||||
@@ -101,7 +96,7 @@ public class DFA<T extends Comparable> extends Automata<T> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public DFA<T> ontkenning() {
|
public DFA<T> ontkenning() {
|
||||||
DFA<T> returnDFA = new DFA<T>(this.symbols);
|
DFA<T> returnDFA = new DFA<T>();
|
||||||
|
|
||||||
SortedSet<T> newEndStates = new TreeSet<>();
|
SortedSet<T> newEndStates = new TreeSet<>();
|
||||||
|
|
||||||
@@ -138,7 +133,7 @@ public class DFA<T extends Comparable> extends Automata<T> {
|
|||||||
|
|
||||||
private DFA<String> maakTupleDFA(DFA<String> dfa1, DFA<String> dfa2, boolean of)
|
private DFA<String> maakTupleDFA(DFA<String> dfa1, DFA<String> dfa2, boolean of)
|
||||||
{
|
{
|
||||||
DFA<String> merged = new DFA<String>(dfa1.symbols);
|
DFA<String> merged = new DFA<String>();
|
||||||
|
|
||||||
//Create all transitions and states
|
//Create all transitions and states
|
||||||
for(Transition t1 : dfa1.transistions)
|
for(Transition t1 : dfa1.transistions)
|
||||||
|
|||||||
@@ -44,8 +44,7 @@ public class FileParser {
|
|||||||
|
|
||||||
public static DFA<String> readDFA(BufferedReader br) throws IOException {
|
public static DFA<String> readDFA(BufferedReader br) throws IOException {
|
||||||
|
|
||||||
Character[] chars = {'a', 'b'};
|
DFA<String> dfa = new DFA<String>();
|
||||||
DFA<String> dfa = new DFA<String>(chars);
|
|
||||||
|
|
||||||
String line;
|
String line;
|
||||||
while ((line = br.readLine()) != null) {
|
while ((line = br.readLine()) != null) {
|
||||||
@@ -70,8 +69,7 @@ public class FileParser {
|
|||||||
|
|
||||||
public static NDFA<String> readNDFA(BufferedReader br) throws IOException {
|
public static NDFA<String> readNDFA(BufferedReader br) throws IOException {
|
||||||
|
|
||||||
Character[] chars = {'a', 'b'};
|
NDFA<String> ndfa = new NDFA<String>();
|
||||||
NDFA<String> ndfa = new NDFA<String>(chars);
|
|
||||||
String line;
|
String line;
|
||||||
while ((line = br.readLine()) != null) {
|
while ((line = br.readLine()) != null) {
|
||||||
if(line.startsWith("B")) {
|
if(line.startsWith("B")) {
|
||||||
|
|||||||
@@ -96,9 +96,9 @@ public class Graph {
|
|||||||
try {
|
try {
|
||||||
MutableGraph g = Parser.read(Graph.generateImageString(a));
|
MutableGraph g = Parser.read(Graph.generateImageString(a));
|
||||||
if (fileName != null) {
|
if (fileName != null) {
|
||||||
Graphviz.fromGraph(g).width(1080).render(Format.PNG).toFile(new File("images/" + fileName + ".png"));
|
Graphviz.fromGraph(g).width(Math.max(a.states.size() * 150, 750)).render(Format.PNG).toFile(new File("images/" + fileName + ".png"));
|
||||||
} else {
|
} else {
|
||||||
Graphviz.fromGraph(g).width(1080).render(Format.PNG).toFile(new File("images/" + a.hashCode() + ".png"));
|
Graphviz.fromGraph(g).width(Math.max(a.states.size() * 150, 750)).render(Format.PNG).toFile(new File("images/" + a.hashCode() + ".png"));
|
||||||
}
|
}
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
|
|||||||
@@ -1,5 +1,7 @@
|
|||||||
package com.imegumii;
|
package com.imegumii;
|
||||||
|
|
||||||
|
import javafx.geometry.HPos;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.SortedSet;
|
import java.util.SortedSet;
|
||||||
@@ -74,17 +76,13 @@ public class HopcroftConverter {
|
|||||||
sets.add(currentSet);
|
sets.add(currentSet);
|
||||||
}
|
}
|
||||||
|
|
||||||
if(t.symbol == 'a')
|
currentSet.addColum(new HopcroftColumn(t.symbol, (String)t.naarState, "-"));
|
||||||
currentSet.transitionAState = (String)t.naarState;
|
|
||||||
else if(t.symbol == 'b')
|
|
||||||
currentSet.transitionBState = (String)t.naarState;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private DFA<String> HopcrofttoDFA()
|
private DFA<String> HopcrofttoDFA()
|
||||||
{
|
{
|
||||||
Character [] characters = {'a', 'b'};
|
DFA<String> result = new DFA<String>();
|
||||||
DFA<String> result = new DFA<String>(characters);
|
|
||||||
|
|
||||||
ArrayList<String> groups = new ArrayList<String>();
|
ArrayList<String> groups = new ArrayList<String>();
|
||||||
|
|
||||||
@@ -92,8 +90,8 @@ public class HopcroftConverter {
|
|||||||
{
|
{
|
||||||
if(!groups.contains(s.group))
|
if(!groups.contains(s.group))
|
||||||
{
|
{
|
||||||
result.addTransition(new Transition<String>(s.group, 'a', s.transitionAGroup));
|
for(HopcroftColumn c : s.columns)
|
||||||
result.addTransition(new Transition<String>(s.group, 'b', s.transitionBGroup));
|
result.addTransition(new Transition<String>(s.group, c.symbol, c.group));
|
||||||
|
|
||||||
if(s.isEndState)
|
if(s.isEndState)
|
||||||
result.defineAsEndState(s.group);
|
result.defineAsEndState(s.group);
|
||||||
@@ -112,13 +110,13 @@ public class HopcroftConverter {
|
|||||||
{
|
{
|
||||||
for(HopcroftSet s : sets)
|
for(HopcroftSet s : sets)
|
||||||
{
|
{
|
||||||
for(HopcroftSet h : sets)
|
for(HopcroftColumn c : s.columns)
|
||||||
{
|
{
|
||||||
if(s.transitionAState == h.state)
|
for(HopcroftSet h : sets)
|
||||||
s.transitionAGroup = h.group;
|
{
|
||||||
|
if(c.state.equals(h.state))
|
||||||
if(s.transitionBState == h.state)
|
c.group = h.group;
|
||||||
s.transitionBGroup = h.group;
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -129,7 +127,10 @@ public class HopcroftConverter {
|
|||||||
|
|
||||||
for(HopcroftSet s : sets)
|
for(HopcroftSet s : sets)
|
||||||
{
|
{
|
||||||
String key = s.group + s.transitionAGroup + s.transitionBGroup;
|
String key = s.group;
|
||||||
|
|
||||||
|
for(HopcroftColumn c : s.columns)
|
||||||
|
key += c.symbol + c.group;
|
||||||
|
|
||||||
if(!groups.containsKey(key))
|
if(!groups.containsKey(key))
|
||||||
{
|
{
|
||||||
@@ -181,11 +182,7 @@ class HopcroftSet implements Comparable<HopcroftSet>{
|
|||||||
public String group;
|
public String group;
|
||||||
public String state;
|
public String state;
|
||||||
|
|
||||||
public String transitionAState;
|
public ArrayList<HopcroftColumn> columns;
|
||||||
public String transitionAGroup;
|
|
||||||
|
|
||||||
public String transitionBState;
|
|
||||||
public String transitionBGroup;
|
|
||||||
|
|
||||||
public boolean isEndState = false;
|
public boolean isEndState = false;
|
||||||
|
|
||||||
@@ -194,6 +191,18 @@ class HopcroftSet implements Comparable<HopcroftSet>{
|
|||||||
this.group = group;
|
this.group = group;
|
||||||
this.state = state;
|
this.state = state;
|
||||||
this.isEndState = isEndState;
|
this.isEndState = isEndState;
|
||||||
|
|
||||||
|
columns = new ArrayList<>();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void addColum(HopcroftColumn c)
|
||||||
|
{
|
||||||
|
this.columns.add(c);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void clearColumns()
|
||||||
|
{
|
||||||
|
this.columns.clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -204,6 +213,27 @@ class HopcroftSet implements Comparable<HopcroftSet>{
|
|||||||
@Override
|
@Override
|
||||||
public String toString()
|
public String toString()
|
||||||
{
|
{
|
||||||
return group + " " + state + " | " + transitionAState + " " + transitionAGroup + " | " + transitionBState + " " + transitionBGroup + " " + (isEndState?"*":"");
|
String s = group + " " + state + " | ";
|
||||||
|
|
||||||
|
for(HopcroftColumn c : columns)
|
||||||
|
s += "(" + c.symbol + ") " + c.state + " " + c.group + " | ";
|
||||||
|
|
||||||
|
s+= " " + (isEndState?"*":"");
|
||||||
|
|
||||||
|
return s;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class HopcroftColumn {
|
||||||
|
|
||||||
|
public char symbol;
|
||||||
|
public String state;
|
||||||
|
public String group;
|
||||||
|
|
||||||
|
public HopcroftColumn(char s, String state, String group)
|
||||||
|
{
|
||||||
|
this.symbol = s;
|
||||||
|
this.state = state;
|
||||||
|
this.group = group;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -6,13 +6,9 @@ import java.util.*;
|
|||||||
* Created by imegumii on 01/05/2017.
|
* Created by imegumii on 01/05/2017.
|
||||||
*/
|
*/
|
||||||
public class NDFA<T extends Comparable> extends Automata<T> {
|
public class NDFA<T extends Comparable> extends Automata<T> {
|
||||||
public NDFA(Character[] symbols) {
|
|
||||||
super(symbols);
|
|
||||||
this.type = Type.NDFA;
|
|
||||||
}
|
|
||||||
|
|
||||||
public NDFA(SortedSet<Character> symbols) {
|
public NDFA() {
|
||||||
super(symbols);
|
super();
|
||||||
this.type = Type.NDFA;
|
this.type = Type.NDFA;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -42,7 +38,7 @@ public class NDFA<T extends Comparable> extends Automata<T> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public DFA<String> toDFA () {
|
public DFA<String> toDFA () {
|
||||||
DFA<String> eindDFA = new DFA<>(this.symbols);
|
DFA<String> eindDFA = new DFA<>();
|
||||||
|
|
||||||
SortedSet<T> startSet = new TreeSet<T>();
|
SortedSet<T> startSet = new TreeSet<T>();
|
||||||
//er kunnen meer start states zijn,
|
//er kunnen meer start states zijn,
|
||||||
|
|||||||
@@ -67,7 +67,7 @@ public class Test {
|
|||||||
|
|
||||||
public static void P1Opdracht1(TreeSet<String> stringsToParse) {
|
public static void P1Opdracht1(TreeSet<String> stringsToParse) {
|
||||||
Character [] characters = {'a', 'b'};
|
Character [] characters = {'a', 'b'};
|
||||||
DFA<String> myAutomata = new DFA<String>(characters);
|
DFA<String> myAutomata = new DFA<String>();
|
||||||
|
|
||||||
// AAB
|
// AAB
|
||||||
myAutomata.addTransition(new Transition<String>("q0", 'a', "q1"));
|
myAutomata.addTransition(new Transition<String>("q0", 'a', "q1"));
|
||||||
@@ -177,8 +177,7 @@ public class Test {
|
|||||||
|
|
||||||
public static void Hopcroft()
|
public static void Hopcroft()
|
||||||
{
|
{
|
||||||
Character [] characters = {'a', 'b'};
|
DFA<String> automata = new DFA<String>();
|
||||||
DFA<String> automata = new DFA<String>(characters);
|
|
||||||
|
|
||||||
|
|
||||||
automata.addTransition(new Transition<String>("q0", 'a', "q2"));
|
automata.addTransition(new Transition<String>("q0", 'a', "q2"));
|
||||||
@@ -240,8 +239,7 @@ public class Test {
|
|||||||
|
|
||||||
public static void ReverseAutomata()
|
public static void ReverseAutomata()
|
||||||
{
|
{
|
||||||
Character [] characters = {'a', 'b'};
|
DFA<String> myAutomata = new DFA<String>();
|
||||||
DFA<String> myAutomata = new DFA<String>(characters);
|
|
||||||
|
|
||||||
// AAB
|
// AAB
|
||||||
myAutomata.addTransition(new Transition<String>("0", 'a', "0"));
|
myAutomata.addTransition(new Transition<String>("0", 'a', "0"));
|
||||||
@@ -277,8 +275,7 @@ public class Test {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public static void Practicum4() {
|
public static void Practicum4() {
|
||||||
Character [] characters = {'a', 'b'};
|
NDFA<String> myAutomata = new NDFA<String>();
|
||||||
NDFA<String> myAutomata = new NDFA<String>(characters);
|
|
||||||
|
|
||||||
// myAutomata.addTransition(new Transition<String>("0", 'a', "1"));
|
// myAutomata.addTransition(new Transition<String>("0", 'a', "1"));
|
||||||
// myAutomata.addTransition(new Transition<String>("0", 'a', "2"));
|
// myAutomata.addTransition(new Transition<String>("0", 'a', "2"));
|
||||||
@@ -359,8 +356,7 @@ public class Test {
|
|||||||
|
|
||||||
public static void Practicum5 () {
|
public static void Practicum5 () {
|
||||||
|
|
||||||
Character [] characters = {'a', 'b'};
|
DFA<String> myAutomata = new DFA<String>();
|
||||||
DFA<String> myAutomata = new DFA<String>(characters);
|
|
||||||
// Test 4 minimalisatie
|
// Test 4 minimalisatie
|
||||||
|
|
||||||
myAutomata.addTransition(new Transition<String>("0", 'a', "0"));
|
myAutomata.addTransition(new Transition<String>("0", 'a', "0"));
|
||||||
@@ -400,8 +396,7 @@ public class Test {
|
|||||||
|
|
||||||
public static void TupleConstructie()
|
public static void TupleConstructie()
|
||||||
{
|
{
|
||||||
Character [] characters = {'a', 'b'};
|
DFA<String> aut1 = new DFA<String>();
|
||||||
DFA<String> aut1 = new DFA<String>(characters);
|
|
||||||
|
|
||||||
aut1.addTransition(new Transition<String>("1", 'a', "2"));
|
aut1.addTransition(new Transition<String>("1", 'a', "2"));
|
||||||
aut1.addTransition(new Transition<String>("1", 'b', "1"));
|
aut1.addTransition(new Transition<String>("1", 'b', "1"));
|
||||||
@@ -418,7 +413,7 @@ public class Test {
|
|||||||
Graph.generateImage(aut1, "automaat1");
|
Graph.generateImage(aut1, "automaat1");
|
||||||
|
|
||||||
|
|
||||||
DFA<String> aut2 = new DFA<String>(characters);
|
DFA<String> aut2 = new DFA<String>();
|
||||||
|
|
||||||
aut2.addTransition(new Transition<String>("1", 'a', "1"));
|
aut2.addTransition(new Transition<String>("1", 'a', "1"));
|
||||||
aut2.addTransition(new Transition<String>("1", 'b', "2"));
|
aut2.addTransition(new Transition<String>("1", 'b', "2"));
|
||||||
@@ -454,8 +449,7 @@ public class Test {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public static void TestUitOpdrachtBeschrijving() {
|
public static void TestUitOpdrachtBeschrijving() {
|
||||||
Character [] characters = {'a', 'b'};
|
NDFA<String> ndfa = new NDFA<String>();
|
||||||
NDFA<String> ndfa = new NDFA<String>(characters);
|
|
||||||
|
|
||||||
ndfa.addTransition(new Transition<String>("q0", 'a', "q1"));
|
ndfa.addTransition(new Transition<String>("q0", 'a', "q1"));
|
||||||
|
|
||||||
|
|||||||
@@ -9,8 +9,7 @@ public class ThompsonConverter {
|
|||||||
|
|
||||||
public static NDFA<String> convert(RegExp regex)
|
public static NDFA<String> convert(RegExp regex)
|
||||||
{
|
{
|
||||||
Character [] characters = {'a', 'b'};
|
NDFA<String> generated = new NDFA<String>();
|
||||||
NDFA<String> generated = new NDFA<String>(characters);
|
|
||||||
|
|
||||||
convertStatement(regex, generated);
|
convertStatement(regex, generated);
|
||||||
|
|
||||||
|
|||||||
@@ -13,7 +13,7 @@ public class Frame extends JFrame {
|
|||||||
super("Automata");
|
super("Automata");
|
||||||
|
|
||||||
this.setSize(700, 800);
|
this.setSize(700, 800);
|
||||||
this.setResizable(false);
|
//this.setResizable(false);
|
||||||
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||||
|
|
||||||
JPanel container = new JPanel(new BorderLayout());
|
JPanel container = new JPanel(new BorderLayout());
|
||||||
|
|||||||
@@ -48,7 +48,7 @@ public class GraphImagePanel extends JPanel {
|
|||||||
width = (int) ((height * image.getWidth()) / image.getHeight());
|
width = (int) ((height * image.getWidth()) / image.getHeight());
|
||||||
}
|
}
|
||||||
|
|
||||||
g2d.drawImage(image, 0,50, width, height, this);
|
g2d.drawImage(image, 0,0, width, height, this);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -124,7 +124,7 @@ public class GraphPanel extends JPanel {
|
|||||||
|
|
||||||
StatusPanel.Instance().setStatus("Minimizing cdfa." + name, 50);
|
StatusPanel.Instance().setStatus("Minimizing cdfa." + name, 50);
|
||||||
|
|
||||||
DFA<String> mini = convert.minimaliseer();
|
DFA<String> mini = convert.minimaliseerHopcroft();
|
||||||
|
|
||||||
StatusPanel.Instance().setStatus("Generating minimized image", 70);
|
StatusPanel.Instance().setStatus("Generating minimized image", 70);
|
||||||
|
|
||||||
@@ -137,6 +137,23 @@ public class GraphPanel extends JPanel {
|
|||||||
mini.geefTaalTotLengte(10).getSymbols().forEach(s -> System.out.println("Accepteer " + s + " ? " + mini.accepteer(s)));
|
mini.geefTaalTotLengte(10).getSymbols().forEach(s -> System.out.println("Accepteer " + s + " ? " + mini.accepteer(s)));
|
||||||
StatusPanel.Instance().setStatus("Done", 100);
|
StatusPanel.Instance().setStatus("Done", 100);
|
||||||
}
|
}
|
||||||
|
else if(automata.type == Importable.Type.DFA)
|
||||||
|
{
|
||||||
|
DFA<String> dfa = (DFA<String>) automata;
|
||||||
|
|
||||||
|
StatusPanel.Instance().setStatus("Minimizing dfa." + name, 40);
|
||||||
|
|
||||||
|
DFA<String> mini = dfa.minimaliseerHopcroft();
|
||||||
|
|
||||||
|
StatusPanel.Instance().setStatus("Generating minimized image", 60);
|
||||||
|
|
||||||
|
Graph.generateImage(mini, "mcdfa");
|
||||||
|
|
||||||
|
TabPanel.Instance().addGraph("DFA: mcdfa." + name, new File("images/mcdfa.png"), mini);
|
||||||
|
|
||||||
|
StatusPanel.Instance().setStatus("Done", 100);
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user