diff --git a/src/main/java/nl/han/aim/asd/veilveilig/modules/infrastructure/compiler/Compiler.java b/src/main/java/nl/han/aim/asd/veilveilig/modules/infrastructure/compiler/Compiler.java index dfc96828ce6e76925031148923ffe69d2c4426f4..05a6f55b942f313d11ca58137caf3fe78e30e7ee 100644 --- a/src/main/java/nl/han/aim/asd/veilveilig/modules/infrastructure/compiler/Compiler.java +++ b/src/main/java/nl/han/aim/asd/veilveilig/modules/infrastructure/compiler/Compiler.java @@ -15,14 +15,6 @@ public class Compiler implements ICompiler { @Inject private Pipeline pipeline; -// /** -// * The constructor of the compiler. -// * This immediately creates an instance ofe the pipeline. -// */ -// public Compiler() { -// pipeline = new Pipeline(); -// } - /** * This function compiles written strategies by running them through the pipeline. diff --git a/src/main/java/nl/han/aim/asd/veilveilig/modules/infrastructure/compiler/gui/ASTPane.java b/src/main/java/nl/han/aim/asd/veilveilig/modules/infrastructure/compiler/gui/ASTPane.java deleted file mode 100644 index d46852052a6a31d6583d825ec040f0ea3387812f..0000000000000000000000000000000000000000 --- a/src/main/java/nl/han/aim/asd/veilveilig/modules/infrastructure/compiler/gui/ASTPane.java +++ /dev/null @@ -1,64 +0,0 @@ -package nl.han.aim.asd.veilveilig.modules.infrastructure.compiler.gui; - -import javafx.geometry.Insets; -import javafx.scene.control.Label; -import javafx.scene.control.TreeCell; -import javafx.scene.control.TreeItem; -import javafx.scene.control.TreeView; -import javafx.scene.layout.BorderPane; -import nl.han.aim.asd.veilveilig.modules.infrastructure.compiler.ast.AST; -import nl.han.aim.asd.veilveilig.modules.infrastructure.compiler.ast.ASTNode; - -public class ASTPane extends BorderPane { - - private TreeView<ASTNode> content; - private Label title; - - public ASTPane () { - super(); - - title = new Label("Internal (AST):"); - content = new TreeView<ASTNode>(); - content.setCellFactory(treeview -> new TreeCell<ASTNode>() { - @Override - public void updateItem(ASTNode item, boolean empty) { - super.updateItem(item, empty); - - getStyleClass().removeAll("error"); - - if(empty) { - setText(""); - } else { - setText(item.getNodeLabel()); - if(item.hasError()) { - getStyleClass().add("error"); - } - } - } - }); - title.setPadding(new Insets(5, 5, 5, 5)); - - setTop(title); - setCenter(content); - setMinWidth(200); - setPrefWidth(400); - } - /** - * Updates the panes based on the current content of the AST - * @param ast - */ - public void update(AST ast) { - content.setRoot(astNodeToTreeItem(ast.root)); - - } - private TreeItem<ASTNode> astNodeToTreeItem(ASTNode astNode) { - - TreeItem<ASTNode> tvNode = new TreeItem<ASTNode>(astNode); - tvNode.setExpanded(true); - - for(ASTNode child : astNode.getChildren()) { - tvNode.getChildren().add(astNodeToTreeItem(child)); - } - return tvNode; - } -} diff --git a/src/main/java/nl/han/aim/asd/veilveilig/modules/infrastructure/compiler/gui/FeedbackPane.java b/src/main/java/nl/han/aim/asd/veilveilig/modules/infrastructure/compiler/gui/FeedbackPane.java deleted file mode 100644 index 16d8a2281a0fddc3828ef5c42d8d463f2cbcd67a..0000000000000000000000000000000000000000 --- a/src/main/java/nl/han/aim/asd/veilveilig/modules/infrastructure/compiler/gui/FeedbackPane.java +++ /dev/null @@ -1,19 +0,0 @@ -package nl.han.aim.asd.veilveilig.modules.infrastructure.compiler.gui; - - -import javafx.scene.control.TextArea; - -@SuppressWarnings("restriction") -public class FeedbackPane extends TextArea { - public FeedbackPane() { - super(); - - setEditable(false); - } - public void clear() { - this.setText(""); - } - public void addLine(String line) { - this.setText( this.getText() + "\n" + line); - } -} diff --git a/src/main/java/nl/han/aim/asd/veilveilig/modules/infrastructure/compiler/gui/InputPane.java b/src/main/java/nl/han/aim/asd/veilveilig/modules/infrastructure/compiler/gui/InputPane.java deleted file mode 100644 index 771cd67a8d90e903318c8cf518a03df21a37c6d9..0000000000000000000000000000000000000000 --- a/src/main/java/nl/han/aim/asd/veilveilig/modules/infrastructure/compiler/gui/InputPane.java +++ /dev/null @@ -1,42 +0,0 @@ -package nl.han.aim.asd.veilveilig.modules.infrastructure.compiler.gui; - - - - -import javafx.geometry.Insets; -import javafx.scene.layout.BorderPane; - -import java.io.File; -import java.io.IOException; -import java.nio.charset.Charset; -import java.nio.file.Files; - -@SuppressWarnings("restriction") - public class InputPane extends BorderPane { - private javafx.scene.control.TextArea content; - private javafx.scene.control.Label title; - - public InputPane() { - super(); - - title = new javafx.scene.control.Label("Input (Lango):"); - content = new javafx.scene.control.TextArea(); - title.setPadding(new Insets(5, 5, 5, 5)); - - this.setTop(title); - this.setCenter(content); - } - public void setText(String text) { - this.content.setText(text); - } - public void setText(File file) { - try { - this.setText(new String(Files.readAllBytes(file.toPath()), Charset.defaultCharset())); - } catch (IOException e) { - System.err.println(e); - } - } - public String getText() { - return content.getText(); - } -} diff --git a/src/main/java/nl/han/aim/asd/veilveilig/modules/infrastructure/compiler/gui/Main.java b/src/main/java/nl/han/aim/asd/veilveilig/modules/infrastructure/compiler/gui/Main.java deleted file mode 100644 index b2ca589786a9fe6e3dde37f13804727e8eb45f9a..0000000000000000000000000000000000000000 --- a/src/main/java/nl/han/aim/asd/veilveilig/modules/infrastructure/compiler/gui/Main.java +++ /dev/null @@ -1,9 +0,0 @@ -package nl.han.aim.asd.veilveilig.modules.infrastructure.compiler.gui; - -import java.io.IOException; - -public class Main { - public static void main(String[] args) throws IOException { - MainGui.launch(MainGui.class,args); - } -} diff --git a/src/main/java/nl/han/aim/asd/veilveilig/modules/infrastructure/compiler/gui/MainGui.java b/src/main/java/nl/han/aim/asd/veilveilig/modules/infrastructure/compiler/gui/MainGui.java deleted file mode 100644 index 51362901eca77d31fa391ec07ed73e6a672ae231..0000000000000000000000000000000000000000 --- a/src/main/java/nl/han/aim/asd/veilveilig/modules/infrastructure/compiler/gui/MainGui.java +++ /dev/null @@ -1,269 +0,0 @@ -package nl.han.aim.asd.veilveilig.modules.infrastructure.compiler.gui; - -import com.google.common.io.Resources; - -import javafx.application.Application; -import javafx.application.Platform; -import javafx.event.ActionEvent; -import javafx.event.EventHandler; -import javafx.geometry.Insets; -import javafx.scene.Scene; -import javafx.scene.control.*; -import javafx.scene.layout.BorderPane; -import javafx.scene.layout.HBox; -import javafx.stage.FileChooser; -import javafx.stage.Stage; -import nl.han.aim.asd.veilveilig.modules.infrastructure.compiler.NumberCheck; -import nl.han.aim.asd.veilveilig.modules.infrastructure.compiler.Pipeline; -import nl.han.aim.asd.veilveilig.modules.infrastructure.compiler.SpellCheck; - -import java.io.File; -import java.io.IOException; -import java.net.URL; -import java.nio.charset.Charset; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.List; - -//We use this google library, because it makes life so much easier when -//reading the examples lango files as packaged resource - -@SuppressWarnings("restriction") -public class MainGui extends Application { - - private final String title = "Lango Tool April 2023, version 1 (Gepiraat)"; - //Example files (for menu) - private final List<String> examples = addStrategies(); - - //UI Components - private InputPane inputPane; - private ASTPane astPane; - private OutputPane outputPane; - private FeedbackPane feedbackPane; - - //Toolbar buttons - private Button parseButton; - private Button checkButton; - private Button generateButton; - - //Model - private Pipeline pipeline; - - - @Override - public void start(Stage stage) { - //Setup pipeline - pipeline = new Pipeline(); - - //Setup UI - stage.setTitle(title); - - inputPane = new InputPane(); - astPane = new ASTPane(); - outputPane = new OutputPane(); - feedbackPane = new FeedbackPane(); - - //Reference for the callbacks - final MainGui me = this; - - //Create buttons - parseButton = new Button("Parse"); - parseButton.setOnAction(new EventHandler<ActionEvent>() { - @Override - public void handle(ActionEvent e) { - try { - me.parse(); - } catch (IOException ex) { - ex.printStackTrace(); - } - } - }); - - checkButton = new Button("Check"); - checkButton.setOnAction(new EventHandler<ActionEvent>() { - @Override - public void handle(ActionEvent e) { - me.check(); - } - }); - - generateButton = new Button("Generate"); - generateButton.setOnAction(new EventHandler<ActionEvent>() { - @Override - public void handle(ActionEvent e) { - me.generate(); - } - }); - - //Create menus - MenuBar menuBar = new MenuBar(); - - Menu fileMenu = new Menu("File"); - MenuItem loadInput = new MenuItem("Load input Lango..."); - loadInput.setOnAction(new EventHandler<ActionEvent>() { - public void handle(ActionEvent e) { - FileChooser fileChooser = new FileChooser(); - fileChooser.setTitle("Open input Lango..."); - fileChooser.getExtensionFilters().add(new FileChooser.ExtensionFilter("Lango", "*.lango")); - - File file = fileChooser.showOpenDialog(stage); - if (file != null) { - inputPane.setText(file); - } - } - }); - Menu exampleFilesMenu = new Menu("Load example Lango"); - - //We load them as resources straight from the application's jar - for(String level: examples) { - - MenuItem levelItem = new MenuItem(level); - levelItem.setOnAction(new EventHandler<ActionEvent>() { - public void handle(ActionEvent e) { - try { - ClassLoader classLoader = MainGui.class.getClassLoader(); - URL url = classLoader.getResource(level); - inputPane.setText(Resources.toString(url, Charset.defaultCharset())); - } catch (IOException ioe) { - feedbackPane.addLine(ioe.toString()); - } - } - }); - exampleFilesMenu.getItems().add(levelItem); - } - - MenuItem saveOutput = new MenuItem("Save generated JSON..."); - saveOutput.setOnAction(new EventHandler<ActionEvent>() { - @Override - public void handle(ActionEvent e) { - //Create file dialog - FileChooser fileChooser = new FileChooser(); - fileChooser.setTitle("Save generated JSON..."); - fileChooser.setInitialFileName("output.json"); - - File file = fileChooser.showSaveDialog(stage); - if (file != null) { - outputPane.writeToFile(file); - } - } - }); - - MenuItem quit = new MenuItem("Quit"); - quit.setOnAction(new EventHandler<ActionEvent>() { - public void handle(ActionEvent e) { - Platform.exit(); - } - }); - - fileMenu.getItems().addAll(loadInput, exampleFilesMenu, new SeparatorMenuItem(), - saveOutput, new SeparatorMenuItem(), quit); - menuBar.getMenus().addAll(fileMenu); - - //Layout components - BorderPane main = new BorderPane(); - SplitPane center = new SplitPane(); - center.getItems().addAll(inputPane, astPane, outputPane); - - //Toolbar - HBox toolbar = new HBox(); - toolbar.setPadding(new Insets(5, 5, 5, 5)); - toolbar.getChildren().addAll(new Label("Pipeline: "), parseButton, checkButton, generateButton); - updateToolbar(); - - BorderPane bottom = new BorderPane(); - bottom.setPadding(new Insets(10, 10, 10, 10)); - bottom.setTop(toolbar); - bottom.setCenter(feedbackPane); - - main.setTop(menuBar); - main.setCenter(center); - main.setBottom(bottom); - - Scene scene = new Scene(main, 1200, 600); - scene.getStylesheets().add("gui.css"); - - stage.setScene(scene); - stage.show(); - } - - private void clear() { - feedbackPane.clear(); - pipeline.clearErrors(); - } - - private void parse() throws IOException { - clear(); - feedbackPane.addLine("Parsing..."); - spellCheckAndParse(); - for(String e : pipeline.getErrors()) { - feedbackPane.addLine(e); - } - if (pipeline.isParsed()) { - feedbackPane.addLine("Parsing succeeded"); - } - astPane.update(pipeline.getAST()); - updateToolbar(); - System.out.println(pipeline.getAST().toString()); - } - private void spellCheckAndParse() throws IOException{ - String text = inputPane.getText(); - StringBuilder newText = new StringBuilder(); - List<String> textList = Arrays.stream(text.split(" ")).toList(); - for (String word : textList) { - word = word.trim(); - if(word.length() < 3){ - newText.append(word); - } - else { - String newWord = SpellCheck.changeWord(word); - newText.append(NumberCheck.getNumberFromString(newWord)); - } - newText.append(" "); - } - pipeline.parser(newText.toString()); - } - - private void check() { - clear(); - pipeline.check(); - feedbackPane.addLine("Checking..."); - if (pipeline.check()) { - feedbackPane.addLine("AST is ok!"); - } else { - for (String e : pipeline.getErrors()) { - feedbackPane.addLine(e); - } - } - astPane.update(pipeline.getAST()); - updateToolbar(); - } - - private void generate() { - clear(); - feedbackPane.addLine("Generating output..."); - outputPane.setText(pipeline.generate()); - feedbackPane.addLine("Generating succeeded"); - updateToolbar(); - } - - private void updateToolbar() { - //Quick and ugly way... - checkButton.setDisable(true); - generateButton.setDisable(true); - - if (pipeline.isParsed()) { - checkButton.setDisable(false); - if (pipeline.isChecked()) { - generateButton.setDisable(false); - } - } - } - - private List<String> addStrategies() { - List<String> list = new ArrayList<>(); - for(int i =0; i < 12; i++) { - list.add("level"+i+".lango"); - } - return list; - } -} diff --git a/src/main/java/nl/han/aim/asd/veilveilig/modules/infrastructure/compiler/gui/OutputPane.java b/src/main/java/nl/han/aim/asd/veilveilig/modules/infrastructure/compiler/gui/OutputPane.java deleted file mode 100644 index 3b23da8e343aa0479543e469323d32dc964ae43f..0000000000000000000000000000000000000000 --- a/src/main/java/nl/han/aim/asd/veilveilig/modules/infrastructure/compiler/gui/OutputPane.java +++ /dev/null @@ -1,45 +0,0 @@ -package nl.han.aim.asd.veilveilig.modules.infrastructure.compiler.gui; - -import javafx.geometry.Insets; -import javafx.scene.control.Label; -import javafx.scene.control.TextArea; -import javafx.scene.layout.BorderPane; - -import java.io.File; -import java.io.FileOutputStream; -import java.io.PrintStream; - -@SuppressWarnings("restriction") -public class OutputPane extends BorderPane { - - private Label title; - private TextArea content; - - public OutputPane () { - super(); - - title = new Label("Output (JSON):"); - title.setPadding(new Insets(5, 5, 5, 5)); - - content = new TextArea(); - content.setEditable(false); - - setTop(title); - setCenter(content); - } - public void setText(String text) { - content.setText(text); - } - public String getText() { - return content.getText(); - } - public void writeToFile(File file) { - try { - PrintStream out = new PrintStream(new FileOutputStream(file)); - out.print(this.getText()); - out.close(); - } catch(Exception exception) { - System.err.println(exception); - } - } -}