Skip to content

Commit

Permalink
puzzle type searching in autograder, and fixed puzzle exportation fro…
Browse files Browse the repository at this point in the history
…m puzzle editor
  • Loading branch information
Lukas-Petervary committed Nov 22, 2024
1 parent d11b7ce commit a07bdef
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 16 deletions.
16 changes: 16 additions & 0 deletions src/main/java/edu/rpi/legup/model/Puzzle.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/edu/rpi/legup/model/PuzzleExporter.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/edu/rpi/legup/model/PuzzleImporter.java
Original file line number Diff line number Diff line change
Expand Up @@ -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++) {
Expand Down
59 changes: 45 additions & 14 deletions src/main/java/edu/rpi/legup/ui/HomePanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand All @@ -38,6 +37,7 @@
public class HomePanel extends LegupPanel {
private static final Logger LOGGER = LogManager.getLogger(HomePanel.class.getName());
private static final ArrayList<String> _tagsToGrade = new ArrayList<>();
private static final ArrayList<String> _typesToGrade = new ArrayList<>();
private LegupUI legupUI;
private JFrame frame;
private JButton[] buttons;
Expand Down Expand Up @@ -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
Expand All @@ -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");
Expand All @@ -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();
Expand All @@ -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);
Expand Down Expand Up @@ -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();
Expand Down
1 change: 1 addition & 0 deletions src/main/java/edu/rpi/legup/ui/PuzzleEditorPanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit a07bdef

Please sign in to comment.