Added background worker
This commit is contained in:
+5
-3
@@ -1,9 +1,11 @@
|
|||||||
NDFA
|
NDFA
|
||||||
B0
|
B0
|
||||||
E1
|
E1,2
|
||||||
0>a>1
|
0>a>1
|
||||||
0>b>1
|
0>b>1
|
||||||
0>$>1
|
0>$>2
|
||||||
1>a>0
|
1>a>0
|
||||||
1>b>1
|
1>b>1
|
||||||
1>$>0
|
2>a>1
|
||||||
|
2>b>0
|
||||||
|
2>b>2
|
||||||
@@ -0,0 +1,69 @@
|
|||||||
|
package com.imegumii;
|
||||||
|
|
||||||
|
import com.sun.jmx.remote.internal.ArrayQueue;
|
||||||
|
|
||||||
|
import java.util.ArrayDeque;
|
||||||
|
import java.util.Queue;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Created by kenny on 12-6-2017.
|
||||||
|
*/
|
||||||
|
public class BackgroundWorker implements Runnable{
|
||||||
|
|
||||||
|
public interface Worker {
|
||||||
|
void execute();
|
||||||
|
};
|
||||||
|
|
||||||
|
private Thread t;
|
||||||
|
private boolean running;
|
||||||
|
private Queue<Worker> workers;
|
||||||
|
|
||||||
|
private static BackgroundWorker worker;
|
||||||
|
|
||||||
|
public static BackgroundWorker instance()
|
||||||
|
{
|
||||||
|
if(worker == null)
|
||||||
|
worker = new BackgroundWorker();
|
||||||
|
|
||||||
|
return worker;
|
||||||
|
}
|
||||||
|
|
||||||
|
private BackgroundWorker()
|
||||||
|
{
|
||||||
|
running = true;
|
||||||
|
workers = new ArrayDeque<Worker>();
|
||||||
|
|
||||||
|
t = new Thread(this);
|
||||||
|
t.start();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void addWorker(Worker w)
|
||||||
|
{
|
||||||
|
workers.add(w);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void kill()
|
||||||
|
{
|
||||||
|
running = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
|
||||||
|
while(running)
|
||||||
|
{
|
||||||
|
try {
|
||||||
|
Thread.sleep(100);
|
||||||
|
} catch (InterruptedException e) {
|
||||||
|
}
|
||||||
|
|
||||||
|
if(workers.isEmpty())
|
||||||
|
continue;
|
||||||
|
|
||||||
|
Worker current = workers.poll();
|
||||||
|
|
||||||
|
current.execute();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -160,15 +160,18 @@ public class HopcroftConverter {
|
|||||||
|
|
||||||
|
|
||||||
private int count = -1;
|
private int count = -1;
|
||||||
|
private int suffix = 0;
|
||||||
private String[] alphabet = {"A", "B", "C", "D", "E", "F", "G","H","I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"};
|
private String[] alphabet = {"A", "B", "C", "D", "E", "F", "G","H","I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"};
|
||||||
|
|
||||||
private String getGroupName()
|
private String getGroupName()
|
||||||
{
|
{
|
||||||
count++;
|
count++;
|
||||||
if(count >= alphabet.length)
|
if(count >= alphabet.length) {
|
||||||
count = 0;
|
count = 0;
|
||||||
|
suffix++;
|
||||||
|
}
|
||||||
|
|
||||||
return alphabet[count];
|
return alphabet[count] + suffix;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -106,15 +106,9 @@ public class GraphPanel extends JPanel {
|
|||||||
minimizeButton.addActionListener(new ActionListener() {
|
minimizeButton.addActionListener(new ActionListener() {
|
||||||
@Override
|
@Override
|
||||||
public void actionPerformed(ActionEvent e) {
|
public void actionPerformed(ActionEvent e) {
|
||||||
|
BackgroundWorker.instance().addWorker(new BackgroundWorker.Worker() {
|
||||||
System.out.println("Kaas");
|
|
||||||
|
|
||||||
new Thread(new Runnable() {
|
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void execute() {
|
||||||
|
|
||||||
System.out.println(automata.type);
|
|
||||||
|
|
||||||
if(automata.type == Importable.Type.NDFA)
|
if(automata.type == Importable.Type.NDFA)
|
||||||
{
|
{
|
||||||
StatusPanel.Instance().setStatus("Converting to DFA " + name, 20);
|
StatusPanel.Instance().setStatus("Converting to DFA " + name, 20);
|
||||||
@@ -123,26 +117,25 @@ public class GraphPanel extends JPanel {
|
|||||||
|
|
||||||
StatusPanel.Instance().setStatus("Generating image for DFA " + name, 30);
|
StatusPanel.Instance().setStatus("Generating image for DFA " + name, 30);
|
||||||
|
|
||||||
Graph.generateImage(convert, "cdfa." + name);
|
Graph.generateImage(convert, "cdfa");
|
||||||
|
|
||||||
TabPanel.Instance().addGraph("DFA: " + name, new File("images/cdfa." + name), convert);
|
TabPanel.Instance().addGraph("DFA: " + name, new File("images/cdfa.png"), convert);
|
||||||
|
|
||||||
StatusPanel.Instance().setStatus("Minimizing cdfa." + name, 50);
|
StatusPanel.Instance().setStatus("Minimizing cdfa." + name, 50);
|
||||||
|
|
||||||
DFA<String> mini = convert.minimaliseerHopcroft();
|
DFA<String> mini = convert.minimaliseer();
|
||||||
|
|
||||||
StatusPanel.Instance().setStatus("Generating minimized image", 70);
|
StatusPanel.Instance().setStatus("Generating minimized image", 70);
|
||||||
|
|
||||||
Graph.generateImage(mini, "mcdfa." + name);
|
Graph.generateImage(mini, "mcdfa");
|
||||||
|
|
||||||
TabPanel.Instance().addGraph("DFA: mcdfa." + name, new File("images/mcdfa." + name), mini);
|
TabPanel.Instance().addGraph("DFA: mcdfa." + name, new File("images/mcdfa.png"), mini);
|
||||||
|
|
||||||
StatusPanel.Instance().setStatus("Done", 100);
|
StatusPanel.Instance().setStatus("Done", 100);
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}).start();
|
});
|
||||||
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
@@ -16,7 +16,6 @@ import java.util.ArrayList;
|
|||||||
public class InputPanel extends JPanel {
|
public class InputPanel extends JPanel {
|
||||||
|
|
||||||
private static InputPanel panel;
|
private static InputPanel panel;
|
||||||
private static boolean threadRunning = false;
|
|
||||||
|
|
||||||
public static InputPanel Instance()
|
public static InputPanel Instance()
|
||||||
{
|
{
|
||||||
@@ -48,31 +47,36 @@ public class InputPanel extends JPanel {
|
|||||||
@Override
|
@Override
|
||||||
public void actionPerformed(ActionEvent e) {
|
public void actionPerformed(ActionEvent e) {
|
||||||
|
|
||||||
String regS = text.getText();
|
BackgroundWorker.instance().addWorker(new BackgroundWorker.Worker() {
|
||||||
String name = regS.toLowerCase().trim();
|
@Override
|
||||||
|
public void execute() {
|
||||||
|
String regS = text.getText().trim();
|
||||||
|
String name = "regex1";
|
||||||
|
|
||||||
text.setText("");
|
text.setText("");
|
||||||
|
|
||||||
StatusPanel.Instance().setStatus("Parsing REGEX", 20);
|
StatusPanel.Instance().setStatus("Parsing REGEX", 20);
|
||||||
|
|
||||||
RegExp regex = new RegExp();
|
RegExp regex = new RegExp();
|
||||||
regex = regex.naarRegExp(regS);
|
regex = regex.naarRegExp(regS);
|
||||||
|
|
||||||
StatusPanel.Instance().setStatus("Converting REGEX to NDFA", 40);
|
StatusPanel.Instance().setStatus("Converting REGEX to NDFA", 40);
|
||||||
|
|
||||||
NDFA<String> ndfa = ThompsonConverter.convert(regex);
|
NDFA<String> ndfa = ThompsonConverter.convert(regex);
|
||||||
try {
|
try {
|
||||||
Thread.sleep(500);
|
Thread.sleep(500);
|
||||||
} catch (InterruptedException e1) {
|
} catch (InterruptedException e1) {
|
||||||
}
|
}
|
||||||
|
|
||||||
StatusPanel.Instance().setStatus("Generating image for NDFA", 70);
|
StatusPanel.Instance().setStatus("Generating image for NDFA", 70);
|
||||||
|
|
||||||
Graph.generateImage(ndfa, name);
|
Graph.generateImage(ndfa, name);
|
||||||
|
|
||||||
TabPanel.Instance().addGraph("REGEX: " + name, new File("images/" + name + ".png"), ndfa);
|
TabPanel.Instance().addGraph("REGEX: " + name, new File("images/" + name + ".png"), ndfa);
|
||||||
|
|
||||||
StatusPanel.Instance().setStatus("Done", 100);
|
StatusPanel.Instance().setStatus("Done", 100);
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -103,13 +107,11 @@ public class InputPanel extends JPanel {
|
|||||||
fileButton.addActionListener(new ActionListener() {
|
fileButton.addActionListener(new ActionListener() {
|
||||||
@Override
|
@Override
|
||||||
public void actionPerformed(ActionEvent e) {
|
public void actionPerformed(ActionEvent e) {
|
||||||
new Thread(new Runnable() {
|
|
||||||
@Override public void run() {
|
|
||||||
|
|
||||||
while(threadRunning){};
|
|
||||||
|
|
||||||
threadRunning = true;
|
|
||||||
|
|
||||||
|
BackgroundWorker.instance().addWorker(new BackgroundWorker.Worker() {
|
||||||
|
@Override
|
||||||
|
public void execute() {
|
||||||
|
;
|
||||||
StatusPanel.Instance().setStatus("Parsing file", 10);
|
StatusPanel.Instance().setStatus("Parsing file", 10);
|
||||||
|
|
||||||
String file = (String)fileCombo.getSelectedItem();
|
String file = (String)fileCombo.getSelectedItem();
|
||||||
@@ -159,9 +161,8 @@ public class InputPanel extends JPanel {
|
|||||||
|
|
||||||
StatusPanel.Instance().setStatus("Done", 100);
|
StatusPanel.Instance().setStatus("Done", 100);
|
||||||
|
|
||||||
threadRunning = false;
|
|
||||||
}
|
}
|
||||||
}).start();
|
});
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user