[jboss-svn-commits] JBL Code SVN: r11005 - in labs/jbossrules/trunk/drools-examples: src/main/java/org/drools/examples/conway and 3 other directories.

jboss-svn-commits at lists.jboss.org jboss-svn-commits at lists.jboss.org
Mon Apr 16 09:37:21 EDT 2007


Author: mark.proctor at jboss.com
Date: 2007-04-16 09:37:20 -0400 (Mon, 16 Apr 2007)
New Revision: 11005

Added:
   labs/jbossrules/trunk/drools-examples/src/main/java/org/drools/examples/conway/Neighbor.java
   labs/jbossrules/trunk/drools-examples/src/main/java/org/drools/examples/conway/Phase.java
Modified:
   labs/jbossrules/trunk/drools-examples/.project
   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/ConwayRuleBaseFactory.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/ConwayGUI.java
   labs/jbossrules/trunk/drools-examples/src/main/rules/org/drools/examples/conway/conway.drl
Log:
JBRULES-800 Move conways game of life to a stateful example
-Stateful example using agenda-groups

Modified: labs/jbossrules/trunk/drools-examples/.project
===================================================================
--- labs/jbossrules/trunk/drools-examples/.project	2007-04-16 13:34:04 UTC (rev 11004)
+++ labs/jbossrules/trunk/drools-examples/.project	2007-04-16 13:37:20 UTC (rev 11005)
@@ -1,18 +1,22 @@
-<projectDescription>
-  <name>drools-examples</name>
-  <comment/>
-  <projects/>
-  <buildSpec>
-    <buildCommand>
-      <name>org.eclipse.jdt.core.javabuilder</name>
-      <arguments/>
-    </buildCommand>
-    <buildCommand>
-      <name>org.drools.ide.droolsbuilder</name>
-      <arguments/>
-    </buildCommand>
-  </buildSpec>
-  <natures>
-    <nature>org.eclipse.jdt.core.javanature</nature>
-  </natures>
-</projectDescription>
\ No newline at end of file
+<?xml version="1.0" encoding="UTF-8"?>
+<projectDescription>
+	<name>drools-examples</name>
+	<comment></comment>
+	<projects>
+	</projects>
+	<buildSpec>
+		<buildCommand>
+			<name>org.eclipse.jdt.core.javabuilder</name>
+			<arguments>
+			</arguments>
+		</buildCommand>
+		<buildCommand>
+			<name>org.drools.eclipse.droolsbuilder</name>
+			<arguments>
+			</arguments>
+		</buildCommand>		
+	</buildSpec>
+	<natures>
+		<nature>org.eclipse.jdt.core.javanature</nature>
+	</natures>
+</projectDescription>

Modified: 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	2007-04-16 13:34:04 UTC (rev 11004)
+++ labs/jbossrules/trunk/drools-examples/src/main/java/org/drools/examples/conway/Cell.java	2007-04-16 13:37:20 UTC (rev 11005)
@@ -1,9 +1,5 @@
 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/>
@@ -14,89 +10,53 @@
  */
 public class Cell {
 
-    private final Set       neighbors   = new HashSet();
+    private CellState cellState = CellState.DEAD;
 
-    private CellState state       = CellState.DEAD;
+    private int       phase     = Phase.DONE;
 
-    private CellState queuedState = null;
+    private int       liveNeighbors;
 
-    /**
-     * @return the number of neighbors that this cell has
-     * @see #getNumberOfLiveNeighbors()
-     */
-    public int getNumberOfNeighboringCells() {
-        return this.neighbors.size();
+    private int       col;
+
+    private int       row;
+
+    public Cell(int col,
+                int row) {
+        this.col = col;
+        this.row = row;
     }
 
-    /**
-     * @return the number of live neighbors that this cell has
-     * @see #getNumberOfNeighboringCells()
-     */
-    public int getNumberOfLiveNeighbors() {
-        int numberOfLiveNeighbors = 0;
-        final Iterator it = this.neighbors.iterator();
-        Cell cell = null;
-        while ( it.hasNext() ) {
-            cell = (Cell) it.next();
-            if ( cell.getCellState() == CellState.LIVE ) {
-                numberOfLiveNeighbors++;
-            }
-        }
-        return numberOfLiveNeighbors;
+    public int getCol() {
+        return col;
     }
 
-    /**
-     * ads a new neighbor to this neighbor
-     *
-     * @param neighbor
-     *            new neighbor
-     */
-    public void addNeighbor(final Cell neighbor) {
-        this.neighbors.add( neighbor );
-        neighbor.neighbors.add( this );
+    public int getRow() {
+        return row;
     }
 
-    /**
-     * 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(final CellState nextLiveState) {
-        if ( nextLiveState != this.state ) {
-            this.queuedState = nextLiveState;
-        }
+    public int getPhase() {
+        return this.phase;
     }
 
-    /**
-     * 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 ( this.queuedState != null ) {
-            this.state = this.queuedState;
-            this.queuedState = null;
-            stateChanged = true;
-        }
-        return stateChanged;
+    public void setPhase(int phase) {
+        this.phase = phase;
     }
 
+    public int getLiveNeighbors() {
+        return this.liveNeighbors;
+    }
+
+    public void setLiveNeighbors(int liveNeighbors) {
+        this.liveNeighbors = liveNeighbors;
+    }
+
     /**
      * @return this cell's current life state
      * @see #queueNextCellState(org.drools.examples.conway.CellState)
      * @see CellState
      */
     public CellState getCellState() {
-        return this.state;
+        return this.cellState;
     }
 
     /**
@@ -107,6 +67,10 @@
      * @see CellState
      */
     public void setCellState(final CellState newState) {
-        this.state = newState;
+        this.cellState = newState;
     }
+
+    public String toString() {
+        return cellState + " col=" + this.col + " row=" + this.row + " phase '" + phase + "' liveNeighbors '" + liveNeighbors + "'";
+    }
 }

Modified: 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	2007-04-16 13:34:04 UTC (rev 11004)
+++ labs/jbossrules/trunk/drools-examples/src/main/java/org/drools/examples/conway/CellGrid.java	2007-04-16 13:37:20 UTC (rev 11005)
@@ -2,6 +2,8 @@
 
 import org.drools.RuleBase;
 import org.drools.WorkingMemory;
+import org.drools.event.AgendaGroupPoppedEvent;
+import org.drools.event.DefaultAgendaEventListener;
 import org.drools.examples.conway.patterns.ConwayPattern;
 
 /**
@@ -15,6 +17,8 @@
 
     private final Cell[][] cells;
 
+    private WorkingMemory  workingMemory;
+
     /**
      * Constructs a CellGrid
      * 
@@ -27,30 +31,34 @@
                     final int columns) {
         this.cells = new Cell[rows][columns];
 
+        final RuleBase ruleBase = ConwayRuleBaseFactory.getRuleBase();
+        this.workingMemory = ruleBase.newWorkingMemory();
+
+        DefaultAgendaEventListener listener = new DefaultAgendaEventListener() {
+            public void agendaGroupPopped(AgendaGroupPoppedEvent event,
+                                          WorkingMemory workingMemory) {
+                System.out.println( "popped AgendaGroup = '" + event.getAgendaGroup().getName() + "'" );
+                System.out.println( CellGrid.this.toString() );
+                System.out.println( "" );
+            }
+        };
+
+        this.workingMemory.addEventListener( listener );
+
+        this.workingMemory.assertObject( this );
+
         // 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++ ) {
-                final Cell newCell = new Cell();
+                final Cell newCell = new Cell( column,
+                                               row );
                 this.cells[row][column] = newCell;
-                if ( row > 0 ) {
-                    // neighbor to the north
-                    newCell.addNeighbor( this.cells[row - 1][column] );
-                    if ( column <= (columns - 2) ) {
-                        // neighbor to the northeast
-                        newCell.addNeighbor( this.cells[row - 1][column + 1] );
-                    }
-                }
-                if ( column > 0 ) {
-                    // neighbor to the west
-                    newCell.addNeighbor( this.cells[row][column - 1] );
-                    if ( row > 0 ) {
-                        // neighbor to the northwest
-                        newCell.addNeighbor( this.cells[row - 1][column - 1] );
-                    }
-                }
+                this.workingMemory.assertObject( newCell );
             }
         }
+        this.workingMemory.setFocus( "register neighbor" );
+        this.workingMemory.fireAllRules();
     }
 
     /**
@@ -89,87 +97,28 @@
      * @see #transitionState()
      */
     public boolean nextGeneration() {
-        boolean didStateChange = false;
-        try {
-            final RuleBase ruleBase = ConwayRuleBaseFactory.getRuleBase();
-            final WorkingMemory workingMemory = ruleBase.newWorkingMemory();
-            // for (Cell[] rowOfCells : cells) {
-            Cell[] rowOfCells = null;
-            Cell cell = null;
-            for ( int i = 0; i < this.cells.length; i++ ) {
-                rowOfCells = this.cells[i];
-                for ( int j = 0; j < rowOfCells.length; j++ ) {
-                    cell = rowOfCells[j];
-                    workingMemory.assertObject( cell );
-                }
-            }
-            workingMemory.fireAllRules();
-            didStateChange = transitionState();
-        } catch ( final Exception e ) {
-            e.printStackTrace();
-        }
-        return didStateChange;
+        System.out.println( "next generation" );
+        workingMemory.setFocus( "calculate" );
+        workingMemory.setFocus( "kill" );
+        workingMemory.setFocus( "birth" );
+        workingMemory.setFocus( "reset calculate" );
+        workingMemory.setFocus( "rest" );
+        workingMemory.setFocus( "evaluate" );
+        workingMemory.fireAllRules();
+        return workingMemory.getAgenda().getAgendaGroup( "evaluate" ).size() != 0;
     }
 
     /**
-     * @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 < this.cells.length; i++ ) {
-            rowOfCells = this.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 < this.cells.length; i++ ) {
-            rowOfCells = this.cells[i];
-            // for (Cell cell : rowOfCells) {
-            for ( int j = 0; j < rowOfCells.length; j++ ) {
-                cell = rowOfCells[j];
-                cell.setCellState( CellState.DEAD );
-            }
-        }
+        this.workingMemory.setFocus( "calculate" );
+        this.workingMemory.setFocus( "kill all" );
+        this.workingMemory.setFocus( "reset calculate" );
+        this.workingMemory.fireAllRules();
     }
 
     /**
-     * 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 < this.cells.length; i++ ) {
-            rowOfCells = this.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
@@ -197,14 +146,34 @@
         }
 
         killAll();
+
         for ( int column = 0; column < gridWidth; column++ ) {
             for ( int row = 0; row < gridHeight; row++ ) {
                 if ( gridData[row][column] ) {
                     final Cell cell = getCellAt( row + rowOffset,
-                                           column + columnOffset );
+                                                 column + columnOffset );
                     cell.setCellState( CellState.LIVE );
+                    this.workingMemory.modifyObject( this.workingMemory.getFactHandle( cell ),
+                                                     cell );
                 }
             }
         }
+        workingMemory.setFocus( "calculate" );
+        workingMemory.fireAllRules();
+        System.out.println( "" );
     }
+
+    public String toString() {
+        StringBuffer buf = new StringBuffer();
+
+        for ( int i = 0; i < this.cells.length; i++ ) {
+            for ( int j = 0; j < this.cells[i].length; j++ ) {
+                Cell cell = this.cells[i][j];
+                System.out.print( cell.getLiveNeighbors() + ((cell.getCellState() == CellState.DEAD) ? "D" : "L") + " " );
+            }
+            System.out.println( "" );
+        }
+
+        return buf.toString();
+    }
 }

Modified: 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	2007-04-16 13:34:04 UTC (rev 11004)
+++ labs/jbossrules/trunk/drools-examples/src/main/java/org/drools/examples/conway/ConwayRuleBaseFactory.java	2007-04-16 13:37:20 UTC (rev 11005)
@@ -33,7 +33,7 @@
             final Reader source = new InputStreamReader( ConwayRuleBaseFactory.class.getResourceAsStream( "/org/drools/examples/conway/conway.drl" ) );
 
             // optionally read in the DSL (if you are using it).
-            final Reader dsl = new InputStreamReader( ConwayRuleBaseFactory.class.getResourceAsStream( "/org/drools/examples/conway/conway.dsl" ) );
+            //final Reader dsl = new InputStreamReader( ConwayRuleBaseFactory.class.getResourceAsStream( "/org/drools/examples/conway/conway.dsl" ) );
 
             // Use package builder to build up a rule package.
             // An alternative lower level class called "DrlParser" can also be
@@ -47,9 +47,11 @@
             // builder.addPackageFromDrl( source );
 
             // Use the following instead of above if you are using a DSL:
-            builder.addPackageFromDrl( source,
-                                       dsl );
+            builder.addPackageFromDrl( source );//,
+                                       //dsl );
 
+            
+            
             // get the compiled package (which is serializable)
             final Package pkg = builder.getPackage();
 

Added: labs/jbossrules/trunk/drools-examples/src/main/java/org/drools/examples/conway/Neighbor.java
===================================================================
--- labs/jbossrules/trunk/drools-examples/src/main/java/org/drools/examples/conway/Neighbor.java	                        (rev 0)
+++ labs/jbossrules/trunk/drools-examples/src/main/java/org/drools/examples/conway/Neighbor.java	2007-04-16 13:37:20 UTC (rev 11005)
@@ -0,0 +1,24 @@
+package org.drools.examples.conway;
+
+public class Neighbor {
+    private Cell cell;
+    private Cell neighbor;
+    
+    public Neighbor(Cell cell, Cell neighbor) {
+        this.cell = cell;
+        this.neighbor = neighbor;
+    }
+
+    public Cell getCell() {
+        return cell;
+    }
+
+    public Cell getNeighbor() {
+        return neighbor;
+    }       
+    
+    public String toString() {
+        return "cell '"+ this.cell + "' neighbour '" + this.neighbor + "'"; 
+    }
+    
+}

Added: labs/jbossrules/trunk/drools-examples/src/main/java/org/drools/examples/conway/Phase.java
===================================================================
--- labs/jbossrules/trunk/drools-examples/src/main/java/org/drools/examples/conway/Phase.java	                        (rev 0)
+++ labs/jbossrules/trunk/drools-examples/src/main/java/org/drools/examples/conway/Phase.java	2007-04-16 13:37:20 UTC (rev 11005)
@@ -0,0 +1,8 @@
+package org.drools.examples.conway;
+
+public class Phase {
+    public static final int EVALUATE = 0;
+    public static final int KILL     = 1;
+    public static final int BIRTH    = 2;
+    public static final int DONE     = 3;
+}

Modified: 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	2007-04-16 13:34:04 UTC (rev 11004)
+++ labs/jbossrules/trunk/drools-examples/src/main/java/org/drools/examples/conway/patterns/SimpleGlider.java	2007-04-16 13:37:20 UTC (rev 11005)
@@ -12,6 +12,7 @@
     ConwayPattern {
 
     private final boolean[][] grid = {{false, true, false}, {true, false, false}, {true, true, true}};
+    //private final boolean[][] grid = {{false, false, false}, {true, true, false}, {false, false, false}};
 
     /**
      * This method should return a 2 dimensional array of boolean that represent

Modified: 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	2007-04-16 13:34:04 UTC (rev 11004)
+++ labs/jbossrules/trunk/drools-examples/src/main/java/org/drools/examples/conway/ui/ConwayGUI.java	2007-04-16 13:37:20 UTC (rev 11005)
@@ -52,16 +52,16 @@
                    canvas );
         final Border etchedBorder = BorderFactory.createEtchedBorder( EtchedBorder.LOWERED );
         final Border outerBlankBorder = BorderFactory.createEmptyBorder( 5,
-                                                                   5,
-                                                                   5,
-                                                                   5 );
+                                                                         5,
+                                                                         5,
+                                                                         5 );
         final Border innerBlankBorder = BorderFactory.createEmptyBorder( 5,
-                                                                   5,
-                                                                   5,
-                                                                   5 );
+                                                                         5,
+                                                                         5,
+                                                                         5 );
         final Border border = BorderFactory.createCompoundBorder( BorderFactory.createCompoundBorder( outerBlankBorder,
-                                                                                                etchedBorder ),
-                                                            innerBlankBorder );
+                                                                                                      etchedBorder ),
+                                                                  innerBlankBorder );
         panel.setBorder( border );
         add( BorderLayout.CENTER,
              panel );
@@ -104,7 +104,7 @@
             }
         };
         this.timer = new Timer( 500,
-                           timerAction );
+                                timerAction );
         this.startStopButton.addActionListener( new ActionListener() {
             public void actionPerformed(final ActionEvent e) {
                 if ( ConwayGUI.this.timer.isRunning() ) {
@@ -171,7 +171,7 @@
 
     private JPanel createControlPanel() {
         final FormLayout layout = new FormLayout( "pref, 3dlu, pref, 3dlu:grow",
-                                            "pref, 15dlu, pref, 15dlu, pref, 3dlu:grow, pref" );
+                                                  "pref, 15dlu, pref, 15dlu, pref, 3dlu:grow, pref" );
         final PanelBuilder builder = new PanelBuilder( layout );
         final CellConstraints cc = new CellConstraints();
 
@@ -198,8 +198,8 @@
                      cc.xy( 3,
                             5 ) );
         final JPanel buttonPanel = ButtonBarFactory.buildLeftAlignedBar( this.nextGenerationButton,
-                                                                   this.startStopButton,
-                                                                   this.clearButton );
+                                                                         this.startStopButton,
+                                                                         this.clearButton );
         builder.add( buttonPanel,
                      cc.xywh( 1,
                               7,
@@ -208,16 +208,16 @@
 
         final Border etchedBorder = BorderFactory.createEtchedBorder( EtchedBorder.LOWERED );
         final Border outerBlankBorder = BorderFactory.createEmptyBorder( 5,
-                                                                   5,
-                                                                   5,
-                                                                   5 );
+                                                                         5,
+                                                                         5,
+                                                                         5 );
         final Border innerBlankBorder = BorderFactory.createEmptyBorder( 5,
-                                                                   5,
-                                                                   5,
-                                                                   5 );
+                                                                         5,
+                                                                         5,
+                                                                         5 );
         final Border border = BorderFactory.createCompoundBorder( BorderFactory.createCompoundBorder( outerBlankBorder,
-                                                                                                etchedBorder ),
-                                                            innerBlankBorder );
+                                                                                                      etchedBorder ),
+                                                                  innerBlankBorder );
         builder.setBorder( border );
         return builder.getPanel();
     }

Modified: labs/jbossrules/trunk/drools-examples/src/main/rules/org/drools/examples/conway/conway.drl
===================================================================
--- labs/jbossrules/trunk/drools-examples/src/main/rules/org/drools/examples/conway/conway.drl	2007-04-16 13:34:04 UTC (rev 11004)
+++ labs/jbossrules/trunk/drools-examples/src/main/rules/org/drools/examples/conway/conway.drl	2007-04-16 13:37:20 UTC (rev 11005)
@@ -1,29 +1,154 @@
 package org.drools.examples
-
-expander conway.dsl
  
 import org.drools.examples.conway.Cell;
+import org.drools.examples.conway.CellGrid;
+import org.drools.examples.conway.Neighbor;
+import org.drools.examples.conway.Phase;
 import org.drools.examples.conway.CellState;
 
+import org.drools.WorkingMemory;
+import org.drools.common.InternalWorkingMemoryActions;
+import org.drools.RuleBase;
 
+
+rule "register north east"
+	agenda-group "register neighbor"
+when
+    CellGrid( $numberOfColumns : numberOfColumns )
+	$cell: Cell( $row : row > 0, $col : col < ( $numberOfColumns - 1 ) )			
+	$northEast : Cell( row  == ($row - 1), col == $col )	
+then					
+	assert( new Neighbor( $cell, $northEast ) );
+	assert( new Neighbor( $northEast, $cell ) );		
+end
+
+rule "register north"
+	agenda-group "register neighbor"	
+when
+	$cell: Cell( $row : row > 0, $col : col )	
+	$north : Cell( row  == ($row - 1), col == $col )	
+then		
+	assert( new Neighbor( $cell, $north ) );
+	assert( new Neighbor( $north, $cell ) );		
+end
+
+rule "register north west"
+	agenda-group "register neighbor"
+when
+	$cell: Cell( $row : row > 0, $col : col > 0 )			
+	$northWest : Cell( row  == ($row - 1), col == ( $col - 1 ) )						
+then		
+	assert( new Neighbor( $cell, $northWest ) );
+	assert( new Neighbor( $northWest, $cell ) );		
+end
+
+rule "register west"
+	agenda-group "register neighbor"
+when
+	$cell: Cell( $row : row > 0, $col : col > 0 )			
+	$west : Cell( row  == $row, col == ( $col - 1 ) )						
+then		
+	assert( new Neighbor( $cell, $west ) );
+	assert( new Neighbor( $west, $cell ) );			
+end
+
 rule "Kill The Lonely"
-when
-	A live cell has fewer than 2 live neighbors
+	salience 10
+	agenda-group "evaluate"	
+	lock-on-active	
+when
+#	A live cell has fewer than 2 live neighbors
+	theCell: Cell(liveNeighbors < 2, cellState == CellState.LIVE, phase == Phase.EVALUATE)
 then
-	Kill the cell
+	theCell.setPhase(Phase.KILL);
+	modify( theCell );
 end
 
 rule "Kill The Overcrowded"
+	salience 10
+	agenda-group "evaluate"
+	lock-on-active	
 when
-	A live cell has more than 3 live neighbors
+#	A live cell has more than 3 live neighbors
+	theCell: Cell(liveNeighbors > 3, cellState == CellState.LIVE, phase == Phase.EVALUATE)
 then
-	Kill the cell
+	theCell.setPhase(Phase.KILL);
+	modify( theCell );
 end
 
 rule "Give Birth"
+	salience 10
+	agenda-group "evaluate"
+	lock-on-active	
 when
-	A dead cell has 3 live neighbors
+#	A dead cell has 3 live neighbors
+	theCell: Cell(liveNeighbors == 3, cellState == CellState.DEAD, phase == Phase.EVALUATE)
 then
-	Bring the cell to life
+	theCell.setPhase(Phase.BIRTH);
+	modify( theCell );
 end
+
+rule "reset calculate"
+	agenda-group "reset calculate"
+when
+then
+	WorkingMemory wm = drools.getWorkingMemory();
+	wm.getAgenda().clearAgendaGroup( "calculate" );
+end
+
+rule "kill"
+	agenda-group "kill"
+	lock-on-active	
+when
+	theCell: Cell(phase == Phase.KILL)
+then
+	theCell.setCellState(CellState.DEAD);
+	theCell.setPhase(Phase.DONE);	
+	modify( theCell );
+end	
  
+rule "birth"
+	agenda-group "birth"
+	lock-on-active	
+when
+	theCell: Cell(phase == Phase.BIRTH)
+then
+	theCell.setCellState(CellState.LIVE);
+	theCell.setPhase(Phase.DONE);
+	modify( theCell );	
+end	
+
+rule "Calculate Live"
+	lock-on-active
+	agenda-group "calculate"
+when
+	theCell: Cell(cellState == CellState.LIVE)
+	Neighbor(cell == theCell, $neighbor : neighbor)	
+then
+	$neighbor.setLiveNeighbors( $neighbor.getLiveNeighbors() + 1 );
+	$neighbor.setPhase( Phase.EVALUATE );	
+	modify( $neighbor );
+end	
+
+rule "Calculate Dead"
+	lock-on-active
+	agenda-group "calculate"
+when
+	theCell: Cell(cellState == CellState.DEAD)
+    Neighbor(cell == theCell, $neighbor : neighbor )
+then
+	$neighbor.setLiveNeighbors( $neighbor.getLiveNeighbors() - 1 );
+	$neighbor.setPhase( Phase.EVALUATE );
+	modify( $neighbor );	
+end	
+
+rule "Kill All"
+	salience 10
+	agenda-group "kill all"	
+	lock-on-active	
+when
+	theCell: Cell(cellState == CellState.LIVE)
+then
+	theCell.setCellState(CellState.DEAD);
+	modify( theCell );
+end
\ No newline at end of file




More information about the jboss-svn-commits mailing list