Author: brown_j Date: 2006-04-20 21:37:31 -0400 (Thu, 20 Apr 2006) New Revision: 3872 Added: labs/jbossrules/trunk/drools-examples/src/main/java/org/ labs/jbossrules/trunk/drools-examples/src/main/java/org/drools/ labs/jbossrules/trunk/drools-examples/src/main/java/org/drools/examples/ labs/jbossrules/trunk/drools-examples/src/main/java/org/drools/examples/conway/ labs/jbossrules/trunk/drools-examples/src/main/java/org/drools/examples/conway/Cell.java labs/jbossrules/trunk/drools-examples/src/main/java/org/drools/examples/conway/CellGrid.java labs/jbossrules/trunk/drools-examples/src/main/java/org/drools/examples/conway/CellState.java labs/jbossrules/trunk/drools-examples/src/main/java/org/drools/examples/conway/ConwayApplicationProperties.java labs/jbossrules/trunk/drools-examples/src/main/java/org/drools/examples/conway/ConwayRuleBaseFactory.java labs/jbossrules/trunk/drools-examples/src/main/java/org/drools/examples/conway/patterns/ labs/jbossrules/trunk/drools-examples/src/main/java/org/drools/examples/conway/patterns/Border.java labs/jbossrules/trunk/drools-examples/src/main/java/org/drools/examples/conway/patterns/ConwayPattern.java labs/jbossrules/trunk/drools-examples/src/main/java/org/drools/examples/conway/patterns/Hi.java labs/jbossrules/trunk/drools-examples/src/main/java/org/drools/examples/conway/patterns/Pentadecathalon.java labs/jbossrules/trunk/drools-examples/src/main/java/org/drools/examples/conway/patterns/Pulsar.java labs/jbossrules/trunk/drools-examples/src/main/java/org/drools/examples/conway/patterns/SimpleGlider.java labs/jbossrules/trunk/drools-examples/src/main/java/org/drools/examples/conway/ui/ labs/jbossrules/trunk/drools-examples/src/main/java/org/drools/examples/conway/ui/CellGridCanvas.java labs/jbossrules/trunk/drools-examples/src/main/java/org/drools/examples/conway/ui/ConwayGUI.java labs/jbossrules/trunk/drools-examples/src/main/java/org/drools/examples/conway/ui/liveCellImage.gif labs/jbossrules/trunk/drools-examples/src/main/resources/ labs/jbossrules/trunk/drools-examples/src/main/resources/conway.drl labs/jbossrules/trunk/drools-examples/src/main/resources/conway.dsl labs/jbossrules/trunk/drools-examples/src/main/resources/conway.properties labs/jbossrules/trunk/drools-examples/src/test/ labs/jbossrules/trunk/drools-examples/src/test/java/ labs/jbossrules/trunk/drools-examples/src/test/java/com/ labs/jbossrules/trunk/drools-examples/src/test/java/com/sample/ labs/jbossrules/trunk/drools-examples/src/test/java/com/sample/benchmark/ labs/jbossrules/trunk/drools-examples/src/test/java/com/sample/benchmark/RuleSetLoad.java labs/jbossrules/trunk/drools-examples/src/test/java/com/sample/benchmark/RuleSetPerformance.java labs/jbossrules/trunk/drools-examples/src/test/java/org/ labs/jbossrules/trunk/drools-examples/src/test/java/org/drools/ labs/jbossrules/trunk/drools-examples/src/test/java/org/drools/examples/ labs/jbossrules/trunk/drools-examples/src/test/java/org/drools/examples/conway/ labs/jbossrules/trunk/drools-examples/src/test/java/org/drools/examples/conway/CellGridTest.java labs/jbossrules/trunk/drools-examples/src/test/java/org/drools/examples/conway/CellTest.java Removed: labs/jbossrules/trunk/drools-examples/src/main/java/com/sample/benchmark/RuleSetLoad.java labs/jbossrules/trunk/drools-examples/src/main/java/com/sample/benchmark/RuleSetPerformance.java Log: added conway code Deleted: labs/jbossrules/trunk/drools-examples/src/main/java/com/sample/benchmark/RuleSetLoad.java =================================================================== --- labs/jbossrules/trunk/drools-examples/src/main/java/com/sample/benchmark/RuleSetLoad.java 2006-04-21 01:10:34 UTC (rev 3871) +++ labs/jbossrules/trunk/drools-examples/src/main/java/com/sample/benchmark/RuleSetLoad.java 2006-04-21 01:37:31 UTC (rev 3872) @@ -1,89 +0,0 @@ -package com.sample.benchmark; - -import junit.framework.TestCase; - -import java.io.IOException; -import java.io.InputStreamReader; -import java.io.Reader; -import java.util.ArrayList; -import java.util.Iterator; - -import org.drools.PackageIntegrationException; -import org.drools.RuleBase; -import org.drools.RuleIntegrationException; -import org.drools.WorkingMemory; -import org.drools.compiler.DrlParser; -import org.drools.compiler.DroolsParserException; -import org.drools.compiler.PackageBuilder; -import org.drools.lang.descr.PackageDescr; -import org.drools.reteoo.RuleBaseImpl; -import org.drools.rule.InvalidPatternException; -import org.drools.rule.Package; - -import com.sample.benchmark.models.Account; - -/** - * @author Peter Lin - * - */ -public class RuleSetLoad extends TestCase { - public RuleSetLoad() { - super(); - } - - public void setUp() { - - } - - public void tearDown() { - - } - - private static RuleBase readRule(String file) throws IOException, DroolsParserException, RuleIntegrationException, PackageIntegrationException, InvalidPatternException { - //read in the source - Reader reader = - new InputStreamReader( - RuleSetLoad.class.getResourceAsStream( file ) ); - DrlParser parser = new DrlParser(); - PackageDescr packageDescr = parser.parse( reader ); - - //pre build the package - PackageBuilder builder = new PackageBuilder(); - builder.addPackage( packageDescr ); - Package pkg = builder.getPackage(); - - //add the package to a rulebase - RuleBaseImpl ruleBase = new RuleBaseImpl(); - ruleBase.addPackage( pkg ); - return ruleBase; - } - - public void testOneThousandLoad() { - try { - String file = "1000_rules.drl"; - long loadStart = System.currentTimeMillis(); - RuleBase ruleBase = readRule(file); - long loadEnd = System.currentTimeMillis(); - System.out.println("time to load " + file + - " " + (loadEnd - loadStart) + "ms"); - WorkingMemory workingMemory = ruleBase.newWorkingMemory(); - } catch (Exception e) { - e.printStackTrace(); - } - } - - public void testFourThousandLoad() { - try { - String file = "4000_rules.drl"; - long loadStart = System.currentTimeMillis(); - RuleBase ruleBase = readRule(file); - long loadEnd = System.currentTimeMillis(); - System.out.println("time to load " + file + - " " + (loadEnd - loadStart) + "ms"); - WorkingMemory workingMemory = ruleBase.newWorkingMemory(); - } catch (Exception e) { - e.printStackTrace(); - } - } - -} Deleted: labs/jbossrules/trunk/drools-examples/src/main/java/com/sample/benchmark/RuleSetPerformance.java =================================================================== --- labs/jbossrules/trunk/drools-examples/src/main/java/com/sample/benchmark/RuleSetPerformance.java 2006-04-21 01:10:34 UTC (rev 3871) +++ labs/jbossrules/trunk/drools-examples/src/main/java/com/sample/benchmark/RuleSetPerformance.java 2006-04-21 01:37:31 UTC (rev 3872) @@ -1,237 +0,0 @@ -package com.sample.benchmark; - -import junit.framework.TestCase; - -import java.io.IOException; -import java.io.InputStreamReader; -import java.io.Reader; -import java.util.ArrayList; -import java.util.Iterator; - -import org.drools.PackageIntegrationException; -import org.drools.RuleBase; -import org.drools.RuleIntegrationException; -import org.drools.WorkingMemory; -import org.drools.compiler.DrlParser; -import org.drools.compiler.DroolsParserException; -import org.drools.compiler.PackageBuilder; -import org.drools.lang.descr.PackageDescr; -import org.drools.reteoo.RuleBaseImpl; -import org.drools.rule.InvalidPatternException; -import org.drools.rule.Package; - -import com.sample.benchmark.models.Account; - -/** - * @author Peter Lin - * - */ -public class RuleSetPerformance extends TestCase { - public RuleSetPerformance() { - super(); - } - - public void setUp() { - - } - - public void tearDown() { - - } - - private static RuleBase readRule(String file) throws IOException, DroolsParserException, RuleIntegrationException, PackageIntegrationException, InvalidPatternException { - //read in the source - Reader reader = - new InputStreamReader( - RuleSetPerformance.class.getResourceAsStream( file ) ); - DrlParser parser = new DrlParser(); - PackageDescr packageDescr = parser.parse( reader ); - - //pre build the package - PackageBuilder builder = new PackageBuilder(); - builder.addPackage( packageDescr ); - Package pkg = builder.getPackage(); - - //add the package to a rulebase - RuleBaseImpl ruleBase = new RuleBaseImpl(); - ruleBase.addPackage( pkg ); - return ruleBase; - } - - /** - * test the performance with 50 facts and 5 rules. The test measures - * the load, assert and fire time. - */ - public void testFiveRuleFire() { - try { - int factCount = 50000; - String file = "5_rules.drl"; - int loop = 5; - long totalload = 0; - long totalassert = 0; - long totalfire = 0; - for (int c=0; c < loop; c++) { - long loadStart = System.currentTimeMillis(); - RuleBase ruleBase = readRule(file); - long loadEnd = System.currentTimeMillis(); - long loadet = loadEnd - loadStart; - totalload += loadet; - System.out.println("time to load " + file + - " " + loadet + "ms"); - WorkingMemory workingMemory = ruleBase.newWorkingMemory(); - ArrayList objects = new ArrayList(); - // create the objects - for (int idx=0; idx < factCount; idx++) { - Account acc = new Account(); - acc.setAccountId("acc"+idx); - acc.setTitle("mr"); - acc.setStatus("standard"); - objects.add(acc); - } - Iterator itr = objects.iterator(); - long assertStart = System.currentTimeMillis(); - while( itr.hasNext() ) { - workingMemory.assertObject(itr.next()); - } - long assertEnd = System.currentTimeMillis(); - long assertet = assertEnd - assertStart; - totalassert += assertet; - System.out.println("time to assert " + assertet + - " ms"); - long fireStart = System.currentTimeMillis(); - workingMemory.fireAllRules(); - long fireEnd = System.currentTimeMillis(); - long fireet = fireEnd - fireStart; - totalfire += fireet; - System.out.println("time to fireAllRules " + fireet + - " ms"); - workingMemory.dispose(); - } - System.out.println(file); - System.out.println("number of objects asserted " + factCount); - System.out.println("average load " + (totalload/loop) + " ms"); - System.out.println("average assert " + (totalassert/loop) + " ms"); - System.out.println("average fire " + (totalfire/loop) + " ms"); - } catch (Exception e) { - e.printStackTrace(); - } - } - - /** - * - * - */ - public void testFiftyRuleFire() { - try { - int factCount = 50000; - String file = "50_rules.drl"; - int loop = 5; - long totalload = 0; - long totalassert = 0; - long totalfire = 0; - for (int c=0; c < loop; c++) { - long loadStart = System.currentTimeMillis(); - RuleBase ruleBase = readRule(file); - long loadEnd = System.currentTimeMillis(); - long loadet = loadEnd - loadStart; - totalload += loadet; - System.out.println("time to load " + file + - " " + loadet + "ms"); - WorkingMemory workingMemory = ruleBase.newWorkingMemory(); - ArrayList objects = new ArrayList(); - // create the objects - for (int idx=0; idx < factCount; idx++) { - Account acc = new Account(); - acc.setAccountId("acc"+idx); - acc.setTitle("mr"); - acc.setStatus("standard"); - objects.add(acc); - } - Iterator itr = objects.iterator(); - long assertStart = System.currentTimeMillis(); - while( itr.hasNext() ) { - workingMemory.assertObject(itr.next()); - } - long assertEnd = System.currentTimeMillis(); - long assertet = assertEnd - assertStart; - totalassert += assertet; - System.out.println("time to assert " + assertet + - " ms"); - long fireStart = System.currentTimeMillis(); - workingMemory.fireAllRules(); - long fireEnd = System.currentTimeMillis(); - long fireet = fireEnd - fireStart; - totalfire += fireet; - System.out.println("time to fireAllRules " + fireet + - " ms"); - workingMemory.dispose(); - } - System.out.println(file); - System.out.println("number of objects asserted " + factCount); - System.out.println("average load " + (totalload/loop) + " ms"); - System.out.println("average assert " + (totalassert/loop) + " ms"); - System.out.println("average fire " + (totalfire/loop) + " ms"); - } catch (Exception e) { - e.printStackTrace(); - } - } - - /** - * - * - */ - public void testFiveHundredRuleFire() { - try { - int factCount = 50000; - String file = "500_rules.drl"; - int loop = 5; - long totalload = 0; - long totalassert = 0; - long totalfire = 0; - for (int c=0; c < loop; c++) { - long loadStart = System.currentTimeMillis(); - RuleBase ruleBase = readRule(file); - long loadEnd = System.currentTimeMillis(); - long loadet = loadEnd - loadStart; - totalload += loadet; - System.out.println("time to load " + file + - " " + loadet + "ms"); - WorkingMemory workingMemory = ruleBase.newWorkingMemory(); - ArrayList objects = new ArrayList(); - // create the objects - for (int idx=0; idx < factCount; idx++) { - Account acc = new Account(); - acc.setAccountId("acc"+idx); - acc.setTitle("mr"); - acc.setStatus("standard"); - objects.add(acc); - } - Iterator itr = objects.iterator(); - long assertStart = System.currentTimeMillis(); - while( itr.hasNext() ) { - workingMemory.assertObject(itr.next()); - } - long assertEnd = System.currentTimeMillis(); - long assertet = assertEnd - assertStart; - totalassert += assertet; - System.out.println("time to assert " + assertet + - " ms"); - long fireStart = System.currentTimeMillis(); - workingMemory.fireAllRules(); - long fireEnd = System.currentTimeMillis(); - long fireet = fireEnd - fireStart; - totalfire += fireet; - System.out.println("time to fireAllRules " + fireet + - " ms"); - workingMemory.dispose(); - } - System.out.println(file); - System.out.println("number of objects asserted " + factCount); - System.out.println("average load " + (totalload/loop) + " ms"); - System.out.println("average assert " + (totalassert/loop) + " ms"); - System.out.println("average fire " + (totalfire/loop) + " ms"); - } catch (Exception e) { - e.printStackTrace(); - } - } -} Added: labs/jbossrules/trunk/drools-examples/src/main/java/org/drools/examples/conway/Cell.java =================================================================== --- labs/jbossrules/trunk/drools-examples/src/main/java/org/drools/examples/conway/Cell.java 2006-04-21 01:10:34 UTC (rev 3871) +++ labs/jbossrules/trunk/drools-examples/src/main/java/org/drools/examples/conway/Cell.java 2006-04-21 01:37:31 UTC (rev 3872) @@ -0,0 +1,125 @@ +package org.drools.examples.conway; + +import java.util.HashSet; +import java.util.Iterator; +import java.util.Set; + +/** + * A <code>Cell</code> represents a single cell within a <code>CellGrid</code>. + * A cell may be either live or dead. <p/> + * + * @author <a href="mailto:br...@oc...">Jeff Brown</a> + * @version $Id: Cell.java,v 1.3 2005/05/08 19:54:48 mproctor Exp $ + * @see CellState + * @see CellGrid + */ +public class Cell +{ + + private Set neighbors = new HashSet( ); + + private CellState state = CellState.DEAD; + + private CellState queuedState = null; + + /** + * @return the number of neighbors that this cell has + * @see #getNumberOfLiveNeighbors() + */ + public int getNumberOfNeighboringCells() + { + return neighbors.size( ); + } + + /** + * @return the number of live neighbors that this cell has + * @see #getNumberOfNeighboringCells() + */ + public int getNumberOfLiveNeighbors() + { + int numberOfLiveNeighbors = 0; + Iterator it = neighbors.iterator( ); + Cell cell = null; + while ( it.hasNext( ) ) + { + cell = (Cell) it.next( ); + if (cell.getCellState( ) == CellState.LIVE) + { + numberOfLiveNeighbors++; + } + } + return numberOfLiveNeighbors; + } + + /** + * ads a new neighbor to this neighbor + * + * @param neighbor + * new neighbor + */ + public void addNeighbor(Cell neighbor) + { + neighbors.add( neighbor ); + neighbor.neighbors.add( this ); + } + + /** + * tell this cell to queue its next live state. this is the state that this + * cell will be in after the cell is transitioned (after the next + * iteration). This transition state is necessary because of the 2 phase + * process involved in evolution. + * + * @param nextLiveState + * this cell's next live state + * @see CellState + * @see #getCellState() + * @see #transitionState() + */ + public void queueNextCellState(CellState nextLiveState) + { + if ( nextLiveState != state ) + { + queuedState = nextLiveState; + } + } + + /** + * Transitions this cell to its next state of evolution + * + * @return <code>true</code> if the state changed, otherwise false + * @see #queueNextCellState(CellState) + */ + public boolean transitionState() + { + boolean stateChanged = false; + if ( queuedState != null ) + { + state = queuedState; + queuedState = null; + stateChanged = true; + } + return stateChanged; + } + + /** + * @return this cell's current life state + * @see #queueNextCellState(org.drools.examples.conway.CellState) + * @see CellState + */ + public CellState getCellState() + { + return state; + } + + /** + * Sets this cells state + * + * @param newState + * new state for this cell + * @see CellState + */ + public void setCellState(CellState newState) + { + state = newState; + } +} Added: labs/jbossrules/trunk/drools-examples/src/main/java/org/drools/examples/conway/CellGrid.java =================================================================== --- labs/jbossrules/trunk/drools-examples/src/main/java/org/drools/examples/conway/CellGrid.java 2006-04-21 01:10:34 UTC (rev 3871) +++ labs/jbossrules/trunk/drools-examples/src/main/java/org/drools/examples/conway/CellGrid.java 2006-04-21 01:37:31 UTC (rev 3872) @@ -0,0 +1,248 @@ +package org.drools.examples.conway; + +import org.drools.RuleBase; +import org.drools.WorkingMemory; +import org.drools.examples.conway.patterns.ConwayPattern; + +/** + * A <code>CellGrid</code> represents a grid of <code>Cell</code> objects. + * <p/> + * + * @author <a href="mailto:br...@oc...">Jeff Brown</a> + * @version $Id: CellGrid.java,v 1.4 2005/05/08 19:54:48 mproctor Exp $ + * @see Cell + */ +public class CellGrid +{ + + private final Cell[][] cells; + + /** + * Constructs a CellGrid + * + * @param rows + * number of rows in the grid + * @param columns + * number of columns in the grid + */ + public CellGrid(int rows, + int columns) + { + cells = new Cell[rows][columns]; + + // populate the array of Cells and hook each + // cell up with its neighbors... + for ( int row = 0; row < rows; row++ ) + { + for ( int column = 0; column < columns; column++ ) + { + Cell newCell = new Cell( ); + cells[row][column] = newCell; + if ( row > 0 ) + { + // neighbor to the north + newCell.addNeighbor( cells[row - 1][column] ); + if ( column <= (columns - 2) ) + { + // neighbor to the northeast + newCell.addNeighbor( cells[row - 1][column + 1] ); + } + } + if ( column > 0 ) + { + // neighbor to the west + newCell.addNeighbor( cells[row][column - 1] ); + if ( row > 0 ) + { + // neighbor to the northwest + newCell.addNeighbor( cells[row - 1][column - 1] ); + } + } + } + } + } + + /** + * @param row + * row of the requested cell + * @param column + * column of the requested cell + * @return the cell at the specified coordinates + * @see Cell + */ + public Cell getCellAt(int row, + int column) + { + return cells[row][column]; + } + + /** + * @return the number of rows in this grid + * @see #getNumberOfColumns() + */ + public int getNumberOfRows() + { + return cells.length; + } + + /** + * @return the number of columns in this grid + * @see #getNumberOfRows() + */ + public int getNumberOfColumns() + { + return cells[0].length; + } + + /** + * Moves this grid to its next generation + * + * @return <code>true</code> if the state changed, otherwise false + * @see #transitionState() + */ + public boolean nextGeneration() + { + boolean didStateChange = false; + try + { + RuleBase ruleBase = ConwayRuleBaseFactory.getRuleBase( ); + WorkingMemory workingMemory = ruleBase.newWorkingMemory( ); + // for (Cell[] rowOfCells : cells) { + Cell[] rowOfCells = null; + Cell cell = null; + for ( int i = 0; i < cells.length; i++ ) + { + rowOfCells = cells[i]; + for ( int j = 0; j < rowOfCells.length; j++ ) + { + cell = rowOfCells[j]; + workingMemory.assertObject( cell ); + } + } + workingMemory.fireAllRules( ); + didStateChange = transitionState( ); + } + catch ( Exception e ) + { + e.printStackTrace( ); + } + return didStateChange; + } + + /** + * @return the number of cells in the grid that are alive + * @see CellState + */ + public int getNumberOfLiveCells() + { + int number = 0; + Cell[] rowOfCells = null; + Cell cell = null; + for ( int i = 0; i < cells.length; i++ ) + { + rowOfCells = cells[i]; + // for (Cell cell : rowOfCells) { + for ( int j = 0; j < rowOfCells.length; j++ ) + { + cell = rowOfCells[j]; + if ( cell.getCellState( ) == CellState.LIVE ) + { + number++; + } + } + } + return number; + } + + /** + * kills all cells in the grid + */ + public void killAll() + { + Cell[] rowOfCells = null; + Cell cell = null; + for ( int i = 0; i < cells.length; i++ ) + { + rowOfCells = cells[i]; + // for (Cell cell : rowOfCells) { + for ( int j = 0; j < rowOfCells.length; j++ ) + { + cell = rowOfCells[j]; + cell.setCellState( CellState.DEAD ); + } + } + } + + /** + * Transitions this grid to its next state of evolution + * + * @return <code>true</code> if the state changed, otherwise false + * @see #nextGeneration() + */ + public boolean transitionState() + { + boolean stateChanged = false; + Cell[] rowOfCells = null; + Cell cell = null; + for ( int i = 0; i < cells.length; i++ ) + { + rowOfCells = cells[i]; + // for (Cell cell : rowOfCells) { + for ( int j = 0; j < rowOfCells.length; j++ ) + { + cell = rowOfCells[j]; + stateChanged |= cell.transitionState( ); + } + } + return stateChanged; + } + + /** + * Populates the grid with a <code>ConwayPattern</code> + * + * @param pattern + * pattern to populate the grid with + * @see ConwayPattern + */ + public void setPattern(ConwayPattern pattern) + { + boolean[][] gridData = pattern.getPattern( ); + int gridWidth = gridData[0].length; + int gridHeight = gridData.length; + + int columnOffset = 0; + int rowOffset = 0; + + if ( gridWidth > getNumberOfColumns( ) ) + { + gridWidth = getNumberOfColumns( ); + } + else + { + columnOffset = (getNumberOfColumns( ) - gridWidth) / 2; + } + + if ( gridHeight > getNumberOfRows( ) ) + { + gridHeight = getNumberOfRows( ); + } + else + { + rowOffset = (getNumberOfRows( ) - gridHeight) / 2; + } + + killAll( ); + for ( int column = 0; column < gridWidth; column++ ) + { + for ( int row = 0; row < gridHeight; row++ ) + { + if ( gridData[row][column] ) + { + Cell cell = getCellAt( row + rowOffset, + column + columnOffset ); + cell.setCellState( CellState.LIVE ); + } + } + } + } +} Added: labs/jbossrules/trunk/drools-examples/src/main/java/org/drools/examples/conway/CellState.java =================================================================== --- labs/jbossrules/trunk/drools-examples/src/main/java/org/drools/examples/conway/CellState.java 2006-04-21 01:10:34 UTC (rev 3871) +++ labs/jbossrules/trunk/drools-examples/src/main/java/org/drools/examples/conway/CellState.java 2006-04-21 01:37:31 UTC (rev 3872) @@ -0,0 +1,29 @@ +package org.drools.examples.conway; + +/** + * <code>CellState</code> enumerates all of the valid states that a Cell may + * be in. + * + * @author <a href="mailto:br...@oc...">Jeff Brown</a> + * @version $Id: CellState.java,v 1.3 2005/05/08 19:54:48 mproctor Exp $ + * @see Cell + * @see CellGrid + */ +public class CellState +{ + + public static final CellState LIVE = new CellState("LIVE"); + public static final CellState DEAD = new CellState("DEAD"); + + private final String name; + + private CellState(String name) + { + this.name = name; + } + + public String toString() + { + return "CellState: " + name; + } +} Added: labs/jbossrules/trunk/drools-examples/src/main/java/org/drools/examples/conway/ConwayApplicationProperties.java =================================================================== --- labs/jbossrules/trunk/drools-examples/src/main/java/org/drools/examples/conway/ConwayApplicationProperties.java 2006-04-21 01:10:34 UTC (rev 3871) +++ labs/jbossrules/trunk/drools-examples/src/main/java/org/drools/examples/conway/ConwayApplicationProperties.java 2006-04-21 01:37:31 UTC (rev 3872) @@ -0,0 +1,32 @@ +package org.drools.examples.conway; + +import java.util.ResourceBundle; + +/** + * A utility class for retrieving application properties + * + * @author <a href="mailto:br...@oc...">Jeff Brown</a> + * @version $Id: ConwayApplicationProperties.java,v 1.2 2005/04/13 00:01:47 + * brown_j Exp $ + */ +public class ConwayApplicationProperties +{ + private static ConwayApplicationProperties ourInstance = new ConwayApplicationProperties( ); + + public static ConwayApplicationProperties getInstance() + { + return ourInstance; + } + + private final ResourceBundle resources; + + private ConwayApplicationProperties() + { + resources = ResourceBundle.getBundle( "conway" ); + } + + public static String getProperty(String propertyName) + { + return ourInstance.resources.getString( propertyName ); + } +} Added: labs/jbossrules/trunk/drools-examples/src/main/java/org/drools/examples/conway/ConwayRuleBaseFactory.java =================================================================== --- labs/jbossrules/trunk/drools-examples/src/main/java/org/drools/examples/conway/ConwayRuleBaseFactory.java 2006-04-21 01:10:34 UTC (rev 3871) +++ labs/jbossrules/trunk/drools-examples/src/main/java/org/drools/examples/conway/ConwayRuleBaseFactory.java 2006-04-21 01:37:31 UTC (rev 3872) @@ -0,0 +1,70 @@ +/** + * + */ +package org.drools.examples.conway; + +import java.io.InputStreamReader; +import java.io.Reader; + +import org.drools.RuleBase; +import org.drools.compiler.PackageBuilder; +import org.drools.rule.Package; + + +/** + * @author <a href="mailto:br...@oc...">Jeff Brown</a> + * @version $Id: RuleBaseFactory.java,v 1.4 2005/11/25 02:35:33 mproctor Exp $ + */ +public class ConwayRuleBaseFactory { + + private static ConwayRuleBaseFactory ourInstance = new ConwayRuleBaseFactory (); + + private RuleBase ruleBase; + + public static ConwayRuleBaseFactory getInstance() { + return ourInstance; + } + + private ConwayRuleBaseFactory () { + try { + /** + * Please note that this is the "low level" rule assembly API. + */ + // private static RuleBase readRule() throws Exception { + // read in the source + Reader source = new InputStreamReader(ConwayRuleBaseFactory.class + .getResourceAsStream("/conway.drl")); + + // optionally read in the DSL (if you are using it). + Reader dsl = new InputStreamReader(ConwayRuleBaseFactory.class + .getResourceAsStream("/conway.dsl")); + + // Use package builder to build up a rule package. + // An alternative lower level class called "DrlParser" can also be + // used... + + PackageBuilder builder = new PackageBuilder(); + + // this wil parse and compile in one step + // NOTE: There are 2 methods here, the one argument one is for + // normal DRL. + // builder.addPackageFromDrl( source ); + + // Use the following instead of above if you are using a DSL: + builder.addPackageFromDrl(source, dsl); + + // get the compiled package (which is serializable) + Package pkg = builder.getPackage(); + + // add the package to a rulebase (deploy the rule package). + ruleBase = org.drools.RuleBaseFactory.newRuleBase(); + ruleBase.addPackage(pkg); + } catch (Exception e) { + e.printStackTrace(); + } + } + + public static RuleBase getRuleBase() { + return ourInstance.ruleBase; + } +} Added: labs/jbossrules/trunk/drools-examples/src/main/java/org/drools/examples/conway/patterns/Border.java =================================================================== --- labs/jbossrules/trunk/drools-examples/src/main/java/org/drools/examples/conway/patterns/Border.java 2006-04-21 01:10:34 UTC (rev 3871) +++ labs/jbossrules/trunk/drools-examples/src/main/java/org/drools/examples/conway/patterns/Border.java 2006-04-21 01:37:31 UTC (rev 3872) @@ -0,0 +1,71 @@ +package org.drools.examples.conway.patterns; + +/** + * The Border <p/> + * + * @see ConwayPattern + * @see org.drools.examples.conway.CellGrid + * @version $Id: Border.java,v 1.3 2005/05/08 19:54:48 mproctor Exp $ + * @author <a href="mailto:br...@oc...">Jeff Brown</a> + */ +public class Border + implements + ConwayPattern +{ + + private boolean[][] grid = {{true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true}, + {true, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, true}, + {true, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, true}, + {true, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, true}, + {true, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, true}, + {true, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, true}, + {true, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, true}, + {true, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, true}, + {true, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, true}, + {true, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, true}, + {true, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, true}, + {true, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, true}, + {true, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, true}, + {true, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, true}, + {true, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, true}, + {true, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, true}, + {true, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, true}, + {true, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, true}, + {true, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, true}, + {true, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, true}, + {true, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, true}, + {true, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, true}, + {true, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, true}, + {true, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, true}, + {true, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, true}, + {true, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, true}, + {true, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, true}, + {true, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, true}, + {true, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, true}, + {true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true}}; + + /** + * This method should return a 2 dimensional array of boolean that represent + * a conway grid, with <code>true</code> values in the positions where + * cells are alive + * + * @return array representing a conway grid + */ + public boolean[][] getPattern() + { + return grid; + } + + /** + * @return the name of this pattern + */ + public String getPatternName() + { + return "Border"; + } + + public String toString() + { + return getPatternName( ); + } +} Added: labs/jbossrules/trunk/drools-examples/src/main/java/org/drools/examples/conway/patterns/ConwayPattern.java =================================================================== --- labs/jbossrules/trunk/drools-examples/src/main/java/org/drools/examples/conway/patterns/ConwayPattern.java 2006-04-21 01:10:34 UTC (rev 3871) +++ labs/jbossrules/trunk/drools-examples/src/main/java/org/drools/examples/conway/patterns/ConwayPattern.java 2006-04-21 01:37:31 UTC (rev 3872) @@ -0,0 +1,31 @@ +package org.drools.examples.conway.patterns; + +import java.io.Serializable; + +/** + * A <code>ConwayPattern</code> describes the state of a conway grid. + * <code>ConwayPattern</code> objects are useful for persisting grid states + * for recall later. + * + * @version $Id: ConwayPattern.java,v 1.3 2005/05/08 19:54:48 mproctor Exp $ + * @author <a href="mailto:br...@oc...">Jeff Brown</a> + */ +public interface ConwayPattern + extends + Serializable +{ + + /** + * This method should return a 2 dimensional array of boolean that represent + * a conway grid, with <code>true</code> values in the positions where + * cells are alive + * + * @return array representing a conway grid + */ + public boolean[][] getPattern(); + + /** + * @return the name of this pattern + */ + public String getPatternName(); +} Added: labs/jbossrules/trunk/drools-examples/src/main/java/org/drools/examples/conway/patterns/Hi.java =================================================================== --- labs/jbossrules/trunk/drools-examples/src/main/java/org/drools/examples/conway/patterns/Hi.java 2006-04-21 01:10:34 UTC (rev 3871) +++ labs/jbossrules/trunk/drools-examples/src/main/java/org/drools/examples/conway/patterns/Hi.java 2006-04-21 01:37:31 UTC (rev 3872) @@ -0,0 +1,47 @@ +package org.drools.examples.conway.patterns; + +/** + * The Hi pattern <p/> + * + * @see ConwayPattern + * @see org.drools.examples.conway.CellGrid + * + * @version $Id: Hi.java,v 1.3 2005/05/08 19:54:48 mproctor Exp $ + * @author <a href="mailto:br...@oc...">Jeff Brown</a> + */ +public class Hi + implements + ConwayPattern +{ + + private boolean[][] grid = {{true, false, false, false, true, false, false, true, true, true, true, true, true, true}, {true, false, false, false, true, false, false, false, false, false, true, false, false, false}, + {true, false, false, false, true, false, false, false, false, false, true, false, false, false}, {true, false, false, false, true, false, false, false, false, false, true, false, false, false}, + {true, true, true, true, true, false, false, false, false, false, true, false, false, false}, {true, false, false, false, true, false, false, false, false, false, true, false, false, false}, + {true, false, false, false, true, false, false, false, false, false, true, false, false, false}, {true, false, false, false, true, false, false, false, false, false, true, false, false, false}, + {true, false, false, false, true, false, false, true, true, true, true, true, true, true}}; + + /** + * This method should return a 2 dimensional array of boolean that represent + * a conway grid, with <code>true</code> values in the positions where + * cells are alive + * + * @return array representing a conway grid + */ + public boolean[][] getPattern() + { + return grid; + } + + /** + * @return the name of this pattern + */ + public String getPatternName() + { + return "Hi"; + } + + public String toString() + { + return getPatternName( ); + } +} Added: labs/jbossrules/trunk/drools-examples/src/main/java/org/drools/examples/conway/patterns/Pentadecathalon.java =================================================================== --- labs/jbossrules/trunk/drools-examples/src/main/java/org/drools/examples/conway/patterns/Pentadecathalon.java 2006-04-21 01:10:34 UTC (rev 3871) +++ labs/jbossrules/trunk/drools-examples/src/main/java/org/drools/examples/conway/patterns/Pentadecathalon.java 2006-04-21 01:37:31 UTC (rev 3872) @@ -0,0 +1,43 @@ +package org.drools.examples.conway.patterns; + +/** + * The Pentadecathalon <p/> + * + * @see ConwayPattern + * @see org.drools.examples.conway.CellGrid + * + * @version $Id: Pentadecathalon.java,v 1.3 2005/05/08 19:54:48 mproctor Exp $ + * @author <a href="mailto:br...@oc...">Jeff Brown</a> + */ +public class Pentadecathalon + implements + ConwayPattern +{ + + private boolean[][] grid = {{true, true, true, true, true, true, true, true, true}}; + + /** + * This method should return a 2 dimensional array of boolean that represent + * a conway grid, with <code>true</code> values in the positions where + * cells are alive + * + * @return array representing a conway grid + */ + public boolean[][] getPattern() + { + return grid; + } + + /** + * @return the name of this pattern + */ + public String getPatternName() + { + return "Pentadecathalon"; + } + + public String toString() + { + return getPatternName( ); + } +} Added: labs/jbossrules/trunk/drools-examples/src/main/java/org/drools/examples/conway/patterns/Pulsar.java =================================================================== --- labs/jbossrules/trunk/drools-examples/src/main/java/org/drools/examples/conway/patterns/Pulsar.java 2006-04-21 01:10:34 UTC (rev 3871) +++ labs/jbossrules/trunk/drools-examples/src/main/java/org/drools/examples/conway/patterns/Pulsar.java 2006-04-21 01:37:31 UTC (rev 3872) @@ -0,0 +1,42 @@ +package org.drools.examples.conway.patterns; + +/** + * The Pulsar <p/> + * + * @see ConwayPattern + * @see org.drools.examples.conway.CellGrid + * @version $Id: Pulsar.java,v 1.3 2005/05/08 19:54:48 mproctor Exp $ + * @author <a href="mailto:br...@oc...">Jeff Brown</a> + */ +public class Pulsar + implements + ConwayPattern +{ + + private boolean[][] grid = {{false, true, false}, {true, true, true}, {true, false, true}, {true, true, true}, {false, true, false}}; + + /** + * This method should return a 2 dimensional array of boolean that represent + * a conway grid, with <code>true</code> values in the positions where + * cells are alive + * + * @return array representing a conway grid + */ + public boolean[][] getPattern() + { + return grid; + } + + /** + * @return the name of this pattern + */ + public String getPatternName() + { + return "Pulsar"; + } + + public String toString() + { + return getPatternName( ); + } +} Added: labs/jbossrules/trunk/drools-examples/src/main/java/org/drools/examples/conway/patterns/SimpleGlider.java =================================================================== --- labs/jbossrules/trunk/drools-examples/src/main/java/org/drools/examples/conway/patterns/SimpleGlider.java 2006-04-21 01:10:34 UTC (rev 3871) +++ labs/jbossrules/trunk/drools-examples/src/main/java/org/drools/examples/conway/patterns/SimpleGlider.java 2006-04-21 01:37:31 UTC (rev 3872) @@ -0,0 +1,42 @@ +package org.drools.examples.conway.patterns; + +/** + * Represents a simple glider + * + * @author <a href="mailto:br...@oc...">Jeff Brown</a> + * @version $Id: SimpleGlider.java,v 1.3 2005/05/08 19:54:48 mproctor Exp $ + * @see ConwayPattern + * @see org.drools.examples.conway.CellGrid + */ +public class SimpleGlider + implements + ConwayPattern +{ + + private boolean[][] grid = {{false, true, false}, {true, false, false}, {true, true, true}}; + + /** + * This method should return a 2 dimensional array of boolean that represent + * a conway grid, with <code>true</code> values in the positions where + * cells are alive + * + * @return array representing a conway grid + */ + public boolean[][] getPattern() + { + return grid; + } + + /** + * @return the name of this pattern + */ + public String getPatternName() + { + return "Simple Glider"; + } + + public String toString() + { + return getPatternName( ); + } +} Added: labs/jbossrules/trunk/drools-examples/src/main/java/org/drools/examples/conway/ui/CellGridCanvas.java =================================================================== --- labs/jbossrules/trunk/drools-examples/src/main/java/org/drools/examples/conway/ui/CellGridCanvas.java 2006-04-21 01:10:34 UTC (rev 3871) +++ labs/jbossrules/trunk/drools-examples/src/main/java/org/drools/examples/conway/ui/CellGridCanvas.java 2006-04-21 01:37:31 UTC (rev 3872) @@ -0,0 +1,217 @@ +package org.drools.examples.conway.ui; + + +import javax.swing.*; + +import org.drools.examples.conway.Cell; +import org.drools.examples.conway.CellGrid; +import org.drools.examples.conway.CellState; + +import java.awt.*; +import java.awt.event.MouseAdapter; +import java.awt.event.MouseEvent; +import java.awt.event.MouseMotionAdapter; + +/** + * @author <a href="mailto:br...@oc...">Jeff Brown</a> + * @version $Id: CellGridCanvas.java,v 1.3 2005/05/08 19:54:48 mproctor Exp $ + */ +public class CellGridCanvas extends Canvas +{ + private Image offScreenImage; + private Image backgroundImage; + private final int cellSize; + private final CellGrid cellGrid; + private final Image liveCellImage = new ImageIcon( CellGridCanvas.class.getResource( "liveCellImage.gif" ) ).getImage( ); + + private static final Color BACKGROUND_COLOR = Color.gray; + private static final Color GRID_COLOR = BACKGROUND_COLOR.brighter( ); + + /** + * Constructs a CellGridCanvas. + * + * @param cellGrid + * the GoL cellgrid + */ + public CellGridCanvas(CellGrid cellGrid) + { + this.cellGrid = cellGrid; + this.cellSize = liveCellImage.getWidth( this ); + + setBackground( GRID_COLOR ); + + addMouseListener( new MouseAdapter( ) { + /** + * Invoked when a mouse button has been pressed on a component. + */ + public void mousePressed(MouseEvent e) + { + toggleCellAt( e.getX( ), + e.getY( ) ); + } + } ); + + addMouseMotionListener( new MouseMotionAdapter( ) { + + public void mouseDragged(MouseEvent e) + { + Cell cell = getCellAtPoint( e.getX( ), + e.getY( ) ); + if ( cell != null ) + { + cell.setCellState( CellState.LIVE ); + repaint( ); + } + } + } ); + } + + private void toggleCellAt(int x, + int y) + { + Cell cell = getCellAtPoint( x, + y ); + if ( cell != null ) + { + if ( cell.getCellState( ) == CellState.LIVE ) + { + cell.setCellState( CellState.DEAD ); + } + else + { + cell.setCellState( CellState.LIVE ); + } + repaint( ); + } + } + + private Cell getCellAtPoint(int x, + int y) + { + Cell cell = null; + + int column = x / cellSize; + int row = y / cellSize; + final int numberOfColumns = cellGrid.getNumberOfColumns( ); + final int numberOfRows = cellGrid.getNumberOfRows( ); + + if ( column >= 0 && column < numberOfColumns && row >= 0 && row < numberOfRows ) + { + cell = cellGrid.getCellAt( row, + column ); + } + + return cell; + } + + /** + * Use double buffering. + * + * @see java.awt.Component#update(java.awt.Graphics) + */ + public void update(Graphics g) + { + Dimension d = getSize( ); + if ( (offScreenImage == null) ) + { + offScreenImage = createImage( d.width, + d.height ); + } + paint( offScreenImage.getGraphics( ) ); + g.drawImage( offScreenImage, + 0, + 0, + null ); + } + + /** + * Draw this generation. + * + * @see java.awt.Component#paint(java.awt.Graphics) + */ + public void paint(Graphics g) + { + // Draw grid on background image, which is faster + final int numberOfColumns = cellGrid.getNumberOfColumns( ); + final int numberOfRows = cellGrid.getNumberOfRows( ); + if ( backgroundImage == null ) + { + Dimension d = getSize( ); + backgroundImage = createImage( d.width, + d.height ); + Graphics backgroundImageGraphics = backgroundImage.getGraphics( ); + // draw background (MSIE doesn't do that) + backgroundImageGraphics.setColor( getBackground( ) ); + backgroundImageGraphics.fillRect( 0, + 0, + d.width, + d.height ); + backgroundImageGraphics.setColor( BACKGROUND_COLOR ); + backgroundImageGraphics.fillRect( 0, + 0, + cellSize * numberOfColumns - 1, + cellSize * numberOfRows - 1 ); + backgroundImageGraphics.setColor( GRID_COLOR ); + for ( int x = 1; x < numberOfColumns; x++ ) + { + backgroundImageGraphics.drawLine( x * cellSize - 1, + 0, + x * cellSize - 1, + cellSize * numberOfRows - 1 ); + } + for ( int y = 1; y < numberOfRows; y++ ) + { + backgroundImageGraphics.drawLine( 0, + y * cellSize - 1, + cellSize * numberOfColumns - 1, + y * cellSize - 1 ); + } + } + g.drawImage( backgroundImage, + 0, + 0, + null ); + + // draw populated cells + for ( int row = 0; row < numberOfRows; row++ ) + { + for ( int column = 0; column < numberOfColumns; column++ ) + { + Cell cell = cellGrid.getCellAt( row, + column ); + if ( cell.getCellState( ) == CellState.LIVE ) + { + g.drawImage( liveCellImage, + column * cellSize, + row * cellSize, + this ); + } + } + } + } + + /** + * This is the preferred size. + * + * @see java.awt.Component#getPreferredSize() + */ + public Dimension getPreferredSize() + { + final int numberOfColumns = cellGrid.getNumberOfColumns( ); + final int numberOfRows = cellGrid.getNumberOfRows( ); + return new Dimension( cellSize * numberOfColumns, + cellSize * numberOfRows ); + } + + /** + * This is the minimum size (size of one cell). + * + * @see java.awt.Component#getMinimumSize() + */ + public Dimension getMinimumSize() + { + return new Dimension( cellSize, + cellSize ); + } + +} Added: labs/jbossrules/trunk/drools-examples/src/main/java/org/drools/examples/conway/ui/ConwayGUI.java =================================================================== --- labs/jbossrules/trunk/drools-examples/src/main/java/org/drools/examples/conway/ui/ConwayGUI.java 2006-04-21 01:10:34 UTC (rev 3871) +++ labs/jbossrules/trunk/drools-examples/src/main/java/org/drools/examples/conway/ui/ConwayGUI.java 2006-04-21 01:37:31 UTC (rev 3872) @@ -0,0 +1,267 @@ +package org.drools.examples.conway.ui; + +import com.jgoodies.forms.builder.PanelBuilder; +import com.jgoodies.forms.factories.ButtonBarFactory; +import com.jgoodies.forms.layout.CellConstraints; +import com.jgoodies.forms.layout.FormLayout; +import foxtrot.Job; +import foxtrot.Worker; + +import javax.swing.*; +import javax.swing.border.Border; +import javax.swing.border.EtchedBorder; + +import org.drools.examples.conway.CellGrid; +import org.drools.examples.conway.ConwayApplicationProperties; +import org.drools.examples.conway.patterns.ConwayPattern; + +import java.awt.*; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.util.StringTokenizer; + +/** + * @author <a href="mailto:br...@oc...">Jeff Brown</a> + * @version $Id: ConwayGUI.java,v 1.3 2005/05/08 19:54:48 mproctor Exp $ + */ +public class ConwayGUI extends JPanel +{ + private final JButton nextGenerationButton; + private final JButton startStopButton; + private final JButton clearButton; + private final JComboBox patternSelector = new JComboBox( ); + private final Timer timer; + + public ConwayGUI() + { + super( new BorderLayout( ) ); + final String nextGenerationLabel = ConwayApplicationProperties.getProperty( "next.generation.label" ); + nextGenerationButton = new JButton( nextGenerationLabel ); + final String startLabel = ConwayApplicationProperties.getProperty( "start.label" ); + startStopButton = new JButton( startLabel ); + final String clearLabel = ConwayApplicationProperties.getProperty( "clear.label" ); + clearButton = new JButton( clearLabel ); + final CellGrid grid = new CellGrid( 30, + 30 ); + final CellGridCanvas canvas = new CellGridCanvas( grid ); + JPanel panel = new JPanel( new BorderLayout( ) ); + panel.add( BorderLayout.CENTER, + canvas ); + Border etchedBorder = BorderFactory.createEtchedBorder( EtchedBorder.LOWERED ); + Border outerBlankBorder = BorderFactory.createEmptyBorder( 5, + 5, + 5, + 5 ); + Border innerBlankBorder = BorderFactory.createEmptyBorder( 5, + 5, + 5, + 5 ); + Border border = BorderFactory.createCompoundBorder( BorderFactory.createCompoundBorder( outerBlankBorder, + etchedBorder ), + innerBlankBorder ); + panel.setBorder( border ); + add( BorderLayout.CENTER, + panel ); + add( BorderLayout.EAST, + createControlPanel( ) ); + nextGenerationButton.addActionListener( new ActionListener( ) { + public void actionPerformed(ActionEvent e) + { + Worker.post( new Job( ) { + public Object run() + { + grid.nextGeneration( ); + return null; + } + } ); + canvas.repaint( ); + } + } ); + clearButton.addActionListener( new ActionListener( ) { + public void actionPerformed(ActionEvent e) + { + Worker.post( new Job( ) { + public Object run() + { + grid.killAll( ); + return null; + } + } ); + canvas.repaint( ); + } + } ); + + ActionListener timerAction = new ActionListener( ) { + public void actionPerformed(ActionEvent ae) + { + Worker.post( new Job( ) { + public Object run() + { + if ( !grid.nextGeneration( ) ) + { + stopTimer( ); + } + return null; + } + } ); + canvas.repaint( ); + } + }; + timer = new Timer( 500, + timerAction ); + startStopButton.addActionListener( new ActionListener( ) { + public void actionPerformed(ActionEvent e) + { + if ( timer.isRunning( ) ) + { + st... [truncated message content] |