Skip to content

Commit 4a88502

Browse files
committed
Added finding expression level with tests
Added expression paren simplifier with tests Added expression tree generation without tests yet Added GUI for importing boolean expressions
1 parent 06f1ce7 commit 4a88502

36 files changed

+1426
-17
lines changed

TODO

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ TODO: import as Canonical
5757
TO BE DONE:
5858

5959
TODO: minimization algorithm
60+
TODO: aggregate circuit DTO
6061

6162
MAYBE:
6263
TODO: ctrl-z, ctrl-shift-z - way too hard

build.gradle

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,30 +16,22 @@ plugins {
1616

1717
//Apply the jlink plugin to add support for creating custom runtime images
1818
id "org.beryx.jlink" version "2.19.0"
19+
20+
//Apply the ANTLR java parser generator plugin
21+
id 'antlr'
1922
}
2023

2124
repositories {
2225
// Use jcenter for resolving dependencies.
2326
jcenter()
2427
}
2528

26-
/*run {
27-
jvmArgs = {
28-
"--add-exports=javafx.controls/com.sun.javafx.scene.control.behavior=com.jfoenix",

29-
"--add-exports=javafx.controls/com.sun.javafx.scene.control=com.jfoenix",

30-
"--add-exports=javafx.base/com.sun.javafx.binding=com.jfoenix",
31-
"--add-exports=javafx.graphics/com.sun.javafx.stage=com.jfoenix",
32-
"--add-exports=javafx.controls/com.sun.javafx.scene.control.behavior=com.jfoenix"
33-
}
34-
*//*
 jvmArgs = [

35-
36-
]*//*
37-

}*/
38-
3929
dependencies {
4030
// This dependency is used by the application.
4131
implementation 'com.google.guava:guava:28.2-jre'
42-
implementation 'com.jfoenix:jfoenix:9.0.8'
32+
33+
// Java parser generator
34+
antlr "org.antlr:antlr4:4.7.2"
4335

4436
// Use JUnit test framework
4537
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.4.2'
@@ -57,6 +49,11 @@ dependencies {
5749

5850
}
5951

52+
generateGrammarSource {
53+
outputDirectory = file("src/main/java/LogicCircuitSimulator/BooleanExpressionParser/GrammarParser")
54+
arguments += ["-visitor", "-long-messages"]
55+
}
56+
6057
java {
6158
sourceCompatibility = JavaVersion.VERSION_14
6259
targetCompatibility = JavaVersion.VERSION_14

jfoenix-9.0.8.jar

-2.31 MB
Binary file not shown.

src/main/antlr/BooleanExpr.g4

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
grammar BooleanExpr;
2+
@header {
3+
package LogicCircuitSimulator.BooleanExpressionParser.GrammarParser;
4+
}
5+
6+
7+
BLANK: [ \t\r\n] -> skip;
8+
AND: 'AND';
9+
OR: 'OR';
10+
11+
LEFT_PAREN: '(';
12+
RIGHT_PAREN: ')';
13+
NOT: 'NOT';
14+
IDENTIFIER: [a-zA-Z]+;
15+
16+
booleanExpression : orExpression EOF
17+
;
18+
19+
orExpression : orExpression OR andExpression
20+
| andExpression
21+
;
22+
23+
24+
andExpression : andExpression AND valueExpression
25+
| valueExpression
26+
;
27+
28+
valueExpression : IDENTIFIER
29+
| NOT orExpression
30+
| LEFT_PAREN orExpression RIGHT_PAREN
31+
;
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package LogicCircuitSimulator.BooleanExpressionParser;
2+
3+
import LogicCircuitSimulator.FxGUI.CircuitGrid.FXMLController.SelectionDTO;
4+
5+
public interface BooleanExpressionParser{
6+
SelectionDTO parse(String expression);
7+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package LogicCircuitSimulator.BooleanExpressionParser.BooleanExpressionTree;
2+
3+
public class ExpressionNode {
4+
private ExpressionNode firstNode;
5+
private ExpressionNode secondNode;
6+
private String terminalText;
7+
private Operand operand;
8+
9+
public ExpressionNode getFirstNode() {
10+
return firstNode;
11+
}
12+
13+
public void setFirstNode(ExpressionNode firstNode) {
14+
this.firstNode = firstNode;
15+
}
16+
17+
public ExpressionNode getSecondNode() {
18+
return secondNode;
19+
}
20+
21+
public void setSecondNode(ExpressionNode secondNode) {
22+
this.secondNode = secondNode;
23+
}
24+
25+
public String getTerminalText() {
26+
return terminalText;
27+
}
28+
29+
public void setTerminalText(String terminalText) {
30+
this.terminalText = terminalText;
31+
}
32+
33+
public Operand getOperand() {
34+
return operand;
35+
}
36+
37+
public void setOperand(Operand operand) {
38+
this.operand = operand;
39+
}
40+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package LogicCircuitSimulator.BooleanExpressionParser.BooleanExpressionTree;
2+
3+
public interface ExpressionTreeGenerator {
4+
ExpressionNode generate(String expression);
5+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package LogicCircuitSimulator.BooleanExpressionParser.BooleanExpressionTree;
2+
3+
import LogicCircuitSimulator.BooleanExpressionParser.GrammarParser.BooleanExprBaseVisitor;
4+
import LogicCircuitSimulator.BooleanExpressionParser.GrammarParser.BooleanExprParser;
5+
import org.antlr.v4.runtime.tree.TerminalNode;
6+
7+
public class ExpressionTreeGeneratorVisitor extends BooleanExprBaseVisitor<ExpressionNode> {
8+
9+
@Override
10+
public ExpressionNode visitBooleanExpression(BooleanExprParser.BooleanExpressionContext ctx) {
11+
return visitOrExpression(ctx.orExpression());
12+
}
13+
14+
@Override
15+
public ExpressionNode visitOrExpression(BooleanExprParser.OrExpressionContext ctx) {
16+
if(ctx.OR() != null){
17+
ExpressionNode node = new ExpressionNode();
18+
node.setOperand(Operand.OR);
19+
ExpressionNode firstNode = visitOrExpression(ctx.orExpression());
20+
ExpressionNode secondNode = visitAndExpression(ctx.andExpression());
21+
node.setFirstNode(firstNode);
22+
node.setSecondNode(secondNode);
23+
return node;
24+
}
25+
else{
26+
return visitAndExpression(ctx.andExpression());
27+
}
28+
}
29+
30+
@Override
31+
public ExpressionNode visitAndExpression(BooleanExprParser.AndExpressionContext ctx) {
32+
if(ctx.AND() != null){
33+
ExpressionNode node = new ExpressionNode();
34+
node.setOperand(Operand.AND);
35+
ExpressionNode firstNode = visitAndExpression(ctx.andExpression());
36+
ExpressionNode secondNode = visitValueExpression(ctx.valueExpression());
37+
node.setFirstNode(firstNode);
38+
node.setSecondNode(secondNode);
39+
return node;
40+
}
41+
else{
42+
return visitValueExpression(ctx.valueExpression());
43+
}
44+
}
45+
46+
@Override
47+
public ExpressionNode visitValueExpression(BooleanExprParser.ValueExpressionContext ctx) {
48+
if(ctx.NOT() != null){
49+
ExpressionNode node = new ExpressionNode();
50+
node.setOperand(Operand.NOT);
51+
ExpressionNode firstNode = visitOrExpression(ctx.orExpression());
52+
node.setFirstNode(firstNode);
53+
return node;
54+
}
55+
else if(ctx.LEFT_PAREN() != null){
56+
ExpressionNode node = new ExpressionNode();
57+
ExpressionNode firstNode = visitOrExpression(ctx.orExpression());
58+
node.setFirstNode(firstNode);
59+
return node;
60+
}
61+
else{
62+
ExpressionNode node = new ExpressionNode();
63+
node.setTerminalText(ctx.getText());
64+
return node;
65+
}
66+
}
67+
68+
@Override
69+
public ExpressionNode visitTerminal(TerminalNode node) {
70+
ExpressionNode expressionNode = new ExpressionNode();
71+
expressionNode.setTerminalText(node.getText());
72+
return expressionNode;
73+
}
74+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package LogicCircuitSimulator.BooleanExpressionParser.BooleanExpressionTree;
2+
3+
public enum Operand{
4+
AND,
5+
OR,
6+
NOT
7+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package LogicCircuitSimulator.BooleanExpressionParser.BooleanExpressionTree;
2+
3+
import LogicCircuitSimulator.BooleanExpressionParser.GrammarParser.BooleanExprLexer;
4+
import LogicCircuitSimulator.BooleanExpressionParser.GrammarParser.BooleanExprParser;
5+
import org.antlr.v4.runtime.CharStreams;
6+
import org.antlr.v4.runtime.CommonTokenStream;
7+
import org.antlr.v4.runtime.tree.ParseTree;
8+
9+
public class SimpleExpressionTreeGenerator implements ExpressionTreeGenerator{
10+
@Override
11+
public ExpressionNode generate(String expression) {
12+
BooleanExprLexer lexer = new BooleanExprLexer(CharStreams.fromString(expression));
13+
BooleanExprParser parser = new BooleanExprParser(new CommonTokenStream(lexer));
14+
ParseTree tree = parser.booleanExpression();
15+
ExpressionTreeGeneratorVisitor treeGeneratorVisitor = new ExpressionTreeGeneratorVisitor();
16+
return treeGeneratorVisitor.visit(tree);
17+
}
18+
}

0 commit comments

Comments
 (0)