Added file loading and displaying of images in tabs

This commit is contained in:
2017-06-09 15:49:26 +02:00
parent a991a23339
commit 3958c68fd0
24 changed files with 594 additions and 181 deletions
Binary file not shown.

After

Width:  |  Height:  |  Size: 30 KiB

View File
+1
View File
@@ -0,0 +1 @@
Raw
+1
View File
@@ -1,3 +1,4 @@
DFA
B0
E1
0>a>1
+13
View File
@@ -0,0 +1,13 @@
DFA
B0
E4,2
0>a>1
0>b>2
1>a>0
1>b>2
2>a>3
2>b>4
3>a>0
3>b>2
4>a>1
4>b>0
+1
View File
@@ -1,3 +1,4 @@
NDFA
B0
E1
0>a>1
+2
View File
@@ -0,0 +1,2 @@
REGEX
(a|b)
+7
View File
@@ -0,0 +1,7 @@
Tabs voor openstaande grafieken, met manier om te sluiten ofzo
File selector voor dot files
Error message
loading bar
regex input
exporteren van plaatje
exporteren van raw data
+12 -1
View File
@@ -8,7 +8,7 @@ import java.util.TreeSet;
/**
* Created by imegumii on 18/04/2017.
*/
public class Automata <T extends Comparable> {
public class Automata <T extends Comparable> extends Importable {
protected Set<Transition<T>> transistions;
@@ -23,6 +23,7 @@ public class Automata <T extends Comparable> {
}
public Automata(SortedSet<Character> symbols) {
super(Type.ERROR);
states = new TreeSet<T>();
beginStates = new TreeSet<T>();
eindStates = new TreeSet<T>();
@@ -50,7 +51,17 @@ public class Automata <T extends Comparable> {
public void printTransitions () {
transistions.forEach(System.out::println);
}
public String getTransitions()
{
String s = "";
for(Transition t : transistions)
{
s += t.toString() + "\n";
}
return s;
}
public void print() {
+2
View File
@@ -12,10 +12,12 @@ 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);
this.type = Type.DFA;
}
public boolean accepteer(String s) {
+71 -64
View File
@@ -1,101 +1,108 @@
package com.imegumii;
import java.io.*;
import java.nio.Buffer;
/**
* Created by kenny on 8-6-2017.
*/
public class FileParser {
public static DFA<String> readDFA(String filename)
public static Importable read(String filename)
{
File f = new File("input/" + filename + ".dot");
File f = new File("input/" + filename);
Character[] chars = {'a', 'b'};
DFA<String> dfa = new DFA<String>(chars);
Importable imp = null;
try (BufferedReader br = new BufferedReader(new FileReader(f))) {
String line;
while ((line = br.readLine()) != null) {
if(line.startsWith("B")) {
String[] states = line.substring(1,line.length()).split(",");
for(String s : states) { dfa.defineAsStartState(s); }
}
else if(line.startsWith("E")) {
String[] states = line.substring(1,line.length()).split(",");
for(String s : states) { dfa.defineAsEndState(s); }
}
String type = br.readLine();
else {
String[] trans = line.split(">");
dfa.addTransition(new Transition<String>(trans[0], trans[1].toCharArray()[0], trans[2]));
}
switch (type)
{
case "DFA":
imp = readDFA(br);
break;
case "NDFA":
imp = readNDFA(br);
break;
case "REGEX":
imp = readRegex(br);
break;
default:
break;
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return imp;
}
public static DFA<String> readDFA(BufferedReader br) throws IOException {
Character[] chars = {'a', 'b'};
DFA<String> dfa = new DFA<String>(chars);
String line;
while ((line = br.readLine()) != null) {
if(line.startsWith("B")) {
String[] states = line.substring(1,line.length()).split(",");
for(String s : states) { dfa.defineAsStartState(s); }
}
else if(line.startsWith("E")) {
String[] states = line.substring(1,line.length()).split(",");
for(String s : states) { dfa.defineAsEndState(s); }
}
else {
String[] trans = line.split(">");
dfa.addTransition(new Transition<String>(trans[0], trans[1].toCharArray()[0], trans[2]));
}
}
return dfa;
}
public static NDFA<String> readNDFA(String filename)
{
File f = new File("input/" + filename + ".dot");
public static NDFA<String> readNDFA(BufferedReader br) throws IOException {
Character[] chars = {'a', 'b'};
NDFA<String> ndfa = new NDFA<String>(chars);
try (BufferedReader br = new BufferedReader(new FileReader(f))) {
String line;
while ((line = br.readLine()) != null) {
if(line.startsWith("B")) {
String[] states = line.substring(1,line.length()).split(",");
for(String s : states) { ndfa.defineAsStartState(s); }
}
else if(line.startsWith("E")) {
String[] states = line.substring(1,line.length()).split(",");
for(String s : states) { ndfa.defineAsEndState(s); }
}
else {
String[] trans = line.split(">");
if(trans[1].equals("$")) {
ndfa.addTransition(new Transition<String>(trans[0], Transition.EPSILON, trans[2]));
}
else
ndfa.addTransition(new Transition<String>(trans[0], trans[1].toCharArray()[0], trans[2]));
}
Character[] chars = {'a', 'b'};
NDFA<String> ndfa = new NDFA<String>(chars);
String line;
while ((line = br.readLine()) != null) {
if(line.startsWith("B")) {
String[] states = line.substring(1,line.length()).split(",");
for(String s : states) { ndfa.defineAsStartState(s); }
}
else if(line.startsWith("E")) {
String[] states = line.substring(1,line.length()).split(",");
for(String s : states) { ndfa.defineAsEndState(s); }
}
else {
String[] trans = line.split(">");
if(trans[1].equals("$")) {
ndfa.addTransition(new Transition<String>(trans[0], Transition.EPSILON, trans[2]));
}
else
ndfa.addTransition(new Transition<String>(trans[0], trans[1].toCharArray()[0], trans[2]));
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return ndfa;
}
public static RegExp readRegex(String filename)
{
File f = new File("input/" + filename + ".dot");
BufferedReader br = null;
public static RegExp readRegex(BufferedReader br) throws IOException {
String line = "Error";
try {
br = new BufferedReader(new FileReader(f));
line = br.readLine();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
line = br.readLine();
RegExp regex = new RegExp("a"); //RegExp.parse(line);
RegExp regex = new RegExp("a").of(new RegExp("b").ster()); //RegExp.parse(line);
return regex;
}
+36 -2
View File
@@ -23,7 +23,7 @@ public class Graph {
if(a.eindStates.size() > 0) {
text += "node [shape = doublecircle];";
text += "node [shape=cirlce, peripheries=2];";
for (String s : a.eindStates) {
text += "\"" + s + "\" ";
@@ -58,11 +58,45 @@ public class Graph {
return text;
}
public static String generateImageString(Automata<String> a)
{
String text = "digraph {\nrankdir=LR;\n";
if(a.eindStates.size() > 0) {
for (String s : a.eindStates) {
text += "\"" + s + "\" [peripheries=2]\n";
}
}
if(a.beginStates.size() > 0) {
text += "\nstart [shape = point];\n";
for (String s : a.beginStates) {
text += "\"start\" -> \"" + s + "\" [label = \" \", color=aquamarine4];";
}
}
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;
}
public static void generateImage(Automata<String> a, String fileName) {
try {
File f = new File("images/" + a.hashCode() + ".dot");
PrintWriter w = new PrintWriter(f);
w.print(Graph.generateGraphString(a));
w.print(Graph.generateImageString(a));
w.flush();
w.close();
MutableGraph g = Parser.read(f);
+15
View File
@@ -0,0 +1,15 @@
package com.imegumii;
/**
* Created by kenny on 9-6-2017.
*/
public class Importable {
public static enum Type {DFA, NDFA, REGEX, ERROR};
public Type type;
public Importable(Type t)
{
this.type = t;
}
}
+2
View File
@@ -8,10 +8,12 @@ import java.util.*;
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);
this.type = Type.NDFA;
}
public SortedSet<T> statesBereikbaarVanaf(T vanaf, char s) {
+3 -1
View File
@@ -8,7 +8,7 @@ import java.util.TreeSet;
/**
* Created by kenny on 2-5-2017.
*/
public class RegExp {
public class RegExp extends Importable{
public enum Operator { PLUS, STER, OF, PUNT, EEN}
@@ -27,6 +27,7 @@ public class RegExp {
public RegExp()
{
super(Type.REGEX);
operator = Operator.EEN;
characters = "";
links = null;
@@ -34,6 +35,7 @@ public class RegExp {
}
public RegExp(String chars){
super(Type.REGEX);
operator = Operator.EEN;
characters = chars;
links = null;
+10 -15
View File
@@ -20,9 +20,9 @@ public class Test {
// Hopcroft();
// TupleConstructie();
TupleConstructie();
FileReadTest();
// FileReadTest();
}
public static void P1Opdracht1(TreeSet<String> stringsToParse) {
@@ -374,7 +374,8 @@ public class Test {
aut1.defineAsEndState("1");
System.out.println("\n\nA1\n--------");
System.out.println(Graph.generateGraphString(aut1));
System.out.println(Graph.generateImageString(aut1));
Graph.generateImage(aut1, "automaat1");
DFA<String> aut2 = new DFA<String>(characters);
@@ -396,25 +397,19 @@ public class Test {
aut2.defineAsEndState("2");
System.out.println("\n\nA2\n--------");
System.out.println(Graph.generateGraphString(aut2));
System.out.println(Graph.generateImageString(aut2));
Graph.generateImage(aut2, "automaat2");
DFA<String> en = aut1.en(aut2);
System.out.println("\n\nEN\n--------");
System.out.println(Graph.generateGraphString(en));
System.out.println(Graph.generateImageString(en));
Graph.generateImage(en, "en");
DFA<String> of = aut1.of(aut2);
System.out.println("\n\nOF\n--------");
System.out.println(Graph.generateGraphString(of));
}
public static void FileReadTest()
{
DFA<String> dfa1 = FileParser.readDFA("dfa1");
System.out.println(Graph.generateGraphString(dfa1));
NDFA<String> ndfa1 = FileParser.readNDFA("ndfa1");
System.out.println(Graph.generateGraphString(ndfa1));
System.out.println(Graph.generateImageString(of));
Graph.generateImage(of, "of");
}
}
+3 -2
View File
@@ -17,8 +17,9 @@ public class Frame extends JFrame {
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel container = new JPanel(new BorderLayout());
container.add(ImagePanel.Instance(), BorderLayout.CENTER);
container.add(RegexPanel.Instance(), BorderLayout.NORTH);
container.add(TabPanel.Instance(), BorderLayout.CENTER);
container.add(InputPanel.Instance(), BorderLayout.NORTH);
container.add(StatusPanel.Instance(), BorderLayout.SOUTH);
this.setContentPane(container);
+54
View File
@@ -0,0 +1,54 @@
package com.imegumii.ui;
import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.*;
/**
* Created by kenny on 9-6-2017.
*/
public class GraphImagePanel extends JPanel {
private BufferedImage image;
public GraphImagePanel(File f)
{
super();
this.setBackground(Color.WHITE);
try {
image = ImageIO.read(f);
} catch (IOException e) {
e.printStackTrace();
}
repaint();
}
public void paintComponent(Graphics g){
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
g2d.addRenderingHints(new RenderingHints(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON));
g2d.addRenderingHints(new RenderingHints(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC));
if(image != null){
int width = this.getWidth();
int height = this.getHeight();
if(image.getWidth() > image.getHeight()) {
height = (int)((width * image.getHeight()) / image.getWidth());
}
else {
width = (int) ((height * image.getWidth()) / image.getHeight());
}
g2d.drawImage(image, 0,50, width, height, this);
}
}
}
+114
View File
@@ -0,0 +1,114 @@
package com.imegumii.ui;
import org.apache.commons.io.FileUtils;
import javax.imageio.ImageIO;
import javax.swing.*;
import javax.swing.border.Border;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
import java.awt.image.BufferedImage;
/**
* Created by kenny on 8-6-2017.
*/
public class GraphPanel extends JPanel {
private String name;
private File imageLocation;
private String export;
public String getName()
{
return name;
}
public GraphPanel(String name, File f, String export)
{
super(new BorderLayout());
this.name = name;
this.imageLocation = f;
this.export = export;
this.add(new GraphImagePanel(imageLocation), BorderLayout.CENTER);
JPanel buttonPanel = new JPanel();
buttonPanel.setLayout(new BoxLayout(buttonPanel, BoxLayout.LINE_AXIS));
buttonPanel.setBackground(Color.WHITE);
buttonPanel.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createMatteBorder(1, 0, 0, 0, Color.BLACK), BorderFactory.createEmptyBorder(10, 10, 10, 10)));
JButton closeTab = new JButton("Close");
closeTab.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
TabPanel.Instance().remove(GraphPanel.this);
}
});
JButton exportData = new JButton("Export raw data");
exportData.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String filename = "raw." + name.toLowerCase() + ".dot";
File file = new File("export/" + filename);
if(file.exists()){
int reply = JOptionPane.showConfirmDialog(null, "The file " + filename + "already exists. Do you want to overwrite it?", "File exists", JOptionPane.YES_NO_OPTION);
if (reply != JOptionPane.YES_OPTION) {
StatusPanel.Instance().setStatus("Cancelled export");
return;
}
}
PrintWriter w = null;
try {
w = new PrintWriter(file);
w.print(export);
w.flush();
w.close();
StatusPanel.Instance().setStatus("Success: " + filename);
} catch (FileNotFoundException e1) {
StatusPanel.Instance().setStatus("Something went wrong while exporting the file");
}
}
});
JButton exportImage = new JButton("Export image");
exportImage.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String filename = "img." + name.toLowerCase() + ".png";
File file = new File("export/" + filename);
if(file.exists()){
int reply = JOptionPane.showConfirmDialog(null, "The file " + filename + "already exists. Do you want to overwrite it?", "File exists", JOptionPane.YES_NO_OPTION);
if (reply != JOptionPane.YES_OPTION) {
StatusPanel.Instance().setStatus("Cancelled export");
return;
}
}
try {
FileUtils.copyFile(f, file);
StatusPanel.Instance().setStatus("Success: " + filename);
} catch (IOException e1) {
StatusPanel.Instance().setStatus("Something went wrong while exporting the file");
}
}
});
buttonPanel.add(Box.createHorizontalGlue());
buttonPanel.add(exportImage);
buttonPanel.add(Box.createRigidArea(new Dimension(10,0)));
buttonPanel.add(exportData);
buttonPanel.add(Box.createRigidArea(new Dimension(10,0)));
buttonPanel.add(closeTab);
this.add(buttonPanel, BorderLayout.SOUTH);
}
}
-47
View File
@@ -1,47 +0,0 @@
package com.imegumii.ui;
import javax.imageio.ImageIO;
import javax.swing.*;
import java.io.*;
import java.awt.image.BufferedImage;
/**
* Created by kenny on 8-6-2017.
*/
public class ImagePanel extends JPanel {
private static ImagePanel panel;
public static ImagePanel Instance()
{
if(panel == null)
panel = new ImagePanel();
return panel;
}
private ImageIcon image;
private JLabel label;
private ImagePanel()
{
super();
image = new ImageIcon();
label = new JLabel();
label.setIcon(image);
this.add(label);
}
public void setImage(File f)
{
try {
BufferedImage bfimg = ImageIO.read(f);
image = new ImageIcon(bfimg);
label.setIcon(image);
} catch (IOException e) {
e.printStackTrace();
}
}
}
+148
View File
@@ -0,0 +1,148 @@
package com.imegumii.ui;
import com.imegumii.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.util.ArrayList;
/**
* Created by kenny on 8-6-2017.
*/
public class InputPanel extends JPanel {
private static InputPanel panel;
public static InputPanel Instance()
{
if(panel == null)
panel = new InputPanel();
return panel;
}
private InputPanel()
{
super(new BorderLayout());
JPanel topPanel = new JPanel();
topPanel.setLayout(new BoxLayout(topPanel, BoxLayout.LINE_AXIS));
topPanel.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
topPanel.add(new JLabel("Regular expression"));
topPanel.add(Box.createRigidArea(new Dimension(10,0)));
JTextField text = new JTextField();
topPanel.add(text);
topPanel.add(Box.createRigidArea(new Dimension(10, 0)));
JButton button = new JButton("Generate");
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
TabPanel.Instance().addGraph("DFA: En", new File("images/en.png"), "Dit is nog meer rauwe en data");
}
});
topPanel.add(button);
topPanel.add(Box.createHorizontalGlue());
this.add(topPanel, BorderLayout.NORTH);
//------------
//BOTTOM PANEL
//-----------
JPanel bottomPanel = new JPanel();
bottomPanel.setLayout(new BoxLayout(bottomPanel, BoxLayout.LINE_AXIS));
bottomPanel.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
bottomPanel.add(new JLabel("File"));
bottomPanel.add(Box.createRigidArea(new Dimension(10,0)));
JComboBox<String> fileCombo = new JComboBox<String>(findFiles());
bottomPanel.add(fileCombo);
bottomPanel.add(Box.createRigidArea(new Dimension(10, 0)));
JButton fileButton = new JButton("Generate");
fileButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
StatusPanel.Instance().setStatus("Parsing file", 10);
String file = (String)fileCombo.getSelectedItem();
String name = file.replaceAll(".dot", "");
Importable p = FileParser.read(file);
if(p.type == Importable.Type.NDFA)
{
StatusPanel.Instance().setStatus("Generating image for NDFA", 60);
NDFA<String> ndfa = (NDFA<String>)p;
Graph.generateImage(ndfa, name);
TabPanel.Instance().addGraph("NDFA: " + name, new File("images/" + name + ".png"), ndfa.getTransitions());
}
if(p.type == Importable.Type.DFA)
{
StatusPanel.Instance().setStatus("Generating image for DFA", 60);
DFA<String> dfa = (DFA<String>)p;
Graph.generateImage(dfa, name);
TabPanel.Instance().addGraph("DFA: " + name, new File("images/" + name + ".png"), dfa.getTransitions());
}
if(p.type == Importable.Type.REGEX)
{
StatusPanel.Instance().setStatus("Converting REGEX to NDFA", 40);
RegExp regex = (RegExp) p;
NDFA<String> ndfa = ThompsonConverter.convert(regex);
StatusPanel.Instance().setStatus("Generating image for NDFA", 70);
Graph.generateImage(ndfa, name);
TabPanel.Instance().addGraph("REGEX: " + name, new File("images/" + name + ".png"), ndfa.getTransitions());
}
StatusPanel.Instance().setStatus("Done", 100);
}
});
bottomPanel.add(fileButton);
bottomPanel.add(Box.createHorizontalGlue());
this.add(bottomPanel, BorderLayout.SOUTH);
}
private String[] findFiles()
{
File folder = new File("input");
File[] listOfFiles = folder.listFiles();
ArrayList<String> fileNames = new ArrayList<String>();
for (int i = 0; i < listOfFiles.length; i++) {
if (listOfFiles[i].isFile() && listOfFiles[i].getName().endsWith(".dot")) {
fileNames.add(listOfFiles[i].getName());
}
}
String[] files = new String[fileNames.size()];
files = fileNames.toArray(files);
return files;
}
}
-49
View File
@@ -1,49 +0,0 @@
package com.imegumii.ui;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
/**
* Created by kenny on 8-6-2017.
*/
public class RegexPanel extends JPanel {
private static RegexPanel panel;
public static RegexPanel Instance()
{
if(panel == null)
panel = new RegexPanel();
return panel;
}
private RegexPanel()
{
super();
this.setLayout(new BoxLayout(this, BoxLayout.LINE_AXIS));
this.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
JTextField text = new JTextField();
this.add(text);
this.add(Box.createRigidArea(new Dimension(10, 0)));
JButton button = new JButton("Generate");
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
ImagePanel.Instance().setImage(new File("images/en.png"));
System.out.println("Refreshing image");
}
});
this.add(button);
this.add(Box.createHorizontalGlue());
}
}
+61
View File
@@ -0,0 +1,61 @@
package com.imegumii.ui;
import javax.swing.*;
import java.awt.*;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;
/**
* Created by kenny on 9-6-2017.
*/
public class StatusPanel extends JPanel {
private static StatusPanel panel;
private JLabel status;
private JProgressBar progress;
public static StatusPanel Instance()
{
if(panel == null)
panel = new StatusPanel();
return panel;
}
public StatusPanel()
{
super();
this.setLayout(new BoxLayout(this, BoxLayout.LINE_AXIS));
this.setBackground(Color.LIGHT_GRAY);
this.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
progress = new JProgressBar();
progress.setValue(0);
progress.setPreferredSize(new Dimension(250, 0));
status = new JLabel("Application ready");
this.add(progress);
this.add(Box.createHorizontalGlue());
this.add(status);
}
public void setStatus(String text, int progress)
{
this.progress.setValue(progress);
this.status.setText(text);
}
public void setStatus(int progress)
{
this.progress.setValue(progress);
}
public void setStatus(String text)
{
this.status.setText(text);
}
}
+38
View File
@@ -0,0 +1,38 @@
package com.imegumii.ui;
import javax.swing.*;
import java.awt.*;
import java.io.*;
/**
* Created by kenny on 9-6-2017.
*/
public class TabPanel extends JTabbedPane {
private static TabPanel panel;
public static TabPanel Instance()
{
if(panel == null)
panel = new TabPanel();
return panel;
}
private TabPanel()
{
super();
}
public void addGraph(String name, File image, String rawData)
{
this.addTab(name, new GraphPanel(name, image, rawData));
this.setSelectedIndex(this.getTabCount() - 1);
}
public void removeGraph(GraphPanel component)
{
this.remove(component);
}
}