Added background worker
This commit is contained in:
+5
-3
@@ -1,9 +1,11 @@
|
||||
NDFA
|
||||
B0
|
||||
E1
|
||||
E1,2
|
||||
0>a>1
|
||||
0>b>1
|
||||
0>$>1
|
||||
0>$>2
|
||||
1>a>0
|
||||
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 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 getGroupName()
|
||||
{
|
||||
count++;
|
||||
if(count >= alphabet.length)
|
||||
if(count >= alphabet.length) {
|
||||
count = 0;
|
||||
suffix++;
|
||||
}
|
||||
|
||||
return alphabet[count];
|
||||
return alphabet[count] + suffix;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -106,15 +106,9 @@ public class GraphPanel extends JPanel {
|
||||
minimizeButton.addActionListener(new ActionListener() {
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
|
||||
System.out.println("Kaas");
|
||||
|
||||
new Thread(new Runnable() {
|
||||
BackgroundWorker.instance().addWorker(new BackgroundWorker.Worker() {
|
||||
@Override
|
||||
public void run() {
|
||||
|
||||
System.out.println(automata.type);
|
||||
|
||||
public void execute() {
|
||||
if(automata.type == Importable.Type.NDFA)
|
||||
{
|
||||
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);
|
||||
|
||||
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);
|
||||
|
||||
DFA<String> mini = convert.minimaliseerHopcroft();
|
||||
DFA<String> mini = convert.minimaliseer();
|
||||
|
||||
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);
|
||||
|
||||
}
|
||||
}
|
||||
}).start();
|
||||
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
@@ -16,7 +16,6 @@ import java.util.ArrayList;
|
||||
public class InputPanel extends JPanel {
|
||||
|
||||
private static InputPanel panel;
|
||||
private static boolean threadRunning = false;
|
||||
|
||||
public static InputPanel Instance()
|
||||
{
|
||||
@@ -48,31 +47,36 @@ public class InputPanel extends JPanel {
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
|
||||
String regS = text.getText();
|
||||
String name = regS.toLowerCase().trim();
|
||||
BackgroundWorker.instance().addWorker(new BackgroundWorker.Worker() {
|
||||
@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();
|
||||
regex = regex.naarRegExp(regS);
|
||||
RegExp regex = new RegExp();
|
||||
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);
|
||||
try {
|
||||
Thread.sleep(500);
|
||||
} catch (InterruptedException e1) {
|
||||
}
|
||||
NDFA<String> ndfa = ThompsonConverter.convert(regex);
|
||||
try {
|
||||
Thread.sleep(500);
|
||||
} 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() {
|
||||
@Override
|
||||
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);
|
||||
|
||||
String file = (String)fileCombo.getSelectedItem();
|
||||
@@ -159,9 +161,8 @@ public class InputPanel extends JPanel {
|
||||
|
||||
StatusPanel.Instance().setStatus("Done", 100);
|
||||
|
||||
threadRunning = false;
|
||||
}
|
||||
}).start();
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user