[jboss-svn-commits] JBL Code SVN: r11157 - in labs/jbossrules/trunk: drools-compiler/src/main/java/org/drools/rule/builder/dialect/java and 10 other directories.

jboss-svn-commits at lists.jboss.org jboss-svn-commits at lists.jboss.org
Fri Apr 20 10:22:54 EDT 2007


Author: tirelli
Date: 2007-04-20 10:22:54 -0400 (Fri, 20 Apr 2007)
New Revision: 11157

Modified:
   labs/jbossrules/trunk/drools-compiler/src/main/java/org/drools/rule/builder/ColumnBuilder.java
   labs/jbossrules/trunk/drools-compiler/src/main/java/org/drools/rule/builder/dialect/java/BuildUtils.java
   labs/jbossrules/trunk/drools-compiler/src/test/java/org/drools/brms/server/converter/BRXMLToDescrConverterTest.java
   labs/jbossrules/trunk/drools-compiler/src/test/java/org/drools/integrationtests/DynamicRulesTest.java
   labs/jbossrules/trunk/drools-compiler/src/test/java/org/drools/rule/builder/dialect/mvel/MVELReturnValueBuilderTest.java
   labs/jbossrules/trunk/drools-compiler/src/test/resources/org/drools/integrationtests/test_Dynamic1.drl
   labs/jbossrules/trunk/drools-core/src/main/java/org/drools/reteoo/Rete.java
   labs/jbossrules/trunk/drools-core/src/main/java/org/drools/reteoo/ReteooBuilder.java
   labs/jbossrules/trunk/drools-core/src/main/java/org/drools/reteoo/TupleSink.java
   labs/jbossrules/trunk/drools-core/src/main/java/org/drools/reteoo/builder/BuildContext.java
   labs/jbossrules/trunk/drools-core/src/main/java/org/drools/reteoo/builder/BuildUtils.java
   labs/jbossrules/trunk/drools-core/src/main/java/org/drools/reteoo/builder/ReteooRuleBuilder.java
   labs/jbossrules/trunk/drools-core/src/main/java/org/drools/rule/PredicateConstraint.java
   labs/jbossrules/trunk/drools-core/src/main/java/org/drools/rule/ReturnValueRestriction.java
   labs/jbossrules/trunk/drools-core/src/main/java/org/drools/spi/GlobalExtractor.java
   labs/jbossrules/trunk/drools-core/src/test/java/org/drools/reteoo/builder/ReteooRuleBuilderTest.java
   labs/jbossrules/trunk/drools-core/src/test/java/org/drools/rule/FieldConstraintTest.java
Log:
JBRULES-759: removing the attached nodes cache as it was becoming corrupt when removing packages. Test cases updated

Modified: labs/jbossrules/trunk/drools-compiler/src/main/java/org/drools/rule/builder/ColumnBuilder.java
===================================================================
--- labs/jbossrules/trunk/drools-compiler/src/main/java/org/drools/rule/builder/ColumnBuilder.java	2007-04-20 14:16:58 UTC (rev 11156)
+++ labs/jbossrules/trunk/drools-compiler/src/main/java/org/drools/rule/builder/ColumnBuilder.java	2007-04-20 14:22:54 UTC (rev 11157)
@@ -390,9 +390,12 @@
 
         final Declaration[] previousDeclarations = (Declaration[]) tupleDeclarations.toArray( new Declaration[tupleDeclarations.size()] );
         final Declaration[] localDeclarations = (Declaration[]) factDeclarations.toArray( new Declaration[factDeclarations.size()] );
+        final String[] requiredGlobals = (String[]) usedIdentifiers[1].toArray( new String[usedIdentifiers[1].size()] );
 
-        final PredicateConstraint predicateConstraint = new PredicateConstraint( previousDeclarations,
-                                                                                 localDeclarations );
+        final PredicateConstraint predicateConstraint = new PredicateConstraint( null,
+                                                                                 previousDeclarations,
+                                                                                 localDeclarations,
+                                                                                 requiredGlobals );
         column.addConstraint( predicateConstraint );
 
         final PredicateBuilder builder = this.dialect.getPredicateBuilder();
@@ -604,9 +607,11 @@
 
         final Declaration[] previousDeclarations = (Declaration[]) tupleDeclarations.toArray( new Declaration[tupleDeclarations.size()] );
         final Declaration[] localDeclarations = (Declaration[]) factDeclarations.toArray( new Declaration[factDeclarations.size()] );
+        final String[] requiredGlobals = (String[]) usedIdentifiers[1].toArray( new String[usedIdentifiers[1].size()] );
         final ReturnValueRestriction returnValueRestriction = new ReturnValueRestriction( extractor,
                                                                                           previousDeclarations,
                                                                                           localDeclarations,
+                                                                                          requiredGlobals,
                                                                                           evaluator );
 
         final ReturnValueBuilder builder = this.dialect.getReturnValueBuilder();

Modified: labs/jbossrules/trunk/drools-compiler/src/main/java/org/drools/rule/builder/dialect/java/BuildUtils.java
===================================================================
--- labs/jbossrules/trunk/drools-compiler/src/main/java/org/drools/rule/builder/dialect/java/BuildUtils.java	2007-04-20 14:16:58 UTC (rev 11156)
+++ labs/jbossrules/trunk/drools-compiler/src/main/java/org/drools/rule/builder/dialect/java/BuildUtils.java	2007-04-20 14:22:54 UTC (rev 11157)
@@ -119,7 +119,7 @@
     public void setStringTemplateAttributes(final BuildContext context,
                                             final StringTemplate st,
                                             final Declaration[] declarations,
-                                            final String[] globals) {
+                                            final String[] globals ) {
         final String[] declarationTypes = new String[declarations.length];
         for ( int i = 0, size = declarations.length; i < size; i++ ) {
             declarationTypes[i] = this.typeFixer.fix( declarations[i] );

Modified: labs/jbossrules/trunk/drools-compiler/src/test/java/org/drools/brms/server/converter/BRXMLToDescrConverterTest.java
===================================================================
--- labs/jbossrules/trunk/drools-compiler/src/test/java/org/drools/brms/server/converter/BRXMLToDescrConverterTest.java	2007-04-20 14:16:58 UTC (rev 11156)
+++ labs/jbossrules/trunk/drools-compiler/src/test/java/org/drools/brms/server/converter/BRXMLToDescrConverterTest.java	2007-04-20 14:22:54 UTC (rev 11157)
@@ -103,5 +103,84 @@
                       col3.getObjectType() );
 
     }
+    
+    public void testDSLTransformation() {
+        final BRXMLToDescrConverter converter = new BRXMLToDescrConverter();
 
+        final RuleModel model = new RuleModel();
+
+        // adding rule's attributes
+        final RuleAttribute salience = new RuleAttribute( "salience",
+                                                    "10" );
+        final RuleAttribute noloop = new RuleAttribute( "no-loop",
+                                                  null );
+        model.addAttribute( salience );
+        model.addAttribute( noloop );
+        
+        
+
+        // adding simple pattern
+        final FactPattern pat1 = new FactPattern( "Cheese" );
+        pat1.boundName = "cheese";
+        final Constraint constr = new Constraint( "type" );
+        constr.operator = "==";
+        constr.value = "stilton";
+        constr.constraintValueType = IConstraint.TYPE_LITERAL;
+        pat1.addConstraint( constr );
+        model.addLhsItem( pat1 );
+
+        final CompositeFactPattern comp1 = new CompositeFactPattern();
+        comp1.type = CompositeFactPattern.COMPOSITE_TYPE_OR;
+
+        final FactPattern pat2 = new FactPattern( "Person" );
+        final FactPattern pat3 = new FactPattern( "People" );
+        comp1.addFactPattern( pat2 );
+        comp1.addFactPattern( pat3 );
+
+        model.addLhsItem( comp1 );
+
+        // converting into descr
+        final RuleDescr rule = converter.toDescr( model,
+                                            "" );
+        // checking Attributes
+        final List attributes = rule.getAttributes();
+        assertEquals( 2,
+                      attributes.size() );
+        final AttributeDescr salienceDescr = (AttributeDescr) attributes.get( 0 );
+        assertEquals( salience.attributeName,
+                      salienceDescr.getName() );
+        assertEquals( salience.value,
+                      salienceDescr.getValue() );
+        final AttributeDescr noloopDescr = (AttributeDescr) attributes.get( 1 );
+        assertEquals( noloop.attributeName,
+                      noloopDescr.getName() );
+        assertEquals( "true",
+                      noloopDescr.getValue() );
+
+        // checking LHS patterns
+        final ColumnDescr col1 = (ColumnDescr) rule.getLhs().getDescrs().get( 0 );
+        assertEquals( pat1.boundName,
+                      col1.getIdentifier() );
+        assertEquals( pat1.factType,
+                      col1.getObjectType() );
+        final FieldConstraintDescr field = (FieldConstraintDescr) col1.getDescrs().get( 0 );
+        assertEquals( pat1.constraints[0].fieldName,
+                      field.getFieldName() );
+        final LiteralRestrictionDescr lit = (LiteralRestrictionDescr) field.getRestrictions().get( 0 );
+        assertEquals( pat1.constraints[0].operator,
+                      lit.getEvaluator() );
+        assertEquals( pat1.constraints[0].value,
+                      lit.getText() );
+
+        final OrDescr or1 = (OrDescr) rule.getLhs().getDescrs().get( 1 );
+        final ColumnDescr col2 = (ColumnDescr) or1.getDescrs().get( 0 );
+        assertEquals( pat2.factType,
+                      col2.getObjectType() );
+
+        final ColumnDescr col3 = (ColumnDescr) or1.getDescrs().get( 1 );
+        assertEquals( pat3.factType,
+                      col3.getObjectType() );
+        
+    }
+
 }

Modified: labs/jbossrules/trunk/drools-compiler/src/test/java/org/drools/integrationtests/DynamicRulesTest.java
===================================================================
--- labs/jbossrules/trunk/drools-compiler/src/test/java/org/drools/integrationtests/DynamicRulesTest.java	2007-04-20 14:16:58 UTC (rev 11156)
+++ labs/jbossrules/trunk/drools-compiler/src/test/java/org/drools/integrationtests/DynamicRulesTest.java	2007-04-20 14:22:54 UTC (rev 11157)
@@ -5,6 +5,9 @@
 import java.util.ArrayList;
 import java.util.List;
 
+import junit.framework.Assert;
+import junit.framework.TestCase;
+
 import org.drools.Cheese;
 import org.drools.FactA;
 import org.drools.FactB;
@@ -18,9 +21,6 @@
 import org.drools.compiler.PackageBuilder;
 import org.drools.rule.Package;
 
-import junit.framework.Assert;
-import junit.framework.TestCase;
-
 public class DynamicRulesTest extends TestCase {
     protected RuleBase getRuleBase() throws Exception {
 
@@ -433,5 +433,25 @@
 
         workingMemory.fireAllRules();
     }    
+    
+    public void testRuleBaseAddRemove() throws Exception
+    {
+        RuleBase ruleBase = RuleBaseFactory.newRuleBase();
+        
+        //add and remove
+        PackageBuilder builder = new PackageBuilder();
+        builder.addPackageFromDrl(new InputStreamReader( getClass().getResourceAsStream( "test_Dynamic1.drl" ) ));
+        Package pkg = builder.getPackage();
+        ruleBase.addPackage(pkg);
+        ruleBase.removePackage(pkg.getName());
+            
+        //add and remove again
+        builder = new PackageBuilder();
+        builder.addPackageFromDrl(new InputStreamReader( getClass().getResourceAsStream( "test_Dynamic1.drl" ) ));
+        pkg = builder.getPackage();
+        ruleBase.addPackage(pkg);
+        ruleBase.removePackage(pkg.getName());
+    }
+    
 
 }

Modified: labs/jbossrules/trunk/drools-compiler/src/test/java/org/drools/rule/builder/dialect/mvel/MVELReturnValueBuilderTest.java
===================================================================
--- labs/jbossrules/trunk/drools-compiler/src/test/java/org/drools/rule/builder/dialect/mvel/MVELReturnValueBuilderTest.java	2007-04-20 14:16:58 UTC (rev 11156)
+++ labs/jbossrules/trunk/drools-compiler/src/test/java/org/drools/rule/builder/dialect/mvel/MVELReturnValueBuilderTest.java	2007-04-20 14:22:54 UTC (rev 11157)
@@ -78,10 +78,12 @@
 
         final Declaration[] previousDeclarations = new Declaration[]{a, b};
         final Declaration[] localDeclarations = new Declaration[]{};
+        final String[] requiredGlobals = new String[]{};
 
         final ReturnValueRestriction returnValue = new ReturnValueRestriction( extractor,
                                                                                previousDeclarations,
                                                                                localDeclarations,
+                                                                               requiredGlobals,
                                                                                ValueType.PINTEGER_TYPE.getEvaluator( Operator.EQUAL ) );
 
         final BuildUtils utils = new BuildUtils( new KnowledgeHelperFixer(),

Modified: labs/jbossrules/trunk/drools-compiler/src/test/resources/org/drools/integrationtests/test_Dynamic1.drl
===================================================================
--- labs/jbossrules/trunk/drools-compiler/src/test/resources/org/drools/integrationtests/test_Dynamic1.drl	2007-04-20 14:16:58 UTC (rev 11156)
+++ labs/jbossrules/trunk/drools-compiler/src/test/resources/org/drools/integrationtests/test_Dynamic1.drl	2007-04-20 14:22:54 UTC (rev 11157)
@@ -7,7 +7,7 @@
 
 rule "like stilton"
     when
-        Cheese( type:type == "stilton" )
+        Cheese( type:type == "stilton", price > ( -1 )  )
     then
 		list.add( type );
 end  
\ No newline at end of file

Modified: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/reteoo/Rete.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/reteoo/Rete.java	2007-04-20 14:16:58 UTC (rev 11156)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/reteoo/Rete.java	2007-04-20 14:22:54 UTC (rev 11157)
@@ -53,7 +53,7 @@
  * @author <a href="mailto:mark.proctor at jboss.com">Mark Proctor</a>
  * @author <a href="mailto:bob at werken.com">Bob McWhirter</a>
  */
-class Rete extends ObjectSource
+public class Rete extends ObjectSource
     implements
     Serializable,
     ObjectSink,

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	2007-04-20 14:16:58 UTC (rev 11156)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/reteoo/ReteooBuilder.java	2007-04-20 14:22:54 UTC (rev 11157)
@@ -53,9 +53,6 @@
 
     private transient ReteooWorkingMemory[] workingMemories;
 
-    /** Nodes that have been attached. */
-    private final Map                       attachedNodes;
-
     private Map                             rules;
 
     private transient ReteooRuleBuilder     ruleBuilder;
@@ -72,7 +69,6 @@
      */
     ReteooBuilder(final ReteooRuleBase ruleBase) {
         this.ruleBase = ruleBase;
-        this.attachedNodes = new HashMap();
         this.rules = new HashMap();
 
         //Set to 1 as Rete node is set to 0
@@ -114,7 +110,6 @@
     void addRule(final Rule rule) throws InvalidPatternException {
         final List terminals = this.ruleBuilder.addRule( rule,
                                                    this.ruleBase,
-                                                   this.attachedNodes,
                                                    this.idGenerator );
 
         this.rules.put( rule,

Modified: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/reteoo/TupleSink.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/reteoo/TupleSink.java	2007-04-20 14:16:58 UTC (rev 11156)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/reteoo/TupleSink.java	2007-04-20 14:22:54 UTC (rev 11157)
@@ -30,7 +30,7 @@
  * @author <a href="mailto:mark.proctor at jboss.com">Mark Proctor</a>
  * @author <a href="mailto:bob at werken.com">Bob McWhirter</a>
  */
-interface TupleSink
+public interface TupleSink
     extends
     Serializable,
     Sink {

Modified: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/reteoo/builder/BuildContext.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/reteoo/builder/BuildContext.java	2007-04-20 14:16:58 UTC (rev 11156)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/reteoo/builder/BuildContext.java	2007-04-20 14:22:54 UTC (rev 11157)
@@ -21,7 +21,6 @@
 import java.util.ListIterator;
 import java.util.Map;
 
-import org.drools.common.BaseNode;
 import org.drools.common.BetaConstraints;
 import org.drools.reteoo.ObjectSource;
 import org.drools.reteoo.ReteooBuilder;
@@ -49,9 +48,6 @@
     // offset of the column
     private int                       currentColumnOffset;
 
-    // attached nodes cache
-    private Map                       attachedNodes;
-
     // rule base to add rules to
     private ReteooRuleBase            rulebase;
 
@@ -68,11 +64,9 @@
     BetaConstraints                   betaconstraints;
 
     public BuildContext(final ReteooRuleBase rulebase,
-                        final Map attachedNodes,
                         final ReteooBuilder.IdGenerator idGenerator) {
         this.rulebase = rulebase;
         this.workingMemories = (ReteooWorkingMemory[]) this.rulebase.getWorkingMemories().toArray( new ReteooWorkingMemory[this.rulebase.getWorkingMemories().size()] );
-        this.attachedNodes = attachedNodes;
         this.idGenerator = idGenerator;
 
         this.objectType = new LinkedHashMap();
@@ -149,26 +143,6 @@
     }
 
     /**
-     * Checks if the given candidate node is in cache, and if it is returns it. 
-     * Returns null otherwise.
-     *
-     * @param candidate
-     * @return
-     */
-    public BaseNode getNodeFromCache(final BaseNode candidate) {
-        return (BaseNode) this.attachedNodes.get( candidate );
-    }
-
-    /**
-     * Adds given node to node cache
-     * @param candidate
-     */
-    public void addNodeToCache(final BaseNode node) {
-        this.attachedNodes.put( node,
-                                node );
-    }
-
-    /**
      * Returns context rulebase
      * @return
      */

Modified: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/reteoo/builder/BuildUtils.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/reteoo/builder/BuildUtils.java	2007-04-20 14:16:58 UTC (rev 11156)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/reteoo/builder/BuildUtils.java	2007-04-20 14:22:54 UTC (rev 11157)
@@ -22,6 +22,7 @@
 import java.util.ListIterator;
 import java.util.Map;
 
+import org.drools.RuntimeDroolsException;
 import org.drools.common.BaseNode;
 import org.drools.common.BetaConstraints;
 import org.drools.common.DefaultBetaConstraints;
@@ -30,7 +31,10 @@
 import org.drools.common.QuadroupleBetaConstraints;
 import org.drools.common.SingleBetaConstraints;
 import org.drools.common.TripleBetaConstraints;
+import org.drools.reteoo.ObjectSink;
 import org.drools.reteoo.ObjectSource;
+import org.drools.reteoo.ObjectTypeNode;
+import org.drools.reteoo.TupleSink;
 import org.drools.reteoo.TupleSource;
 import org.drools.rule.Declaration;
 import org.drools.rule.InvalidPatternException;
@@ -82,39 +86,53 @@
      */
     public BaseNode attachNode(final BuildContext context,
                                final BaseNode candidate) {
-        // checks if node is in the cache
-        BaseNode node = context.getNodeFromCache( candidate );
-
-        // if node sharing is enabled and node was found in the cache
-        if ( (node != null) && isSharingEnabledForNode( context,
-                                                        node ) ) {
-            // if node was not previously in use, attach it
-            if ( !node.isInUse() ) {
-                if ( context.getWorkingMemories().length == 0 ) {
-                    node.attach();
-                } else {
-                    node.attach( context.getWorkingMemories() );
+        BaseNode node = null;
+        if( isSharingEnabledForNode( context, candidate ) ) {
+            if ( candidate instanceof ObjectTypeNode ) {
+                ObjectTypeNode otn = (ObjectTypeNode) candidate;
+                otn = (ObjectTypeNode) context.getRuleBase().getRete().getObjectTypeNodes().get( otn.getObjectType() );
+                if ( otn != null ) {
+                    node = otn;
                 }
+            } else if ( (context.getTupleSource() != null) && ( candidate instanceof TupleSink ) ) {
+                TupleSink[] sinks = context.getTupleSource().getSinkPropagator().getSinks(); 
+                for( int i = 0; i < sinks.length; i++ ) {
+                    if( candidate.equals( sinks[i] ) ) {
+                        node = (BaseNode) sinks[i];
+                        break;
+                    }
+                }
+            } else if ( (context.getObjectSource() != null) && (candidate instanceof ObjectSink) ) {
+                ObjectSink[] sinks = context.getObjectSource().getSinkPropagator().getSinks();
+                for( int i = 0; i < sinks.length; i++ ) {
+                    if( candidate.equals( sinks[i] ) ) {
+                        node = (BaseNode) sinks[i];
+                        break;
+                    }
+                }
+            } else {
+                throw new RuntimeDroolsException( "This is a bug on node sharing verification. Please report to development team." );
             }
-            // increment share counter
-            node.addShare();
-            // undo previous id assignment
-            context.releaseLastId();
-        } else {
-            // attach candidate node
+            if( node != null ) {
+                // shared node found
+                // undo previous id assignment
+                context.releaseLastId();
+                node.addShare();
+            }
+        }
+        
+
+        if ( node == null ) {
+            // only attach() if it is a new node
+            node = candidate;
             if ( context.getWorkingMemories().length == 0 ) {
-                candidate.attach();
+                node.attach();
             } else {
-                candidate.attach( context.getWorkingMemories() );
+                node.attach( context.getWorkingMemories() );
             }
-
-            // add it to cache
-            context.addNodeToCache( candidate );
-
-            node = candidate;
         }
-
         return node;
+        
     }
 
     /**

Modified: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/reteoo/builder/ReteooRuleBuilder.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/reteoo/builder/ReteooRuleBuilder.java	2007-04-20 14:16:58 UTC (rev 11156)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/reteoo/builder/ReteooRuleBuilder.java	2007-04-20 14:22:54 UTC (rev 11157)
@@ -59,19 +59,19 @@
         this.utils = new BuildUtils();
 
         this.utils.addBuilder( GroupElement.class,
-                          new GroupElementBuilder() );
+                               new GroupElementBuilder() );
         this.utils.addBuilder( Column.class,
-                          new ColumnBuilder() );
+                               new ColumnBuilder() );
         this.utils.addBuilder( EvalCondition.class,
-                          new EvalBuilder() );
+                               new EvalBuilder() );
         this.utils.addBuilder( From.class,
-                          new FromBuilder() );
+                               new FromBuilder() );
         this.utils.addBuilder( Collect.class,
-                          new CollectBuilder() );
+                               new CollectBuilder() );
         this.utils.addBuilder( Accumulate.class,
-                          new AccumulateBuilder() );
+                               new AccumulateBuilder() );
         this.utils.addBuilder( Forall.class,
-                          new ForallBuilder() );
+                               new ForallBuilder() );
     }
 
     /**
@@ -92,7 +92,6 @@
      */
     public List addRule(final Rule rule,
                         final ReteooRuleBase rulebase,
-                        final Map attachedNodes,
                         final ReteooBuilder.IdGenerator idGenerator) throws InvalidPatternException {
 
         // the list of terminal nodes
@@ -104,12 +103,11 @@
         for ( int i = 0; i < subrules.length; i++ ) {
             // creates a clean build context for each subrule
             final BuildContext context = new BuildContext( rulebase,
-                                                     attachedNodes,
-                                                     idGenerator );
+                                                           idGenerator );
             // adds subrule
             final TerminalNode node = this.addSubRule( context,
-                                                 subrules[i],
-                                                 rule );
+                                                       subrules[i],
+                                                       rule );
 
             // adds the terminal node to the list of terminal nodes
             nodes.add( node );

Modified: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/rule/PredicateConstraint.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/rule/PredicateConstraint.java	2007-04-20 14:16:58 UTC (rev 11156)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/rule/PredicateConstraint.java	2007-04-20 14:22:54 UTC (rev 11157)
@@ -16,6 +16,8 @@
  * limitations under the License.
  */
 
+import java.util.Arrays;
+
 import org.drools.RuntimeDroolsException;
 import org.drools.common.InternalFactHandle;
 import org.drools.common.InternalWorkingMemory;
@@ -41,25 +43,31 @@
     private final Declaration[]        previousDeclarations;
 
     private final Declaration[]        localDeclarations;
+    
+    private final String[]             requiredGlobals;
 
     private static final Declaration[] EMPTY_DECLARATIONS = new Declaration[0];
+    private static final String[] EMPTY_GLOBALS = new String[0];
 
     public PredicateConstraint(final PredicateExpression evaluator) {
         this( evaluator,
               null,
-              null );
+              null,
+              null);
     }
 
     public PredicateConstraint(final Declaration[] previousDeclarations,
                                final Declaration[] localDeclarations) {
         this( null,
               previousDeclarations,
-              localDeclarations );
+              localDeclarations,
+              null);
     }
 
     public PredicateConstraint(final PredicateExpression expression,
                                final Declaration[] previousDeclarations,
-                               final Declaration[] localDeclarations) {
+                               final Declaration[] localDeclarations,
+                               final String[] requiredGlobals ) {
 
         this.expression = expression;
 
@@ -75,6 +83,12 @@
             this.localDeclarations = localDeclarations;
         }
 
+        if ( requiredGlobals == null ) {
+            this.requiredGlobals = PredicateConstraint.EMPTY_GLOBALS;
+        } else {
+            this.requiredGlobals = requiredGlobals;
+        }
+
         this.requiredDeclarations = new Declaration[this.previousDeclarations.length + this.localDeclarations.length];
         System.arraycopy( this.previousDeclarations,
                           0,
@@ -135,6 +149,10 @@
             return false;
         }
 
+        if ( this.requiredGlobals.length != other.requiredGlobals.length ) {
+            return false;
+        }
+
         for ( int i = 0, length = this.previousDeclarations.length; i < length; i++ ) {
             if ( this.previousDeclarations[i].getColumn().getOffset() != other.previousDeclarations[i].getColumn().getOffset() ) {
                 return false;
@@ -155,6 +173,11 @@
             }
         }
 
+        if ( !Arrays.equals( this.requiredGlobals,
+                             other.requiredGlobals ) ) {
+            return false;
+        }
+
         return this.expression.equals( other.expression );
     }
 

Modified: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/rule/ReturnValueRestriction.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/rule/ReturnValueRestriction.java	2007-04-20 14:16:58 UTC (rev 11156)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/rule/ReturnValueRestriction.java	2007-04-20 14:22:54 UTC (rev 11157)
@@ -39,6 +39,8 @@
     private ReturnValueExpression         expression;
 
     private final Declaration[]           requiredDeclarations;
+    
+    private final String[]                requiredGlobals;
 
     private final Declaration[]           previousDeclarations;
 
@@ -48,16 +50,20 @@
 
     private static final Declaration[]    noRequiredDeclarations = new Declaration[]{};
 
+    private static final String[]         noRequiredGlobals = new String[]{};
+
     private final ReturnValueContextEntry contextEntry;
 
     public ReturnValueRestriction(final FieldExtractor fieldExtractor,
                                   final Declaration[] previousDeclarations,
                                   final Declaration[] localDeclarations,
+                                  final String[] requiredGlobals,
                                   final Evaluator evaluator) {
         this( fieldExtractor,
               null,
               previousDeclarations,
               localDeclarations,
+              requiredGlobals,
               evaluator );
     }
 
@@ -65,6 +71,7 @@
                                   final ReturnValueExpression returnValueExpression,
                                   final Declaration[] previousDeclarations,
                                   final Declaration[] localDeclarations,
+                                  final String[] requiredGlobals,
                                   final Evaluator evaluator) {
         this.expression = returnValueExpression;
 
@@ -79,6 +86,12 @@
         } else {
             this.localDeclarations = ReturnValueRestriction.noRequiredDeclarations;
         }
+        
+        if ( requiredGlobals != null ) {
+            this.requiredGlobals = requiredGlobals;
+        } else {
+            this.requiredGlobals = ReturnValueRestriction.noRequiredGlobals;
+        }
 
         this.evaluator = evaluator;
         this.contextEntry = new ReturnValueContextEntry( fieldExtractor,
@@ -162,6 +175,7 @@
         result = PRIME * result + ((this.expression != null) ? this.expression.hashCode() : 0);
         result = PRIME * result + ReturnValueRestriction.hashCode( this.localDeclarations );
         result = PRIME * result + ReturnValueRestriction.hashCode( this.previousDeclarations );
+        result = PRIME * result + ReturnValueRestriction.hashCode( this.requiredGlobals );
         return result;
     }
 
@@ -184,17 +198,26 @@
             return false;
         }
 
-        if ( !Arrays.equals( this.previousDeclarations,
-                             other.previousDeclarations ) ) {
+        if ( this.requiredGlobals.length != other.requiredGlobals.length ) {
             return false;
         }
 
+        if ( !Arrays.equals( this.localDeclarations,
+                             other.localDeclarations ) ) {
+            return false;
+        }
+
         if ( !Arrays.equals( this.previousDeclarations,
                              other.previousDeclarations ) ) {
             return false;
         }
 
-        return this.expression.equals( other.expression );
+        if ( !Arrays.equals( this.requiredGlobals,
+                             other.requiredGlobals ) ) {
+            return false;
+        }
+
+        return this.evaluator.equals( other.evaluator ) && this.expression.equals( other.expression );
     }
 
     private static int hashCode(final Object[] array) {

Modified: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/spi/GlobalExtractor.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/spi/GlobalExtractor.java	2007-04-20 14:16:58 UTC (rev 11156)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/spi/GlobalExtractor.java	2007-04-20 14:22:54 UTC (rev 11157)
@@ -19,12 +19,13 @@
 import java.lang.reflect.Method;
 import java.util.Map;
 
-import org.drools.RuntimeDroolsException;
 import org.drools.base.ClassObjectType;
 import org.drools.base.ValueType;
 
 /**
- * A special extractor for globals
+ * This is a dummy extractor used during rule compilation and build. It is not
+ * supposed to be used to extract real global values during runtime, so
+ * all getValueXXX() methods will raise unsupported operation exceptions.
  * 
  * @author etirelli
  */
@@ -34,18 +35,16 @@
 
     private static final long serialVersionUID = -756967384190918798L;
     private final String            key;
-    private Map               map;
-    private ObjectType        objectType;
+    private final ObjectType        objectType;
 
     public GlobalExtractor(final String key,
                            final Map map) {
         this.key = key;
-        this.map = map;
-        this.objectType = new ClassObjectType( Object.class );
+        this.objectType = new ClassObjectType( (Class) map.get( this.key ));
     }
 
     public Object getValue(final Object object) {
-        return this.map.get( this.key );
+        throw new UnsupportedOperationException("Operation not suported for globals");
     }
 
     public ObjectType getObjectType() {
@@ -53,7 +52,7 @@
     }
 
     public Class getExtractToClass() {
-        return Object.class;
+        return this.objectType.getValueType().getClassType();
     }
 
     public ValueType getValueType() {
@@ -61,106 +60,43 @@
     }
 
     public boolean getBooleanValue(final Object object) {
-        final Object value = this.map.get( this.key );
-        if ( value != null ) {
-            if ( value instanceof Boolean ) {
-                return ((Boolean) value).booleanValue();
-            }
-            throw new RuntimeDroolsException( "Conversion to boolean not supported for type: " + value.getClass() );
-        }
-        throw new RuntimeDroolsException( "Conversion to boolean not supported for a null value" );
+        throw new UnsupportedOperationException("Operation not suported for globals");
     }
 
     public byte getByteValue(final Object object) {
-        final Object value = this.map.get( this.key );
-        if ( value != null ) {
-            if ( value instanceof Number ) {
-                return ((Number) value).byteValue();
-            }
-            throw new RuntimeDroolsException( "Conversion to byte not supported for type: " + value.getClass() );
-        }
-        throw new RuntimeDroolsException( "Conversion to byte not supported for a null value" );
+        throw new UnsupportedOperationException("Operation not suported for globals");
     }
 
     public char getCharValue(final Object object) {
-        final Object value = this.map.get( this.key );
-        if ( value != null ) {
-            if ( value instanceof Character ) {
-                return ((Character) value).charValue();
-            }
-            throw new RuntimeDroolsException( "Conversion to char not supported for type: " + value.getClass() );
-        }
-        throw new RuntimeDroolsException( "Conversion to char not supported for a null value" );
+        throw new UnsupportedOperationException("Operation not suported for globals");
     }
 
     public double getDoubleValue(final Object object) {
-        final Object value = this.map.get( this.key );
-        if ( value != null ) {
-            if ( value instanceof Number ) {
-                return ((Number) value).doubleValue();
-            }
-            throw new RuntimeDroolsException( "Conversion to double not supported for type: " + value.getClass() );
-        }
-        throw new RuntimeDroolsException( "Conversion to double not supported for a null value" );
+        throw new UnsupportedOperationException("Operation not suported for globals");
     }
 
     public float getFloatValue(final Object object) {
-        final Object value = this.map.get( this.key );
-        if ( value != null ) {
-            if ( value instanceof Number ) {
-                return ((Number) value).floatValue();
-            }
-            throw new RuntimeDroolsException( "Conversion to float not supported for type: " + value.getClass() );
-        }
-        throw new RuntimeDroolsException( "Conversion to float not supported for a null value" );
+        throw new UnsupportedOperationException("Operation not suported for globals");
     }
 
     public int getIntValue(final Object object) {
-        final Object value = this.map.get( this.key );
-        if ( value != null ) {
-            if ( value instanceof Number ) {
-                return ((Number) value).intValue();
-            }
-            throw new RuntimeDroolsException( "Conversion to int not supported for type: " + value.getClass() );
-        }
-        throw new RuntimeDroolsException( "Conversion to int not supported for a null value" );
+        throw new UnsupportedOperationException("Operation not suported for globals");
     }
 
     public long getLongValue(final Object object) {
-        final Object value = this.map.get( this.key );
-        if ( value != null ) {
-            if ( value instanceof Number ) {
-                return ((Number) value).longValue();
-            }
-            throw new RuntimeDroolsException( "Conversion to long not supported for type: " + value.getClass() );
-        }
-        throw new RuntimeDroolsException( "Conversion to long not supported for a null value" );
+        throw new UnsupportedOperationException("Operation not suported for globals");
     }
 
     public short getShortValue(final Object object) {
-        final Object value = this.map.get( this.key );
-        if ( value != null ) {
-            if ( value instanceof Number ) {
-                return ((Number) value).shortValue();
-            }
-            throw new RuntimeDroolsException( "Conversion to short not supported for type: " + value.getClass() );
-        }
-        throw new RuntimeDroolsException( "Conversion to short not supported for a null value" );
+        throw new UnsupportedOperationException("Operation not suported for globals");
     }
 
     public Method getNativeReadMethod() {
-        try {
-            return this.getClass().getDeclaredMethod( "getValue",
-                                                      new Class[]{Object.class} );
-        } catch ( final Exception e ) {
-            throw new RuntimeDroolsException( "This is a bug. Please report to development team: " + e.getMessage(),
-                                              e );
-        }
+        throw new UnsupportedOperationException("Operation not suported for globals");
     }
 
     public int getHashCode(final Object object) {
-        final Object value = this.map.get( this.key );
-        return value != null ? value.hashCode() : 0;
+        throw new UnsupportedOperationException("Operation not suported for globals");
     }
 
     public int hashCode() {
@@ -175,8 +111,7 @@
             return false;
         }
         final GlobalExtractor other = (GlobalExtractor) obj;
-        final Object value = this.map.get( this.key );
-        final Object othervalue = other.map.get( this.key );
-        return value == null ? othervalue == null : value.equals( othervalue );
+        return ( key == null ? other.key == null : key.equals( other.key ) ) &&
+               ( this.objectType == null ? other.objectType == null : this.objectType.equals( other.objectType ));
     }
 }

Modified: labs/jbossrules/trunk/drools-core/src/test/java/org/drools/reteoo/builder/ReteooRuleBuilderTest.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/test/java/org/drools/reteoo/builder/ReteooRuleBuilderTest.java	2007-04-20 14:16:58 UTC (rev 11156)
+++ labs/jbossrules/trunk/drools-core/src/test/java/org/drools/reteoo/builder/ReteooRuleBuilderTest.java	2007-04-20 14:22:54 UTC (rev 11157)
@@ -16,9 +16,11 @@
 
 package org.drools.reteoo.builder;
 
-import java.util.HashMap;
 import java.util.List;
 
+import junit.framework.Assert;
+import junit.framework.TestCase;
+
 import org.drools.WorkingMemory;
 import org.drools.base.ClassObjectType;
 import org.drools.reteoo.ReteooBuilder;
@@ -31,9 +33,6 @@
 import org.drools.spi.Consequence;
 import org.drools.spi.KnowledgeHelper;
 
-import junit.framework.Assert;
-import junit.framework.TestCase;
-
 /**
  * @author etirelli
  *
@@ -90,7 +89,6 @@
 
         final List terminals = this.builder.addRule( rule,
                                                this.rulebase,
-                                               new HashMap(),
                                                new ReteooBuilder.IdGenerator( 1 ) );
 
         Assert.assertEquals( "Rule must have a single terminal node",

Modified: labs/jbossrules/trunk/drools-core/src/test/java/org/drools/rule/FieldConstraintTest.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/test/java/org/drools/rule/FieldConstraintTest.java	2007-04-20 14:16:58 UTC (rev 11156)
+++ labs/jbossrules/trunk/drools-core/src/test/java/org/drools/rule/FieldConstraintTest.java	2007-04-20 14:22:54 UTC (rev 11157)
@@ -199,7 +199,8 @@
 
         final PredicateConstraint constraint1 = new PredicateConstraint( evaluator,
                                                                          new Declaration[]{price1Declaration},
-                                                                         new Declaration[]{price2Declaration} );
+                                                                         new Declaration[]{price2Declaration},
+                                                                         new String[]{} );
 
         final Cheese cheddar0 = new Cheese( "cheddar",
                                             5 );
@@ -270,6 +271,7 @@
                                                                           isDoubleThePrice,
                                                                           new Declaration[]{priceDeclaration},
                                                                           new Declaration[0],
+                                                                          new String[0],
                                                                           ValueType.INTEGER_TYPE.getEvaluator( Operator.EQUAL ) );
 
         final ReturnValueConstraint constraint1 = new ReturnValueConstraint( priceExtractor,
@@ -279,6 +281,7 @@
                                                                           isDoubleThePrice,
                                                                           new Declaration[]{priceDeclaration},
                                                                           new Declaration[0],
+                                                                          new String[0],
                                                                           ValueType.INTEGER_TYPE.getEvaluator( Operator.GREATER ) );
 
         final ReturnValueConstraint constraint2 = new ReturnValueConstraint( priceExtractor,




More information about the jboss-svn-commits mailing list