[jboss-svn-commits] JBL Code SVN: r18504 - in labs/jbossrules/branches/4.0.x: drools-compiler/src/test/java/org/drools/integrationtests and 2 other directories.

jboss-svn-commits at lists.jboss.org jboss-svn-commits at lists.jboss.org
Tue Feb 19 08:37:06 EST 2008


Author: tirelli
Date: 2008-02-19 08:37:05 -0500 (Tue, 19 Feb 2008)
New Revision: 18504

Added:
   labs/jbossrules/branches/4.0.x/drools-compiler/src/test/resources/org/drools/integrationtests/test_OrCEFollowedByEval.drl
Modified:
   labs/jbossrules/branches/4.0.x/drools-compiler/src/test/java/org/drools/FactA.java
   labs/jbossrules/branches/4.0.x/drools-compiler/src/test/java/org/drools/FactB.java
   labs/jbossrules/branches/4.0.x/drools-compiler/src/test/java/org/drools/FactC.java
   labs/jbossrules/branches/4.0.x/drools-compiler/src/test/java/org/drools/integrationtests/MiscTest.java
   labs/jbossrules/branches/4.0.x/drools-core/src/main/java/org/drools/rule/EvalCondition.java
   labs/jbossrules/branches/4.0.x/drools-core/src/main/java/org/drools/rule/LogicTransformer.java
Log:
JBRULES-1389: fixing CCE when using evals after OR statements

Modified: labs/jbossrules/branches/4.0.x/drools-compiler/src/test/java/org/drools/FactA.java
===================================================================
--- labs/jbossrules/branches/4.0.x/drools-compiler/src/test/java/org/drools/FactA.java	2008-02-19 12:34:26 UTC (rev 18503)
+++ labs/jbossrules/branches/4.0.x/drools-compiler/src/test/java/org/drools/FactA.java	2008-02-19 13:37:05 UTC (rev 18504)
@@ -8,6 +8,10 @@
     public FactA() {
     }
 
+    public FactA( String f1 ) {
+        this.field1 = f1;
+    }
+
     public FactA(final String f1,
                  final Integer f2,
                  final Float f3) {

Modified: labs/jbossrules/branches/4.0.x/drools-compiler/src/test/java/org/drools/FactB.java
===================================================================
--- labs/jbossrules/branches/4.0.x/drools-compiler/src/test/java/org/drools/FactB.java	2008-02-19 12:34:26 UTC (rev 18503)
+++ labs/jbossrules/branches/4.0.x/drools-compiler/src/test/java/org/drools/FactB.java	2008-02-19 13:37:05 UTC (rev 18504)
@@ -8,6 +8,10 @@
     public FactB() {
     }
 
+    public FactB( String f1 ) {
+        this.f1 = f1;
+    }
+
     public FactB(final String a,
                  final Integer b,
                  final Float c) {

Modified: labs/jbossrules/branches/4.0.x/drools-compiler/src/test/java/org/drools/FactC.java
===================================================================
--- labs/jbossrules/branches/4.0.x/drools-compiler/src/test/java/org/drools/FactC.java	2008-02-19 12:34:26 UTC (rev 18503)
+++ labs/jbossrules/branches/4.0.x/drools-compiler/src/test/java/org/drools/FactC.java	2008-02-19 13:37:05 UTC (rev 18504)
@@ -17,6 +17,10 @@
 
     }
 
+    public FactC( String f1 ) {
+        this.f1 = f1;
+    }
+
     public String getF1() {
         return this.f1;
     }

Modified: labs/jbossrules/branches/4.0.x/drools-compiler/src/test/java/org/drools/integrationtests/MiscTest.java
===================================================================
--- labs/jbossrules/branches/4.0.x/drools-compiler/src/test/java/org/drools/integrationtests/MiscTest.java	2008-02-19 12:34:26 UTC (rev 18503)
+++ labs/jbossrules/branches/4.0.x/drools-compiler/src/test/java/org/drools/integrationtests/MiscTest.java	2008-02-19 13:37:05 UTC (rev 18504)
@@ -81,6 +81,7 @@
 import org.drools.audit.WorkingMemoryFileLogger;
 import org.drools.base.ClassObjectFilter;
 import org.drools.common.AbstractWorkingMemory;
+import org.drools.common.InternalFactHandle;
 import org.drools.compiler.DrlParser;
 import org.drools.compiler.DroolsError;
 import org.drools.compiler.DroolsParserException;
@@ -116,7 +117,6 @@
 import org.drools.spi.GlobalResolver;
 import org.drools.xml.XmlDumper;
 
-
 /** Run all the tests with the ReteOO engine implementation */
 public class MiscTest extends TestCase {
 
@@ -234,13 +234,14 @@
         } );
 
         workingMemory.fireAllRules();
-        
-        assertEquals(1, list.size() );
 
+        assertEquals( 1,
+                      list.size() );
+
         assertEquals( new Integer( 5 ),
                       list.get( 0 ) );
     }
-    
+
     public void testCustomGlobalResolverWithWorkingMemoryObject() throws Exception {
         final PackageBuilder builder = new PackageBuilder();
         builder.addPackageFromDrl( new InputStreamReader( getClass().getResourceAsStream( "test_globalCustomResolver.drl" ) ) );
@@ -258,7 +259,7 @@
                  list );
         map.put( "string",
                  string );
-        
+
         workingMemory.setGlobalResolver( new GlobalResolver() {
             public Object resolveGlobal(String identifier) {
                 return map.get( identifier );
@@ -270,24 +271,24 @@
                          value );
             }
 
-        } );                
-        
-        Cheese bree = new Cheese ();
+        } );
+
+        Cheese bree = new Cheese();
         bree.setPrice( 100 );
-        
+
         workingMemory.insert( bree );
 
         workingMemory.fireAllRules();
 
-        assertEquals(2, list.size() );        
-        
+        assertEquals( 2,
+                      list.size() );
+
         assertEquals( new Integer( 5 ),
                       list.get( 0 ) );
-        
+
         assertEquals( new Integer( 6 ),
-                      list.get( 1 ) );        
+                      list.get( 1 ) );
     }
-    
 
     public void testFieldBiningsAndEvalSharing() throws Exception {
         final String drl = "test_FieldBindingsAndEvalSharing.drl";
@@ -404,7 +405,7 @@
                       list.size() );
 
     }
-    
+
     public void NullFieldOnCompositeSink() throws Exception {
         final PackageBuilder builder = new PackageBuilder();
         builder.addPackageFromDrl( new InputStreamReader( getClass().getResourceAsStream( "test_NullFieldOnCompositeSink.drl" ) ) );
@@ -413,18 +414,21 @@
         // add the package to a rulebase
         final RuleBase ruleBase = getRuleBase();
         ruleBase.addPackage( pkg );
-        
+
         WorkingMemory workingMemory = ruleBase.newStatefulSession();
         List list = new ArrayList();
-        workingMemory.setGlobal("list", list);
-        
-        workingMemory.insert(new Attribute());
-        workingMemory.insert(new Message());
+        workingMemory.setGlobal( "list",
+                                 list );
+
+        workingMemory.insert( new Attribute() );
+        workingMemory.insert( new Message() );
         workingMemory.fireAllRules();
-        
-        assertEquals(1, list.size());
-        assertEquals("X", list.get(0));
-    	
+
+        assertEquals( 1,
+                      list.size() );
+        assertEquals( "X",
+                      list.get( 0 ) );
+
     }
 
     public void testEmptyPattern() throws Exception {
@@ -1814,7 +1818,7 @@
         assertTrue( IteratorToList.convert( workingMemory.iterateObjects() ).contains( bob ) );
         assertTrue( IteratorToList.convert( workingMemory.iterateObjects() ).contains( new Person( "help" ) ) );
     }
-    
+
     public void testSerializeWorkingMemoryAndRuleBase() throws Exception {
         final Reader reader = new InputStreamReader( getClass().getResourceAsStream( "test_Serializable.drl" ) );
 
@@ -1827,34 +1831,29 @@
 
         RuleBase ruleBase = getRuleBase();// RuleBaseFactory.newRuleBase();
 
-        
         Map map = new HashMap();
-        map.put( "x", ruleBase );
+        map.put( "x",
+                 ruleBase );
         final byte[] ast = serializeOut( map );
         map = (Map) serializeIn( ast );
         ruleBase = (RuleBase) map.get( "x" );
 
         WorkingMemory workingMemory = ruleBase.newStatefulSession();
 
-        
         final byte[] wm = serializeOut( workingMemory );
 
         workingMemory = ruleBase.newStatefulSession( new ByteArrayInputStream( wm ) );
 
         ruleBase.addPackage( pkg );
 
-        workingMemory.setGlobal( "list", 
-                new ArrayList() );
-        
+        workingMemory.setGlobal( "list",
+                                 new ArrayList() );
 
         final Person bob = new Person( "bob" );
         workingMemory.insert( bob );
 
-
-        
         final Rule[] rules = ruleBase.getPackages()[0].getRules();
 
-                
         assertEquals( 4,
                       rules.length );
 
@@ -1867,8 +1866,6 @@
         assertEquals( "match Integer",
                       rules[3].getName() );
 
-        
-
         assertEquals( 1,
                       IteratorToList.convert( workingMemory.iterateObjects() ).size() );
         assertEquals( bob,
@@ -1892,7 +1889,7 @@
         assertTrue( IteratorToList.convert( workingMemory.iterateObjects() ).contains( bob ) );
         assertTrue( IteratorToList.convert( workingMemory.iterateObjects() ).contains( new Person( "help" ) ) );
 
-    }    
+    }
 
     public void testEmptyRule() throws Exception {
         final PackageBuilder builder = new PackageBuilder();
@@ -3148,7 +3145,7 @@
         final Cheese stilton2 = new Cheese( "stilton2",
                                             12 );
         final Cheese agedStilton = new Cheese( "aged stilton",
-                                            12 );
+                                               12 );
         final Cheese brie = new Cheese( "brie",
                                         10 );
         final Cheese brie2 = new Cheese( "brie2",
@@ -4471,10 +4468,10 @@
     public void testConsequenceBuilderException() throws Exception {
         final PackageBuilder builder = new PackageBuilder();
         builder.addPackageFromDrl( new InputStreamReader( getClass().getResourceAsStream( "test_ConsequenceBuilderException.drl" ) ) );
-        
+
         assertTrue( builder.hasErrors() );
     }
-    
+
     public void testRuntimeTypeCoercion() throws Exception {
         final PackageBuilder builder = new PackageBuilder();
         builder.addPackageFromDrl( new InputStreamReader( getClass().getResourceAsStream( "test_RuntimeTypeCoercion.drl" ) ) );
@@ -4487,30 +4484,36 @@
         final List list = new ArrayList();
         workingMemory.setGlobal( "results",
                                  list );
-        
+
         final PolymorphicFact fact = new PolymorphicFact( new Integer( 10 ) );
         final FactHandle handle = workingMemory.insert( fact );
-        
+
         workingMemory.fireAllRules();
-        
-        assertEquals( 1, list.size() );
-        assertEquals( fact.getData(), list.get( 0 ) );
-        
+
+        assertEquals( 1,
+                      list.size() );
+        assertEquals( fact.getData(),
+                      list.get( 0 ) );
+
         fact.setData( "10" );
-        workingMemory.update( handle, fact );
+        workingMemory.update( handle,
+                              fact );
         workingMemory.fireAllRules();
-        
-        assertEquals( 2, list.size() );
-        assertEquals( fact.getData(), list.get( 1 ) );
-        
+
+        assertEquals( 2,
+                      list.size() );
+        assertEquals( fact.getData(),
+                      list.get( 1 ) );
+
         try {
-            fact.setData( new Boolean(true) );
-            workingMemory.update( handle, fact );
-            fail("Should not allow to compare < with a Boolean object");
-        } catch( ClassCastException cce ) {
+            fact.setData( new Boolean( true ) );
+            workingMemory.update( handle,
+                                  fact );
+            fail( "Should not allow to compare < with a Boolean object" );
+        } catch ( ClassCastException cce ) {
             // success, as can't use "<" to compare to a boolean
         }
-        
+
     }
 
     public void testRuntimeTypeCoercion2() throws Exception {
@@ -4525,38 +4528,50 @@
         final List list = new ArrayList();
         workingMemory.setGlobal( "results",
                                  list );
-        
-        final Primitives fact = new Primitives( );
+
+        final Primitives fact = new Primitives();
         fact.setBooleanPrimitive( true );
-        fact.setBooleanWrapper( new Boolean(true) );
-        fact.setObject( new Boolean(true) );
+        fact.setBooleanWrapper( new Boolean( true ) );
+        fact.setObject( new Boolean( true ) );
         fact.setCharPrimitive( 'X' );
         final FactHandle handle = workingMemory.insert( fact );
-        
+
         workingMemory.fireAllRules();
-        
+
         int index = 0;
-        assertEquals( list.toString(), 4, list.size() );
-        assertEquals( "boolean", list.get( index++ ));
-        assertEquals( "boolean wrapper", list.get( index++ ));
-        assertEquals( "boolean object", list.get( index++ ));
-        assertEquals( "char", list.get( index++ ));
-        
+        assertEquals( list.toString(),
+                      4,
+                      list.size() );
+        assertEquals( "boolean",
+                      list.get( index++ ) );
+        assertEquals( "boolean wrapper",
+                      list.get( index++ ) );
+        assertEquals( "boolean object",
+                      list.get( index++ ) );
+        assertEquals( "char",
+                      list.get( index++ ) );
+
         fact.setBooleanPrimitive( false );
         fact.setBooleanWrapper( null );
         fact.setCharPrimitive( '\0' );
-        fact.setObject( new Character('X') );
-        workingMemory.update( handle, fact );
+        fact.setObject( new Character( 'X' ) );
+        workingMemory.update( handle,
+                              fact );
         workingMemory.fireAllRules();
-        assertEquals( 5, list.size() );
-        assertEquals( "char object", list.get( index++ ) );
-        
+        assertEquals( 5,
+                      list.size() );
+        assertEquals( "char object",
+                      list.get( index++ ) );
+
         fact.setObject( null );
-        workingMemory.update( handle, fact );
+        workingMemory.update( handle,
+                              fact );
         workingMemory.fireAllRules();
-        assertEquals( 6, list.size() );
-        assertEquals( "null object", list.get( index++ ) );
-        
+        assertEquals( 6,
+                      list.size() );
+        assertEquals( "null object",
+                      list.get( index++ ) );
+
     }
 
     public void testAlphaEvalWithOrCE() throws Exception {
@@ -4571,7 +4586,7 @@
         final List list = new ArrayList();
         workingMemory.setGlobal( "results",
                                  list );
-        
+
         FactA a = new FactA();
         a.setField1( "a value" );
 
@@ -4581,13 +4596,13 @@
 
         workingMemory.fireAllRules();
 
-        assertEquals( "should not have fired", 
+        assertEquals( "should not have fired",
                       0,
                       list.size() );
     }
 
     // this test requires mvel 1.2.19. Leaving it commented until mvel is released.
-    public void FIXME_testJavaModifyBlock() throws Exception {
+    public void testJavaModifyBlock() throws Exception {
         final PackageBuilder builder = new PackageBuilder();
         builder.addPackageFromDrl( new InputStreamReader( getClass().getResourceAsStream( "test_JavaModifyBlock.drl" ) ) );
         final Package pkg = builder.getPackage();
@@ -4600,7 +4615,8 @@
         workingMemory.setGlobal( "results",
                                  list );
 
-        Person bob = new Person( "Bob", 30 );
+        Person bob = new Person( "Bob",
+                                 30 );
         bob.setStatus( "hungry" );
         workingMemory.insert( bob );
         workingMemory.insert( new Cheese() );
@@ -4608,13 +4624,15 @@
 
         workingMemory.fireAllRules( 2 );
 
-        assertEquals( "should have fired only once", 
+        assertEquals( "should have fired only once",
                       1,
                       list.size() );
-        assertEquals( "full", bob.getStatus() );
-        assertEquals( 31, bob.getAge() );
+        assertEquals( "full",
+                      bob.getStatus() );
+        assertEquals( 31,
+                      bob.getAge() );
     }
-    
+
     public void testOrCE() throws Exception {
         final PackageBuilder builder = new PackageBuilder();
         builder.addPackageFromDrl( new InputStreamReader( getClass().getResourceAsStream( "test_OrCE.drl" ) ) );
@@ -4627,17 +4645,18 @@
         final List list = new ArrayList();
         workingMemory.setGlobal( "results",
                                  list );
-        
-        workingMemory.insert( new Cheese( "brie", 10 ) );
+
+        workingMemory.insert( new Cheese( "brie",
+                                          10 ) );
         workingMemory.insert( new Person( "bob" ) );
 
         workingMemory.fireAllRules();
 
-        assertEquals( "should have fired once", 
+        assertEquals( "should have fired once",
                       1,
                       list.size() );
     }
-    
+
     public void testGetFactHandleEqualityBehavior() throws Exception {
         final RuleBaseConfiguration conf = new RuleBaseConfiguration();
         conf.setAssertBehaviour( RuleBaseConfiguration.AssertBehaviour.EQUALITY );
@@ -4645,11 +4664,13 @@
 
         final StatefulSession session = ruleBase.newStatefulSession();
 
-        CheeseEqual cheese = new CheeseEqual("stilton", 10);
-        session.insert(cheese);
-        FactHandle fh = session.getFactHandle(new CheeseEqual("stilton", 10));
-        assertNotNull(fh);
-    }    
+        CheeseEqual cheese = new CheeseEqual( "stilton",
+                                              10 );
+        session.insert( cheese );
+        FactHandle fh = session.getFactHandle( new CheeseEqual( "stilton",
+                                                                10 ) );
+        assertNotNull( fh );
+    }
 
     public void testGetFactHandleIdentityBehavior() throws Exception {
         final RuleBaseConfiguration conf = new RuleBaseConfiguration();
@@ -4658,12 +4679,38 @@
 
         final StatefulSession session = ruleBase.newStatefulSession();
 
-        CheeseEqual cheese = new CheeseEqual("stilton", 10);
-        session.insert(cheese);
-        FactHandle fh1 = session.getFactHandle(new Cheese("stilton", 10));
-        assertNull(fh1);
+        CheeseEqual cheese = new CheeseEqual( "stilton",
+                                              10 );
+        session.insert( cheese );
+        FactHandle fh1 = session.getFactHandle( new Cheese( "stilton",
+                                                            10 ) );
+        assertNull( fh1 );
         FactHandle fh2 = session.getFactHandle( cheese );
-        assertNotNull(fh2);
-    }    
+        assertNotNull( fh2 );
+    }
 
+    public void testOrCEFollowedByEval() throws Exception {
+        final PackageBuilder builder = new PackageBuilder();
+        builder.addPackageFromDrl( new InputStreamReader( getClass().getResourceAsStream( "test_OrCEFollowedByEval.drl" ) ) );
+        final Package pkg = builder.getPackage();
+
+        final RuleBase ruleBase = getRuleBase();
+        ruleBase.addPackage( pkg );
+        final WorkingMemory workingMemory = ruleBase.newStatefulSession();
+
+        final List list = new ArrayList();
+        workingMemory.setGlobal( "results",
+                                 list );
+
+        workingMemory.insert( new FactA( "X" ) );
+        InternalFactHandle b = (InternalFactHandle) workingMemory.insert( new FactB( "X" ) );
+
+        workingMemory.fireAllRules();
+
+        assertEquals( "should have fired",
+                      2,
+                      list.size() );
+        assertTrue( list.contains( b.getObject() ) );
+    }
+
 }

Added: labs/jbossrules/branches/4.0.x/drools-compiler/src/test/resources/org/drools/integrationtests/test_OrCEFollowedByEval.drl
===================================================================
--- labs/jbossrules/branches/4.0.x/drools-compiler/src/test/resources/org/drools/integrationtests/test_OrCEFollowedByEval.drl	                        (rev 0)
+++ labs/jbossrules/branches/4.0.x/drools-compiler/src/test/resources/org/drools/integrationtests/test_OrCEFollowedByEval.drl	2008-02-19 13:37:05 UTC (rev 18504)
@@ -0,0 +1,12 @@
+package org.drools;
+
+global java.util.List results;
+
+rule "test eval with OR"
+when
+    FactA( $a : field1 )
+    $f : FactB( $b : f1 ) or $f : FactB( $b : f1 == "X" )
+    eval( $a.equals( $b ) ) 
+then
+    results.add( $f );
+end
\ No newline at end of file

Modified: labs/jbossrules/branches/4.0.x/drools-core/src/main/java/org/drools/rule/EvalCondition.java
===================================================================
--- labs/jbossrules/branches/4.0.x/drools-core/src/main/java/org/drools/rule/EvalCondition.java	2008-02-19 12:34:26 UTC (rev 18503)
+++ labs/jbossrules/branches/4.0.x/drools-core/src/main/java/org/drools/rule/EvalCondition.java	2008-02-19 13:37:05 UTC (rev 18504)
@@ -85,7 +85,7 @@
 
     public Object clone() {
         final EvalCondition eval = new EvalCondition( this.expression,
-                                                      this.requiredDeclarations );
+                                                      (Declaration[]) this.requiredDeclarations.clone() );
         return eval;
     }
 
@@ -140,4 +140,13 @@
         return Collections.EMPTY_LIST;
     }
 
+    public void replaceDeclaration(Declaration declaration,
+                                   Declaration resolved) {
+        for ( int i = 0; i < this.requiredDeclarations.length; i++ ) {
+            if ( this.requiredDeclarations[i].equals( declaration ) ) {
+                this.requiredDeclarations[i] = resolved;
+            }
+        }
+    }
+
 };
\ No newline at end of file

Modified: labs/jbossrules/branches/4.0.x/drools-core/src/main/java/org/drools/rule/LogicTransformer.java
===================================================================
--- labs/jbossrules/branches/4.0.x/drools-core/src/main/java/org/drools/rule/LogicTransformer.java	2008-02-19 12:34:26 UTC (rev 18503)
+++ labs/jbossrules/branches/4.0.x/drools-core/src/main/java/org/drools/rule/LogicTransformer.java	2008-02-19 13:37:05 UTC (rev 18504)
@@ -163,6 +163,15 @@
                     }
                 }
             }
+        } else if ( element instanceof EvalCondition ) {
+            Declaration[] decl = ((EvalCondition) element).getRequiredDeclarations();
+            for ( int i = 0; i < decl.length; i++ ) {
+                Declaration resolved = resolver.getDeclaration( decl[i].getIdentifier() );
+                if ( resolved != null && resolved != decl[i] ) {
+                    ((EvalCondition) element).replaceDeclaration( decl[i],
+                                                                  resolved );
+                }
+            }
         } else {
             contextStack.push( element );
             for ( Iterator it = element.getNestedElements().iterator(); it.hasNext(); ) {
@@ -194,20 +203,20 @@
         // first we elimininate any redundancy
         ce.pack();
 
-        Object[] children = (Object[]) ce.getChildren().toArray(); 
+        Object[] children = (Object[]) ce.getChildren().toArray();
         for ( int i = 0; i < children.length; i++ ) {
             if ( children[i] instanceof GroupElement ) {
                 final GroupElement child = (GroupElement) children[i];
 
                 processTree( child );
-                if( ( child.isOr() || child.isAnd() ) && child.getType() == ce.getType() ) {
+                if ( (child.isOr() || child.isAnd()) && child.getType() == ce.getType() ) {
                     child.pack( ce );
                 } else if ( child.isOr() ) {
                     hasChildOr = true;
                 }
             }
         }
-        
+
         if ( hasChildOr ) {
             applyOrTransformation( ce );
         }




More information about the jboss-svn-commits mailing list