[jboss-svn-commits] JBL Code SVN: r30225 - 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
Wed Nov 18 15:22:31 EST 2009
Author: mark.proctor at jboss.com
Date: 2009-11-18 15:22:31 -0500 (Wed, 18 Nov 2009)
New Revision: 30225
Added:
labs/jbossrules/trunk/drools-examples/drools-examples-drl/src/main/java/org/drools/examples/pacman/DirectionDiff.java
Modified:
labs/jbossrules/trunk/drools-examples/drools-examples-drl/src/main/java/org/drools/examples/pacman/Character.java
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/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
labs/jbossrules/trunk/drools-examples/drools-examples-drl/src/main/rules/org/drools/examples/pacman/pacman.drl
Log:
JBRULES-2342 Pacman
-The monster now lives and tracks pacman
Modified: labs/jbossrules/trunk/drools-examples/drools-examples-drl/src/main/java/org/drools/examples/pacman/Character.java
===================================================================
--- labs/jbossrules/trunk/drools-examples/drools-examples-drl/src/main/java/org/drools/examples/pacman/Character.java 2009-11-18 18:40:53 UTC (rev 30224)
+++ labs/jbossrules/trunk/drools-examples/drools-examples-drl/src/main/java/org/drools/examples/pacman/Character.java 2009-11-18 20:22:31 UTC (rev 30225)
@@ -23,6 +23,6 @@
@Override
public String toString() {
- return "pacman speed = " + getSpeed();
+ return this.name + " speed = " + getSpeed();
}
}
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 2009-11-18 18:40:53 UTC (rev 30224)
+++ labs/jbossrules/trunk/drools-examples/drools-examples-drl/src/main/java/org/drools/examples/pacman/Direction.java 2009-11-18 20:22:31 UTC (rev 30225)
@@ -29,29 +29,62 @@
public int getVertical() {
return vertical;
}
-
- public static Direction newDirection(Character character, DirectionEnum directionEnum) {
- switch(directionEnum) {
- case LEFT: {
- return new Direction(character, -1, 0);
+
+ public static Direction newDirection(Character character,
+ DirectionEnum directionEnum) {
+ switch ( directionEnum ) {
+ case LEFT : {
+ return new Direction( character,
+ -1,
+ 0 );
}
- case RIGHT: {
- return new Direction(character, 1, 0);
+ case RIGHT : {
+ return new Direction( character,
+ 1,
+ 0 );
}
- case UP: {
- return new Direction(character, 0, 1);
+ case UP : {
+ return new Direction( character,
+ 0,
+ 1 );
}
- case DOWN: {
- return new Direction(character, 0, -1);
+ case DOWN : {
+ return new Direction( character,
+ 0,
+ -1 );
}
- default: {
-
+ default : {
+
}
}
throw new IllegalArgumentException( "Direction must be a valid DirectionEnum" );
}
@Override
+ public int hashCode() {
+ final int prime = 31;
+ int result = 1;
+ result = prime * result + ((character == null) ? 0 : character.hashCode());
+ result = prime * result + horizontal;
+ result = prime * result + vertical;
+ return result;
+ }
+
+ @Override
+ public boolean equals(Object obj) {
+ if ( this == obj ) return true;
+ if ( obj == null ) return false;
+ if ( getClass() != obj.getClass() ) return false;
+ Direction other = (Direction) obj;
+ if ( character == null ) {
+ if ( other.character != null ) return false;
+ } else if ( !character.equals( other.character ) ) return false;
+ if ( horizontal != other.horizontal ) return false;
+ if ( vertical != other.vertical ) return false;
+ return true;
+ }
+
+ @Override
public String toString() {
if ( horizontal != 0 ) {
return "Direction " + character + " " + ((horizontal == LEFT) ? "LEFT" : "RIGHT");
Added: labs/jbossrules/trunk/drools-examples/drools-examples-drl/src/main/java/org/drools/examples/pacman/DirectionDiff.java
===================================================================
--- labs/jbossrules/trunk/drools-examples/drools-examples-drl/src/main/java/org/drools/examples/pacman/DirectionDiff.java (rev 0)
+++ labs/jbossrules/trunk/drools-examples/drools-examples-drl/src/main/java/org/drools/examples/pacman/DirectionDiff.java 2009-11-18 20:22:31 UTC (rev 30225)
@@ -0,0 +1,53 @@
+package org.drools.examples.pacman;
+
+public class DirectionDiff {
+ private Character fromChar;
+ private Character toChar;
+ private int col;
+ private int row;
+ private int colDiff;
+ private int rowDiff;
+
+ public DirectionDiff(Character fromChar,
+ Character toChar,
+ int col,
+ int row,
+ int colDiff,
+ int rowDiff) {
+ this.fromChar = fromChar;
+ this.toChar = toChar;
+ this.col = col;
+ this.row = row;
+ this.colDiff = colDiff;
+ this.rowDiff = rowDiff;
+ }
+
+ public Character getFromChar() {
+ return fromChar;
+ }
+
+ public Character getToChar() {
+ return toChar;
+ }
+
+ public int getCol() {
+ return col;
+ }
+
+ public int getRow() {
+ return row;
+ }
+
+ public int getColDiff() {
+ return colDiff;
+ }
+
+ public int getRowDiff() {
+ return rowDiff;
+ }
+
+ public String toString() {
+ return "from: " + fromChar + " to: " + toChar + " col: " + col + " row: " + row + " colDiff: " + colDiff + " rowDiff: " + rowDiff;
+ }
+
+}
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 2009-11-18 18:40:53 UTC (rev 30224)
+++ labs/jbossrules/trunk/drools-examples/drools-examples-drl/src/main/rules/org/drools/examples/pacman/base.drl 2009-11-18 20:22:31 UTC (rev 30225)
@@ -4,25 +4,25 @@
* 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 100 when
$t : Tick()
then
modify( $t ) { tock += 1 };
- exitPoints["ConsoleExitPoint" ].insert( "tick " + $t.tock + "\n" );
+ //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 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" );
+ exitPoints["ConsoleExitPoint"].insert( "retract " + $d + "\n" );
retract( $d );
end
@@ -31,7 +31,7 @@
* 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
+rule ValidDirection dialect "mvel" when
$l : Location( )
$d : Direction( character == $l.character )
$target : Cell( row == ($l.row + $d.vertical), col == ($l.col + $d.horizontal) )
@@ -45,7 +45,7 @@
/**
* When the Tick increases, update any Locations from inserted scheduled updates.
*/
-rule setNewDirection dialect "mvel" when
+rule SetNewDirection dialect "mvel" when
$s : ScheduledLocationUpdate()
$l : Location( this == $s.location )
Tick( tock == $s.tock )
@@ -58,7 +58,7 @@
/**
* When all the cells are empty stock the clock (retract the tick), as the level is now finishedW
*/
-rule isLevelFinished dialect "mvel" when
+rule IsLevelFinished dialect "mvel" when
not CellContents( cellType == CellType.FOOD )
not CellContents( cellType == CellType.POWER_PILL )
$t : Tick()
@@ -66,7 +66,7 @@
retract( $t );
end
-rule finishedCompleted dialect "mvel" when
+rule FinishedCompleted dialect "mvel" when
not CellContents( cellType == CellType.FOOD )
not CellContents( cellType == CellType.POWER_PILL )
not Tick()
@@ -76,7 +76,7 @@
kcontext.knowledgeRuntime.halt();
end
-rule finishedKilled dialect "mvel" when
+rule FinishedKilled dialect "mvel" when
$pac : Character( name == "Pacman" )
$pacLoc : Location( character == $pac )
$mon : Character( name == "Monster" )
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 2009-11-18 18:40:53 UTC (rev 30224)
+++ labs/jbossrules/trunk/drools-examples/drools-examples-drl/src/main/rules/org/drools/examples/pacman/key-handlers.drl 2009-11-18 20:22:31 UTC (rev 30225)
@@ -38,7 +38,7 @@
* is detected it creates derived possible Direction and validates it. If it's valid
* then insert it.
*/
-rule keyListenerRuleNoDirection dialect "mvel" when
+rule KeyListenerRuleNoDirection dialect "mvel" when
$keyEvent : KeyEvent() from entry-point "KeyListener"
$char : Character( name == "Pacman" )
$l : Location( character == $char )
@@ -56,7 +56,7 @@
* 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
+rule KeyListenerRule dialect "mvel" when
$keyEvent : KeyEvent() from entry-point "KeyListener"
$char : Character( name == "Pacman" )
$l : Location( character == $char )
@@ -74,7 +74,7 @@
/**
* This rule is needed to stop events building up, so if they don't match and fire, retract anyway
*/
-rule keyListenerRuleRetractEvent dialect "mvel" salience -5 when
+rule KeyListenerRuleRetractEvent dialect "mvel" salience -5 when
$keyEvent : KeyEvent() from entry-point "KeyListener"
then
retract( $keyEvent );
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 2009-11-18 18:40:53 UTC (rev 30224)
+++ labs/jbossrules/trunk/drools-examples/drools-examples-drl/src/main/rules/org/drools/examples/pacman/monster.drl 2009-11-18 20:22:31 UTC (rev 30225)
@@ -1,2 +1,136 @@
package org.drools.examples.pacman
+import java.lang.Math;
+
+
+rule determineDistance dialect "mvel" when
+ $pac : Character( name == "Pacman" )
+ $pacLoc : Location( character == $pac )
+ $mon : Character( name == "Monster" )
+ $monLoc : Location( character == $mon )
+ $t : Tick()
+then
+ df = new DirectionDiff( $mon, $pac, $monLoc.col, $monLoc.row, $pacLoc.col - $monLoc.col, $pacLoc.row - $monLoc.row );
+ insert( df );
+end
+
+
+/**
+ * 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)
+then
+ retract( $d );
+ retract( $df );
+ insert( new Direction($df.fromChar, Direction.LEFT, 0 ) );
+end
+
+/**
+ * Go Right
+ */
+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)
+then
+ retract( $d );
+ retract( $df );
+ insert( new Direction($df.fromChar, Direction.RIGHT, 0 ) );
+end
+
+/**
+ * Go Down
+ */
+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)
+then
+ retract( $d );
+ retract( $df );
+ insert( new Direction($df.fromChar, 0, Direction.DOWN ) );
+end
+
+
+/**
+ * Go Up
+ */
+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)
+then
+ retract( $d );
+ retract( $df );
+ insert( new Direction($df.fromChar, 0 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()
+then
+ retract($df);
+end
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-18 18:40:53 UTC (rev 30224)
+++ labs/jbossrules/trunk/drools-examples/drools-examples-drl/src/main/rules/org/drools/examples/pacman/pacman.drl 2009-11-18 20:22:31 UTC (rev 30225)
@@ -3,7 +3,7 @@
/**
* When we move onto a FOOD cell, increase the score by 1
*/
-rule eatFood dialect "mvel" no-loop when
+rule EatFood dialect "mvel" no-loop when
$char : Character( name == "Pacman" )
$l : Location( character == $char )
$target : Cell( row == $l.row, col == $l.col)
@@ -17,7 +17,7 @@
/**
* When we move onto a POWER_PILL cell, increase the score by 5
*/
-rule eatPowerPill dialect "mvel" no-loop when
+rule EatPowerPill dialect "mvel" no-loop when
$char : Character( name == "Pacman" )
$l : Location( character == $char )
$target : Cell( row == $l.row, col == $l.col)
@@ -28,7 +28,7 @@
modify( $s ) { score += 5 };
end
-rule monsterCollision dialect "mvel" no-loop when
+rule MonsterCollision dialect "mvel" no-loop when
$pac : Character( name == "Pacman" )
$pacLoc : Location( character == $pac )
$mon : Character( name == "Monster" )
More information about the jboss-svn-commits
mailing list