[jboss-svn-commits] JBL Code SVN: r14774 - labs/jbossrules/trunk/drools-examples/drools-examples-drl/src/main/rules/org/drools/examples/sudoku.

jboss-svn-commits at lists.jboss.org jboss-svn-commits at lists.jboss.org
Fri Aug 31 05:23:09 EDT 2007


Author: pete.bennett at jboss.com
Date: 2007-08-31 05:23:09 -0400 (Fri, 31 Aug 2007)
New Revision: 14774

Added:
   labs/jbossrules/trunk/drools-examples/drools-examples-drl/src/main/rules/org/drools/examples/sudoku/Sample.sdo
   labs/jbossrules/trunk/drools-examples/drools-examples-drl/src/main/rules/org/drools/examples/sudoku/sudokuSolver.drl
   labs/jbossrules/trunk/drools-examples/drools-examples-drl/src/main/rules/org/drools/examples/sudoku/sudokuValidator.drl
Removed:
   labs/jbossrules/trunk/drools-examples/drools-examples-drl/src/main/rules/org/drools/examples/sudoku/sudoku.drl
Log:


Added: labs/jbossrules/trunk/drools-examples/drools-examples-drl/src/main/rules/org/drools/examples/sudoku/Sample.sdo
===================================================================
--- labs/jbossrules/trunk/drools-examples/drools-examples-drl/src/main/rules/org/drools/examples/sudoku/Sample.sdo	                        (rev 0)
+++ labs/jbossrules/trunk/drools-examples/drools-examples-drl/src/main/rules/org/drools/examples/sudoku/Sample.sdo	2007-08-31 09:23:09 UTC (rev 14774)
@@ -0,0 +1,9 @@
+8 346  12
+7   8 64 
+  6  2  8
+37   9   
+18     36
+   8   75
+5  3  1  
+ 31 4   9
+92  583 4
\ No newline at end of file

Deleted: labs/jbossrules/trunk/drools-examples/drools-examples-drl/src/main/rules/org/drools/examples/sudoku/sudoku.drl
===================================================================
--- labs/jbossrules/trunk/drools-examples/drools-examples-drl/src/main/rules/org/drools/examples/sudoku/sudoku.drl	2007-08-31 09:22:44 UTC (rev 14773)
+++ labs/jbossrules/trunk/drools-examples/drools-examples-drl/src/main/rules/org/drools/examples/sudoku/sudoku.drl	2007-08-31 09:23:09 UTC (rev 14774)
@@ -1,168 +0,0 @@
-package org.drools.examples.sudoku
- 
-import org.drools.examples.sudoku.Field;
-import org.drools.examples.sudoku.PossibleValue;
-
-rule "Rule 1: If a field has the value 0, it is empty: remove it and insert the PossibleValues"
-	salience 100
-	when
-		$field : Field( value == 0, $row : row, $column : column, $zone : zone )
-	then
-		insert( new PossibleValue("1", $field ) );
-		insert( new PossibleValue("2", $field ) );
-		insert( new PossibleValue("3", $field ) );
-		insert( new PossibleValue("4", $field ) );
-		insert( new PossibleValue("5", $field ) );
-		insert( new PossibleValue("6", $field ) );
-		insert( new PossibleValue("7", $field ) );
-		insert( new PossibleValue("8", $field ) );
-		insert( new PossibleValue("9", $field ) );
-		retract( $field );
-		#System.out.println("Rule 1 fired.");
-end  
-
-rule "Rule 2: If there is one PossibleValue left at a certain position, it should be the Fields value"
-	salience 5
-	when
-		$pv : PossibleValue ( $row : row, $zone : zone, $column : column, $value : value )
-		not ( PossibleValue ( row == $row, zone == $zone, column == $column, value != $value ) )
-	then
-		insert( new Field( $value, $column, $row, $zone ) );
-		retract( $pv );
-		#System.out.println("Rule 2 fired.");
-end  
-
-
-rule "Rule 3: If there is a field with a value in a row, remove all PossibleValues with this value in this row"
-	salience 15
-	when		
-		$field2 : Field( $row : row, $value : value != 0 )
-		$possible : PossibleValue( row == $row, value == $value )
-	then
-		retract( $possible );
-		#System.out.println("Rule 3 fired.");
-end
-
-rule "Rule 4: If there is a field with a value in a column, remove all PossibleValues with this value in this column"
-	salience 15
-	when		
-		$field1 : Field( $column : column, $value : value != 0 )
-		$possible : PossibleValue( column == $column, value == $value )
-	then
-		retract( $possible );
-		#System.out.println("Rule 4 fired.");
-end
-
-rule "Rule 5: If there is a field with a value in a zone, remove all PossibleValues with this value in this zone"
-	salience 15
-	when		
-		$field1 : Field( $zone : zone, $value : value != 0 )
-		$possible : PossibleValue( zone == $zone, value == $value )
-	then
-		retract( $possible );
-		#System.out.println("Rule 5 fired.");
-end
-
-rule "Rule 6: For fields with a value remove all PossibleValues"
-	salience 250
-	when
-		Field( value != 0, $col : column, $row : row, $zone : zone )
-		$pv : PossibleValue( column == $col, row == $row, zone == $zone )
-	then
-		retract( $pv );
-		#System.out.println("Rule 6 fired.");
-end
-
-rule "Rule 7: If there is only one PossibleValue left in a zone, it must have the value of the field"
-	salience 4
-	when		
-		$pv : PossibleValue( $zone : zone, $value : value, $col : column, $row : row)
-		not (PossibleValue( zone == $zone, value == $value ))
-		not (Field( value == $value, zone == $zone) )
-		not (Field( value == $value, row == $row) )
-		not (Field( value == $value, column == $col) )
-	then
-		insert( new Field( $value, $col, $row, $zone ) );
-		retract( $pv );
-		#System.out.println("Rule 7 fired.");
-end
-
-rule "Rule 8: If there is only one PossibleValue left in a column, it must have the value of the field"
-	salience 4
-	when		
-		$pv1 : PossibleValue( $zone : zone, $value : value, $col : column, $row : row)
-		not (PossibleValue( column == $col, value == $value ))
-		not (Field( value == $value, zone == $zone) )
-		not (Field( value == $value, row == $row) )
-		not (Field( value == $value, column == $col) )
-	then
-		insert( new Field( $value, $col, $row, $zone ) );
-		retract( $pv1 );
-		#System.out.println("Rule 8 fired.");
-end
-
-rule "Rule 9: If there is only one PossibleValue left in a row, it must have the value of the field"
-	salience 4
-	when		
-		$pv : PossibleValue( $zone : zone, $value : value, $col : column, $row : row)
-		not (PossibleValue( row == $row, value == $value ))
-		not (Field( value == $value, zone == $zone) )
-		not (Field( value == $value, row == $row) )
-		not (Field( value == $value, column == $col) )
-	then
-		insert( new Field( $value, $col, $row, $zone ) );
-		retract( $pv );
-		#System.out.println("Rule 9 fired.");
-end
-
-rule "Rule 10: If there are two fields with only two possible values, remove the PossibleValues for the same Value in the rest of the zone"
-	salience 1
-	when
-		PossibleValue( $zone : zone, $val1 : value, $row1 : row, $col1 : column )
-		PossibleValue( zone == $zone, value == $val1, $row2 : row, $col2 : column != $col1)
-		PossibleValue( zone == $zone, row == $row1, column == $col1, $val2 : value )
-		PossibleValue( zone == $zone, row == $row2, column == $col2, value == $val2 )
-		not ( PossibleValue( zone == $zone, row == $row1, column == $col1, value != $val1, value != $val2 ) )
-		not ( PossibleValue( zone == $zone, row == $row2, column == $col2, value != $val1, value != $val2 ) )
-		$pv : PossibleValue( zone == $zone, value == $val1)
-	then
-		retract( $pv );
-		#System.out.println("Rule 10 fired.");
-end
-
-# This rule differs from the one above, that the 2nd PV should not be in the same row (above: not in the same column
-rule "Rule 11: If there are two fields with only two possible values, remove the PossibleValues for the same Value in the rest of the zone"
-	salience 1
-	when
-		PossibleValue( $zone : zone, $val1 : value, $row1 : row, $col1 : column )
-		PossibleValue( zone == $zone, value == $val1, $row2 : row != $row1, $col2 : column)
-		PossibleValue( zone == $zone, row == $row1, column == $col1, $val2 : value )
-		PossibleValue( zone == $zone, row == $row2, column == $col2, value == $val2 )
-		not ( PossibleValue( zone == $zone, row == $row1, column == $col1, value != $val1, value != $val2 ) )
-		not ( PossibleValue( zone == $zone, row == $row2, column == $col2, value != $val1, value != $val2 ) )
-		$pv : PossibleValue( zone == $zone, value == $val1)
-	then
-		retract( $pv );
-		#System.out.println("Rule 11 fired.");
-end
-
-##################################################
-# Rules not needed, just for debugging purpose:
-##################################################
-rule "log PossibleValues from certain row"
-	salience -25
-	when
-		$pos1 : PossibleValue ( row == 1, $value : value)
-	then
-		System.out.println("Left PV: " + $pos1.getRow() + $pos1.getColumn() + $pos1.getZone() + ":" + $pos1.getValue() );#+ $pos2.getValue());
-end
-
-
-
-rule "log all fields"
-	salience -20
-	when
-		$field : Field ( value == 0 )
-	then
-		System.out.println("left Field " + $field.toString());
-end
\ No newline at end of file

Added: labs/jbossrules/trunk/drools-examples/drools-examples-drl/src/main/rules/org/drools/examples/sudoku/sudokuSolver.drl
===================================================================
--- labs/jbossrules/trunk/drools-examples/drools-examples-drl/src/main/rules/org/drools/examples/sudoku/sudokuSolver.drl	                        (rev 0)
+++ labs/jbossrules/trunk/drools-examples/drools-examples-drl/src/main/rules/org/drools/examples/sudoku/sudokuSolver.drl	2007-08-31 09:23:09 UTC (rev 14774)
@@ -0,0 +1,151 @@
+package org.drools.examples.sudoku
+
+import org.drools.examples.sudoku.rules.PossibleCellValue;
+import org.drools.examples.sudoku.rules.ResolvedCellValue;
+
+/**
+ * This rule package defines a set of rules that can be used to solve a partially filled Sudoku grid.
+ * It expects to have objects of type PossibleCellValue and ResolvedCellValue
+ * inserted into the working memory and will then work to remove PossibleCellValues and to 
+ * insert ResolvedCellValues until a single ResolvedCellValue is present for each row and column
+ * and not PossibleCellValues remain.
+ *
+ * Any issues discovered are inserted as Strings into the issues global.
+ *
+ * @author <a href="pbennett at redhat.com">Pete Bennett</a>
+ * @author <a href="mailto:michael.frandsen at syngenio.de">Michael Frandsen</a>
+ * @version $Revision: 1.1 $
+ */
+
+rule "#1 Once a specific cell has a ResolvedCellValue, remove all the other PossibleCellValues for the same cell"
+	# this rule requires a high salience so that, when it is activated it is executed, as soon as a 
+	# ResolvedCellValue is set, we need to clear out the PossibleCellValues for the same cell location, 
+	# otherwise other rules misfire - a good example of a valid use for salience
+	salience 50 
+	when
+		$resolved : ResolvedCellValue($resolvedRow : row, $resolvedCol : col)
+		$possible : PossibleCellValue(row == $resolvedRow, col == $resolvedCol)
+	then
+		retract($possible);
+		update ($resolved); # this is done so that listeners can be triggered to know this cell is now resolved
+		System.out.println("Rule #1 cleared the other PossibleCellValues for ("+$resolved.getRow()+","+$resolved.getCol()+") as a ResolvedCellValue of "+$resolved.getValue()+" exists for this cell.");
+end
+
+rule "#2 If a cell has only a single PossibleCellValue, remove it and replace it with the equivalent ResolvedCellValue"
+	when
+		$possible : PossibleCellValue ( $possibleRow : row, $possibleCol : col, $possibleValue : value )
+		not ( PossibleCellValue ( row == $possibleRow, col == $possibleCol, value != $possibleValue ) )
+	then
+		retract( $possible );
+		insert( new ResolvedCellValue( $possibleValue, $possibleRow, $possibleCol ) );
+		System.out.println("Rule #2 changed the only PossibleCellValue at ("+$possibleRow+","+$possibleCol+") to a ResolvedCellValue for with the value of "+$possibleValue);
+end
+
+rule "#3 If a row has a ResolvedCellValue in it, remove this value as a PossibleCellValue from all other cells in the same row"
+  	when		
+		$resolved : ResolvedCellValue( $resolvedRow : row, $resolvedValue : value )
+		$possible : PossibleCellValue( row == $resolvedRow, value == $resolvedValue )
+	then
+		retract( $possible );
+		System.out.println("Rule #3 determined the value at ("+$possible.getRow()+","+$possible.getCol()+") could not be "+$possible.getValue()+" as this value already exists in the same row at ("+$resolved.getRow()+","+$resolved.getCol()+")");
+end
+
+rule "#4 If a column has a ResolvedCellValue in it, remove this value as a PossibleCellValue from all other cells in the same column"
+    when		
+		$resolved : ResolvedCellValue( $resolvedCol : col, $resolvedValue : value )
+		$possible : PossibleCellValue( col == $resolvedCol, value == $resolvedValue )
+	then
+		retract( $possible );
+		System.out.println("Rule #3 determined the value at ("+$possible.getRow()+","+$possible.getCol()+") could not be "+$possible.getValue()+" as this value already exists in the same column at ("+$resolved.getRow()+","+$resolved.getCol()+")");
+end
+
+rule "#5 If a zone has a ResolvedCellValue in it, remove this value as a PossibleCellValue from all other cells in the same zone"
+	when		
+		$resolved : ResolvedCellValue( $resolvedZone : zone, $resolvedValue : value )
+		$possible : PossibleCellValue( zone == $resolvedZone, value == $resolvedValue )
+	then
+		retract( $possible );
+		System.out.println("Rule #3 determined the value at ("+$possible.getRow()+","+$possible.getCol()+") could not be "+$possible.getValue()+" as this value already exists in the same zone at ("+$resolved.getRow()+","+$resolved.getCol()+")");
+end
+
+rule "#6 If a PossibleCellValue only appears once in a row, then this must be the ResolvedCellValue"
+	when		
+		$possible : PossibleCellValue( $zone : zone, $value : value, $col : col, $row : row)
+		not (PossibleCellValue( row == $row, value == $value ))
+		not (ResolvedCellValue( value == $value, zone == $zone) )
+		not (ResolvedCellValue( value == $value, row == $row) )
+		not (ResolvedCellValue( value == $value, col == $col) )
+	then
+		retract( $possible );
+		insert( new ResolvedCellValue( $value, $row, $col) );
+		System.out.println("Rule #6 determined ("+$row+","+$col+") is "+$value+" as this is the only possible cell in the row that can have this value");
+end
+
+rule "#7 If a PossibleCellValue only appears once in a column, then this must be the ResolvedCellValue"
+	when		
+		$possible : PossibleCellValue( $zone : zone, $value : value, $col : col, $row : row)
+		not (PossibleCellValue( col == $col, value == $value ))
+		not (ResolvedCellValue( value == $value, zone == $zone) )
+		not (ResolvedCellValue( value == $value, row == $row) )
+		not (ResolvedCellValue( value == $value, col == $col) )
+	then
+		retract( $possible );
+		insert( new ResolvedCellValue( $value, $row, $col) );
+		System.out.println("Rule #7 determined ("+$row+","+$col+") is "+$value+" as this is the only possible cell in the column that can have this value");
+end
+
+rule "#8 If a PossibleCellValue only appears once in a zone, then this must be the ResolvedCellValue"
+	when		
+		$possible : PossibleCellValue( $zone : zone, $value : value, $col : col, $row : row)
+		not (PossibleCellValue( zone == $zone, value == $value ))
+		not (ResolvedCellValue( value == $value, zone == $zone) )
+		not (ResolvedCellValue( value == $value, row == $row) )
+		not (ResolvedCellValue( value == $value, col == $col) )
+	then
+		retract( $possible );
+		insert( new ResolvedCellValue( $value, $row, $col) );
+		System.out.println("Rule #8 determined ("+$row+","+$col+") is "+$value+" as this is the only possible cell in the zone that can have this value");
+end
+
+rule "#9 If there are two fields with only two possible values in the same row, remove the PossibleValues for the same Value in the rest of the zone"
+	when
+		PossibleCellValue( $zone1 : zone, $val1 : value, $row1 : row, $col1 : col )
+		PossibleCellValue( zone == $zone1, value == $val1, $row2 : row, $col2 : col != $col1)
+		PossibleCellValue( zone == $zone1, row == $row1, col == $col1, $val2 : value )
+		PossibleCellValue( zone == $zone1, row == $row2, col == $col2, value == $val2 )
+		not ( PossibleCellValue( zone == $zone1, row == $row1, col == $col1, value != $val1, value != $val2 ) )
+		not ( PossibleCellValue( zone == $zone1, row == $row2, col == $col2, value != $val1, value != $val2 ) )
+		$possible : PossibleCellValue( zone == $zone1, value == $val1)
+	then
+		retract( $possible );
+		System.out.println("Rule #9 fired");
+end
+
+rule "#10 If there are two fields with only two possible values in the same column, remove the PossibleValues for the same Value in the rest of the zone"
+	when
+		PossibleCellValue( $zone1 : zone, $val1 : value, $row1 : row, $col1 : col )
+		PossibleCellValue( zone == $zone1, value == $val1, $row2 : row != $row1, $col2 : col)
+		PossibleCellValue( zone == $zone1, row == $row1, col == $col1, $val2 : value )
+		PossibleCellValue( zone == $zone1, row == $row2, col == $col2, value == $val2 )
+		not ( PossibleCellValue( zone == $zone1, row == $row1, col == $col1, value != $val1, value != $val2 ) )
+		not ( PossibleCellValue( zone == $zone1, row == $row2, col == $col2, value != $val1, value != $val2 ) )
+		$possible : PossibleCellValue( zone == $zone1, value == $val1)
+	then
+		retract( $possible );
+		System.out.println("Rule #10 fired");
+end
+
+rule "#11 If there are two fields with only two possible values in the same zone, remove the PossibleValues for the same Value in the rest of the zone"
+#	salience 4
+	when
+		PossibleCellValue( $zone1 : zone, $val1 : value, $row1 : row, $col1 : col )
+		PossibleCellValue( $zone2 : zone != $zone1, value == $val1, $row2 : row, $col2 : col)
+		PossibleCellValue( zone == $zone1, row == $row1, col == $col1, $val2 : value )
+		PossibleCellValue( zone == $zone1, row == $row2, col == $col2, value == $val2 )
+		not ( PossibleCellValue( zone == $zone1, row == $row1, col == $col1, value != $val1, value != $val2 ) )
+		not ( PossibleCellValue( zone == $zone1, row == $row2, col == $col2, value != $val1, value != $val2 ) )
+		$possible : PossibleCellValue( zone == $zone1, value == $val1)
+	then
+		retract( $possible );
+		System.out.println("Rule #11 fired");
+end
\ No newline at end of file

Added: labs/jbossrules/trunk/drools-examples/drools-examples-drl/src/main/rules/org/drools/examples/sudoku/sudokuValidator.drl
===================================================================
--- labs/jbossrules/trunk/drools-examples/drools-examples-drl/src/main/rules/org/drools/examples/sudoku/sudokuValidator.drl	                        (rev 0)
+++ labs/jbossrules/trunk/drools-examples/drools-examples-drl/src/main/rules/org/drools/examples/sudoku/sudokuValidator.drl	2007-08-31 09:23:09 UTC (rev 14774)
@@ -0,0 +1,76 @@
+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 Suoku 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 <a href="pbennett at redhat.com">Pete Bennett</a>
+ * @author <a href="mailto:michael.frandsen at syngenio.de">Michael Frandsen</a>
+ * @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
+
+/**
+ * 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
\ No newline at end of file




More information about the jboss-svn-commits mailing list