[jboss-svn-commits] JBL Code SVN: r33355 - in labs/jbossrules/contrib/lotrc/src: main/java/org/drools/examples/lotrc/functions and 5 other directories.

jboss-svn-commits at lists.jboss.org jboss-svn-commits at lists.jboss.org
Fri Jun 4 14:47:33 EDT 2010


Author: tirelli
Date: 2010-06-04 14:47:33 -0400 (Fri, 04 Jun 2010)
New Revision: 33355

Modified:
   labs/jbossrules/contrib/lotrc/src/main/java/org/drools/examples/lotrc/Main.java
   labs/jbossrules/contrib/lotrc/src/main/java/org/drools/examples/lotrc/functions/RandomSelectAccumulateFunction.java
   labs/jbossrules/contrib/lotrc/src/main/rules/fellowship/fellowship.drl
   labs/jbossrules/contrib/lotrc/src/main/rules/game/combat.drl
   labs/jbossrules/contrib/lotrc/src/main/rules/game/combat.rf
   labs/jbossrules/contrib/lotrc/src/main/rules/game/gameflow.rf
   labs/jbossrules/contrib/lotrc/src/main/rules/game/misc.drl
   labs/jbossrules/contrib/lotrc/src/main/rules/game/move.drl
   labs/jbossrules/contrib/lotrc/src/main/rules/game/setup.drl
   labs/jbossrules/contrib/lotrc/src/main/rules/game/victory.drl
   labs/jbossrules/contrib/lotrc/src/main/rules/players/common.drl
   labs/jbossrules/contrib/lotrc/src/main/rules/sauron/sauron.drl
   labs/jbossrules/contrib/lotrc/src/test/java/org/drools/examples/TowardsEvaluatorTest.java
Log:
Fixing example

Modified: labs/jbossrules/contrib/lotrc/src/main/java/org/drools/examples/lotrc/Main.java
===================================================================
--- labs/jbossrules/contrib/lotrc/src/main/java/org/drools/examples/lotrc/Main.java	2010-06-04 18:46:17 UTC (rev 33354)
+++ labs/jbossrules/contrib/lotrc/src/main/java/org/drools/examples/lotrc/Main.java	2010-06-04 18:47:33 UTC (rev 33355)
@@ -34,7 +34,7 @@
 
     public static void main(String[] args) {
         initResources();
-        initRulebase();
+        initKnowledgeBase();
         initSession();
         run();
     }
@@ -43,7 +43,7 @@
         PropertyConfigurator.configure(Main.class.getResource( "/log4j.properties" ));
     }
 
-    private static void initRulebase() {
+    private static void initKnowledgeBase() {
         logger.debug( "Creating knowledge base" );
         KnowledgeBuilderConfiguration conf = KnowledgeBuilderFactory.newKnowledgeBuilderConfiguration();
         KnowledgeBuilder builder = KnowledgeBuilderFactory.newKnowledgeBuilder( conf );
@@ -69,6 +69,7 @@
             //logger.debug( "Loading "+resource );
             builder.add( ResourceFactory.newClassPathResource( resource ),
                          resource.endsWith( ".rf" ) ? ResourceType.DRF : ResourceType.DRL );
+            checkErrors(builder);
             int newSize = builder.getKnowledgePackages().iterator().next().getRules().size();
             logger.info( "Loaded: "+resource+" with "+(newSize-previous)+" rules." );
             previous = newSize;
@@ -76,6 +77,15 @@
         logger.info( "All resources loaded. Total of "+previous+" rules." );
     }
 
+    private static void checkErrors(KnowledgeBuilder builder) {
+        if( builder.hasErrors() ) {
+            for (KnowledgeBuilderError knowledgeBuilderError : builder.getErrors()) {
+                System.err.println(knowledgeBuilderError.toString());
+            }
+            System.exit(0);
+        }
+    }
+
     private static void initSession() {
         logger.debug( "Creating Knowledge Session" );
         ksession = kbase.newStatefulKnowledgeSession();
@@ -109,14 +119,17 @@
     }
 
     private static void run() {
-        logger.debug( "Starting Game Flow" );
-        ksession.startProcess( "Game Flow" );
-        logger.debug( "Firing rules" );
-        ksession.fireAllRules();
-        logger.debug( "Session finished" );
-        if( audit != null ) {
-            audit.writeToDisk();
+        try {
+            logger.debug( "Starting Game Flow" );
+            ksession.startProcess( "Game Flow" );
+            logger.debug( "Firing rules" );
+            ksession.fireAllRules();
+            logger.debug( "Session finished" );
+        } finally {
+            if( audit != null ) {
+                audit.writeToDisk();
+            }
         }
     }
-    
+
 }

Modified: labs/jbossrules/contrib/lotrc/src/main/java/org/drools/examples/lotrc/functions/RandomSelectAccumulateFunction.java
===================================================================
--- labs/jbossrules/contrib/lotrc/src/main/java/org/drools/examples/lotrc/functions/RandomSelectAccumulateFunction.java	2010-06-04 18:46:17 UTC (rev 33354)
+++ labs/jbossrules/contrib/lotrc/src/main/java/org/drools/examples/lotrc/functions/RandomSelectAccumulateFunction.java	2010-06-04 18:47:33 UTC (rev 33355)
@@ -16,7 +16,6 @@
  */
 package org.drools.examples.lotrc.functions;
 
-import java.io.Externalizable;
 import java.io.IOException;
 import java.io.ObjectInput;
 import java.io.ObjectOutput;
@@ -45,25 +44,14 @@
         // functions are stateless, so nothing to serialize
     }
 
-    public static class RandomSelectData
+    private static class RandomSelectData
         implements
-        Externalizable {
+        Serializable {
+        private static final long serialVersionUID = 7632007789321541884L;
         public List<Object> list = new ArrayList<Object>();
-        public Random random = new Random(System.currentTimeMillis());
-
+        public transient Random random = new Random(System.currentTimeMillis());
         public RandomSelectData() {
         }
-
-        @SuppressWarnings("unchecked")
-        public void readExternal(ObjectInput in) throws IOException,
-                                                ClassNotFoundException {
-            list = (List<Object>) in.readObject();
-        }
-
-        public void writeExternal(ObjectOutput out) throws IOException {
-            out.writeObject( list );
-        }
-
     }
 
     /* (non-Javadoc)
@@ -104,7 +92,7 @@
      */
     public Object getResult(Serializable context) throws Exception {
         RandomSelectData data = (RandomSelectData) context;
-        return data.list.get( data.random.nextInt( data.list.size() ) );
+        return data.list.isEmpty() ? null : data.list.get( data.random.nextInt( data.list.size() ) );
     }
 
     /* (non-Javadoc)

Modified: labs/jbossrules/contrib/lotrc/src/main/rules/fellowship/fellowship.drl
===================================================================
--- labs/jbossrules/contrib/lotrc/src/main/rules/fellowship/fellowship.drl	2010-06-04 18:46:17 UTC (rev 33354)
+++ labs/jbossrules/contrib/lotrc/src/main/rules/fellowship/fellowship.drl	2010-06-04 18:47:33 UTC (rev 33355)
@@ -24,7 +24,7 @@
 rule "Make a move"
     no-loop
 when
-    $m : MakeAMove( action == null )
+    $m : MakeAMove( action == null ) 
     $c : Character( allegiance == Allegiance.FELLOWSHIP )
     $f : Region( characters contains $c )
     $t : Region( this towardsMordor $f )

Modified: labs/jbossrules/contrib/lotrc/src/main/rules/game/combat.drl
===================================================================
--- labs/jbossrules/contrib/lotrc/src/main/rules/game/combat.drl	2010-06-04 18:46:17 UTC (rev 33354)
+++ labs/jbossrules/contrib/lotrc/src/main/rules/game/combat.drl	2010-06-04 18:47:33 UTC (rev 33355)
@@ -170,7 +170,7 @@
 #    The attacker plays his card 
 # 
 rule "Attacker plays his card"
-    ruleflow-group "play cards"
+    ruleflow-group "play cards" 
 when
     $c    : CombatAction( status == CombatStatus.UNSETTLED, attackerCard == null )
     $char : Character( name == $c.attackerName )

Modified: labs/jbossrules/contrib/lotrc/src/main/rules/game/combat.rf
===================================================================
--- labs/jbossrules/contrib/lotrc/src/main/rules/game/combat.rf	2010-06-04 18:46:17 UTC (rev 33354)
+++ labs/jbossrules/contrib/lotrc/src/main/rules/game/combat.rf	2010-06-04 18:47:33 UTC (rev 33355)
@@ -11,7 +11,7 @@
   </header>
 
   <nodes>
-    <start id="1" name="Start" x="43" y="94" width="80" height="40" />
+    <start id="1" name="Start" x="43" y="98" width="80" height="40" />
     <ruleSet id="2" name="Reveal Characters" x="630" y="251" width="113" height="40" ruleFlowGroup="resolve characters" />
     <ruleSet id="3" name="Select Cards" x="459" y="250" width="104" height="40" ruleFlowGroup="select cards" />
     <ruleSet id="4" name="Play Cards" x="308" y="248" width="96" height="40" ruleFlowGroup="play cards" />
@@ -19,15 +19,15 @@
     <end id="6" name="End" x="959" y="97" width="80" height="40" />
     <split id="7" name="Is there a combat?" x="621" y="99" width="129" height="40" type="2" >
       <constraints>
-        <constraint toNodeId="9" toType="DROOLS_DEFAULT" name="No" priority="1" type="rule" dialect="mvel" >not( 
-    $c1 : Character( $a : allegiance ) and 
-    $c2 : Character( allegiance != $a )  and
-    Region( characters contains $c1, characters contains $c2 ) 
+        <constraint toNodeId="9" toType="DROOLS_DEFAULT" name="No" priority="1" type="rule" dialect="mvel" >not( 
+    $c1 : Character( $a : allegiance ) and 
+    $c2 : Character( allegiance != $a )  and
+    Region( characters contains $c1, characters contains $c2 ) 
 )</constraint>
-        <constraint toNodeId="2" toType="DROOLS_DEFAULT" name="Yes" priority="1" type="rule" dialect="java" >exists( 
-    $c1 : Character( $a : allegiance ) and 
-    $c2 : Character( allegiance != $a )  and
-    Region( characters contains $c1, characters contains $c2 ) 
+        <constraint toNodeId="2" toType="DROOLS_DEFAULT" name="Yes" priority="1" type="rule" dialect="java" >exists( 
+    $c1 : Character( $a : allegiance ) and 
+    $c2 : Character( allegiance != $a )  and
+    Region( characters contains $c1, characters contains $c2 ) 
 )</constraint>
       </constraints>
     </split>

Modified: labs/jbossrules/contrib/lotrc/src/main/rules/game/gameflow.rf
===================================================================
--- labs/jbossrules/contrib/lotrc/src/main/rules/game/gameflow.rf	2010-06-04 18:46:17 UTC (rev 33354)
+++ labs/jbossrules/contrib/lotrc/src/main/rules/game/gameflow.rf	2010-06-04 18:47:33 UTC (rev 33355)
@@ -14,7 +14,7 @@
   </header>
 
   <nodes>
-    <start id="1" name="Start" x="32" y="130" width="80" height="40" />
+    <start id="1" name="Start" x="31" y="125" width="80" height="40" />
     <ruleSet id="2" name="Setup" x="138" y="130" width="80" height="40" ruleFlowGroup="setup" />
     <ruleSet id="3" name="Move" x="222" y="212" width="104" height="40" ruleFlowGroup="move" />
     <split id="5" name="Victory?" x="372" y="290" width="80" height="40" type="2" >
@@ -29,7 +29,7 @@
         <constraint toNodeId="14" toType="DROOLS_DEFAULT" name="No Winner" priority="1" type="rule" dialect="mvel" >not( Winner() )</constraint>
       </constraints>
     </split>
-    <ruleSet id="7" name="Game End" x="601" y="424" width="80" height="40" ruleFlowGroup="game end" />
+    <ruleSet id="7" name="Game End" x="589" y="421" width="80" height="40" ruleFlowGroup="game end" />
     <join id="8" name="New turn" x="233" y="129" width="80" height="40" type="2" />
     <join id="9" name="Game End" x="601" y="354" width="80" height="40" type="2" />
     <end id="10" name="End" x="605" y="504" width="80" height="40" />

Modified: labs/jbossrules/contrib/lotrc/src/main/rules/game/misc.drl
===================================================================
--- labs/jbossrules/contrib/lotrc/src/main/rules/game/misc.drl	2010-06-04 18:46:17 UTC (rev 33354)
+++ labs/jbossrules/contrib/lotrc/src/main/rules/game/misc.drl	2010-06-04 18:47:33 UTC (rev 33355)
@@ -7,7 +7,7 @@
 
 global Logger logger
 
-rule "Next player"
+rule "Next player" 
     ruleflow-group "end of turn"
     lock-on-active
 when

Modified: labs/jbossrules/contrib/lotrc/src/main/rules/game/move.drl
===================================================================
--- labs/jbossrules/contrib/lotrc/src/main/rules/game/move.drl	2010-06-04 18:46:17 UTC (rev 33354)
+++ labs/jbossrules/contrib/lotrc/src/main/rules/game/move.drl	2010-06-04 18:47:33 UTC (rev 33355)
@@ -17,7 +17,7 @@
 
 #**********************************************************************
 #    The active player must make a move
-# 
+#  
 rule "Active player makes a move"
     ruleflow-group "move"
     lock-on-active 

Modified: labs/jbossrules/contrib/lotrc/src/main/rules/game/setup.drl
===================================================================
--- labs/jbossrules/contrib/lotrc/src/main/rules/game/setup.drl	2010-06-04 18:46:17 UTC (rev 33354)
+++ labs/jbossrules/contrib/lotrc/src/main/rules/game/setup.drl	2010-06-04 18:47:33 UTC (rev 33355)
@@ -26,7 +26,7 @@
 	then 
 		PlaceCharacterAction pca = $p.setupCharacter( $c.getName() );
 		insert( pca ); 
-end 
+end
 
 rule "Is legal PCA for the Fellowhisp?" 
     ruleflow-group "setup"

Modified: labs/jbossrules/contrib/lotrc/src/main/rules/game/victory.drl
===================================================================
--- labs/jbossrules/contrib/lotrc/src/main/rules/game/victory.drl	2010-06-04 18:46:17 UTC (rev 33354)
+++ labs/jbossrules/contrib/lotrc/src/main/rules/game/victory.drl	2010-06-04 18:47:33 UTC (rev 33355)
@@ -65,7 +65,7 @@
 # 
 rule "Player is unable to move a character, so opposing player wins"
     ruleflow-group "check victory"
-when
+when 
     $p : Player( )
     $g : Game( activePlayer == $p )
     $m : MoveAction( status in ( MoveStatus.ISSUED, MoveStatus.INVALID ) )

Modified: labs/jbossrules/contrib/lotrc/src/main/rules/players/common.drl
===================================================================
--- labs/jbossrules/contrib/lotrc/src/main/rules/players/common.drl	2010-06-04 18:46:17 UTC (rev 33354)
+++ labs/jbossrules/contrib/lotrc/src/main/rules/players/common.drl	2010-06-04 18:47:33 UTC (rev 33355)
@@ -78,7 +78,7 @@
 end
 
 
-rule "Print board"
+rule "Print board" 
 when
     String( this == "Print Board" )
     $b : Board()

Modified: labs/jbossrules/contrib/lotrc/src/main/rules/sauron/sauron.drl
===================================================================
--- labs/jbossrules/contrib/lotrc/src/main/rules/sauron/sauron.drl	2010-06-04 18:46:17 UTC (rev 33354)
+++ labs/jbossrules/contrib/lotrc/src/main/rules/sauron/sauron.drl	2010-06-04 18:47:33 UTC (rev 33355)
@@ -27,7 +27,7 @@
     $m : MakeAMove( action == null )
     $c : Character( allegiance == Allegiance.SAURON )
     $f : Region( characters contains $c )
-    $t : Region( this towardsShire $f )
+    $t : Region( this towardsShire $f ) 
     Number( intValue < $t.capacity ) from accumulate( 
                       Character( allegiance == Allegiance.SAURON ) from $t.characters,
                       count( 1 ) )

Modified: labs/jbossrules/contrib/lotrc/src/test/java/org/drools/examples/TowardsEvaluatorTest.java
===================================================================
--- labs/jbossrules/contrib/lotrc/src/test/java/org/drools/examples/TowardsEvaluatorTest.java	2010-06-04 18:46:17 UTC (rev 33354)
+++ labs/jbossrules/contrib/lotrc/src/test/java/org/drools/examples/TowardsEvaluatorTest.java	2010-06-04 18:47:33 UTC (rev 33355)
@@ -16,13 +16,10 @@
 import org.drools.rule.VariableRestriction.VariableContextEntry;
 import org.drools.spi.Evaluator;
 import org.drools.spi.InternalReadAccessor;
-import org.jmock.Expectations;
-import org.jmock.Mockery;
+import static org.mockito.Mockito.*;
 
 public class TowardsEvaluatorTest extends TestCase {
 
-    private Mockery mockery = new Mockery();
-
     private EvaluatorRegistry registry = new EvaluatorRegistry();
     private Region[][] regions = new Board().getRegions();
     
@@ -174,15 +171,13 @@
      */
     private void runEvaluatorTest(final Object[][] data,
                                   final ValueType valueType) {
-        final InternalReadAccessor extractor = mockery.mock( InternalReadAccessor.class );
+        final InternalReadAccessor extractor = mock( InternalReadAccessor.class );
         for ( int i = 0; i < data.length; i++ ) {
             final Object[] row = data[i];
-            mockery.checking( new Expectations() {{
-                allowing(extractor).getValue( with( row[0] ) ); will( returnValue( row[0] ) );
-                allowing(extractor).getValue( with( row[2] ) ); will( returnValue( row[2] ) );
-                allowing(extractor).getValue( with( any( InternalWorkingMemory.class) ), with( row[0] ) ); will( returnValue( row[0] ) );
-                allowing(extractor).getValue( with( any( InternalWorkingMemory.class) ), with( row[2] ) ); will( returnValue( row[2] ) );
-            }} );
+            when( extractor.getValue( row[0] ) ).thenReturn( row[0] );
+            when( extractor.getValue( row[2] ) ).thenReturn( row[2] );
+            when( extractor.getValue( any( InternalWorkingMemory.class), eq( row[0] ) ) ).thenReturn( row[0] ); 
+            when( extractor.getValue( any( InternalWorkingMemory.class), eq( row[2] ) ) ).thenReturn( row[2] );
             
             boolean isNegated = ((String) row[1]).startsWith( "not " );
             String evaluatorStr = isNegated ? ((String) row[1]).substring( 4 ) : (String) row[1];



More information about the jboss-svn-commits mailing list