org.drools.examples.sudoku.sudokuValidator.drl Maven / Gradle / Ivy
The newest version!
package org.drools.examples.sudoku
import org.drools.examples.sudoku.rules.PossibleCellValue;
import org.drools.examples.sudoku.rules.ResolvedCellValue;
global java.util.List issues;
/**
* This rule package defines a set of rules that can be used to validate whether a valid Sudoku solution
* has been produced or not. It expects to have objects of type PossibleCellValue and ResolvedCellValue
* inserted into the working memory and will then check that only a single ResolvedCellValue exists
* in a 9x9 Sudoku grid for each row, column and 3x3 zone.
*
* Any issues discovered are inserted as Strings into the issues global.
*
* @author Pete Bennett
* @author Michael Frandsen
* @version $Revision: 1.1 $
*/
// TODO: I would like to have a rule that checks there are exactly SudokuGridModel.NUM_ROWS x SudokuGridModel.NUM_COLS = 81
// ResolvedCellValues in the working memory
/**
rule "There should be exactly 81 ResolvedCellValues in working memory"
when
# count number of ResovledCellValues in working memory and check it is exactly 81
then
issues.add("There are not exactly 81 ResolvedCellValues in working memory");
end
**/
/**
* Checks for any remaining PossibleCellValues in the working memory, such values can be an artifact
* of the solving process. By the time a solution is reached all these objects should have been removed.
*/
rule "There should not be any PossibleCellValues left in working memory"
when
# Run against all PossibleCellValues in the working memory
$possible : PossibleCellValue()
then
issues.add("A PossibleCellValue of "+$possible.getValue()+" remains at ("+$possible.getRow()+","+$possible.getCol()+")");
end
/**
* Checks for two ResolvedCellValues with both the same value and the same row property
*/
rule "There should not be two ResolvedCellValues with the same value in the same row"
when
# Matches all ResolvedCellValues in the working memory and stores the row and the value for each in two local variables
$resolved1 : ResolvedCellValue($resolved1Row : row, $resolved1Value : value)
# Matches any other ResolvedCellValues that have the same row and the same value as these two local variables
$resolved2 : ResolvedCellValue (row == $resolved1Row, value == $resolved1Value)
then
issues.add("There are two cells on the same row with the same value at ("+$resolved1.getRow()+","+$resolved1.getCol()+") and ("+$resolved2.getRow()+","+$resolved2.getCol()+")");
end
/**
* Checks for two ResolvedCellValues with both the same value and the same column property
*/
rule "There should not be two ResolvedCellValues with the same value in the same column"
when
# Matches all ResolvedCellValues in the working memory and stores the column and the value for each in two local variables
$resolved1 : ResolvedCellValue($resolved1Col : col, $resolved1Value : value)
# Matches any other ResolvedCellValues that have the same column and the same value as these two local variables
$resolved2 : ResolvedCellValue (col == $resolved1Col, value == $resolved1Value)
then
issues.add("There are two cells on the same column with the same value at ("+$resolved1.getRow()+","+$resolved1.getCol()+") and ("+$resolved2.getRow()+","+$resolved2.getCol()+")");
end
/**
* Checks for two ResolvedCellValues with both the same value and the same zone property
*/
rule "There should not be two ResolvedCellValues with the same value in the same zone"
when
# Matches all ResolvedCellValues in the working memory and stores the zone and the value for each in two local variables
$resolved1 : ResolvedCellValue($resolved1Zone : zone, $resolved1Value : value)
# Matches any other ResolvedCellValues that have the same zone and the same value as these two local variables
$resolved2 : ResolvedCellValue (zone == $resolved1Zone, value == $resolved1Value)
then
issues.add("There are two cells in the same zone with the same value at ("+$resolved1.getRow()+","+$resolved1.getCol()+") and ("+$resolved2.getRow()+","+$resolved2.getCol()+")");
end
© 2015 - 2025 Weber Informatics LLC | Privacy Policy