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;
|
||||
|
||||
public Automata(Character [] symbols) {
|
||||
this(new TreeSet<Character>(Arrays.asList(symbols)));
|
||||
}
|
||||
|
||||
public Automata(SortedSet<Character> symbols) {
|
||||
public Automata() {
|
||||
super(Type.ERROR);
|
||||
states = new TreeSet<T>();
|
||||
beginStates = new TreeSet<T>();
|
||||
@@ -30,10 +26,13 @@ public class Automata <T extends Comparable> extends Importable {
|
||||
|
||||
transistions = new TreeSet<>();
|
||||
|
||||
this.symbols = symbols;
|
||||
this.symbols = new TreeSet<>();
|
||||
}
|
||||
|
||||
public void addTransition (Transition<T> t) {
|
||||
if(t.symbol != Transition.EPSILON)
|
||||
symbols.add(t.symbol);
|
||||
|
||||
transistions.add(t);
|
||||
states.add(t.vanState);
|
||||
states.add(t.naarState);
|
||||
|
||||
@@ -10,13 +10,8 @@ import javafx.util.Pair;
|
||||
*/
|
||||
public class DFA<T extends Comparable> extends Automata<T> {
|
||||
|
||||
public DFA(Character[] symbols) {
|
||||
super(symbols);
|
||||
this.type = Type.DFA;
|
||||
}
|
||||
|
||||
public DFA(SortedSet<Character> symbols) {
|
||||
super(symbols);
|
||||
public DFA() {
|
||||
super();
|
||||
this.type = Type.DFA;
|
||||
}
|
||||
|
||||
@@ -73,7 +68,7 @@ public class DFA<T extends Comparable> extends Automata<T> {
|
||||
|
||||
public NDFA<String> reverse()
|
||||
{
|
||||
NDFA<String> reversed = new NDFA<String>(this.symbols);
|
||||
NDFA<String> reversed = new NDFA<String>();
|
||||
|
||||
for(Transition<T> t : transistions)
|
||||
{
|
||||
@@ -101,7 +96,7 @@ public class DFA<T extends Comparable> extends Automata<T> {
|
||||
}
|
||||
|
||||
public DFA<T> ontkenning() {
|
||||
DFA<T> returnDFA = new DFA<T>(this.symbols);
|
||||
DFA<T> returnDFA = new DFA<T>();
|
||||
|
||||
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)
|
||||
{
|
||||
DFA<String> merged = new DFA<String>(dfa1.symbols);
|
||||
DFA<String> merged = new DFA<String>();
|
||||
|
||||
//Create all transitions and states
|
||||
for(Transition t1 : dfa1.transistions)
|
||||
|
||||
@@ -44,8 +44,7 @@ public class FileParser {
|
||||
|
||||
public static DFA<String> readDFA(BufferedReader br) throws IOException {
|
||||
|
||||
Character[] chars = {'a', 'b'};
|
||||
DFA<String> dfa = new DFA<String>(chars);
|
||||
DFA<String> dfa = new DFA<String>();
|
||||
|
||||
String line;
|
||||
while ((line = br.readLine()) != null) {
|
||||
@@ -70,8 +69,7 @@ public class FileParser {
|
||||
|
||||
public static NDFA<String> readNDFA(BufferedReader br) throws IOException {
|
||||
|
||||
Character[] chars = {'a', 'b'};
|
||||
NDFA<String> ndfa = new NDFA<String>(chars);
|
||||
NDFA<String> ndfa = new NDFA<String>();
|
||||
String line;
|
||||
while ((line = br.readLine()) != null) {
|
||||
if(line.startsWith("B")) {
|
||||
|
||||
@@ -96,9 +96,9 @@ public class Graph {
|
||||
try {
|
||||
MutableGraph g = Parser.read(Graph.generateImageString(a));
|
||||
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 {
|
||||
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) {
|
||||
e.printStackTrace();
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
package com.imegumii;
|
||||
|
||||
import javafx.geometry.HPos;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.SortedSet;
|
||||
@@ -74,17 +76,13 @@ public class HopcroftConverter {
|
||||
sets.add(currentSet);
|
||||
}
|
||||
|
||||
if(t.symbol == 'a')
|
||||
currentSet.transitionAState = (String)t.naarState;
|
||||
else if(t.symbol == 'b')
|
||||
currentSet.transitionBState = (String)t.naarState;
|
||||
currentSet.addColum(new HopcroftColumn(t.symbol, (String)t.naarState, "-"));
|
||||
}
|
||||
}
|
||||
|
||||
private DFA<String> HopcrofttoDFA()
|
||||
{
|
||||
Character [] characters = {'a', 'b'};
|
||||
DFA<String> result = new DFA<String>(characters);
|
||||
DFA<String> result = new DFA<String>();
|
||||
|
||||
ArrayList<String> groups = new ArrayList<String>();
|
||||
|
||||
@@ -92,8 +90,8 @@ public class HopcroftConverter {
|
||||
{
|
||||
if(!groups.contains(s.group))
|
||||
{
|
||||
result.addTransition(new Transition<String>(s.group, 'a', s.transitionAGroup));
|
||||
result.addTransition(new Transition<String>(s.group, 'b', s.transitionBGroup));
|
||||
for(HopcroftColumn c : s.columns)
|
||||
result.addTransition(new Transition<String>(s.group, c.symbol, c.group));
|
||||
|
||||
if(s.isEndState)
|
||||
result.defineAsEndState(s.group);
|
||||
@@ -112,13 +110,13 @@ public class HopcroftConverter {
|
||||
{
|
||||
for(HopcroftSet s : sets)
|
||||
{
|
||||
for(HopcroftSet h : sets)
|
||||
for(HopcroftColumn c : s.columns)
|
||||
{
|
||||
if(s.transitionAState == h.state)
|
||||
s.transitionAGroup = h.group;
|
||||
|
||||
if(s.transitionBState == h.state)
|
||||
s.transitionBGroup = h.group;
|
||||
for(HopcroftSet h : sets)
|
||||
{
|
||||
if(c.state.equals(h.state))
|
||||
c.group = h.group;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -129,7 +127,10 @@ public class HopcroftConverter {
|
||||
|
||||
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))
|
||||
{
|
||||
@@ -181,11 +182,7 @@ class HopcroftSet implements Comparable<HopcroftSet>{
|
||||
public String group;
|
||||
public String state;
|
||||
|
||||
public String transitionAState;
|
||||
public String transitionAGroup;
|
||||
|
||||
public String transitionBState;
|
||||
public String transitionBGroup;
|
||||
public ArrayList<HopcroftColumn> columns;
|
||||
|
||||
public boolean isEndState = false;
|
||||
|
||||
@@ -194,6 +191,18 @@ class HopcroftSet implements Comparable<HopcroftSet>{
|
||||
this.group = group;
|
||||
this.state = state;
|
||||
this.isEndState = isEndState;
|
||||
|
||||
columns = new ArrayList<>();
|
||||
}
|
||||
|
||||
public void addColum(HopcroftColumn c)
|
||||
{
|
||||
this.columns.add(c);
|
||||
}
|
||||
|
||||
public void clearColumns()
|
||||
{
|
||||
this.columns.clear();
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -204,6 +213,27 @@ class HopcroftSet implements Comparable<HopcroftSet>{
|
||||
@Override
|
||||
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.
|
||||
*/
|
||||
public class NDFA<T extends Comparable> extends Automata<T> {
|
||||
public NDFA(Character[] symbols) {
|
||||
super(symbols);
|
||||
this.type = Type.NDFA;
|
||||
}
|
||||
|
||||
public NDFA(SortedSet<Character> symbols) {
|
||||
super(symbols);
|
||||
public NDFA() {
|
||||
super();
|
||||
this.type = Type.NDFA;
|
||||
}
|
||||
|
||||
@@ -42,7 +38,7 @@ public class NDFA<T extends Comparable> extends Automata<T> {
|
||||
}
|
||||
|
||||
public DFA<String> toDFA () {
|
||||
DFA<String> eindDFA = new DFA<>(this.symbols);
|
||||
DFA<String> eindDFA = new DFA<>();
|
||||
|
||||
SortedSet<T> startSet = new TreeSet<T>();
|
||||
//er kunnen meer start states zijn,
|
||||
|
||||
@@ -67,7 +67,7 @@ public class Test {
|
||||
|
||||
public static void P1Opdracht1(TreeSet<String> stringsToParse) {
|
||||
Character [] characters = {'a', 'b'};
|
||||
DFA<String> myAutomata = new DFA<String>(characters);
|
||||
DFA<String> myAutomata = new DFA<String>();
|
||||
|
||||
// AAB
|
||||
myAutomata.addTransition(new Transition<String>("q0", 'a', "q1"));
|
||||
@@ -177,8 +177,7 @@ public class Test {
|
||||
|
||||
public static void Hopcroft()
|
||||
{
|
||||
Character [] characters = {'a', 'b'};
|
||||
DFA<String> automata = new DFA<String>(characters);
|
||||
DFA<String> automata = new DFA<String>();
|
||||
|
||||
|
||||
automata.addTransition(new Transition<String>("q0", 'a', "q2"));
|
||||
@@ -240,8 +239,7 @@ public class Test {
|
||||
|
||||
public static void ReverseAutomata()
|
||||
{
|
||||
Character [] characters = {'a', 'b'};
|
||||
DFA<String> myAutomata = new DFA<String>(characters);
|
||||
DFA<String> myAutomata = new DFA<String>();
|
||||
|
||||
// AAB
|
||||
myAutomata.addTransition(new Transition<String>("0", 'a', "0"));
|
||||
@@ -277,8 +275,7 @@ public class Test {
|
||||
}
|
||||
|
||||
public static void Practicum4() {
|
||||
Character [] characters = {'a', 'b'};
|
||||
NDFA<String> myAutomata = new NDFA<String>(characters);
|
||||
NDFA<String> myAutomata = new NDFA<String>();
|
||||
|
||||
// myAutomata.addTransition(new Transition<String>("0", 'a', "1"));
|
||||
// myAutomata.addTransition(new Transition<String>("0", 'a', "2"));
|
||||
@@ -359,8 +356,7 @@ public class Test {
|
||||
|
||||
public static void Practicum5 () {
|
||||
|
||||
Character [] characters = {'a', 'b'};
|
||||
DFA<String> myAutomata = new DFA<String>(characters);
|
||||
DFA<String> myAutomata = new DFA<String>();
|
||||
// Test 4 minimalisatie
|
||||
|
||||
myAutomata.addTransition(new Transition<String>("0", 'a', "0"));
|
||||
@@ -400,8 +396,7 @@ public class Test {
|
||||
|
||||
public static void TupleConstructie()
|
||||
{
|
||||
Character [] characters = {'a', 'b'};
|
||||
DFA<String> aut1 = new DFA<String>(characters);
|
||||
DFA<String> aut1 = new DFA<String>();
|
||||
|
||||
aut1.addTransition(new Transition<String>("1", 'a', "2"));
|
||||
aut1.addTransition(new Transition<String>("1", 'b', "1"));
|
||||
@@ -418,7 +413,7 @@ public class Test {
|
||||
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", 'b', "2"));
|
||||
@@ -454,8 +449,7 @@ public class Test {
|
||||
}
|
||||
|
||||
public static void TestUitOpdrachtBeschrijving() {
|
||||
Character [] characters = {'a', 'b'};
|
||||
NDFA<String> ndfa = new NDFA<String>(characters);
|
||||
NDFA<String> ndfa = new NDFA<String>();
|
||||
|
||||
ndfa.addTransition(new Transition<String>("q0", 'a', "q1"));
|
||||
|
||||
|
||||
@@ -9,8 +9,7 @@ public class ThompsonConverter {
|
||||
|
||||
public static NDFA<String> convert(RegExp regex)
|
||||
{
|
||||
Character [] characters = {'a', 'b'};
|
||||
NDFA<String> generated = new NDFA<String>(characters);
|
||||
NDFA<String> generated = new NDFA<String>();
|
||||
|
||||
convertStatement(regex, generated);
|
||||
|
||||
|
||||
@@ -13,7 +13,7 @@ public class Frame extends JFrame {
|
||||
super("Automata");
|
||||
|
||||
this.setSize(700, 800);
|
||||
this.setResizable(false);
|
||||
//this.setResizable(false);
|
||||
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||
|
||||
JPanel container = new JPanel(new BorderLayout());
|
||||
|
||||
@@ -48,7 +48,7 @@ public class GraphImagePanel extends JPanel {
|
||||
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);
|
||||
|
||||
DFA<String> mini = convert.minimaliseer();
|
||||
DFA<String> mini = convert.minimaliseerHopcroft();
|
||||
|
||||
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)));
|
||||
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