Added popup for grammar
This commit is contained in:
@@ -14,11 +14,7 @@ public class GrammarConverter {
|
||||
for (Character symbol : automata.symbols) {
|
||||
SortedSet<String> bereikbaarVanafViaEpsilonBereikbareState = automata.statesBereikbaarVanaf(state, symbol);
|
||||
for (String s1 : bereikbaarVanafViaEpsilonBereikbareState) {
|
||||
if (automata.eindStates.contains(s1)) {
|
||||
canGoTo.add("" + symbol);
|
||||
} else {
|
||||
canGoTo.add("" + symbol + s1);
|
||||
}
|
||||
canGoTo.add("" + symbol + s1);
|
||||
}
|
||||
}
|
||||
return canGoTo;
|
||||
@@ -37,6 +33,7 @@ public class GrammarConverter {
|
||||
|
||||
SortedSet<String> bereikbaarVanafEpsilon = automata.statesBereikbaarVanaf(state, Transition.EPSILON);
|
||||
for (String s : bereikbaarVanafEpsilon) {
|
||||
canGoTo.add("" + Transition.EPSILON + s);
|
||||
SortedSet<String> bereikbaarVanafEpsilonViaSymbool = automata.statesBereikbaarVanaf(s, symbol);
|
||||
for (String s1 : bereikbaarVanafEpsilonViaSymbool) {
|
||||
canGoTo.add("" + symbol + s1);
|
||||
@@ -87,6 +84,16 @@ public class GrammarConverter {
|
||||
return other;
|
||||
}
|
||||
|
||||
public String printToString() {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sb.append(toString());
|
||||
sb.append("\n");
|
||||
if (below != null) {
|
||||
sb.append(below.printToString());
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
public void print() {
|
||||
System.out.println(state + " -> " + options);
|
||||
if (below != null) {
|
||||
|
||||
+17
-16
@@ -9,24 +9,25 @@ public class Main {
|
||||
|
||||
|
||||
public static void main(String[] args) {
|
||||
String s = "a*(aa+ |ba*b ) * (abba|baab|bbbb)+";
|
||||
|
||||
String s2 = "a(a+ | b*((a|b)+))";
|
||||
String s3 = "(a|b)+a";
|
||||
|
||||
String s4 = "a+((ab)*b|ab|(b)*bb)+(abba|baab)+";
|
||||
String s5 = "a(ab)*b(a|b)|ab|(b)*bb";
|
||||
String s6 = "(fuck)+";
|
||||
String s7 = "a((a|b)+(baab|abba))";
|
||||
|
||||
// NDFA<String> ndfa = (NDFA<String>) FileParser.read("ndfa1.dot");
|
||||
// ndfa.print();
|
||||
// new GrammarConverter().toGrammar(ndfa).print();
|
||||
|
||||
// String s = "a*(aa+ |ba*b ) * (abba|baab|bbbb)+";
|
||||
//
|
||||
// String s2 = "a(a+ | b*((a|b)+))";
|
||||
// String s3 = "(a|b)+a";
|
||||
//
|
||||
// String s4 = "a+((ab)*b|ab|(b)*bb)+(abba|baab)+";
|
||||
// String s5 = "a(ab)*b(a|b)|ab|(b)*bb";
|
||||
// String s6 = "(fuck)+";
|
||||
// String s7 = "a((a|b)+(baab|abba))";
|
||||
// String s8 = "(( (a|b)*(baab))+)banaan";
|
||||
//
|
||||
//// NDFA<String> ndfa = (NDFA<String>) FileParser.read("ndfa1.dot");
|
||||
//// ndfa.print();
|
||||
//// new GrammarConverter().toGrammar(ndfa).print();
|
||||
//
|
||||
// RegExp r = new RegExp();
|
||||
// String todo = s;
|
||||
// String todo = s8;
|
||||
// System.out.println(todo);
|
||||
// System.out.println(r.naarRegExp(todo).getTaal(4, 10));
|
||||
// System.out.println(r.naarRegExp(todo).getTaal(6, 10));
|
||||
|
||||
new Frame();
|
||||
}
|
||||
|
||||
@@ -12,9 +12,9 @@ public class Frame extends JFrame {
|
||||
{
|
||||
super("Automata");
|
||||
|
||||
this.setSize(700, 800);
|
||||
this.setSize(1000, 800);
|
||||
//this.setResizable(false);
|
||||
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
|
||||
|
||||
JPanel container = new JPanel(new BorderLayout());
|
||||
container.add(TabPanel.Instance(), BorderLayout.CENTER);
|
||||
|
||||
@@ -248,6 +248,8 @@ public class GraphPanel extends JPanel {
|
||||
}
|
||||
});
|
||||
|
||||
JButton showGrammar = new JButton("Show grammar");
|
||||
|
||||
buttonPanel.add(Box.createHorizontalGlue());
|
||||
|
||||
if(automata.type == Importable.Type.DFA) {
|
||||
@@ -257,6 +259,17 @@ public class GraphPanel extends JPanel {
|
||||
buttonPanel.add(Box.createRigidArea(new Dimension(10, 0)));
|
||||
}
|
||||
|
||||
if (automata.type == Importable.Type.NDFA) {
|
||||
showGrammar.addActionListener(e -> {
|
||||
BackgroundWorker.instance().addWorker(() -> {
|
||||
GrammarConverter c = new GrammarConverter();
|
||||
new TextPanel("Grammar", c.toGrammar((NDFA<String>) automata).printToString());
|
||||
});
|
||||
});
|
||||
buttonPanel.add(showGrammar);
|
||||
buttonPanel.add(Box.createRigidArea(new Dimension(10, 0)));
|
||||
}
|
||||
|
||||
buttonPanel.add(minimize2Button);
|
||||
buttonPanel.add(Box.createRigidArea(new Dimension(10, 0)));
|
||||
buttonPanel.add(minimizeButton);
|
||||
|
||||
@@ -8,12 +8,17 @@ import javax.swing.text.BadLocationException;
|
||||
import java.awt.*;
|
||||
import java.awt.event.ActionEvent;
|
||||
import java.awt.event.ActionListener;
|
||||
import java.awt.event.ItemEvent;
|
||||
import java.util.SortedSet;
|
||||
import java.util.TreeSet;
|
||||
|
||||
/**
|
||||
* Created by kenny on 12-6-2017.
|
||||
*/
|
||||
public class PopupFrame extends JFrame {
|
||||
|
||||
boolean selected = false;
|
||||
|
||||
public PopupFrame(String name, RegExp r)
|
||||
{
|
||||
super(name);
|
||||
@@ -53,6 +58,7 @@ public class PopupFrame extends JFrame {
|
||||
|
||||
JButton generateButton = new JButton("Generate language");
|
||||
// JButton cancelButton = new JButton("Cancel operation");
|
||||
JCheckBox checkBox = new JCheckBox("Negate");
|
||||
|
||||
JTextField acceptString = new JTextField();
|
||||
|
||||
@@ -62,13 +68,37 @@ public class PopupFrame extends JFrame {
|
||||
textArea.setText(""); // reset
|
||||
BackgroundWorker.instance().addWorker(() -> {
|
||||
long currentTime = System.currentTimeMillis();
|
||||
Taal t = r.getTaal(Integer.parseInt(operaties.getText()), Integer.parseInt(lengte.getText()));
|
||||
textArea.append("The language contains " + t.getSymbols().size() + " strings\n");
|
||||
// Taal t = r.getTaal(Integer.parseInt(operaties.getText()), Integer.parseInt(lengte.getText()));
|
||||
SortedSet<String> symbols = minimizedDfa.geefTaalTotLengte(Integer.parseInt(lengte.getText())).getSymbols();
|
||||
SortedSet<String> actuallyContains = new TreeSet<>();
|
||||
if (selected) {
|
||||
for (String symbol : symbols) {
|
||||
if (!minimizedDfa.accepteer(symbol)) {
|
||||
actuallyContains.add(symbol);
|
||||
}
|
||||
}
|
||||
textArea.append("The language does not contain " + actuallyContains.size() + " strings out of a max of " + symbols.size() + "\n");
|
||||
} else {
|
||||
for (String symbol : symbols) {
|
||||
if (minimizedDfa.accepteer(symbol)) {
|
||||
actuallyContains.add(symbol);
|
||||
}
|
||||
}
|
||||
textArea.append("The language contains " + actuallyContains.size() + " strings out of a max of " + symbols.size() + "\n");
|
||||
}
|
||||
textArea.append("It took " + (System.currentTimeMillis() - currentTime) + " milliseconds to generate and filter them\n");
|
||||
t.getSymbols().forEach(s -> textArea.append("The language contains: " + s + "\n"));
|
||||
actuallyContains.forEach(s -> textArea.append("String: " + s + "\n"));
|
||||
});
|
||||
});
|
||||
|
||||
checkBox.addItemListener(e -> {
|
||||
if (e.getStateChange() == ItemEvent.DESELECTED) {
|
||||
selected = false;
|
||||
} else {
|
||||
selected = true;
|
||||
}
|
||||
});
|
||||
|
||||
// cancelButton.addActionListener(e -> {
|
||||
// BackgroundWorker.instance().cancel();
|
||||
// });
|
||||
@@ -93,6 +123,9 @@ public class PopupFrame extends JFrame {
|
||||
generatePanel.add(generateButton);
|
||||
generatePanel.add(Box.createRigidArea(new Dimension(10,0)));
|
||||
|
||||
generatePanel.add(checkBox);
|
||||
generatePanel.add(Box.createRigidArea(new Dimension(10,0)));
|
||||
|
||||
// generatePanel.add(cancelButton);
|
||||
// generatePanel.add(Box.createRigidArea(new Dimension(10,0)));
|
||||
|
||||
|
||||
@@ -0,0 +1,24 @@
|
||||
package com.imegumii.ui;
|
||||
|
||||
import javax.swing.*;
|
||||
|
||||
/**
|
||||
* Created by imegumii on 13/06/2017.
|
||||
*/
|
||||
public class TextPanel extends JFrame {
|
||||
public TextPanel (String name, String text) {
|
||||
super(name);
|
||||
this.setSize(600, 600);
|
||||
//this.setResizable(false);
|
||||
this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
|
||||
|
||||
JTextArea textArea = new JTextArea();
|
||||
JScrollPane scrollPane = new JScrollPane(textArea);
|
||||
|
||||
textArea.append(text);
|
||||
|
||||
this.setContentPane(scrollPane);
|
||||
|
||||
this.setVisible(true);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user