[jboss-svn-commits] JBL Code SVN: r30185 - in labs/jbossrules/trunk/drools-examples/drools-examples-drl/src/main: resources/org/drools/examples/pacman and 1 other directories.

jboss-svn-commits at lists.jboss.org jboss-svn-commits at lists.jboss.org
Mon Nov 16 12:38:43 EST 2009


Author: mark.proctor at jboss.com
Date: 2009-11-16 12:38:43 -0500 (Mon, 16 Nov 2009)
New Revision: 30185

Added:
   labs/jbossrules/trunk/drools-examples/drools-examples-drl/src/main/rules/org/drools/examples/pacman/base.drl
   labs/jbossrules/trunk/drools-examples/drools-examples-drl/src/main/rules/org/drools/examples/pacman/monster.drl
Modified:
   labs/jbossrules/trunk/drools-examples/drools-examples-drl/src/main/java/org/drools/examples/pacman/CellContents.java
   labs/jbossrules/trunk/drools-examples/drools-examples-drl/src/main/java/org/drools/examples/pacman/Main.java
   labs/jbossrules/trunk/drools-examples/drools-examples-drl/src/main/java/org/drools/examples/pacman/Monster.java
   labs/jbossrules/trunk/drools-examples/drools-examples-drl/src/main/resources/org/drools/examples/pacman/grid1.dat
   labs/jbossrules/trunk/drools-examples/drools-examples-drl/src/main/rules/org/drools/examples/pacman/pacman.drl
Log:
JBRULES-2342 Pacman
-Grid now bigger with power pills and white spacing for layout
-Detects POWER_PILLS
-Detects end of game and returns with score

Modified: labs/jbossrules/trunk/drools-examples/drools-examples-drl/src/main/java/org/drools/examples/pacman/CellContents.java
===================================================================
--- labs/jbossrules/trunk/drools-examples/drools-examples-drl/src/main/java/org/drools/examples/pacman/CellContents.java	2009-11-16 15:59:24 UTC (rev 30184)
+++ labs/jbossrules/trunk/drools-examples/drools-examples-drl/src/main/java/org/drools/examples/pacman/CellContents.java	2009-11-16 17:38:43 UTC (rev 30185)
@@ -18,6 +18,10 @@
         return cellType;
     }
 
+    public void setCellType(CellType cellType) {
+        this.cellType = cellType;
+    }
+
     @Override
     public String toString() {
         return "CellType " + cellType;

Modified: labs/jbossrules/trunk/drools-examples/drools-examples-drl/src/main/java/org/drools/examples/pacman/Main.java
===================================================================
--- labs/jbossrules/trunk/drools-examples/drools-examples-drl/src/main/java/org/drools/examples/pacman/Main.java	2009-11-16 15:59:24 UTC (rev 30184)
+++ labs/jbossrules/trunk/drools-examples/drools-examples-drl/src/main/java/org/drools/examples/pacman/Main.java	2009-11-16 17:38:43 UTC (rev 30185)
@@ -37,6 +37,9 @@
 
     public void initKsession() {
         KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
+        kbuilder.add( ResourceFactory.newClassPathResource( "base.drl",
+                                                            getClass() ),
+                      ResourceType.DRL );        
         kbuilder.add( ResourceFactory.newClassPathResource( "key-handlers.drl",
                                                             getClass() ),
                       ResourceType.DRL );
@@ -54,25 +57,32 @@
         this.ksession = kbase.newStatefulKnowledgeSession();
 
         this.pacMan = new PacMan();
-        this.pacMan.setSpeed( 1 );
+        this.pacMan.setSpeed( 2 );
         this.ksession.insert( this.pacMan );
 
+        Monster monster = new Monster();
+        monster.setSpeed( 7 );
+        //this.ksession.insert( monster );
+
         this.ksession.insert( new Score() );
 
         KnowledgeRuntimeLoggerFactory.newThreadedFileLogger( this.ksession,
                                                              "pacman.log",
                                                              3000 );
 
-        Location location = new Location( this.pacMan,
-                                          1,
-                                          1 );
-        this.ksession.insert( location );
+        Location pacLocation = new Location( this.pacMan,
+                                             1,
+                                             5 );
 
+        Location monLocation = new Location( monster,
+                                             9,
+                                             9 );
+
+        this.ksession.insert( pacLocation );
+        //this.ksession.insert( monLocation );
+
         Tick tick = new Tick( 0 );
         this.ksession.insert( tick );
-
-        // execute initialisation data and return
-        this.ksession.fireAllRules();
     }
 
     public void buildGrid() throws Exception {
@@ -88,11 +98,12 @@
 
         for ( int row = lines.size() - 1; row >= 0; row-- ) {
             line = lines.get( row );
+            int whiteCellCount = 0;
             for ( int col = 0; col < line.length(); col++ ) {
                 char c = line.charAt( col );
-
+                
                 Cell cell = new Cell( lines.size() - row - 1,
-                                      col );
+                                      col - whiteCellCount ); // use white spaces for layout, so need to correct
                 CellContents contents = null;
                 switch ( c ) {
                     case '*' : {
@@ -105,18 +116,30 @@
                                                      CellType.FOOD );
                         break;
                     }
+                    case '#' : {
+                        contents = new CellContents( cell,
+                                                     CellType.POWER_PILL );
+                        break;
+                    }                    
                     case '_' : {
                         contents = new CellContents( cell,
                                                      CellType.EMPTY );
                         break;
                     }
+                    case ' ' : {
+                        // ignore, just for spacing
+                        whiteCellCount++;
+                        break;
+                    }
                     default : {
                         throw new IllegalArgumentException( "'" + c + "' is an invalid cell type" );
                     }
                 }
-                System.out.println( cell + " : " + contents );
-                ksession.insert( cell );
-                ksession.insert( contents );
+                if ( contents != null ) {
+                    System.out.println( cell + " : " + contents );
+                    ksession.insert( cell );
+                    ksession.insert( contents );
+                }
             }
         }
     }

Modified: labs/jbossrules/trunk/drools-examples/drools-examples-drl/src/main/java/org/drools/examples/pacman/Monster.java
===================================================================
--- labs/jbossrules/trunk/drools-examples/drools-examples-drl/src/main/java/org/drools/examples/pacman/Monster.java	2009-11-16 15:59:24 UTC (rev 30184)
+++ labs/jbossrules/trunk/drools-examples/drools-examples-drl/src/main/java/org/drools/examples/pacman/Monster.java	2009-11-16 17:38:43 UTC (rev 30185)
@@ -1,5 +1,8 @@
 package org.drools.examples.pacman;
 
 public class Monster extends Character {
-
+    @Override
+    public String toString() {
+        return "monster speed = " + getSpeed();
+    }
 }

Modified: labs/jbossrules/trunk/drools-examples/drools-examples-drl/src/main/resources/org/drools/examples/pacman/grid1.dat
===================================================================
--- labs/jbossrules/trunk/drools-examples/drools-examples-drl/src/main/resources/org/drools/examples/pacman/grid1.dat	2009-11-16 15:59:24 UTC (rev 30184)
+++ labs/jbossrules/trunk/drools-examples/drools-examples-drl/src/main/resources/org/drools/examples/pacman/grid1.dat	2009-11-16 17:38:43 UTC (rev 30185)
@@ -1,11 +1,12 @@
-***********
-*.........*
-*.*******.*
-*.*******.*
-*.*******.*
-*.*******.*
-*.*******.*
-*.*******.*
-*.........*
-*_........*
-***********
\ No newline at end of file
+* * * * * * * * * * *
+* # . . . . . . . # *
+* . * * * * * * * . *
+* . * * * * * * * . *
+* . . . . # . . . . *
+* . * * * * * * * . *
+* . * * * * * * * . *
+* . . . . # . . . . *
+* . * * * * * * * . *
+* . * * * * * * * . *
+* # . . . _ . . . # *
+* * * * * * * * * * *
\ No newline at end of file

Added: labs/jbossrules/trunk/drools-examples/drools-examples-drl/src/main/rules/org/drools/examples/pacman/base.drl
===================================================================
--- labs/jbossrules/trunk/drools-examples/drools-examples-drl/src/main/rules/org/drools/examples/pacman/base.drl	                        (rev 0)
+++ labs/jbossrules/trunk/drools-examples/drools-examples-drl/src/main/rules/org/drools/examples/pacman/base.drl	2009-11-16 17:38:43 UTC (rev 30185)
@@ -0,0 +1,78 @@
+package org.drools.examples.pacman
+
+/**
+ * This controls the flow of time in pacman, currently it's slow 500ms, to make it easy to visualise things as
+ * they happen
+ */
+rule increaseTick dialect "mvel" salience -1 duration 100 when
+    $t : Tick()
+then
+    modify( $t ) { tock +=  1 };
+    exitPoints["ConsoleExitPoint" ].insert( "tick " + $t.tock + "\n" );
+end
+
+/**
+ * The current Direction would move the Location into a WALL, so we can't move and need to retract the Direction,
+ * so that no futher movement for is attempted
+ */
+rule invalidDirection dialect "mvel" when
+    $l : Location( )
+    $d : Direction( character == $l.character )
+    $target : Cell( row == ($l.row + $d.vertical), col == ($l.col + $d.horizontal) )
+    $ct : CellContents( cell == $target, cellType == CellType.WALL )
+    Tick()        
+then
+    exitPoints["ConsoleExitPoint" ].insert( "retract " + $d + "\n" );
+    retract( $d );     
+end
+
+/**
+ * Checks that the current Direction moves to a valid location, i.e. not a WALL.
+ * It does not set the new direction straight away, this is because we need movement to be intime
+ * with the Tick increase, so we schedule the new Location instead.
+ */
+rule validDirection dialect "mvel" when
+    $l : Location( )
+    $d : Direction( character == $l.character )
+    $target : Cell( row == ($l.row + $d.vertical), col == ($l.col + $d.horizontal) )
+    CellContents( cell == $target, cellType != CellType.WALL )
+    not ScheduledLocationUpdate( location == $l )    
+     $t : Tick()  
+then
+    insert( new ScheduledLocationUpdate($l, $l.row += $d.vertical, $l.col += $d.horizontal, $t.tock + $l.character.speed ) );
+end
+
+/**
+ * When the Tick increases, update any Locations from inserted scheduled updates.
+ */
+rule setNewDirection dialect "mvel" when
+    $s : ScheduledLocationUpdate()
+    $l : Location( this == $s.location )
+    Tick( tock == $s.tock )
+then
+   exitPoints["ConsoleExitPoint"].insert( "set new Location " + $l + "\n"  );
+   modify( $l ) { row = $s.row, col = $s.col };
+   retract( $s );
+end
+
+/**
+ * When all the cells are empty stock the clock (retract the tick), as the level is now finishedW
+ */
+rule isLevelFinished dialect "mvel" when
+    forall ( $c : Cell()
+                  CellContents( cell == $c, cellType == CellType.EMPTY || == CellType.WALL ) )
+    $t : Tick()
+then
+    //
+    retract( $t );
+end
+
+rule finishGame  dialect "mvel" when
+    forall ( $c : Cell()
+                  CellContents( cell == $c, cellType == CellType.EMPTY || == CellType.WALL ) )
+    $s : Score()
+    not Tick()
+then
+    exitPoints["ConsoleExitPoint"].insert( "FINISHED!!!! score = " + $s.score + " \n" );
+    kcontext.knowledgeRuntime.halt();
+end
\ No newline at end of file

Added: labs/jbossrules/trunk/drools-examples/drools-examples-drl/src/main/rules/org/drools/examples/pacman/monster.drl
===================================================================
--- labs/jbossrules/trunk/drools-examples/drools-examples-drl/src/main/rules/org/drools/examples/pacman/monster.drl	                        (rev 0)
+++ labs/jbossrules/trunk/drools-examples/drools-examples-drl/src/main/rules/org/drools/examples/pacman/monster.drl	2009-11-16 17:38:43 UTC (rev 30185)
@@ -0,0 +1,2 @@
+package org.drools.examples.pacman
+

Modified: labs/jbossrules/trunk/drools-examples/drools-examples-drl/src/main/rules/org/drools/examples/pacman/pacman.drl
===================================================================
--- labs/jbossrules/trunk/drools-examples/drools-examples-drl/src/main/rules/org/drools/examples/pacman/pacman.drl	2009-11-16 15:59:24 UTC (rev 30184)
+++ labs/jbossrules/trunk/drools-examples/drools-examples-drl/src/main/rules/org/drools/examples/pacman/pacman.drl	2009-11-16 17:38:43 UTC (rev 30185)
@@ -1,68 +1,27 @@
 package org.drools.examples.pacman
 
 /**
- * This controls the flow of time in pacman, currently it's slow 500ms, to make it easy to visualise things as
- * they happen
+ * When we move onto a FOOD cell, increase the score by 1
  */
-rule increaseTick dialect "mvel" salience -1 duration 500 when
-    $t : Tick()
-then
-    modify( $t ) { tock +=  1 };
-    exitPoints["ConsoleExitPoint" ].insert( "tick " + $t.tock + "\n" );
-end
-
-/**
- * The current Direction would move the Location into a WALL, so we can't move and need to retract the Direction,
- * so that no futher movement for is attempted
- */
-rule invalidDirection dialect "mvel" when
+rule eatFood dialect "mvel" no-loop when
     $l : Location( )
-    $d : Direction( character == $l.character )
-    $target : Cell( row == ($l.row + $d.vertical), col == ($l.col + $d.horizontal) )
-    $ct : CellContents( cell == $target, cellType == CellType.WALL )
-    Tick()        
+    $target : Cell( row == $l.row, col == $l.col)
+    $contents : CellContents( cell == $target, cellType == CellType.FOOD )
+    $s : Score()   
 then
-    exitPoints["ConsoleExitPoint" ].insert( "retract " + $d + "\n" );
-    retract( $d );     
+    modify( $contents ) { cellType = CellType.EMPTY };
+    modify( $s ) { score += 1 };    
 end
 
 /**
- * Checks that the current Direction moves to a valid location, i.e. not a WALL.
- * It does not set the new direction straight away, this is because we need movement to be intime
- * with the Tick increase, so we schedule the new Location instead.
+ * When we move onto a POWER_PILL cell, increase the score by 5
  */
-rule validDirection dialect "mvel" when
+rule eatPowerPill dialect "mvel" no-loop when
     $l : Location( )
-    $d : Direction( character == $l.character )
-    $target : Cell( row == ($l.row + $d.vertical), col == ($l.col + $d.horizontal) )
-    CellContents( cell == $target, cellType != CellType.WALL )
-    not ScheduledLocationUpdate( location == $l )    
-    $t : Tick()  
-then
-    insert( new ScheduledLocationUpdate($l, $l.row += $d.vertical, $l.col += $d.horizontal, $t.tock + 1) );
-end
-
-/**
- * When the Tick increases, update any Locations from inserted scheduled updates.
- */
-rule setNewDirection dialect "mvel" when
-    $s : ScheduledLocationUpdate()
-    $l : Location( this == $s.location )
-    Tick( tock == $s.tock )
-then
-   exitPoints["ConsoleExitPoint"].insert( "set new Location " + $l + "\n"  );
-   modify( $l ) { row = $s.row, col = $s.col };
-   retract( $s );
-end
-
-/**
- * When we move onto a FOOD cell, increase the score
- */
-rule eatFood dialect "mvel" no-loop when
-    $l : Location( )
     $target : Cell( row == $l.row, col == $l.col)
-    CellContents( cell == $target, cellType == CellType.FOOD )
+    $contents : CellContents( cell == $target, cellType == CellType.POWER_PILL )
     $s : Score()   
 then
-    modify( $s ) { score += 1 };    
+    modify( $contents ) { cellType = CellType.EMPTY };
+    modify( $s ) { score += 5 };    
 end
\ No newline at end of file



More information about the jboss-svn-commits mailing list