Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prototype getRuleByID() #82

Merged
merged 4 commits into from
Mar 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions src/main/java/edu/rpi/legup/model/Puzzle.java
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,35 @@ public Rule getRuleByName(String name) {
return null;
}

/**
* Gets the rule using the specified name
*
* @param id name of the rule
* @return Rule
*/
public Rule getRuleByID(String id) {
for (Rule rule : basicRules) {
if (rule.getRuleID().equals(id)) {
return rule;
}
}
for (Rule rule : contradictionRules) {
if (rule.getRuleID().equals(id)) {
return rule;
}
}
for (Rule rule : caseRules) {
if (rule.getRuleID().equals(id)) {
return rule;
}
}
Rule mergeRule = new MergeRule();
if (mergeRule.getRuleID().equals(id)) {
return mergeRule;
}
return null;
}

/**
* Gets the current board
*
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/edu/rpi/legup/model/PuzzleExporter.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public void exportPuzzle(String fileName) throws ExportFileException {
Document newDocument = docBuilder.newDocument();

org.w3c.dom.Element legupElement = newDocument.createElement("Legup");
legupElement.setAttribute("version", "2.0.0");
legupElement.setAttribute("version", "3.0.0");
newDocument.appendChild(legupElement);

org.w3c.dom.Element puzzleElement = newDocument.createElement("puzzle");
Expand Down Expand Up @@ -112,6 +112,7 @@ protected Element createTreeElement(Document newDocument) {

if (transition.isJustified()) {
transElement.setAttribute("rule", transition.getRule().getRuleName());
transElement.setAttribute("rule_id", transition.getRule().getRuleID());
}

for (PuzzleElement data : transition.getBoard().getModifiedData()) {
Expand Down
11 changes: 6 additions & 5 deletions src/main/java/edu/rpi/legup/model/PuzzleImporter.java
Original file line number Diff line number Diff line change
Expand Up @@ -154,10 +154,10 @@ protected void createTree(Node node) throws InvalidFileFormatException {
String nodeId = treeNodeElement.getAttribute("id");
String isRoot = treeNodeElement.getAttribute("root");
if (nodeId.isEmpty()) {
throw new InvalidFileFormatException("Proof Tree construction error: cannot find node id");
throw new InvalidFileFormatException("Proof Tree construction error: cannot find node ID");
}
if (treeNodes.containsKey(nodeId)) {
throw new InvalidFileFormatException("Proof Tree construction error: duplicate tree node id found");
throw new InvalidFileFormatException("Proof Tree construction error: duplicate tree node ID found");
}
TreeNode treeNode = new TreeNode(puzzle.getCurrentBoard().copy());
if (isRoot.equalsIgnoreCase("true")) {
Expand Down Expand Up @@ -187,23 +187,24 @@ protected void createTree(Node node) throws InvalidFileFormatException {
treeNode.addChild(transition);
continue;
} else {
throw new InvalidFileFormatException("Proof Tree construction error: duplicate transition id found");
throw new InvalidFileFormatException("Proof Tree construction error: duplicate transition ID found");
}

}

String childId = trans.getAttribute("child");
String ruleName = trans.getAttribute("rule");
String ruleId = trans.getAttribute("rule_id");

TreeNode child = treeNodes.get(childId);

transition = new TreeTransition(treeNode, treeNode.getBoard().copy());

Rule rule;
if (!ruleName.isEmpty()) {
rule = puzzle.getRuleByName(ruleName);
rule = puzzle.getRuleByID(ruleId);
if (rule == null) {
throw new InvalidFileFormatException("Proof Tree construction error: could not find rule by name");
throw new InvalidFileFormatException("Proof Tree construction error: could not find rule by ID");
}
transition.setRule(rule);
}
Expand Down
9 changes: 9 additions & 0 deletions src/main/java/edu/rpi/legup/model/rules/Rule.java
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,15 @@ public String getRuleName() {
return ruleName;
}

/**
* Gets the name of the rule
*
* @return name of the rule
*/
public String getRuleID() {
return ruleID;
}

/**
* Sets the rule name
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public class CantReachWhiteContradictionRule extends ContradictionRule {

public CantReachWhiteContradictionRule() {
super("NURI-CONT-0002",
"Cant Reach white cell",
"Unreachables are Black",
"A white cell must be able to reach a white region",
"edu/rpi/legup/images/nurikabe/contradictions/CantReach.png");
}
Expand Down