From a07bdef2945f910fbfbbb649c9056671bc4485e6 Mon Sep 17 00:00:00 2001 From: Lukas Petervary Date: Fri, 22 Nov 2024 17:08:11 -0500 Subject: [PATCH] puzzle type searching in autograder, and fixed puzzle exportation from puzzle editor --- src/main/java/edu/rpi/legup/model/Puzzle.java | 16 +++++ .../edu/rpi/legup/model/PuzzleExporter.java | 2 +- .../edu/rpi/legup/model/PuzzleImporter.java | 2 +- src/main/java/edu/rpi/legup/ui/HomePanel.java | 59 ++++++++++++++----- .../edu/rpi/legup/ui/PuzzleEditorPanel.java | 1 + 5 files changed, 64 insertions(+), 16 deletions(-) diff --git a/src/main/java/edu/rpi/legup/model/Puzzle.java b/src/main/java/edu/rpi/legup/model/Puzzle.java index 24eb88eaf..8fb615d1f 100644 --- a/src/main/java/edu/rpi/legup/model/Puzzle.java +++ b/src/main/java/edu/rpi/legup/model/Puzzle.java @@ -381,6 +381,22 @@ public void removeDirectRule(DirectRule rule) { directRules.remove(rule); } + /** + * Accessor method for the puzzle UUID + * @return returns the puzzle UUID tag + */ + public String getTag() { + return tag; + } + + /** + * Modifier method to override the puzzle persistent UUID + * @param tag String to overwrite the current puzzle UUID + */ + public void setTag(String tag) { + this.tag = tag; + } + /** * Gets the list of contradiction rules * diff --git a/src/main/java/edu/rpi/legup/model/PuzzleExporter.java b/src/main/java/edu/rpi/legup/model/PuzzleExporter.java index 563624fe7..dc8fa14a5 100644 --- a/src/main/java/edu/rpi/legup/model/PuzzleExporter.java +++ b/src/main/java/edu/rpi/legup/model/PuzzleExporter.java @@ -103,7 +103,7 @@ public void exportPuzzle(String fileName) throws ExportFileException { newDocument.appendChild(legupElement); org.w3c.dom.Element puzzleElement = newDocument.createElement("puzzle"); - String idStr = puzzle.tag.isEmpty() ? fileName.substring(fileName.lastIndexOf("\\") + 1) : puzzle.tag; + String idStr = puzzle.getTag().isEmpty() ? fileName.substring(fileName.lastIndexOf("\\") + 1) : puzzle.getTag(); puzzleElement.setAttribute("tag", idStr); puzzleElement.setAttribute("name", puzzle.getName()); legupElement.appendChild(puzzleElement); diff --git a/src/main/java/edu/rpi/legup/model/PuzzleImporter.java b/src/main/java/edu/rpi/legup/model/PuzzleImporter.java index 6246f964f..adc510b79 100644 --- a/src/main/java/edu/rpi/legup/model/PuzzleImporter.java +++ b/src/main/java/edu/rpi/legup/model/PuzzleImporter.java @@ -82,7 +82,7 @@ public void initializePuzzle(Node node) throws InvalidFileFormatException { boolean initProof = false; String tag = puzzleElement.getAttribute("tag"); - this.puzzle.tag = !tag.isEmpty() ? tag : "generic.import.untagged"; + this.puzzle.setTag(!tag.isEmpty() ? tag : "generic.import.untagged"); NodeList childNodes = puzzleElement.getChildNodes(); for (int i = 0; i < childNodes.getLength(); i++) { diff --git a/src/main/java/edu/rpi/legup/ui/HomePanel.java b/src/main/java/edu/rpi/legup/ui/HomePanel.java index 8754cf7be..a2c280c45 100644 --- a/src/main/java/edu/rpi/legup/ui/HomePanel.java +++ b/src/main/java/edu/rpi/legup/ui/HomePanel.java @@ -27,7 +27,6 @@ import org.apache.logging.log4j.Logger; import org.w3c.dom.Document; import org.w3c.dom.Element; -import org.w3c.dom.Node; import org.w3c.dom.NodeList; /** @@ -38,6 +37,7 @@ public class HomePanel extends LegupPanel { private static final Logger LOGGER = LogManager.getLogger(HomePanel.class.getName()); private static final ArrayList _tagsToGrade = new ArrayList<>(); + private static final ArrayList _typesToGrade = new ArrayList<>(); private LegupUI legupUI; private JFrame frame; private JButton[] buttons; @@ -171,15 +171,13 @@ private void initButtons() { this.buttons[2].setFocusPainted(false); this.buttons[2].setHorizontalTextPosition(AbstractButton.CENTER); this.buttons[2].setVerticalTextPosition(AbstractButton.BOTTOM); - - this.buttons[2].addActionListener( - e -> openBatchGraderMenu()); + this.buttons[2].addActionListener(e -> openBatchGraderMenu()); } /** Initializes screen for autograder options */ public void openBatchGraderMenu() { JDialog batchGraderOptions = new JDialog(frame, "Batch Grader Options", true); - batchGraderOptions.setSize(450, 150); + batchGraderOptions.setSize(450, 200); batchGraderOptions.setLayout(new BorderLayout()); // Create a panel for the directory selection part @@ -197,13 +195,29 @@ public void openBatchGraderMenu() { JLabel puzzleIdLabel = new JLabel("Puzzle IDs:"); JTextField puzzleIdField = new JTextField(10); puzzleIdField.setEnabled(false); - JCheckBox gradeAllCheckbox = new JCheckBox("Grade All"); - gradeAllCheckbox.setSelected(true); + JCheckBox gradeAllIDsCheckbox = new JCheckBox("Grade All IDs"); + gradeAllIDsCheckbox.setSelected(true); puzzleIdPanel.add(puzzleIdLabel); puzzleIdPanel.add(puzzleIdField); - puzzleIdPanel.add(gradeAllCheckbox); - batchGraderOptions.add(puzzleIdPanel, BorderLayout.CENTER); + puzzleIdPanel.add(gradeAllIDsCheckbox); + + // Create a panel for the puzzle tags label, text field, and checkbox + JPanel puzzleTypePanel = new JPanel(new FlowLayout(FlowLayout.CENTER)); + JLabel puzzleTypeLabel = new JLabel("Puzzle Types:"); + JTextField puzzleTypeField = new JTextField(10); + puzzleTypeField.setEnabled(false); + JCheckBox gradeAllTagsCheckbox = new JCheckBox("Grade All Types"); + gradeAllTagsCheckbox.setSelected(true); + + puzzleTypePanel.add(puzzleTypeLabel); + puzzleTypePanel.add(puzzleTypeField); + puzzleTypePanel.add(gradeAllTagsCheckbox); + + JPanel batchGraderConstraints = new JPanel(new FlowLayout(FlowLayout.CENTER)); + batchGraderConstraints.add(puzzleIdPanel); + batchGraderConstraints.add(puzzleTypePanel); + batchGraderOptions.add(batchGraderConstraints, BorderLayout.CENTER); // Create a save button at the bottom JButton gradeButton = new JButton("Grade"); @@ -214,7 +228,8 @@ public void openBatchGraderMenu() { batchGraderOptions.add(gradePanel, BorderLayout.SOUTH); // Action listeners for the buttons - gradeAllCheckbox.addActionListener(e -> puzzleIdField.setEnabled(!gradeAllCheckbox.isSelected())); + gradeAllIDsCheckbox.addActionListener(e -> puzzleIdField.setEnabled(!gradeAllIDsCheckbox.isSelected())); + gradeAllTagsCheckbox.addActionListener(e -> puzzleTypeField.setEnabled(!gradeAllTagsCheckbox.isSelected())); browseButton.addActionListener(e -> { JFileChooser folderBrowser = new JFileChooser(); @@ -233,17 +248,27 @@ public void openBatchGraderMenu() { gradeButton.addActionListener(e -> { String directoryPath = directoryField.getText(); - String puzzles = puzzleIdField.getText(); + String puzzleTags = puzzleIdField.getText(); + String puzzleTypes = puzzleTypeField.getText(); _tagsToGrade.clear(); - if (!puzzles.isEmpty()) { + if (!puzzleTags.isEmpty()) { Pattern pattern = Pattern.compile("\"(.*?)\""); - Matcher matcher = pattern.matcher(puzzles); + Matcher matcher = pattern.matcher(puzzleTags); while (matcher.find()) { _tagsToGrade.add(matcher.group(1)); } } + _typesToGrade.clear(); + if (!puzzleTypes.isEmpty()) { + Pattern pattern = Pattern.compile("\"(.*?)\""); + Matcher matcher = pattern.matcher(puzzleTypes); + + while (matcher.find()) { + _typesToGrade.add(matcher.group(1)); + } + } try { File dir = new File(directoryPath); @@ -415,16 +440,22 @@ private void recursive_parser(File folder, BufferedWriter writer, String path) Document doc; if ( (doc = isxmlfile(fileEntry)) == null) { LOGGER.debug("{} is not a '.xml' file", fName); + writer.write(fName+",Not an xml file!\n"); continue; } NodeList puzzleNodes = doc.getElementsByTagName("puzzle"); Element puzzleElement = (Element) puzzleNodes.item(0); String puzzleTag = puzzleElement.getAttribute("tag"); - if (!_tagsToGrade.isEmpty() && !_tagsToGrade.contains(puzzleTag)) { + if (!_tagsToGrade.isEmpty() && _tagsToGrade.stream().noneMatch(puzzleTag::contains)) { LOGGER.debug("'{}' is not graded with tag '{}'", puzzleElement.getAttribute("name"), puzzleTag); continue; } + String puzzleType = puzzleElement.getAttribute("name"); + if (!_typesToGrade.isEmpty() && _typesToGrade.stream().noneMatch(puzzleType::contains)) { + LOGGER.debug("'{}' is not graded with type '{}'", puzzleElement.getAttribute("name"), puzzleType); + continue; + } // write data path = folder.getAbsolutePath(); diff --git a/src/main/java/edu/rpi/legup/ui/PuzzleEditorPanel.java b/src/main/java/edu/rpi/legup/ui/PuzzleEditorPanel.java index 7c2ba06ff..dcf658bb6 100644 --- a/src/main/java/edu/rpi/legup/ui/PuzzleEditorPanel.java +++ b/src/main/java/edu/rpi/legup/ui/PuzzleEditorPanel.java @@ -767,6 +767,7 @@ private String savePuzzle() { if (exporter == null) { throw new ExportFileException("Puzzle exporter null"); } + puzzle.setTag(path.substring(path.lastIndexOf(File.separator) + 1)); exporter.exportPuzzle(path); } catch (ExportFileException e) { e.printStackTrace();