Este contenido no está disponible en el idioma seleccionado.
Chapter 28. Sudoku Example
28.1. Sudoku Example: Loading the Example Copiar enlaceEnlace copiado en el portapapeles!
Copiar enlaceEnlace copiado en el portapapeles!
Procedure 28.1. Task
- Open
sudoku.drlin the IDE. - Execute
java org.drools.examples.DroolsExamplesAppand click onSudokuExample. The window contains an empty grid, but the program comes with a number of grids stored internally which can be loaded and solved. - Click on
to load one of the examples. All buttons are disabled until a grid is loaded. Loading the Simpleexample fills the grid according to the puzzle's initial state. - Click on the
Solvebutton and the JBoss Rules engine will fill out the remaining values. The buttons will be inactive again. - Alternatively, click on the
Stepbutton to see the next digit found by the rule set. The Console window will display detailed information about the rules which are executing to solve the step in a readable format like the example below:Copy to Clipboard Copied! Toggle word wrap Toggle overflow - Click on the
Dumpbutton to see the state of the grid. The cells show either the established value or the remaining possible candidates. See the example below:Copy to Clipboard Copied! Toggle word wrap Toggle overflow
28.2. Sudoku Example: Debugging a Broken Example Copiar enlaceEnlace copiado en el portapapeles!
Copiar enlaceEnlace copiado en el portapapeles!
Procedure 28.2. Task
- Open
sudoku.drlin your IDE. - Click on
. The JBoss Rules engine will inspect the grid and produce the following output: Copy to Clipboard Copied! Toggle word wrap Toggle overflow - Click on the
Solvebutton to apply the solving rules to this invalid grid. These rules use the values of the cells for problem solving. The rules detecting these situations insert a Setting fact including the solution value for the specified cell. This fact removes the incorrect value from all cells in the group.
28.3. Sudoku Example: Java Source and Rules Copiar enlaceEnlace copiado en el portapapeles!
Copiar enlaceEnlace copiado en el portapapeles!
- The Java source code can be found in the
/src/main/java/org/drools/examples/sudokudirectory, with the two DRL files defining the rules located in the/src/main/rules/org/drools/examples/sudokudirectory. - The package
org.drools.examples.sudoku.swingcontains a set of classes which implement a framework for Sudoku puzzles. This package does not have any dependencies on the JBoss Rules libraries. SudokuGridModeldefines an interface which can be implemented to store a Sudoku puzzle as a 9x9 grid ofCellobjects.SudokuGridViewis a Swing component which can visualize any implementation ofSudokuGridModel.SudokuGridEventandSudokuGridListenerare used to communicate state changes between the model and the view. Events are fired when a cell's value is resolved or changed.SudokuGridSamplesprovides a number of partially filled Sudoku puzzles for demonstration purposes.- The package
org.drools.examples.sudoku.rulescontains a utility class with a method for compiling DRL files. - The package
org.drools.examples.sudokucontains a set of classes implementing the elementaryCellobject and its various aggregations. It contains theCellFilesubtypesCellRow,CellColandCellSqr, all of which are subtypes ofCellGroup.
28.4. Sudoku Example: Cell Objects Copiar enlaceEnlace copiado en el portapapeles!
Copiar enlaceEnlace copiado en el portapapeles!
CellandCellGroupare subclasses ofSetOfNine, which provides a propertyfreewith the typeSet<Integer>. For aCellit represents the individual candidate set. For aCellGroupthe set is the union of all candidate sets of its cells, or the set of digits that still need to be allocated.- You can write rules that detect the specific situations that permit the allocation of a value to a cell or the elimination of a value from some candidate set. For example, you can create a list of
Cellobjects with 81Celland 27CellGroupobjects. You can also combine the linkage provided by theCellpropertiescellRow,cellCol,cellSqrand theCellGrouppropertycells.
28.5. Sudoku Example: Classes and Objects Copiar enlaceEnlace copiado en el portapapeles!
Copiar enlaceEnlace copiado en el portapapeles!
- An object belonging to the
Settingclass is used for triggering the operations that accompany the allocation of a value. The presence of aSettingfact is used in all rules that should detect changes in the process. This is to avoid reactions to inconsistent intermediary states. - An object of class
Steppingis used in a low priority rule to execute an emergency halt when a "Step" ends unexpectedly. This indicates that the puzzle cannot be solved by the program. - The class
org.drools.examples.sudoku.SudokuExampleimplements a Java application combining the above components.
28.6. Sudoku Example: Validate.drl Copiar enlaceEnlace copiado en el portapapeles!
Copiar enlaceEnlace copiado en el portapapeles!
- Sudoku Validator Rules (validate.drl) detect duplicate numbers in cell groups. They are combined in an agenda group which enables them to be activated after loading a puzzle.
- The three rules
duplicate in cell...are very similar. The first pattern locates a cell with an allocated value. The second pattern pulls in any of the three cell groups the cell belongs to. The final pattern finds a cell with the same value as the first cell and in the same row, column or square, respectively. - Rule
terminate groupfires last. It prints a message and calls halt.
28.7. Sudoku Example: Sudoku.drl Copiar enlaceEnlace copiado en el portapapeles!
Copiar enlaceEnlace copiado en el portapapeles!
- There are three types of solving rules in Sudoku.drl: one group handles the allocation of a number to a cell, another group detects feasible allocations, and the third group eliminates values from candidate sets.
- The rules
set a value,eliminate a value from Cellandretract settingdepend on the presence of aSettingobject. Set a valuehandles the assignment to the cell and the operations for removing the value from the "free" sets of the cell's three groups. Also, it decrements a counter that, when zero, returns control to the Java application that has calledfireUntilHalt().Eliminate a value from Cellreduces the candidate lists of all cells that are related to the newly assigned cell.Retract settingretracts the triggeringSettingfact when all of the eliminations have been made.- There are just two rules that detect a situation where an allocation of a number to a cell is possible. Rule
singlefires for aCellwith a candidate set containing a single number. Rulehidden singlefires when there is a cell containing a candidate but this candidate is absent from all other cells in one of the groups the cell belongs to. Both rules create and insert aSettingfact. - Rules from the largest group of rules implement, singly or in groups of two or three, various solving techniques, as they are employed when solving Sudoku puzzles manually.
- Rule
naked pairdetects two identical candidate sets in two cells of a group. These two values may be removed from all other candidate sets of that group. - In
hidden pair inrules, the rules look for a subset of two numbers in exactly two cells of a group, with neither value occurring in any of the other cells of this group. This means that all other candidates can be eliminated from the two cells harbouring the hidden pair. - A pair of rules deals with
X-wingsin rows and columns. When there are only two possible cells for a value in each of two different rows (or columns) and these candidates are in the same columns (or rows), then all other candidates for this value in the columns (or rows) can be eliminated. The conditionssameoronlyresult in patterns with suitable constraints or prefixed withnot. - The rule pair
intersection removal...is based on the restricted occurrence of a number within one square, either in a single row or in a single column. This means that this number must be in one of those two or three cells of the row or column. It can be removed from the candidate sets of all other cells of the group. The pattern establishes the restricted occurrence and then fires for each cell outside the square and within the same cell file. - To solve very difficult grids, the rule set would need to be extended with more complex rules. (Ultimately, there are puzzles that cannot be solved except by trial and error.)