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

jboss-svn-commits at lists.jboss.org jboss-svn-commits at lists.jboss.org
Thu May 20 00:27:18 EDT 2010


Author: mark.proctor at jboss.com
Date: 2010-05-20 00:27:17 -0400 (Thu, 20 May 2010)
New Revision: 32978

Modified:
   labs/jbossrules/trunk/drools-examples/drools-examples-drl/src/main/java/org/drools/examples/pacman/Direction.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/PacmanGui.java
   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/key-handlers.drl
   labs/jbossrules/trunk/drools-examples/drools-examples-drl/src/main/rules/org/drools/examples/pacman/monster.drl
Log:
JBRULES-2496 pacman fixes
-GUI look and fill fixes
-General keyboard handling fixes

Modified: labs/jbossrules/trunk/drools-examples/drools-examples-drl/src/main/java/org/drools/examples/pacman/Direction.java
===================================================================
--- labs/jbossrules/trunk/drools-examples/drools-examples-drl/src/main/java/org/drools/examples/pacman/Direction.java	2010-05-20 03:39:28 UTC (rev 32977)
+++ labs/jbossrules/trunk/drools-examples/drools-examples-drl/src/main/java/org/drools/examples/pacman/Direction.java	2010-05-20 04:27:17 UTC (rev 32978)
@@ -1,6 +1,7 @@
 package org.drools.examples.pacman;
 
 public class Direction {
+    public static final int NONE = 0;    
     public static final int LEFT  = -1;
     public static final int RIGHT = 1;
     public static final int UP    = 1;

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	2010-05-20 03:39:28 UTC (rev 32977)
+++ labs/jbossrules/trunk/drools-examples/drools-examples-drl/src/main/java/org/drools/examples/pacman/Main.java	2010-05-20 04:27:17 UTC (rev 32978)
@@ -60,11 +60,11 @@
         this.ksession = kbase.newStatefulKnowledgeSession();
 
         this.pacMan = new PacMan();
-        this.pacMan.setSpeed( 5 );
+        this.pacMan.setSpeed( 3 );
         this.ksession.insert( this.pacMan );
 
         Monster monster = new Monster();
-        monster.setSpeed( 3 );
+        monster.setSpeed( 5 );
         this.ksession.insert( monster );
 
         this.ksession.insert( new Score() );

Modified: labs/jbossrules/trunk/drools-examples/drools-examples-drl/src/main/java/org/drools/examples/pacman/PacmanGui.java
===================================================================
--- labs/jbossrules/trunk/drools-examples/drools-examples-drl/src/main/java/org/drools/examples/pacman/PacmanGui.java	2010-05-20 03:39:28 UTC (rev 32977)
+++ labs/jbossrules/trunk/drools-examples/drools-examples-drl/src/main/java/org/drools/examples/pacman/PacmanGui.java	2010-05-20 04:27:17 UTC (rev 32978)
@@ -21,15 +21,22 @@
     implements
     KeyListener,
     ActionListener {
-    JTextArea           displayArea;
-    static final String newline = System.getProperty( "line.separator" );
+    JTextArea               displayArea;
+    static final String     newline = System.getProperty( "line.separator" );
     WorkingMemoryEntryPoint keyListenerEntryPoint;
 
     public static void init(final StatefulKnowledgeSession ksession) {
         try {
-            UIManager.setLookAndFeel( "com.sun.java.swing.plaf.windows.WindowsLookAndFeel" );
-            //UIManager.setLookAndFeel("com.sun.java.swing.plaf.gtk.GTKLookAndFeel");
-            //UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
+            String osName = System.getProperty( "os.name" );
+            if ( osName.indexOf( "Linux" ) >= 0 ) {
+                UIManager.setLookAndFeel( "com.sun.java.swing.plaf.gtk.GTKLookAndFeel" );
+            } else if ( osName.indexOf( "Windows" ) >= 0 ) {
+                UIManager.setLookAndFeel( "com.sun.java.swing.plaf.windows.WindowsLookAndFeel" );
+            } else if ( osName.indexOf( "Mac" ) >= 0 ) {
+                UIManager.setLookAndFeel( "com.apple.laf.AquaLookAndFeel" );
+            } else {
+                UIManager.setLookAndFeel( UIManager.getSystemLookAndFeelClassName() );
+            }
         } catch ( UnsupportedLookAndFeelException ex ) {
             ex.printStackTrace();
         } catch ( IllegalAccessException ex ) {
@@ -47,7 +54,7 @@
         try {
             javax.swing.SwingUtilities.invokeAndWait( new Runnable() {
                 public void run() {
-                    createAndShowGUI(ksession);
+                    createAndShowGUI( ksession );
                 }
             } );
         } catch ( Exception e ) {
@@ -57,7 +64,8 @@
 
     private static void createAndShowGUI(StatefulKnowledgeSession ksession) {
         //Create and set up the window.
-        PacmanGui frame = new PacmanGui( "KeyEventDemo", ksession );
+        PacmanGui frame = new PacmanGui( "KeyEventDemo",
+                                         ksession );
         frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
 
         //Set up the content pane.
@@ -67,16 +75,16 @@
         frame.pack();
         frame.setVisible( true );
     }
-    
+
     public void appendText(final String string) {
         javax.swing.SwingUtilities.invokeLater( new Runnable() {
             public void run() {
                 displayArea.append( string );
-                displayArea.setCaretPosition(displayArea.getDocument().getLength());
+                displayArea.setCaretPosition( displayArea.getDocument().getLength() );
             }
-        } );                
+        } );
     }
-        
+
     private void addComponentsToPane() {
         JButton button = new JButton( "Clear" );
         button.addActionListener( this );
@@ -94,15 +102,19 @@
                               BorderLayout.PAGE_END );
     }
 
-    public PacmanGui(String name, StatefulKnowledgeSession ksession) {
-        super( name );        
+    public PacmanGui(String name,
+                     StatefulKnowledgeSession ksession) {
+        super( name );
         this.keyListenerEntryPoint = ksession.getWorkingMemoryEntryPoint( "KeyListener" );
-        ksession.registerExitPoint( "ConsoleExitPoint", new ConsoleExitPoint( this ) );
+        ksession.registerExitPoint( "ConsoleExitPoint",
+                                    new ConsoleExitPoint( this ) );
     }
-    
-    public static class ConsoleExitPoint implements ExitPoint {        
+
+    public static class ConsoleExitPoint
+        implements
+        ExitPoint {
         private PacmanGui gui;
-        
+
         public ConsoleExitPoint(PacmanGui gui) {
             this.gui = gui;
         }
@@ -110,9 +122,9 @@
         public void insert(final Object arg) {
             gui.appendText( (String) arg );
         }
-        
-    }    
 
+    }
+
     /** Handle the key typed event from the text field. */
     public void keyTyped(KeyEvent e) {
         // do nothing
@@ -123,23 +135,23 @@
     }
 
     public void keyReleased(KeyEvent e) {
-//        switch ( e.getKeyCode() ) {
-//            case 38 : { // UP
-//             break;   
-//            }
-//            case 40 : { // DOWN
-//                break;
-//            }
-//            case 37 : { // LEFt
-//                break;
-//            }
-//            case 39 : { // RIGHT
-//                break;
-//            }
-//            default: {
-//                
-//            }
-//        }
+        //        switch ( e.getKeyCode() ) {
+        //            case 38 : { // UP
+        //             break;   
+        //            }
+        //            case 40 : { // DOWN
+        //                break;
+        //            }
+        //            case 37 : { // LEFt
+        //                break;
+        //            }
+        //            case 39 : { // RIGHT
+        //                break;
+        //            }
+        //            default: {
+        //                
+        //            }
+        //        }
         //System.out.println( e );
         this.keyListenerEntryPoint.insert( e );
     }

Modified: 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	2010-05-20 03:39:28 UTC (rev 32977)
+++ labs/jbossrules/trunk/drools-examples/drools-examples-drl/src/main/rules/org/drools/examples/pacman/base.drl	2010-05-20 04:27:17 UTC (rev 32978)
@@ -1,10 +1,14 @@
 package org.drools.examples.pacman
 
+declare Direction
+   @role(event)
+end 
+
 /**
  * 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
+rule IncreaseTick dialect "mvel" salience -1 duration 300 when
     $t : Tick()
 then
     modify( $t ) { tock +=  1 };
@@ -12,6 +16,16 @@
 end
 
 /**
+ * Retract old Directions
+ */
+rule RetractOldDirections dialect "mvel" salience 100 when
+    $oldD     : Direction()   
+    $newD     : Direction( this != $oldD, character == $oldD.character, this after $oldD )
+then
+    retract( $oldD );
+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
  */

Modified: labs/jbossrules/trunk/drools-examples/drools-examples-drl/src/main/rules/org/drools/examples/pacman/key-handlers.drl
===================================================================
--- labs/jbossrules/trunk/drools-examples/drools-examples-drl/src/main/rules/org/drools/examples/pacman/key-handlers.drl	2010-05-20 03:39:28 UTC (rev 32977)
+++ labs/jbossrules/trunk/drools-examples/drools-examples-drl/src/main/rules/org/drools/examples/pacman/key-handlers.drl	2010-05-20 04:27:17 UTC (rev 32978)
@@ -2,7 +2,12 @@
 
 import java.awt.event.KeyEvent;
 	
+declare KeyEvent
+    @role(event)
+    @expires(0s)
+end    
 
+
 /**
  * Create a Direction for a character, based on a given keyCode. Return null
  * if the keyCode does not map to an arrow key.
@@ -34,45 +39,23 @@
 }
 
 /**
- * This is relevant to game initialisation. No direction exists yet. When a keypress (release)
- * is detected it creates derived possible Direction and validates it. If it's valid
- * then insert it.
+ * Only allow one KeyEvent at a time.
  */
-rule KeyListenerRuleNoDirection dialect "mvel" when
-    $keyEvent : KeyEvent() from entry-point "KeyListener"
-    $char     : Character( name == "Pacman" )
-    $l        : Location( character == $char )
-    $newD     : Direction() from createDirection( $l.character, $keyEvent )
-    $target   : Cell( row == ($l.row + $newD.vertical), col == ($l.col + $newD.horizontal) )
-                CellContents( cell == $target, cellType != CellType.WALL )    
-            not Direction( character == $l.character )
-then
-    exitPoints["ConsoleExitPoint" ].insert( "insert " + $newD + "\n" );
-    retract( $keyEvent );    
-    insert( $newD );
-end
-
-/**
- * Detects a new keypress (release). Creates derived possible Direction and validates it.
- * If the Direction is valid, delete the old Direction and replace with the new one.
- */
 rule KeyListenerRule dialect "mvel" when
-    $keyEvent : KeyEvent() from entry-point "KeyListener"
+    $keyEvent : KeyEvent() over window:length(1) from entry-point "KeyListener"
     $char     : Character( name == "Pacman" )
     $l        : Location( character == $char )
     $newD     : Direction() from createDirection( $l.character, $keyEvent )
+    not Direction(character == $newD.character, horizontal == $newD.horizontal, vertical == $newD.vertical )
     $target   : Cell( row == ($l.row + $newD.vertical), col == ($l.col + $newD.horizontal) )
                 CellContents( cell == $target, cellType != CellType.WALL )    
-    $oldD     : Direction( character == $l.character )
 then
-    exitPoints["ConsoleExitPoint" ].insert( "insert " + $newD + "\n" );
-    retract( $keyEvent );
-    retract( $oldD );
+    exitPoints["ConsoleExitPoint" ].insert( "insert direction " + $newD + "\n" );   
     insert( $newD );
 end
 
 /**
- * This rule is needed to stop events building up, so if they don't match and fire, retract anyway
+ * Retract all KeyEvents, ideally we should have expirs 0s, but that doesn't work
  */
 rule KeyListenerRuleRetractEvent dialect "mvel" salience -5 when
     $keyEvent : KeyEvent() from entry-point "KeyListener"

Modified: 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	2010-05-20 03:39:28 UTC (rev 32977)
+++ labs/jbossrules/trunk/drools-examples/drools-examples-drl/src/main/rules/org/drools/examples/pacman/monster.drl	2010-05-20 04:27:17 UTC (rev 32978)
@@ -16,70 +16,17 @@
 
 
 /** 
- * Go Left, when no Direction has been initialised
- */
-rule GoLeftNoExistingDirection dialect "mvel" salience (Math.abs( $df.colDiff )) when
-    $df   : DirectionDiff(colDiff < 0 )
-    $target : Cell(  row == $df.row, col == ($df.col - 1) )
-    CellContents( cell == $target, cellType != CellType.WALL )    
-    not Direction( character == $df.fromChar )    
-then
-    retract( $df );
-    insert( new Direction($df.fromChar, Direction.LEFT, 0 ) );    
-end
-
-/** 
- * Go Right, when no Direction has been initialised
- */
-rule GoRightNoExistingDirection dialect "mvel"  salience (Math.abs( $df.colDiff ))  when
-    $df   : DirectionDiff(colDiff > 0 )
-    $target : Cell(  row == $df.row, col == ($df.col + 1) )
-    CellContents( cell == $target, cellType != CellType.WALL )    
-    not Direction( character == $df.fromChar )     
-then
-    retract( $df );
-    insert( new Direction($df.fromChar, Direction.RIGHT, 0 ) );       
-end
-
-/** 
- * Go Down, when no Direction has been initialised
- */
-rule GoDownNoExistingDirection dialect "mvel"  salience (Math.abs( $df.rowDiff ))  when
-    $df   : DirectionDiff(rowDiff < 0 )
-    $target : Cell(  col == $df.col, row == ($df.row - 1) )
-    $contents : CellContents( cell == $target, cellType != CellType.WALL )    
-    not Direction( character == $df.fromChar )      
-then
-    retract( $df );
-    insert( new Direction($df.fromChar, 0,  Direction.DOWN ) );       
-end
-
-/** 
- * Go Up, when no Direction has been initialised
- */
-rule GoUpNoExistingDirection dialect "mvel"  salience (Math.abs( $df.rowDiff ))  when
-    $df   : DirectionDiff(rowDiff > 0 )
-    $target : Cell(  col == $df.col, row == ($df.row + 1) )
-    CellContents( cell == $target, cellType != CellType.WALL )    
-    not Direction( character == $df.fromChar )      
-then
-    retract( $df );
-    insert( new Direction($df.fromChar, 0 Direction.UP ) );       
-end
-
-
-/** 
  * Go Left
  */
 rule GoLeft dialect "mvel" salience (Math.abs( $df.colDiff ))  when
     $df   : DirectionDiff(colDiff < 0 )
     $target : Cell(  row == $df.row, col == ($df.col - 1) )
-    CellContents( cell == $target, cellType != CellType.WALL )    
-    $d : Direction( character == $df.fromChar, horizontal != Direction.LEFT)    
+    CellContents( cell == $target, cellType != CellType.WALL )        
+    not Direction(character == $df.fromChar, horizontal == Direction.LEFT )
 then
-    retract( $d );
+    System.out.println( "monster left" );
     retract( $df );    
-    insert( new Direction($df.fromChar, Direction.LEFT, 0 ) );
+    insert( new Direction($df.fromChar, Direction.LEFT, Direction.NONE ) ); 
 end
 
 /** 
@@ -88,12 +35,12 @@
 rule GoRight dialect "mvel"  salience (Math.abs( $df.colDiff ))  when
     $df   : DirectionDiff(colDiff > 0 )
     $target : Cell(  row == $df.row, col == ($df.col + 1) )
-    CellContents( cell == $target, cellType != CellType.WALL )    
-    $d : Direction( character == $df.fromChar, horizontal != Direction.RIGHT)    
+    CellContents( cell == $target, cellType != CellType.WALL )  
+    not Direction(character == $df.fromChar, horizontal == Direction.RIGHT )      
 then
-    retract( $d );
+    System.out.println( "monster right" );
     retract( $df ); 
-    insert( new Direction($df.fromChar, Direction.RIGHT, 0 ) );       
+    insert( new Direction($df.fromChar, Direction.RIGHT, Direction.NONE ) );       
 end
 
 /** 
@@ -102,12 +49,12 @@
 rule GoDown dialect "mvel"  salience (Math.abs( $df.rowDiff ))  when
     $df   : DirectionDiff(rowDiff < 0 )
     $target : Cell(  col == $df.col, row == ($df.row - 1))
-    CellContents( cell == $target, cellType != CellType.WALL )    
-    $d : Direction( character == $df.fromChar, vertical != Direction.DOWN)    
+    $contents : CellContents( cell == $target, cellType != CellType.WALL )      
+    not Direction(character == $df.fromChar, vertical == Direction.DOWN )
 then
-    retract( $d );
+    System.out.println( "monster down" + ":" + $df + " : " + $target + " : " + $contents);
     retract( $df ); 
-    insert( new Direction($df.fromChar, 0,  Direction.DOWN ) );
+    insert( new Direction($df.fromChar, Direction.NONE,  Direction.DOWN ) );
 end
 
 
@@ -117,20 +64,17 @@
 rule GoUp dialect "mvel"  salience (Math.abs( $df.rowDiff ))  when
     $df   : DirectionDiff(rowDiff > 0 )
     $target : Cell(  col == $df.col, row == ($df.row + 1) )
-    CellContents( cell == $target, cellType != CellType.WALL )    
-    $d : Direction( character == $df.fromChar, vertical != Direction.UP)    
+    CellContents( cell == $target, cellType != CellType.WALL )       
+    not Direction(character == $df.fromChar, vertical == Direction.UP )
 then
-    retract( $d );
+    System.out.println( "monster up" );
     retract( $df ); 
-    insert( new Direction($df.fromChar, 0 Direction.UP ) );  
+    insert( new Direction($df.fromChar, Direction.NONE, Direction.UP ) );  
 end
 
-/**
- * If no suitable Direction is added, then retract the DirectionDiff, so it can start again
- * on the next Tick
- */
-rule RetractDirectionDiff salience -5 when
-    $df : DirectionDiff()
+rule removeDirectionDiff salience -5 when
+   $df : DirectionDiff()
 then
-    retract($df);
+    retract( $df );
 end
+



More information about the jboss-svn-commits mailing list