Skip to content

Commit

Permalink
Merge pull request #62 from Bram-Hub/truth-table_issue57_and-elim-bug
Browse files Browse the repository at this point in the history
Quick fix for Issue #57
  • Loading branch information
charlestian23 authored Dec 25, 2021
2 parents 940b5b6 + f580651 commit 9126642
Show file tree
Hide file tree
Showing 9 changed files with 72 additions and 33 deletions.
Binary file modified legup-update/build/libs/legup-update-2.0.jar
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public ShortTruthTableCell(Point location){

//Getters

public ShortTruthTableStatement getStatementRefference() {
public ShortTruthTableStatement getStatementReference() {
return statement;
}

Expand Down Expand Up @@ -67,7 +67,7 @@ public boolean isAssigned(){

//Setters

void setStatementRefference(ShortTruthTableStatement statement){
void setStatementReference(ShortTruthTableStatement statement){
this.statement = statement;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ private ShortTruthTableBoard generateBoard(List<List<ShortTruthTableCell>> allCe
//for a cell to exist at (x, y), it must be a valid row and within the statment length
if(y%2==0 && x < statements.get(statementIndex).getLength()) {
cell = allCells.get(statementIndex).get(x);
System.out.println("Importer: check cell statement ref: "+cell.getStatementRefference());
System.out.println("Importer: check cell statement ref: "+cell.getStatementReference());
}else{
//if it is not a valid cell space, add a NOT_IN_PLAY cell
cell = new ShortTruthTableCell(' ', ShortTruthTableCellType.NOT_IN_PLAY, new Point(x, y));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import java.awt.Point;
import java.util.List;
import java.util.ArrayList;
import java.util.Arrays;


public class ShortTruthTableStatement extends PuzzleElement<String>{
Expand Down Expand Up @@ -52,7 +51,7 @@ private ShortTruthTableStatement(String statement, ShortTruthTableStatement pare
//construct the cell for this node in the tree
cell = cells.get(index);
//give the cell a reference back to this statement
cell.setStatementRefference(this);
cell.setStatementReference(this);

//get the strings on either side of this char in the string rep
String left = statement.substring(0, index);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,8 @@

import edu.rpi.legup.puzzle.shorttruthtable.ShortTruthTableBoard;
import edu.rpi.legup.puzzle.shorttruthtable.ShortTruthTableCell;
import edu.rpi.legup.puzzle.shorttruthtable.ShortTruthTableCellType;
import edu.rpi.legup.puzzle.shorttruthtable.ShortTruthTableStatement;
import edu.rpi.legup.model.rules.ContradictionRule;

import java.awt.*;
import java.util.List;

public abstract class BasicRule_Generic extends BasicRule {

final ContradictionRule correspondingContradictionRule;
Expand Down Expand Up @@ -42,7 +37,7 @@ public String checkRuleRawAt(TreeTransition transition, PuzzleElement element){
//check that it is assigned to the right value
ShortTruthTableBoard originalBoard = (ShortTruthTableBoard) transition.getParents().get(0).getBoard();

//Use this board to check what would happen if the cell what the oppisite value
//Use this board to check what would happen if the cell what the opposite value
ShortTruthTableBoard testBoard = originalBoard.copy();
((ShortTruthTableCell) testBoard.getPuzzleElement(element)).setType(cell.getType().getNegation());

Expand All @@ -51,18 +46,62 @@ public String checkRuleRawAt(TreeTransition transition, PuzzleElement element){

//if elimination, check the parent
if(this.eliminationRule){
checkElement = cell.getStatementRefference().getParentStatement().getCell();
System.out.println("Is an elimination rule");
checkElement = cell.getStatementReference().getParentStatement().getCell();
}

//see if there is a contradiction
if (this.eliminationRule)
System.out.println("Parent check contradiction START");
String checkContradiction = correspondingContradictionRule.checkContradictionAt(testBoard, checkElement);
if (this.eliminationRule)
{
System.out.println("Parent check contradiction END");
System.out.println("Parent contradiction: " + checkContradiction);
}

//if there is a contradition when the modified element is negated, then the basic riule must be true
if(checkContradiction==null)
return null;

//if there is a contradiction when the modified element is negated, then the basic rule must be true
if(checkContradiction==null && !eliminationRule)
{
return null;
}
// if it's an elimination rule, check if the original case was also invalid
else if (checkContradiction == null && eliminationRule)
{
String checkOriginalContradiction = correspondingContradictionRule.checkContradictionAt(originalBoard, checkElement);
if (checkOriginalContradiction == null)
return "Invalid use of " + this.ruleName;
else
return null;
}
return "Negated Contradiction Failed: "+checkContradiction;

// if (this.eliminationRule)
// {
// System.out.println("Elimination rule check entered");
// // If the rule is an elimination rule, we can check if the statement contains a contradiction. If it does
// // contain a contradiction, then we know that the rule must be false
//
// String checkContradiction = correspondingContradictionRule.checkContradictionAt(originalBoard, checkElement);
// System.out.println("checkContradiction: " + checkContradiction);
// if (checkContradiction == null) // original board contains a contradiction: this is bad!
// return "This is not a valid use of " + this.ruleName + "!";
// return null;
// }
// else
// {
// // If the rule is not an elimination rule, we can check to see if negating the modified cell will create
// // a contradiction
//
// // Use the original board to check what would happen if the cell what the opposite value
// ShortTruthTableBoard testBoard = originalBoard.copy();
// ((ShortTruthTableCell) testBoard.getPuzzleElement(element)).setType(cell.getType().getNegation());
// String checkContradiction = correspondingContradictionRule.checkContradictionAt(testBoard, checkElement);
// if (checkContradiction == null) // modified board contains a contradiction: this is good!
// return null;
// return "Negated Contradiction Failed: "+checkContradiction;
// }
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ public class BasicRuleAndElimination extends BasicRule_GenericElimination {

public BasicRuleAndElimination() {
super("And", new ContradictionRuleAnd());
System.out.println("and eliminatio constructor");
//System.out.println("and elimination constructor");
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
import edu.rpi.legup.model.gameboard.Board;
import edu.rpi.legup.model.gameboard.CaseBoard;
import edu.rpi.legup.model.gameboard.PuzzleElement;
import edu.rpi.legup.model.rules.CaseRule;
import edu.rpi.legup.model.tree.TreeTransition;

import edu.rpi.legup.puzzle.shorttruthtable.ShortTruthTableBoard;
import edu.rpi.legup.puzzle.shorttruthtable.ShortTruthTableCell;
Expand All @@ -14,7 +12,6 @@


import java.util.ArrayList;
import java.util.List;

public abstract class CaseRule_GenericStatement extends CaseRule_Generic {

Expand Down Expand Up @@ -63,10 +60,10 @@ public CaseBoard getCaseBoard(Board board) {
//the statement must be assigned with unassigned sub-statements
if(!cell.getType().isTrueOrFalse()) continue;
System.out.println(" Operation is known");
if(cell.getStatementRefference().getRightStatement().getCell().getType().isTrueOrFalse()) continue;
if(cell.getStatementReference().getRightStatement().getCell().getType().isTrueOrFalse()) continue;
System.out.println(" right side is unknown");
if(this.operation != ShortTruthTableOperation.NOT &&
cell.getStatementRefference().getRightStatement().getCell().getType().isTrueOrFalse()) continue;
cell.getStatementReference().getRightStatement().getCell().getType().isTrueOrFalse()) continue;
System.out.println(" left side is unknown");

System.out.println(" Valid choice");
Expand Down Expand Up @@ -113,7 +110,7 @@ private ArrayList<Board> getCasesFromCell(ShortTruthTableBoard board, PuzzleElem

//get the statement of the square that was selected
ShortTruthTableCell cell = b.getCellFromElement(puzzleElement);
ShortTruthTableStatement statement = cell.getStatementRefference();
ShortTruthTableStatement statement = cell.getStatementReference();

//modify its children
//avoid error if it is a NOT statement
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ public ContradictionRuleAnd(){
new ShortTruthTableCellType[][] {
{n, T, F},
{F, T, n},
// {F, T, F},
{T, F, T},
}
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,8 @@
import edu.rpi.legup.puzzle.shorttruthtable.ShortTruthTableCell;
import edu.rpi.legup.puzzle.shorttruthtable.ShortTruthTableCellType;
import edu.rpi.legup.puzzle.shorttruthtable.ShortTruthTableStatement;
import edu.rpi.legup.puzzle.shorttruthtable.ShortTruthTableOperation;

import java.util.Set;
import java.util.Arrays;
import java.util.Iterator;


public abstract class ContradictionRule_GenericStatement extends ContradictionRule{
Expand All @@ -36,16 +33,21 @@ public ContradictionRule_GenericStatement(String ruleName, String description, S


@Override
public String checkContradictionAt(Board puzzleBoard, PuzzleElement puzzleElement) {
public String checkContradictionAt(Board puzzleBoard, PuzzleElement operatorPuzzleElement) {

//cast the board toa shortTruthTableBoard
//cast the board to a shortTruthTableBoard
ShortTruthTableBoard board = (ShortTruthTableBoard) puzzleBoard;

//get the cell that contradicts another cell in the board
ShortTruthTableCell cell = board.getCellFromElement(puzzleElement);
ShortTruthTableStatement statement = cell.getStatementRefference();
ShortTruthTableCell cell = board.getCellFromElement(operatorPuzzleElement);
ShortTruthTableStatement statement = cell.getStatementReference();
// ShortTruthTableStatement parentStatement = statement.getParentStatement();
System.out.println("Statement: " + statement);


//must be the correct statement
System.out.println("Symbol: " + cell.getSymbol());

if(cell.getSymbol() != this.operationSymbol)
return "This cell does not contain the correct operation";

Expand All @@ -58,10 +60,11 @@ public String checkContradictionAt(Board puzzleBoard, PuzzleElement puzzleElemen
//get the pattern for this sub-statement
ShortTruthTableCellType[] testPattern = statement.getCellTypePattern();

//if the board pattern matches any contradiction patter, it is a valid contradiction
//if the board pattern matches any contradiction pattern, it is a valid contradiction
System.out.println("Name: " + this.ruleName);
System.out.println("Testing pattern: "+Arrays.toString(testPattern));
for(ShortTruthTableCellType[] pattern : contradictionPatterns){
System.out.println("compareing to: "+Arrays.toString(pattern));
System.out.println("Comparing to: "+Arrays.toString(pattern));
boolean matches = true;
for(int i = 0; i<3; i++){
//null means that part does not affect the statement
Expand All @@ -74,12 +77,12 @@ public String checkContradictionAt(Board puzzleBoard, PuzzleElement puzzleElemen
}
//if testPattern matches one of the valid contradiction patterns, the contradiction is correct
if(matches){
System.out.println("This is a valid contradiction: matches pat: "+pattern);
System.out.println("This is a valid contradiction: matches pat: "+Arrays.toString(pattern));
return null;
}
}

System.out.println("not patterns match. There is not a contradiction");
System.out.println("No patterns match. There is not a contradiction");
return "This cell does not match any contradiction pattern for this rule";

}
Expand Down

0 comments on commit 9126642

Please sign in to comment.