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

Binary #834

Merged
merged 10 commits into from
Jul 9, 2024
11 changes: 4 additions & 7 deletions src/main/java/edu/rpi/legup/history/AutoCaseRuleCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,10 @@ public void executeCommand() {

TreeNode node = (TreeNode) selection.getFirstSelection().getTreeElement();
if (caseTrans.isEmpty()) {
List<Board> cases =
caseRule.getCases(caseBoard.getBaseBoard(), elementView.getPuzzleElement());
List<Board> cases = caseRule.getCases(caseBoard.getBaseBoard(), elementView.getPuzzleElement());
for (Board board : cases) {
final TreeTransition transition = (TreeTransition) tree.addTreeElement(node);
board.setModifiable(false);
//board.setModifiable(false);
transition.setBoard(board);
transition.setRule(caseRule);
transition.setSelection(elementView.getPuzzleElement().copy());
Expand Down Expand Up @@ -111,13 +110,11 @@ public String getErrorString() {
return "The selected data element is not pickable with this case rule.";
}

if (caseRule.getCases(caseBoard.getBaseBoard(), elementView.getPuzzleElement()).size()
== 0) {
if (caseRule.getCases(caseBoard.getBaseBoard(), elementView.getPuzzleElement()).size() == 0) {
return "The selection must produce at least one case";
}

int numberOfCaseRules =
caseRule.getCases(caseBoard.getBaseBoard(), elementView.getPuzzleElement()).size();
int numberOfCaseRules = caseRule.getCases(caseBoard.getBaseBoard(), elementView.getPuzzleElement()).size();
System.out.println("Number of cases:" + numberOfCaseRules);
if (numberOfCaseRules > caseRule.MAX_CASES) {
return "The selection can produce a max of " + caseRule.MAX_CASES + " cases";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,16 +39,15 @@ public void executeCommand() {
}

for (TreeElementView selectedView : selectedViews) {
System.out.println("DELETED");
TreeElement element = selectedView.getTreeElement();
tree.removeTreeElement(element);
puzzle.notifyTreeListeners(listener -> listener.onTreeElementRemoved(element));
}

final TreeViewSelection newSelection = new TreeViewSelection(newSelectedView);
puzzle.notifyBoardListeners(
listener -> listener.onTreeElementChanged(newSelectedView.getTreeElement()));
puzzle.notifyTreeListeners(
(ITreeListener listener) -> listener.onTreeSelectionChanged(newSelection));
puzzle.notifyBoardListeners(listener -> listener.onTreeElementChanged(newSelectedView.getTreeElement()));
puzzle.notifyTreeListeners((ITreeListener listener) -> listener.onTreeSelectionChanged(newSelection));
}

/**
Expand Down
10 changes: 5 additions & 5 deletions src/main/java/edu/rpi/legup/history/PuzzleCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,13 @@ public final String getError() {
*/
public abstract String getErrorString();

/** Executes an command */
/** Executes a command */
public abstract void executeCommand();

/** Undoes an command */
/** Undoes a command */
public abstract void undoCommand();

/** Redoes an command */
/** Redoes a command */
public void redoCommand() {
if (state == CommandState.UNDOED) {
executeCommand();
Expand All @@ -68,7 +68,7 @@ public void redoCommand() {
}
}

/** Undoes an command */
/** Undoes a command */
@Override
public final void undo() {
if (state == CommandState.EXECUTED || state == CommandState.REDOED) {
Expand All @@ -79,7 +79,7 @@ public final void undo() {
}
}

/** Redoes an command */
/** Redoes a command */
public final void redo() {
if (state == CommandState.UNDOED) {
redoCommand();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,12 @@ public void executeCommand() {
final TreeElement finalTreeElement;
if (firstSelectedView.getType() == TreeElementType.NODE) {
TreeNodeView nodeView = (TreeNodeView) firstSelectedView;
finalTreeElement = nodeView.getChildrenViews().get(0).getTreeElement();
if (!nodeView.getChildrenViews().isEmpty()) {
finalTreeElement = nodeView.getChildrenViews().get(0).getTreeElement();
}
else {
finalTreeElement = null;
}
} else {
TreeTransitionView transitionView = (TreeTransitionView) firstSelectedView;
if (transitionView.getChildView() != null) {
Expand Down
93 changes: 89 additions & 4 deletions src/main/java/edu/rpi/legup/history/ValidateDirectRuleCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,15 @@
import edu.rpi.legup.model.rules.Rule;
import edu.rpi.legup.model.tree.*;
import edu.rpi.legup.ui.proofeditorui.treeview.*;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ValidateDirectRuleCommand extends PuzzleCommand {
private static final Logger LOGGER = LogManager.getLogger(History.class.getName());
private TreeViewSelection selection;

private Map<TreeElement, Rule> oldRules;
Expand All @@ -30,7 +34,60 @@ public ValidateDirectRuleCommand(TreeViewSelection selection, DirectRule rule) {
this.addNode = new HashMap<>();
}

/** Executes an command */
// /** Executes a command */
// @Override
// public void executeCommand() {
// Tree tree = GameBoardFacade.getInstance().getTree();
// TreeView treeView = GameBoardFacade.getInstance().getLegupUI().getTreePanel().getTreeView();
// Puzzle puzzle = GameBoardFacade.getInstance().getPuzzleModule();
// final TreeViewSelection newSelection = new TreeViewSelection();
//
// List<TreeElementView> selectedViews = selection.getSelectedViews();
// int count = 1;
// for (TreeElementView selectedView : selectedViews) {
// System.out.println(count);
// count++;
// TreeElement element = selectedView.getTreeElement();
// TreeTransitionView transitionView;
// if (element.getType() == TreeElementType.NODE) {
// TreeNodeView nodeView = (TreeNodeView) selectedView;
// transitionView = nodeView.getChildrenViews().get(0);
// } else {
// transitionView = (TreeTransitionView) selectedView;
// }
// TreeTransition transition = transitionView.getTreeElement();
//
// oldRules.put(transition, transition.getRule());
// transition.setRule(newRule);
//
// TreeNode childNode = transition.getChildNode();
// if (childNode == null) {
// childNode = addNode.get(transition);
// if (childNode == null) {
// childNode = (TreeNode) tree.addTreeElement(transition);
// addNode.put(transition, childNode);
// } else {
// tree.addTreeElement(transition, childNode);
// }
//
// final TreeNode finalNode = childNode;
// puzzle.notifyTreeListeners(listener -> listener.onTreeElementAdded(finalNode));
// }
// newSelection.addToSelection(treeView.getElementView(childNode));
// }
// TreeElementView firstSelectedView = selection.getFirstSelection();
// final TreeElement finalTreeElement;
// if (firstSelectedView.getType() == TreeElementType.NODE) {
// TreeNodeView nodeView = (TreeNodeView) firstSelectedView;
// finalTreeElement = nodeView.getChildrenViews().get(0).getTreeElement();
// } else {
// TreeTransitionView transitionView = (TreeTransitionView) firstSelectedView;
// finalTreeElement = transitionView.getChildView().getTreeElement();
// }
// puzzle.notifyBoardListeners(listener -> listener.onTreeElementChanged(finalTreeElement));
// puzzle.notifyTreeListeners(listener -> listener.onTreeSelectionChanged(newSelection));
// }
/** Executes a command */
@Override
public void executeCommand() {
Tree tree = GameBoardFacade.getInstance().getTree();
Expand All @@ -42,14 +99,15 @@ public void executeCommand() {
for (TreeElementView selectedView : selectedViews) {
TreeElement element = selectedView.getTreeElement();
TreeTransitionView transitionView;

if (element.getType() == TreeElementType.NODE) {
TreeNodeView nodeView = (TreeNodeView) selectedView;
transitionView = nodeView.getChildrenViews().get(0);
} else {
transitionView = (TreeTransitionView) selectedView;
}
TreeTransition transition = transitionView.getTreeElement();

TreeTransition transition = transitionView.getTreeElement();
oldRules.put(transition, transition.getRule());
transition.setRule(newRule);

Expand All @@ -66,21 +124,46 @@ public void executeCommand() {
final TreeNode finalNode = childNode;
puzzle.notifyTreeListeners(listener -> listener.onTreeElementAdded(finalNode));
}
newSelection.addToSelection(treeView.getElementView(childNode));

TreeElementView childView = treeView.getElementView(childNode);
if (childView == null) {
LOGGER.error("Child view is null for child node: " + childNode);
continue;
}
newSelection.addToSelection(childView);
}

TreeElementView firstSelectedView = selection.getFirstSelection();

final TreeElement finalTreeElement;
if (firstSelectedView.getType() == TreeElementType.NODE) {
TreeNodeView nodeView = (TreeNodeView) firstSelectedView;
if (nodeView.getChildrenViews().isEmpty()) {
LOGGER.error("NodeView has no children views");
return;
}
finalTreeElement = nodeView.getChildrenViews().get(0).getTreeElement();
} else {
TreeTransitionView transitionView = (TreeTransitionView) firstSelectedView;
TreeNodeView childView = transitionView.getChildView();
if (childView == null) {
LOGGER.error("Child view is null for transition view: " + transitionView);
TreeNode childNode = transitionView.getTreeElement().getChildNode();
childView = (TreeNodeView) treeView.getElementView(childNode);
transitionView.setChildView(childView);
}
TreeTransition transition = transitionView.getTreeElement();
if (transition.getParents().get(0).getChildren().isEmpty()) {
transition.getParents().get(0).addChild(transition);
}
finalTreeElement = transitionView.getChildView().getTreeElement();
}

puzzle.notifyBoardListeners(listener -> listener.onTreeElementChanged(finalTreeElement));
puzzle.notifyTreeListeners(listener -> listener.onTreeSelectionChanged(newSelection));
}


/**
* Gets the reason why the command cannot be executed
*
Expand Down Expand Up @@ -110,7 +193,7 @@ public String getErrorString() {
return null;
}

/** Undoes an command */
/** Undoes a command */
@Override
public void undoCommand() {
Tree tree = GameBoardFacade.getInstance().getTree();
Expand All @@ -124,8 +207,10 @@ public void undoCommand() {
transitionView = nodeView.getChildrenViews().get(0);
} else {
transitionView = (TreeTransitionView) selectedView;

}
TreeTransition transition = transitionView.getTreeElement();

transition.setRule(oldRules.get(transition));

if (addNode.get(transition) != null) {
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/edu/rpi/legup/model/gameboard/Board.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ public Board(int size) {
* @return equivalent puzzleElement on this board
*/
public PuzzleElement getPuzzleElement(PuzzleElement puzzleElement) {
if (puzzleElement == null) {
return null;
}
int index = puzzleElement.getIndex();
return index < puzzleElements.size() ? puzzleElements.get(index) : null;
}
Expand Down
29 changes: 28 additions & 1 deletion src/main/java/edu/rpi/legup/model/tree/Tree.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public TreeElement addTreeElement(TreeElement element) {
} else {
TreeTransition transition = (TreeTransition) element;
Board copyBoard = transition.board.copy();
copyBoard.setModifiable(false);
copyBoard.setModifiable(true);
return addTreeElement(transition, new TreeNode(copyBoard));
}
}
Expand All @@ -63,17 +63,44 @@ public TreeElement addTreeElement(TreeTransition transition, TreeNode treeNode)
return treeNode;
}

// public void removeTreeElement(TreeElement element) {
// if (element.getType() == TreeElementType.NODE) {
// TreeNode node = (TreeNode) element;
// node.getParent().setChildNode(null);
// } else {
// TreeTransition transition = (TreeTransition) element;
// System.out.println("DELETED CHILD");
// transition.getParents().forEach(n -> n.removeChild(transition));
// transition.getParents().get(0).getChildren().forEach(TreeTransition::reverify);
// }
// }

public void removeTreeElement(TreeElement element) {
if (element.getType() == TreeElementType.NODE) {
TreeNode node = (TreeNode) element;

// Output when node has children
if (!node.getChildren().isEmpty()) {
System.out.println("Deleting children of node: " + node);
for (TreeTransition child : new ArrayList<>(node.getChildren())) {
removeTreeElement(child);
}
}

node.getParent().setChildNode(null);

System.out.println("Deleted node: " + node);
} else {
TreeTransition transition = (TreeTransition) element;
System.out.println("Deleted arrow: " + transition);

transition.getParents().forEach(n -> n.removeChild(transition));
transition.getParents().get(0).getChildren().forEach(TreeTransition::reverify);
}
}



/**
* Determines if the tree is valid by checking whether this tree puzzleElement and all
* descendants of this tree puzzleElement is justified and justified correctly
Expand Down
Loading
Loading