[jboss-svn-commits] JBL Code SVN: r16141 - in labs/jbossrules/trunk/drools-examples/drools-examples-drl/src/main/java/org/drools/examples/sudoku: rules and 1 other directories.
jboss-svn-commits at lists.jboss.org
jboss-svn-commits at lists.jboss.org
Mon Oct 29 10:49:19 EDT 2007
Author: pete.bennett at jboss.com
Date: 2007-10-29 10:49:19 -0400 (Mon, 29 Oct 2007)
New Revision: 16141
Modified:
labs/jbossrules/trunk/drools-examples/drools-examples-drl/src/main/java/org/drools/examples/sudoku/Main.java
labs/jbossrules/trunk/drools-examples/drools-examples-drl/src/main/java/org/drools/examples/sudoku/rules/DroolsSudokuGridModel.java
labs/jbossrules/trunk/drools-examples/drools-examples-drl/src/main/java/org/drools/examples/sudoku/swing/SudokuGridModel.java
labs/jbossrules/trunk/drools-examples/drools-examples-drl/src/main/java/org/drools/examples/sudoku/swing/SudokuGridSamples.java
Log:
Checked differences between my local copy and the repository and they were all just down to Java 5 usage so updating the respoitory with my local copy.
Modified: labs/jbossrules/trunk/drools-examples/drools-examples-drl/src/main/java/org/drools/examples/sudoku/Main.java
===================================================================
--- labs/jbossrules/trunk/drools-examples/drools-examples-drl/src/main/java/org/drools/examples/sudoku/Main.java 2007-10-29 14:40:59 UTC (rev 16140)
+++ labs/jbossrules/trunk/drools-examples/drools-examples-drl/src/main/java/org/drools/examples/sudoku/Main.java 2007-10-29 14:49:19 UTC (rev 16141)
@@ -11,7 +11,6 @@
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
-import java.util.Iterator;
import javax.swing.JButton;
import javax.swing.JFileChooser;
@@ -48,21 +47,21 @@
private BorderLayout borderLayout = new BorderLayout();
private FlowLayout flowLayout = new FlowLayout(FlowLayout.RIGHT);
private JPanel buttonPanel = new JPanel(flowLayout);
- private JButton solveButton = new JButton("Solve Grid");
+ private JButton solveButton = new JButton("Solve");
+ private JButton fireOneRuleButton = new JButton("Step");
private JFileChooser fileChooser;
public static void main(String[] args)
{
- Main main = new Main();
+ @SuppressWarnings("unused")
+ Main main = new Main();
}
public Main()
{
mainFrame = new JFrame("Drools Sudoku Example");
- Iterator iter = SudokuGridSamples.getInstance().getSampleNames().iterator();
- while(iter.hasNext())
+ for (String sampleName : SudokuGridSamples.getInstance().getSampleNames())
{
- String sampleName = (String)iter.next();
JMenuItem menuItem = new JMenuItem(sampleName);
menuItem.addActionListener(this);
samplesMenu.add(menuItem);
@@ -78,6 +77,7 @@
droolsSudokuGridModel = new DroolsSudokuGridModel(SudokuGridSamples.getInstance().getSample("Simple"));
mainFrame.setLayout(borderLayout);
mainFrame.add(BorderLayout.CENTER, sudokuGridView);
+ // buttonPanel.add(fireOneRuleButton);
buttonPanel.add(solveButton);
solveButton.addActionListener(this);
mainFrame.add(BorderLayout.SOUTH, buttonPanel);
Modified: labs/jbossrules/trunk/drools-examples/drools-examples-drl/src/main/java/org/drools/examples/sudoku/rules/DroolsSudokuGridModel.java
===================================================================
--- labs/jbossrules/trunk/drools-examples/drools-examples-drl/src/main/java/org/drools/examples/sudoku/rules/DroolsSudokuGridModel.java 2007-10-29 14:40:59 UTC (rev 16140)
+++ labs/jbossrules/trunk/drools-examples/drools-examples-drl/src/main/java/org/drools/examples/sudoku/rules/DroolsSudokuGridModel.java 2007-10-29 14:49:19 UTC (rev 16141)
@@ -8,7 +8,6 @@
import java.util.ArrayList;
import java.util.HashSet;
-import java.util.Iterator;
import java.util.List;
import java.util.Set;
import org.drools.RuleBase;
@@ -33,6 +32,7 @@
* @author <a href="pbennett at redhat.com">Pete Bennett</a>
* @version $Revision: 1.1 $
*/
+ at SuppressWarnings("unchecked")
public class DroolsSudokuGridModel
extends AbstractSudokuGridModel
implements SudokuGridModel
@@ -48,7 +48,12 @@
public final static String SUDOKU_VALIDATOR_DRL = "../sudokuValidator.drl";
/** A set of AbtractCellValues capturing the current state of the grid */
- private Set allCellValues = new HashSet();
+ private Set<AbstractCellValue> allCellValues
+ = new HashSet<AbstractCellValue>();
+
+ /** A index into the AbstractCellValues based on row and column */
+ private Set<Integer>[][] cellValuesByRowAndCol
+ = new HashSet[SudokuGridModel.NUM_ROWS][SudokuGridModel.NUM_COLS];
/** The solver rule base */
private RuleBase solverRuleBase;
@@ -72,14 +77,20 @@
*
* @param cellValues a two dimensional grid of Integer values for cells, a null means the value is not yet resolved
*/
- public DroolsSudokuGridModel(int[][] cellValues)
+ public DroolsSudokuGridModel(Integer[][] cellValues)
{
this();
setCellValues(cellValues);
}
- public void setCellValues(int[][] cellValues)
+ /**
+ * Set the state of the Grid based on a two dimensional array of Integers.
+ *
+ * @param cellValues a two dimensional grid of Integer values for cells, a null means the value is not yet resolved
+ */
+ public void setCellValues(Integer[][] cellValues)
{
+ long startTime = System.currentTimeMillis();
if (solverRuleBase == null)
{
try
@@ -105,46 +116,78 @@
{
for (int col=0; col<cellValues[row].length; col++)
{
- if(cellValues[row][col] == 0)
+ cellValuesByRowAndCol[row][col] = new HashSet<Integer>();
+
+ if(cellValues[row][col] == null)
{
for(int value=1; value<10; value++)
{
PossibleCellValue cellValue = new PossibleCellValue(value, row, col);
+ addCellValue(cellValue);
allCellValues.add(cellValue);
}
}
else
{
ResolvedCellValue cellValue = new ResolvedCellValue(cellValues[row][col], row, col);
- allCellValues.add(cellValue);
+ addCellValue(cellValue);
}
}
}
insertAllCellValues(solverStatefulSession);
+ System.out.println("Setting up working memory and inserting all cell value POJOs took "+(System.currentTimeMillis()-startTime)+"ms.");
}
+ /**
+ * Determines whether a given cell is editable from another class.
+ *
+ * @param row the row in the grid for the cell
+ * @param col the column in the grid for the cell
+ * @return is the specified cell editable
+ */
public boolean isCellEditable(int row, int col)
{
return false;
}
-
+
+ /**
+ * Determines whether a given cell has been solved.
+ *
+ * @param row the row in the grid for the cell
+ * @param col the column in the grid for the cell
+ * @return is the specified cell solved
+ */
public boolean isCellResolved(int row, int col)
{
return getPossibleCellValues(row, col).size() == 1;
}
+ /**
+ * Evaluates the current state of the Grid against the
+ * validation rules determined in the SUDOKU_VALIDATOR_DRL
+ * and indicates if the grid is currently solved or not.
+ *
+ * @return true if the current state represents a completely filled out
+ * and valid Sudoku solution, false otherwise
+ */
public boolean isGridSolved()
{
boolean solved = true;
+ // TODO: move this logic into SUDOKU_VALIDATOR_DRL and out of Java code
for(int row=0; row<NUM_ROWS; row++)
{
for (int col=0; col<NUM_COLS; col++)
{
if(!isCellResolved(row, col))
{
- System.out.println("("+row+","+col+") has not been resolved");
+ System.out.print("("+row+","+col+") has not been resolved but has been narrowed down to ");
+ for (Integer possibleInt : getPossibleCellValues(row, col))
+ {
+ System.out.print(possibleInt+" ");
+ }
+ System.out.println();
solved=false;
}
}
@@ -169,10 +212,8 @@
else
{
solved = false;
- Iterator iter = issues.iterator();
- while(iter.hasNext())
+ for (Object issue : issues)
{
- Object issue = iter.next();
System.out.println(issue);
}
}
@@ -187,23 +228,31 @@
return solved;
}
- public List getPossibleCellValues(int row, int col)
+ /**
+ * Returns the possible values of the cell at a specific
+ * row and column in the Grid.
+ *
+ * @param row the row in the Grid
+ * @param col the column in the Grid
+ * @return the Set of possible Integer values this cell can have, if
+ * the Set is of size one then this is the value this cell
+ * must have, otherwise it is a list of the possibilities
+ */
+ public Set<Integer> getPossibleCellValues(int row, int col)
{
- List possibleCellValues = new ArrayList();
-
- Iterator iter = allCellValues.iterator();
- while(iter.hasNext())
- {
- AbstractCellValue cellValue = (AbstractCellValue) iter.next();
- if (cellValue.getRow() == row && cellValue.getCol() == col)
- {
- possibleCellValues.add(new Integer(cellValue.getValue()));
- }
- }
-
- return possibleCellValues;
+ return cellValuesByRowAndCol[row][col];
}
+ /**
+ * Attempt to solve the Sudoku puzzle from its current state by
+ * firing all of the rules in SUDOKU_SOLVER_DRL against the
+ * current state of the Grid then validate if we have solved the
+ * Grid after this.
+ *
+ * @return true if the state after firing all rules
+ * represents a completely filled out
+ * and valid Sudoku solution, false otherwise
+ */
public boolean solve()
{
solverStatefulSession.fireAllRules();
@@ -211,16 +260,62 @@
return isGridSolved();
}
+ /**
+ * Fire the next rule on the agenda and return
+ *
+ * @return true if the state after firing the single rule
+ * represents a completely filled out
+ * and valid Sudoku solution, false otherwise
+ */
+ public boolean step()
+ {
+ // TODO: I am not sure where the fireAllRules(int) method has gone
+ // should be solverStatefulSession.fireAllRules(1)
+ solverStatefulSession.fireAllRules();
+
+ return isGridSolved();
+ }
+
+ /**
+ * Inserts all of the current state of the Grid as represented
+ * by the set of AbstractCellValues this class is maintaining
+ * into the specified StatefulSession working memory.
+ *
+ * @param statefulSession the target StatefulSession
+ */
private void insertAllCellValues(StatefulSession statefulSession)
{
- Iterator iter = allCellValues.iterator();
- while(iter.hasNext())
- {
- AbstractCellValue cellValue = (AbstractCellValue) iter.next();
+ for (AbstractCellValue cellValue : allCellValues)
+ {
statefulSession.insert(cellValue);
}
}
+ /**
+ * Adds the specified AbstractCellValue into the set of
+ * AbstractCellValues that this class is maintaining.
+ *
+ * @param cellValue the AbstractCellValue to add
+ */
+ private void addCellValue(AbstractCellValue cellValue)
+ {
+ allCellValues.add(cellValue);
+ cellValuesByRowAndCol[cellValue.getRow()][cellValue.getCol()].add(cellValue.getValue());
+ }
+
+ /**
+ * Removes the specified AbstractCellValue from the set of
+ * AbstractCellValues that this class is maintaining.
+ *
+ * @param cellValue the AbstractCellValue to remove
+ */
+ private void removeCellValue(AbstractCellValue cellValue)
+ {
+ allCellValues.remove(cellValue);
+ cellValuesByRowAndCol[cellValue.getRow()][cellValue.getCol()].remove(cellValue.getValue());
+ }
+
+
class SudokuWorkingMemoryListener
implements WorkingMemoryEventListener
{
@@ -228,7 +323,7 @@
{
if (ev.getObject() instanceof AbstractCellValue)
{
- allCellValues.add(((AbstractCellValue) ev.getObject()));
+ addCellValue(((AbstractCellValue) ev.getObject()));
}
if (ev.getObject() instanceof ResolvedCellValue)
@@ -236,13 +331,21 @@
ResolvedCellValue cellValue = (ResolvedCellValue) ev.getObject();
fireCellResolvedEvent(new SudokuGridEvent(this, cellValue.getRow(), cellValue.getCol(), cellValue.getValue()));
}
+
+ if (ev.getObject() instanceof String)
+ {
+ System.out.println(ev.getObject());
+ }
}
public void objectRetracted(ObjectRetractedEvent ev)
{
if (ev.getOldObject() instanceof AbstractCellValue)
{
- allCellValues.remove(((AbstractCellValue) ev.getOldObject()));
+ AbstractCellValue cellValue = (AbstractCellValue) ev.getOldObject();
+
+ removeCellValue(cellValue);
+ fireCellUpdatedEvent(new SudokuGridEvent(this, cellValue.getRow(), cellValue.getCol(), cellValue.getValue()));
}
}
@@ -252,7 +355,6 @@
{
ResolvedCellValue cellValue = (ResolvedCellValue) ev.getObject();
fireCellUpdatedEvent(new SudokuGridEvent(this, cellValue.getRow(), cellValue.getCol(), cellValue.getValue()));
-
}
}
}
Modified: labs/jbossrules/trunk/drools-examples/drools-examples-drl/src/main/java/org/drools/examples/sudoku/swing/SudokuGridModel.java
===================================================================
--- labs/jbossrules/trunk/drools-examples/drools-examples-drl/src/main/java/org/drools/examples/sudoku/swing/SudokuGridModel.java 2007-10-29 14:40:59 UTC (rev 16140)
+++ labs/jbossrules/trunk/drools-examples/drools-examples-drl/src/main/java/org/drools/examples/sudoku/swing/SudokuGridModel.java 2007-10-29 14:49:19 UTC (rev 16141)
@@ -6,7 +6,7 @@
*/
package org.drools.examples.sudoku.swing;
-import java.util.List;
+import java.util.Set;
/**
* An interface representing a 9x9 Sudoku Grid of Cells.
@@ -24,7 +24,7 @@
* @version $Revision: 1.1 $
*/
public interface SudokuGridModel
-{
+{
/**
* The number of rows in the Grid, i.e. the height
*/
@@ -34,12 +34,18 @@
* The number of columns in the Grid, i.e. the width
*/
public static int NUM_COLS = 9;
-
- public static int INNER_GRID_HEIGHT = 3;
-
+
+ /**
+ * The number of colums that make up a zone within the Grid
+ */
public static int INNER_GRID_WIDTH = 3;
/**
+ * The number of rows that make up a zone within the Grid
+ */
+ public static int INNER_GRID_HEIGHT = 3;
+
+ /**
* A NUM_ROWSxNUM_COLS two dimensional array which maps from rows and columns
* to the 3x3 subzones in a Sudoku grid
*/
@@ -54,13 +60,13 @@
{ 7 , 7 , 7 , 8 , 8 , 8 , 9 , 9 , 9},
{ 7 , 7 , 7 , 8 , 8 , 8 , 9 , 9 , 9} };
- public void setCellValues(int[][] cellValues);
+ public void setCellValues(Integer[][] cellValues);
public boolean isCellEditable(int row, int col);
public boolean isCellResolved(int row, int col);
- public List getPossibleCellValues(int row, int col);
+ public Set<Integer> getPossibleCellValues(int row, int col);
public boolean solve();
Modified: labs/jbossrules/trunk/drools-examples/drools-examples-drl/src/main/java/org/drools/examples/sudoku/swing/SudokuGridSamples.java
===================================================================
--- labs/jbossrules/trunk/drools-examples/drools-examples-drl/src/main/java/org/drools/examples/sudoku/swing/SudokuGridSamples.java 2007-10-29 14:40:59 UTC (rev 16140)
+++ labs/jbossrules/trunk/drools-examples/drools-examples-drl/src/main/java/org/drools/examples/sudoku/swing/SudokuGridSamples.java 2007-10-29 14:49:19 UTC (rev 16141)
@@ -12,7 +12,7 @@
public class SudokuGridSamples
{
- private Map samples = new HashMap();
+ private Map<String, Integer[][]> samples = new HashMap<String, Integer[][]>();
private static SudokuGridSamples INSTANCE;
private SudokuGridSamples()
@@ -20,106 +20,106 @@
samples.put
(
"Simple",
- new int[][]
- {{0, 5, 6, 8, 0, 1, 9, 4, 0},
- {9, 0, 0, 6, 0, 5, 0, 0, 3},
- {7, 0, 0, 4, 9, 3, 0, 0, 8},
- {8, 9, 7, 0, 4, 0, 6, 3, 5},
- {0, 0, 3, 9, 0, 6, 8, 0, 0},
- {4, 6, 5, 0, 8, 0, 2, 9, 1},
- {5, 0, 0, 2, 6, 9, 0, 0, 7},
- {6, 0, 0, 5, 0, 4, 0, 0, 9},
- {0, 4, 9, 7, 0, 8, 3, 5, 0}}
+ new Integer[][]
+ {{null, 5, 6, 8, null, 1, 9, 4, null},
+ {9, null, null, 6, null, 5, null, null, 3},
+ {7, null, null, 4, 9, 3, null, null, 8},
+ {8, 9, 7, null, 4, null, 6, 3, 5},
+ {null, null, 3, 9, null, 6, 8, null, null},
+ {4, 6, 5, null, 8, null, 2, 9, 1},
+ {5, null, null, 2, 6, 9, null, null, 7},
+ {6, null, null, 5, null, 4, null, null, 9},
+ {null, 4, 9, 7, null, 8, 3, 5, null}}
);
samples.put
(
"Medium",
- new int[][]
- {{8, 4, 7, 0, 0, 0, 2, 5, 6},
- {5, 0, 0, 0, 8, 0, 0, 0, 4},
- {2, 0, 0, 0, 7, 0, 0, 0, 8},
- {0, 0, 0, 3, 0, 8, 0, 0, 0},
- {0, 5, 1, 0, 0, 0, 8, 7, 2},
- {0, 0, 0, 5, 0, 7, 0, 0, 0},
- {4, 0, 0, 0, 5, 0, 0, 0, 7},
- {6, 0, 0, 0, 3, 0, 0, 0, 9},
- {1, 3, 2, 0, 0, 0, 4, 8, 5}}
+ new Integer[][]
+ {{8, 4, 7, null, null, null, 2, 5, 6},
+ {5, null, null, null, 8, null, null, null, 4},
+ {2, null, null, null, 7, null, null, null, 8},
+ {null, null, null, 3, null, 8, null, null, null},
+ {null, 5, 1, null, null, null, 8, 7, 2},
+ {null, null, null, 5, null, 7, null, null, null},
+ {4, null, null, null, 5, null, null, null, 7},
+ {6, null, null, null, 3, null, null, null, 9},
+ {1, 3, 2, null, null, null, 4, 8, 5}}
);
samples.put
(
"Hard 1",
- new int[][]
- {{0, 0, 0, 0, 5, 1, 0, 8, 0},
- {0, 8, 0, 0, 4, 0, 0, 0, 5},
- {0, 0, 3, 0, 0, 0, 2, 0, 0},
- {0, 0, 0, 0, 6, 0, 0, 0, 9},
- {6, 7, 0, 4, 0, 9, 0, 1, 3},
- {8, 0, 0, 0, 3, 0, 0, 0, 0},
- {0, 0, 2, 0, 0, 0, 4, 0, 0},
- {5, 0, 0, 0, 9, 0, 0, 2, 0},
- {0, 9, 0, 7, 1, 0, 0, 0, 0}}
+ new Integer[][]
+ {{null, null, null, null, 5, 1, null, 8, null},
+ {null, 8, null, null, 4, null, null, null, 5},
+ {null, null, 3, null, null, null, 2, null, null},
+ {null, null, null, null, 6, null, null, null, 9},
+ {6, 7, null, 4, null, 9, null, 1, 3},
+ {8, null, null, null, 3, null, null, null, null},
+ {null, null, 2, null, null, null, 4, null, null},
+ {5, null, null, null, 9, null, null, 2, null},
+ {null, 9, null, 7, 1, null, null, null, null}}
);
samples.put
(
"Hard 2",
- new int[][]
- {{0,0,0,6,0,0,1,0,0},
- {0,0,0,0,0,5,0,0,6},
- {5,0,7,0,0,0,2,3,0},
- {0,8,0,9,0,7,0,0,0},
- {9,3,0,0,0,0,0,6,7},
- {0,0,0,4,0,6,0,1,0},
- {0,7,4,0,0,0,9,0,1},
- {8,0,0,7,0,0,0,0,0},
- {0,0,3,0,0,8,0,0,0}}
+ new Integer[][]
+ {{null,null,null,6,null,null,1,null,null},
+ {null,null,null,null,null,5,null,null,6},
+ {5,null,7,null,null,null,2,3,null},
+ {null,8,null,9,null,7,null,null,null},
+ {9,3,null,null,null,null,null,6,7},
+ {null,null,null,4,null,6,null,1,null},
+ {null,7,4,null,null,null,9,null,1},
+ {8,null,null,7,null,null,null,null,null},
+ {null,null,3,null,null,8,null,null,null}}
);
samples.put
(
"Hard 3",
- new int[][]
- {{0,8,0,0,0,6,0,0,5},
- {2,0,0,0,0,0,4,8,0},
- {0,0,9,0,0,8,0,1,0},
- {0,0,0,0,8,0,1,0,2},
- {0,0,0,3,0,1,0,0,0},
- {6,0,1,0,9,0,0,0,0},
- {0,9,0,4,0,0,8,0,0},
- {0,7,6,0,0,0,0,0,3},
- {1,0,0,7,0,0,0,5,0}}
+ new Integer[][]
+ {{null,8,null,null,null,6,null,null,5},
+ {2,null,null,null,null,null,4,8,null},
+ {null,null,9,null,null,8,null,1,null},
+ {null,null,null,null,8,null,1,null,2},
+ {null,null,null,3,null,1,null,null,null},
+ {6,null,1,null,9,null,null,null,null},
+ {null,9,null,4,null,null,8,null,null},
+ {null,7,6,null,null,null,null,null,3},
+ {1,null,null,7,null,null,null,5,null}}
);
samples.put
(
"Hard 4",
- new int[][]
- {{0,0,0,0,0,4,0,9,5},
- {6,7,0,5,0,0,0,1,0},
- {0,0,0,6,0,9,0,0,0},
- {0,2,0,0,0,0,4,0,0},
- {8,1,0,0,0,0,0,7,2},
- {0,0,7,0,0,0,0,8,0},
- {0,0,0,3,0,5,0,0,0},
- {0,6,0,0,0,1,0,5,8},
- {7,3,0,9,0,0,0,0,0}}
+ new Integer[][]
+ {{null,null,null,null,null,4,null,9,5},
+ {6,7,null,5,null,null,null,1,null},
+ {null,null,null,6,null,9,null,null,null},
+ {null,2,null,null,null,null,4,null,null},
+ {8,1,null,null,null,null,null,7,2},
+ {null,null,7,null,null,null,null,8,null},
+ {null,null,null,3,null,5,null,null,null},
+ {null,6,null,null,null,1,null,5,8},
+ {7,3,null,9,null,null,null,null,null}}
);
samples.put
(
"!DELIBERATELY BROKEN!",
- new int[][]
- {{5,0,0,0,0,4,0,9,5},
- {6,7,0,5,0,0,0,1,0},
- {0,0,0,6,0,9,0,0,0},
- {0,2,0,0,0,0,4,0,0},
- {8,1,0,0,0,0,0,7,2},
- {0,0,7,0,0,0,0,8,0},
- {8,0,0,3,0,5,0,0,0},
- {0,6,0,0,0,1,0,5,8},
- {7,3,0,9,0,0,0,0,0}}
+ new Integer[][]
+ {{5,null,null,null,null,4,null,9,5},
+ {6,7,null,5,null,null,null,1,null},
+ {null,null,null,6,null,9,null,null,null},
+ {null,2,null,null,null,null,4,null,null},
+ {8,1,null,null,null,null,null,7,2},
+ {null,null,7,null,null,null,null,8,null},
+ {8,null,null,3,null,5,null,null,null},
+ {null,6,null,null,null,1,null,5,8},
+ {7,3,null,9,null,null,null,null,null}}
); }
public static SudokuGridSamples getInstance()
@@ -132,13 +132,13 @@
return INSTANCE;
}
- public Set getSampleNames()
+ public Set<String> getSampleNames()
{
return samples.keySet();
}
- public int[][] getSample(String name)
+ public Integer[][] getSample(String name)
{
- return (int[][]) samples.get(name);
+ return samples.get(name);
}
}
More information about the jboss-svn-commits
mailing list