[jboss-svn-commits] JBL Code SVN: r7567 - in labs/jbossrules/trunk: drools-compiler/src/test/java/org/drools/integrationtests drools-compiler/src/test/resources/org/drools/integrationtests drools-core/src/main/java/org/drools/reteoo drools-core/src/main/java/org/drools/rule

jboss-svn-commits at lists.jboss.org jboss-svn-commits at lists.jboss.org
Mon Nov 13 12:43:59 EST 2006


Author: tirelli
Date: 2006-11-13 12:43:43 -0500 (Mon, 13 Nov 2006)
New Revision: 7567

Added:
   labs/jbossrules/trunk/drools-compiler/src/test/resources/org/drools/integrationtests/test_DeclaringAndUsingBindsInSamePattern.drl
Modified:
   labs/jbossrules/trunk/drools-compiler/src/test/java/org/drools/integrationtests/IntegrationCases.java
   labs/jbossrules/trunk/drools-core/src/main/java/org/drools/reteoo/ObjectTypeNode.java
   labs/jbossrules/trunk/drools-core/src/main/java/org/drools/reteoo/ReteooBuilder.java
   labs/jbossrules/trunk/drools-core/src/main/java/org/drools/rule/VariableConstraint.java
   labs/jbossrules/trunk/drools-core/src/main/java/org/drools/rule/VariableRestriction.java
Log:
JBRULES-515: partial fix for the issue. We now support declaring and using a variable inside the same pattern

Modified: labs/jbossrules/trunk/drools-compiler/src/test/java/org/drools/integrationtests/IntegrationCases.java
===================================================================
--- labs/jbossrules/trunk/drools-compiler/src/test/java/org/drools/integrationtests/IntegrationCases.java	2006-11-13 16:04:25 UTC (rev 7566)
+++ labs/jbossrules/trunk/drools-compiler/src/test/java/org/drools/integrationtests/IntegrationCases.java	2006-11-13 17:43:43 UTC (rev 7567)
@@ -173,7 +173,7 @@
         assertEquals( new Integer( 5 ),
                       list.get( 0 ) );
     }
-    
+
     private RuleBase loadRuleBase(final Reader reader) throws IOException,
                                                       DroolsParserException,
                                                       Exception {
@@ -1409,7 +1409,7 @@
             workingMemory.fireAllRules();
             fail( "Should throw an Exception from the ReturnValue" );
         } catch ( final Exception e ) {
-            assertTrue( e.getCause().getMessage().endsWith( "this should throw an exception"));
+            assertTrue( e.getCause().getMessage().endsWith( "this should throw an exception" ) );
         }
     }
 
@@ -2190,14 +2190,14 @@
         assertEquals( 3,
                       list.size() );
         // because of agenda-groups
-        assertEquals( new Integer(4), 
+        assertEquals( new Integer( 4 ),
                       list.get( 0 ) );
 
         assertEquals( 2,
                       workingMemory.getObjects().size() );
         assertEquals( bob,
                       workingMemory.getObjects().get( 0 ) );
-        assertEquals( new Person("help"),
+        assertEquals( new Person( "help" ),
                       workingMemory.getObjects().get( 1 ) );
     }
 
@@ -2303,8 +2303,8 @@
 
         List list;
 
-        final Person b = new Person( "b");
-        final Person a = new Person( "a");
+        final Person b = new Person( "b" );
+        final Person a = new Person( "a" );
 
         workingMemory.setGlobal( "b",
                                  b );
@@ -2399,26 +2399,34 @@
         final RuleBase ruleBase = getRuleBase();
         ruleBase.addPackage( pkg );
         final WorkingMemory workingMemory = ruleBase.newWorkingMemory();
-        
+
         AgendaEventListener listener = new DefaultAgendaEventListener() {
-            public void activationCreated(ActivationCreatedEvent event, WorkingMemory workingMemory) {
-                super.activationCreated( event, workingMemory );
+            public void activationCreated(ActivationCreatedEvent event,
+                                          WorkingMemory workingMemory) {
+                super.activationCreated( event,
+                                         workingMemory );
             }
-            
-            public void activationCancelled(ActivationCancelledEvent event, WorkingMemory workingMemory) {
-                super.activationCancelled( event, workingMemory );
+
+            public void activationCancelled(ActivationCancelledEvent event,
+                                            WorkingMemory workingMemory) {
+                super.activationCancelled( event,
+                                           workingMemory );
             }
-            
-            public void beforeActivationFired(BeforeActivationFiredEvent event, WorkingMemory workingMemory) {
-                super.beforeActivationFired( event, workingMemory );
+
+            public void beforeActivationFired(BeforeActivationFiredEvent event,
+                                              WorkingMemory workingMemory) {
+                super.beforeActivationFired( event,
+                                             workingMemory );
             }
-            
-            public void afterActivationFired(AfterActivationFiredEvent event, WorkingMemory workingMemory) {
-                super.afterActivationFired( event, workingMemory );
+
+            public void afterActivationFired(AfterActivationFiredEvent event,
+                                             WorkingMemory workingMemory) {
+                super.afterActivationFired( event,
+                                            workingMemory );
             }
         };
 
-        workingMemory.addEventListener( listener );        
+        workingMemory.addEventListener( listener );
 
         final List events = new ArrayList();
 
@@ -2468,7 +2476,8 @@
         List list;
 
         final Person a = new Person( "a" );
-        final Cheese cheese = new Cheese( "brie", 1 );
+        final Cheese cheese = new Cheese( "brie",
+                                          1 );
         workingMemory.setGlobal( "cheese",
                                  cheese );
 
@@ -2528,7 +2537,8 @@
         final List list = new ArrayList();
 
         final Person person = new Person( "person" );
-        final Cheese cheese = new Cheese( "cheese", 0 );
+        final Cheese cheese = new Cheese( "cheese",
+                                          0 );
         workingMemory.setGlobal( "cheese",
                                  cheese );
         workingMemory.setGlobal( "person",
@@ -2578,12 +2588,12 @@
         final FactHandle h = workingMemory.assertObject( c2 );
         workingMemory.assertObject( c3 );
         workingMemory.fireAllRules();
-        
+
         //  Check logical assertions where made for  c2 and c3
         list = workingMemory.getObjects( Person.class );
         assertEquals( 2,
-                      list.size() );     
-        assertFalse( list.contains( new  Person( c1.getType() ) ) );
+                      list.size() );
+        assertFalse( list.contains( new Person( c1.getType() ) ) );
         assertTrue( list.contains( new Person( c2.getType() ) ) );
         assertTrue( list.contains( new Person( c3.getType() ) ) );
 
@@ -2682,7 +2692,7 @@
         final WorkingMemory workingMemory = ruleBase.newWorkingMemory();
 
         List l;
-        Person p = new Person( "person");
+        Person p = new Person( "person" );
         p.setAge( 2 );
         final FactHandle h = workingMemory.assertObject( p );
         assertEquals( 1,
@@ -2713,7 +2723,6 @@
             // is probably non-reteoo engine
         }
     }
-    
 
     public void testLogicalAssertionsWithExists() throws Exception {
         final PackageBuilder builder = new PackageBuilder();
@@ -2749,11 +2758,11 @@
         // europe=[ 1, 2 ], america=[ 3 ]
         p3.setStatus( "america" );
         workingMemory.modifyObject( c3FactHandle,
-                                    p3 );        
+                                    p3 );
         workingMemory.fireAllRules();
         cheeseList = workingMemory.getObjects( Cheese.class );
         assertEquals( 1,
-                      cheeseList.size() );        
+                      cheeseList.size() );
 
         // europe=[ 1 ], america=[ 2, 3 ]
         p2.setStatus( "america" );
@@ -2799,7 +2808,7 @@
         cheeseList = workingMemory.getObjects( Cheese.class );
         assertEquals( 2,
                       cheeseList.size() );
-    }    
+    }
 
     public void testEmptyRule() throws Exception {
         final PackageBuilder builder = new PackageBuilder();
@@ -2855,16 +2864,15 @@
 
         Person hola = new Person( "hola" );
         workingMemory.assertObject( hola );
-        
+
         workingMemory.fireAllRules();
-        
+
         assertEquals( 0,
                       list.size() );
         workingMemory.assertObject( new State( "x" ) );
 
+        workingMemory.fireAllRules();
 
-        workingMemory.fireAllRules();
-        
         assertEquals( 1,
                       list.size() );
         assertTrue( list.contains( hola ) );
@@ -2901,7 +2909,7 @@
         final RuleBase ruleBase = getRuleBase();
         ruleBase.addPackage( pkg1 );
         final WorkingMemory workingMemory = ruleBase.newWorkingMemory();
-        
+
         final List orderedFacts = new ArrayList();
         final List errors = new ArrayList();
 
@@ -3409,7 +3417,7 @@
         FactHandle c3FactHandle = workingMemory.assertObject( p3 );
         workingMemory.fireAllRules();
 
-        QueryResults queryResults = workingMemory.getQueryResults("2 persons with the same status");
+        QueryResults queryResults = workingMemory.getQueryResults( "2 persons with the same status" );
         assertEquals( 2,
                       queryResults.size() );
 
@@ -3418,7 +3426,7 @@
         workingMemory.modifyObject( c3FactHandle,
                                     p3 );
         workingMemory.fireAllRules();
-        queryResults = workingMemory.getQueryResults("2 persons with the same status");
+        queryResults = workingMemory.getQueryResults( "2 persons with the same status" );
         assertEquals( 1,
                       queryResults.size() );
 
@@ -3427,7 +3435,7 @@
         workingMemory.modifyObject( c2FactHandle,
                                     p2 );
         workingMemory.fireAllRules();
-        queryResults = workingMemory.getQueryResults("2 persons with the same status");
+        queryResults = workingMemory.getQueryResults( "2 persons with the same status" );
         assertEquals( 1,
                       queryResults.size() );
 
@@ -3436,7 +3444,7 @@
         workingMemory.modifyObject( c1FactHandle,
                                     p1 );
         workingMemory.fireAllRules();
-        queryResults = workingMemory.getQueryResults("2 persons with the same status");
+        queryResults = workingMemory.getQueryResults( "2 persons with the same status" );
         assertEquals( 2,
                       queryResults.size() );
 
@@ -3445,7 +3453,7 @@
         workingMemory.modifyObject( c2FactHandle,
                                     p2 );
         workingMemory.fireAllRules();
-        queryResults = workingMemory.getQueryResults("2 persons with the same status");
+        queryResults = workingMemory.getQueryResults( "2 persons with the same status" );
         assertEquals( 1,
                       queryResults.size() );
 
@@ -3454,7 +3462,7 @@
         workingMemory.modifyObject( c1FactHandle,
                                     p1 );
         workingMemory.fireAllRules();
-        queryResults = workingMemory.getQueryResults("2 persons with the same status");
+        queryResults = workingMemory.getQueryResults( "2 persons with the same status" );
         assertEquals( 1,
                       queryResults.size() );
 
@@ -3463,11 +3471,11 @@
         workingMemory.modifyObject( c3FactHandle,
                                     p3 );
         workingMemory.fireAllRules();
-        queryResults = workingMemory.getQueryResults("2 persons with the same status");
+        queryResults = workingMemory.getQueryResults( "2 persons with the same status" );
         assertEquals( 2,
                       queryResults.size() );
     }
-    
+
     public void testFunctionWithPrimitives() throws Exception {
         final PackageBuilder builder = new PackageBuilder();
         builder.addPackageFromDrl( new InputStreamReader( getClass().getResourceAsStream( "test_FunctionWithPrimitives.drl" ) ) );
@@ -3490,7 +3498,7 @@
         assertEquals( new Integer( 10 ),
                       list.get( 0 ) );
     }
-    
+
     public void testReturnValueAndGlobal() throws Exception {
 
         final PackageBuilder builder = new PackageBuilder();
@@ -3513,11 +3521,11 @@
                                  "stilton" );
 
         final Cheese stilton1 = new Cheese( "stilton",
-                                           5 );
+                                            5 );
         final Cheese stilton2 = new Cheese( "stilton",
-                                           7 );
+                                            7 );
         final Cheese brie = new Cheese( "brie",
-                                           4 );
+                                        4 );
         workingMemory.assertObject( stilton1 );
         workingMemory.assertObject( stilton2 );
         workingMemory.assertObject( brie );
@@ -3530,4 +3538,39 @@
                       nonmatchlist.size() );
     }
 
+    public void testDeclaringAndUsingBindsInSamePattern() throws Exception {
+        final PackageBuilder builder = new PackageBuilder();
+        builder.addPackageFromDrl( new InputStreamReader( getClass().getResourceAsStream( "test_DeclaringAndUsingBindsInSamePattern.drl" ) ) );
+        final Package pkg = builder.getPackage();
+
+        final RuleBase ruleBase = getRuleBase();
+        ruleBase.addPackage( pkg );
+        final WorkingMemory workingMemory = ruleBase.newWorkingMemory();
+
+        try {
+            List sensors = new ArrayList();
+
+            workingMemory.setGlobal( "sensors",
+                                     sensors );
+
+            Sensor sensor1 = new Sensor( 100,
+                                         150 );
+            workingMemory.assertObject( sensor1 );
+            workingMemory.fireAllRules();
+            assertEquals( 0,
+                          sensors.size() );
+
+            Sensor sensor2 = new Sensor( 200,
+                                         150 );
+            workingMemory.assertObject( sensor2 );
+            workingMemory.fireAllRules();
+            assertEquals( 1,
+                          sensors.size() );
+
+        } catch ( RuntimeException e ) {
+            e.printStackTrace();
+            fail( "Should not throw any exception" );
+        }
+
+    }
 }

Added: labs/jbossrules/trunk/drools-compiler/src/test/resources/org/drools/integrationtests/test_DeclaringAndUsingBindsInSamePattern.drl
===================================================================
--- labs/jbossrules/trunk/drools-compiler/src/test/resources/org/drools/integrationtests/test_DeclaringAndUsingBindsInSamePattern.drl	2006-11-13 16:04:25 UTC (rev 7566)
+++ labs/jbossrules/trunk/drools-compiler/src/test/resources/org/drools/integrationtests/test_DeclaringAndUsingBindsInSamePattern.drl	2006-11-13 17:43:43 UTC (rev 7567)
@@ -0,0 +1,14 @@
+#created on: 10/11/2006
+package org.drools.integrationtests
+
+import org.drools.Sensor;
+
+global java.util.List sensors;
+
+rule "BindsTest"
+	when
+		$sensor : Sensor( $temp : temperature, pressure < $temp )
+	then 
+	    sensors.add( $sensor );
+end
+


Property changes on: labs/jbossrules/trunk/drools-compiler/src/test/resources/org/drools/integrationtests/test_DeclaringAndUsingBindsInSamePattern.drl
___________________________________________________________________
Name: svn:executable
   + *
Name: svn:eol-style
   + native

Modified: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/reteoo/ObjectTypeNode.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/reteoo/ObjectTypeNode.java	2006-11-13 16:04:25 UTC (rev 7566)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/reteoo/ObjectTypeNode.java	2006-11-13 17:43:43 UTC (rev 7567)
@@ -138,9 +138,6 @@
                              final PropagationContext context,
                              final InternalWorkingMemory workingMemory) {
         final FactHashTable memory = (FactHashTable) workingMemory.getNodeMemory( this );
-//        if ( handle.getObject().getClass().getName().indexOf( "Edge" ) > 0 ) {
-//            System.out.println( handle );
-//        }
 
         // checks if shadow is enabled
         if ( this.objectType.isShadowEnabled() ) {

Modified: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/reteoo/ReteooBuilder.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/reteoo/ReteooBuilder.java	2006-11-13 16:04:25 UTC (rev 7566)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/reteoo/ReteooBuilder.java	2006-11-13 17:43:43 UTC (rev 7567)
@@ -26,6 +26,7 @@
 
 import org.drools.InitialFact;
 import org.drools.RuleIntegrationException;
+import org.drools.RuntimeDroolsException;
 import org.drools.base.ClassFieldExtractor;
 import org.drools.base.ClassObjectType;
 import org.drools.base.DroolsQuery;
@@ -264,6 +265,10 @@
             if ( object instanceof Column ) {
                 column = (Column) object;
 
+                // @REMOVEME after the milestone period
+                if(( binder != null) && ( binder != EmptyBetaConstraints.getInstance()))
+                    throw new RuntimeDroolsException("This is a bug! Please report to Drools development team!");
+                
                 binder = attachColumn( (Column) object,
                                        and,
                                        this.removeIdentities );
@@ -304,6 +309,10 @@
                                                                              objectSource ) );
                 }
 
+                // @REMOVEME after the milestone period
+                if(( binder != null) && ( binder != EmptyBetaConstraints.getInstance()))
+                    throw new RuntimeDroolsException("This is a bug! Please report to Drools development team!");
+
                 binder = attachColumn( column,
                                        and,
                                        false );
@@ -315,12 +324,14 @@
                            this.objectSource,
                            binder,
                            column );
+                binder = null;
             } else if ( object.getClass() == Exists.class ) {
                 attachExists( this.tupleSource,
                               (Exists) object,
                               this.objectSource,
                               binder,
                               column );
+                binder = null;
             } else if ( object.getClass() == From.class ) {
                 attachFrom( this.tupleSource,
                             (From) object );
@@ -337,6 +348,7 @@
                                                              this.tupleSource,
                                                              this.objectSource,
                                                              binder ) );
+                binder = null;
             }
         }
     }
@@ -437,7 +449,15 @@
             }
 
             final Constraint constraint = (Constraint) object;
-            if ( constraint.getRequiredDeclarations().length == 0 ) {
+            Declaration[] declarations = constraint.getRequiredDeclarations();
+
+            boolean isAlphaConstraint = true;
+            for(int i = 0; isAlphaConstraint && i < declarations.length; i++ ) {
+                if( declarations[i].getColumn() != column ) {
+                    isAlphaConstraint = false;
+                }
+            }
+            if ( isAlphaConstraint ) {
                 this.objectSource = attachNode( new AlphaNode( this.id++,
                                                                (AlphaNodeFieldConstraint) constraint,
                                                                this.objectSource ) );

Modified: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/rule/VariableConstraint.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/rule/VariableConstraint.java	2006-11-13 16:04:25 UTC (rev 7566)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/rule/VariableConstraint.java	2006-11-13 17:43:43 UTC (rev 7567)
@@ -16,13 +16,16 @@
  * limitations under the License.
  */
 
+import org.drools.common.InternalWorkingMemory;
 import org.drools.reteoo.ReteTuple;
+import org.drools.spi.AlphaNodeFieldConstraint;
 import org.drools.spi.BetaNodeFieldConstraint;
 import org.drools.spi.Evaluator;
 import org.drools.spi.FieldExtractor;
 
 public class VariableConstraint
     implements
+    AlphaNodeFieldConstraint,
     BetaNodeFieldConstraint {
 
     private static final long         serialVersionUID = 320L;
@@ -57,6 +60,13 @@
         return this.restriction.getEvaluator();
     }
 
+    public boolean isAllowed(Object object,
+                             InternalWorkingMemory workingMemory) {
+        return this.restriction.isAllowed( this.fieldExtractor,
+                                           object,
+                                           workingMemory );
+    }
+
     public boolean isAllowedCachedLeft(final ContextEntry context,
                                        final Object object) {
         return this.restriction.isAllowedCachedLeft( context,
@@ -99,5 +109,4 @@
         return this.fieldExtractor.equals( other.fieldExtractor ) && this.restriction.equals( other.restriction );
     }
 
-
 }
\ No newline at end of file

Modified: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/rule/VariableRestriction.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/rule/VariableRestriction.java	2006-11-13 16:04:25 UTC (rev 7566)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/rule/VariableRestriction.java	2006-11-13 17:43:43 UTC (rev 7567)
@@ -60,7 +60,7 @@
     public boolean isAllowed(final Extractor extractor,
                              final Object object,
                              final InternalWorkingMemory workingMemoiry) {
-        throw new UnsupportedOperationException( "does not support method call isAllowed(Extractor extractor, Object object, InternalWorkingMemory workingMemoiry)" );
+        return this.evaluator.evaluate( this.contextEntry.extractor, object, this.contextEntry.declaration.getExtractor(), object );
     }
 
     public boolean isAllowedCachedLeft(final ContextEntry context,
@@ -171,6 +171,8 @@
     }
 
     public static class ObjectVariableContextEntry extends VariableContextEntry {
+
+        private static final long serialVersionUID = 3607107040739298581L;
         public Object left;
         public Object right;
 
@@ -194,6 +196,9 @@
     }
 
     public static class LongVariableContextEntry extends VariableContextEntry {
+
+        private static final long serialVersionUID = -5316792696755228175L;
+
         public long left;
         public long right;
 
@@ -217,6 +222,9 @@
     }
 
     public static class DoubleVariableContextEntry extends VariableContextEntry {
+
+        private static final long serialVersionUID = 6996094205302851397L;
+
         public double left;
         public double right;
 
@@ -240,6 +248,8 @@
     }
 
     public static class BooleanVariableContextEntry extends VariableContextEntry {
+
+        private static final long serialVersionUID = -7664012658143075200L;
         public boolean left;
         public boolean right;
 




More information about the jboss-svn-commits mailing list