[jboss-svn-commits] JBL Code SVN: r14552 - in labs/jbossrules/trunk: drools-compiler/src/main/java/org/drools/lang/descr and 8 other directories.

jboss-svn-commits at lists.jboss.org jboss-svn-commits at lists.jboss.org
Sat Aug 25 10:41:22 EDT 2007


Author: tirelli
Date: 2007-08-25 10:41:22 -0400 (Sat, 25 Aug 2007)
New Revision: 14552

Added:
   labs/jbossrules/trunk/drools-compiler/src/test/resources/org/drools/integrationtests/test_ShadowProxyOnCollections.drl
Modified:
   labs/jbossrules/trunk/drools-compiler/src/main/java/org/drools/compiler/FunctionError.java
   labs/jbossrules/trunk/drools-compiler/src/main/java/org/drools/lang/descr/PatternDescr.java
   labs/jbossrules/trunk/drools-compiler/src/main/java/org/drools/rule/builder/PatternBuilder.java
   labs/jbossrules/trunk/drools-compiler/src/main/resources/org/drools/rule/builder/dialect/java/javaInvokers.mvel
   labs/jbossrules/trunk/drools-compiler/src/main/resources/org/drools/rule/builder/dialect/java/javaRule.mvel
   labs/jbossrules/trunk/drools-compiler/src/test/java/org/drools/integrationtests/MiscTest.java
   labs/jbossrules/trunk/drools-core/src/main/java/org/drools/base/ClassFieldExtractorCache.java
   labs/jbossrules/trunk/drools-core/src/main/java/org/drools/reteoo/builder/PatternBuilder.java
   labs/jbossrules/trunk/drools-core/src/main/java/org/drools/rule/Declaration.java
   labs/jbossrules/trunk/drools-core/src/main/java/org/drools/rule/Pattern.java
   labs/jbossrules/trunk/drools-core/src/main/java/org/drools/spi/PatternExtractor.java
Log:
JBRULES-1115: unwrapping shadow objects when collected by collect CE

Modified: labs/jbossrules/trunk/drools-compiler/src/main/java/org/drools/compiler/FunctionError.java
===================================================================
--- labs/jbossrules/trunk/drools-compiler/src/main/java/org/drools/compiler/FunctionError.java	2007-08-25 14:23:23 UTC (rev 14551)
+++ labs/jbossrules/trunk/drools-compiler/src/main/java/org/drools/compiler/FunctionError.java	2007-08-25 14:41:22 UTC (rev 14552)
@@ -16,6 +16,7 @@
  * limitations under the License.
  */
 
+import org.drools.commons.jci.problems.CompilationProblem;
 import org.drools.lang.descr.FunctionDescr;
 
 public class FunctionError extends DroolsError {
@@ -43,5 +44,17 @@
     public String getMessage() {
         return this.message;
     }
+    
+    public String toString() {
+        StringBuffer detail = new StringBuffer();
+        if( object instanceof CompilationProblem[] ) {
+            CompilationProblem[] cp = (CompilationProblem[]) object;
+            for( int i = 0; i < cp.length ; i ++ ) {
+               detail.append( cp[i].toString() );
+               detail.append( "\n" );
+            }
+        }
+        return "[ "+functionDescr.getName()+" : "+this.getMessage() + "\n"+detail.toString()+" ]";
+    }
 
 }
\ No newline at end of file

Modified: labs/jbossrules/trunk/drools-compiler/src/main/java/org/drools/lang/descr/PatternDescr.java
===================================================================
--- labs/jbossrules/trunk/drools-compiler/src/main/java/org/drools/lang/descr/PatternDescr.java	2007-08-25 14:23:23 UTC (rev 14551)
+++ labs/jbossrules/trunk/drools-compiler/src/main/java/org/drools/lang/descr/PatternDescr.java	2007-08-25 14:41:22 UTC (rev 14552)
@@ -75,6 +75,10 @@
     public ConditionalElementDescr getConstraint() {
         return this.constraint;
     }
+    
+    public boolean isInternalFact() {
+        return this.getSource() != null && this.getSource() instanceof CollectDescr;
+    }
 
     public String toString() {
         return "[Pattern: id=" + this.identifier + "; objectType=" + this.objectType + "]";

Modified: labs/jbossrules/trunk/drools-compiler/src/main/java/org/drools/rule/builder/PatternBuilder.java
===================================================================
--- labs/jbossrules/trunk/drools-compiler/src/main/java/org/drools/rule/builder/PatternBuilder.java	2007-08-25 14:23:23 UTC (rev 14551)
+++ labs/jbossrules/trunk/drools-compiler/src/main/java/org/drools/rule/builder/PatternBuilder.java	2007-08-25 14:41:22 UTC (rev 14552)
@@ -145,7 +145,8 @@
             pattern = new Pattern( context.getNextPatternId(),
                                    0, // offset is 0 by default
                                    objectType,
-                                   patternDescr.getIdentifier() );
+                                   patternDescr.getIdentifier(),
+                                   patternDescr.isInternalFact());
         } else {
             pattern = new Pattern( context.getNextPatternId(),
                                    0, // offset is 0 by default

Modified: labs/jbossrules/trunk/drools-compiler/src/main/resources/org/drools/rule/builder/dialect/java/javaInvokers.mvel
===================================================================
--- labs/jbossrules/trunk/drools-compiler/src/main/resources/org/drools/rule/builder/dialect/java/javaInvokers.mvel	2007-08-25 14:23:23 UTC (rev 14551)
+++ labs/jbossrules/trunk/drools-compiler/src/main/resources/org/drools/rule/builder/dialect/java/javaInvokers.mvel	2007-08-25 14:41:22 UTC (rev 14552)
@@ -64,9 +64,9 @@
                             org.drools.rule.Declaration[] localDeclarations, 
                             org.drools.WorkingMemory workingMemory) throws Exception {                               
 
-        @foreach{declarationTypes, declarations as type, declr} @{type} @{declr.identifier} = ( @{type} ) previousDeclarations[@{i0}].@{declr.extractor.nativeReadMethod.name}( (org.drools.common.InternalWorkingMemory) workingMemory, ( (org.drools.common.InternalFactHandle) tuple.get( previousDeclarations[@{i0}] ) ).getObject() );
+        @foreach{declarationTypes, declarations as type, declr} @{type} @{declr.identifier} = ( @{type} ) previousDeclarations[@{i0}].@{declr.nativeReadMethod.name}( (org.drools.common.InternalWorkingMemory) workingMemory, ( (org.drools.common.InternalFactHandle) tuple.get( previousDeclarations[@{i0}] ) ).getObject() );
         @end{}                
-        @foreach{localDeclarationTypes, localDeclarations as type, declr} @{type} @{declr.identifier} = ( @{type} ) localDeclarations[@{i0}].@{declr.extractor.nativeReadMethod.name}( (org.drools.common.InternalWorkingMemory) workingMemory, object );
+        @foreach{localDeclarationTypes, localDeclarations as type, declr} @{type} @{declr.identifier} = ( @{type} ) localDeclarations[@{i0}].@{declr.nativeReadMethod.name}( (org.drools.common.InternalWorkingMemory) workingMemory, object );
         @end{}
         @foreach{globalTypes, globals as type, identifier} @{type} @{identifier} = ( @{type} ) workingMemory.getGlobal( "@{identifier}" );
         @end{}
@@ -101,9 +101,9 @@
                             org.drools.rule.Declaration[] localDeclarations, 
                             org.drools.WorkingMemory workingMemory) throws Exception {                               
 
-        @foreach{declarationTypes, declarations as type, declr} @{type} @{declr.identifier} = ( @{type} ) previousDeclarations[@{i0}].@{declr.extractor.nativeReadMethod.name}( (org.drools.common.InternalWorkingMemory) workingMemory, ( (org.drools.common.InternalFactHandle) tuple.get( previousDeclarations[@{i0}] ) ).getObject() );
+        @foreach{declarationTypes, declarations as type, declr} @{type} @{declr.identifier} = ( @{type} ) previousDeclarations[@{i0}].@{declr.nativeReadMethod.name}( (org.drools.common.InternalWorkingMemory) workingMemory, ( (org.drools.common.InternalFactHandle) tuple.get( previousDeclarations[@{i0}] ) ).getObject() );
         @end{}                
-        @foreach{localDeclarationTypes, localDeclarations as type, declr} @{type} @{declr.identifier} = ( @{type} ) localDeclarations[@{i0}].@{declr.extractor.nativeReadMethod.name}( (org.drools.common.InternalWorkingMemory) workingMemory, object );
+        @foreach{localDeclarationTypes, localDeclarations as type, declr} @{type} @{declr.identifier} = ( @{type} ) localDeclarations[@{i0}].@{declr.nativeReadMethod.name}( (org.drools.common.InternalWorkingMemory) workingMemory, object );
         @end{}
         @foreach{globalTypes, globals as type, identifier} @{type} @{identifier} = ( @{type} ) workingMemory.getGlobal( "@{identifier}" );
         @end{}
@@ -136,7 +136,7 @@
                             org.drools.rule.Declaration[] declarations, 
                             org.drools.WorkingMemory workingMemory) throws Exception {                             
 
-        @foreach{declarationTypes, declarations as type, declr} @{type} @{declr.identifier} = ( @{type} ) declarations[@{i0}].@{declr.extractor.nativeReadMethod.name}( (org.drools.common.InternalWorkingMemory) workingMemory, ( (org.drools.common.InternalFactHandle) tuple.get( declarations[@{i0}] ) ).getObject() );
+        @foreach{declarationTypes, declarations as type, declr} @{type} @{declr.identifier} = ( @{type} ) declarations[@{i0}].@{declr.nativeReadMethod.name}( (org.drools.common.InternalWorkingMemory) workingMemory, ( (org.drools.common.InternalFactHandle) tuple.get( declarations[@{i0}] ) ).getObject() );
         @end{}                
         @foreach{globalTypes, globals as type, identifier} @{type} @{identifier} = ( @{type} ) workingMemory.getGlobal( "@{identifier}" );
         @end{}
@@ -176,7 +176,7 @@
                      org.drools.spi.Tuple leftTuple,
                      org.drools.rule.Declaration[] declarations,
                      org.drools.WorkingMemory workingMemory) throws Exception {
-        @foreach{declarationTypes, declarations as type, declr} @{type} @{declr.identifier} = ( @{type} ) declarations[@{i0}].@{declr.extractor.nativeReadMethod.name}( (org.drools.common.InternalWorkingMemory) workingMemory, ( (org.drools.common.InternalFactHandle) leftTuple.get( declarations[@{i0}] ) ).getObject() );
+        @foreach{declarationTypes, declarations as type, declr} @{type} @{declr.identifier} = ( @{type} ) declarations[@{i0}].@{declr.nativeReadMethod.name}( (org.drools.common.InternalWorkingMemory) workingMemory, ( (org.drools.common.InternalFactHandle) leftTuple.get( declarations[@{i0}] ) ).getObject() );
         @end{}
         @foreach{globalTypes, globals as type, identifier} @{type} @{identifier} = ( @{type} ) workingMemory.getGlobal( "@{identifier}" );
         @end{}    
@@ -194,7 +194,7 @@
                            org.drools.rule.Declaration[] declarations,
                            org.drools.rule.Declaration[] innerDeclarations,
                            org.drools.WorkingMemory workingMemory) throws Exception {
-        @foreach{declarationTypes, declarations as type, declr} @{type} @{declr.identifier} = ( @{type} ) declarations[@{i0}].@{declr.extractor.nativeReadMethod.name}( (org.drools.common.InternalWorkingMemory) workingMemory, ( (org.drools.common.InternalFactHandle) leftTuple.get( declarations[@{i0}] ) ).getObject() );
+        @foreach{declarationTypes, declarations as type, declr} @{type} @{declr.identifier} = ( @{type} ) declarations[@{i0}].@{declr.nativeReadMethod.name}( (org.drools.common.InternalWorkingMemory) workingMemory, ( (org.drools.common.InternalFactHandle) leftTuple.get( declarations[@{i0}] ) ).getObject() );
         @end{}
         @foreach{globalTypes, globals as type, identifier} @{type} @{identifier} = ( @{type} ) workingMemory.getGlobal( "@{identifier}" );
         @end{}    
@@ -214,7 +214,7 @@
                            org.drools.rule.Declaration[] declarations,
                            org.drools.rule.Declaration[] innerDeclarations,
                            org.drools.WorkingMemory workingMemory) throws Exception {
-        @foreach{declarationTypes, declarations as type, declr} @{type} @{declr.identifier} = ( @{type} ) declarations[@{i0}].@{declr.extractor.nativeReadMethod.name}( (org.drools.common.InternalWorkingMemory) workingMemory, ( (org.drools.common.InternalFactHandle) leftTuple.get( declarations[@{i0}] ) ).getObject() );
+        @foreach{declarationTypes, declarations as type, declr} @{type} @{declr.identifier} = ( @{type} ) declarations[@{i0}].@{declr.nativeReadMethod.name}( (org.drools.common.InternalWorkingMemory) workingMemory, ( (org.drools.common.InternalFactHandle) leftTuple.get( declarations[@{i0}] ) ).getObject() );
         @end{}
         @foreach{globalTypes, globals as type, identifier} @{type} @{identifier} = ( @{type} ) workingMemory.getGlobal( "@{identifier}" );
         @end{}    
@@ -232,7 +232,7 @@
                             org.drools.spi.Tuple leftTuple,
                             org.drools.rule.Declaration[] declarations,
                             org.drools.WorkingMemory workingMemory) throws Exception {
-        @foreach{declarationTypes, declarations as type, declr} @{type} @{declr.identifier} = ( @{type} ) declarations[@{i0}].@{declr.extractor.nativeReadMethod.name}( (org.drools.common.InternalWorkingMemory) workingMemory, ( (org.drools.common.InternalFactHandle) leftTuple.get( declarations[@{i0}] ) ).getObject() );
+        @foreach{declarationTypes, declarations as type, declr} @{type} @{declr.identifier} = ( @{type} ) declarations[@{i0}].@{declr.nativeReadMethod.name}( (org.drools.common.InternalWorkingMemory) workingMemory, ( (org.drools.common.InternalFactHandle) leftTuple.get( declarations[@{i0}] ) ).getObject() );
         @end{}
         @foreach{globalTypes, globals as type, identifier} @{type} @{identifier} = ( @{type} ) workingMemory.getGlobal( "@{identifier}" );
         @end{}    
@@ -291,7 +291,7 @@
 
         @foreach{declarationTypes, declarations, indexes, notPatterns as type, declr, index, notPattern}
           org.drools.common.InternalFactHandle @{declr.identifier}__Handle__ = ( org.drools.common.InternalFactHandle ) tuple.get( knowledgeHelper.getDeclaration( declarations[@{index}].getIdentifier() ) );        
-          @{type} @{declr.identifier} = ( @{type} )  declarations[@{index}].@{declr.extractor.nativeReadMethod.name}( (org.drools.common.InternalWorkingMemory) workingMemory, @{declr.identifier}__Handle__.getObject() );
+          @{type} @{declr.identifier} = ( @{type} )  declarations[@{index}].@{declr.nativeReadMethod.name}( (org.drools.common.InternalWorkingMemory) workingMemory, @{declr.identifier}__Handle__.getObject() );
           @if{notPattern}@{declr.identifier}__Handle__ =  (org.drools.common.InternalFactHandle) knowledgeHelper.getWorkingMemory().getFactHandle( @{declr.identifier} );@end{}
         @end{}
         

Modified: labs/jbossrules/trunk/drools-compiler/src/main/resources/org/drools/rule/builder/dialect/java/javaRule.mvel
===================================================================
--- labs/jbossrules/trunk/drools-compiler/src/main/resources/org/drools/rule/builder/dialect/java/javaRule.mvel	2007-08-25 14:23:23 UTC (rev 14551)
+++ labs/jbossrules/trunk/drools-compiler/src/main/resources/org/drools/rule/builder/dialect/java/javaRule.mvel	2007-08-25 14:41:22 UTC (rev 14552)
@@ -42,7 +42,7 @@
                            @foreach{declarationTypes, declarations as type, declr} @{type} @{declr.identifier}@end{","}@if{globals != empty}, at end{} 
                            @foreach{globalTypes, globals as type, identifier} @{type} @{identifier} @end{","}
                            ) throws Exception {
-        @foreach{innerDeclarations as declr} @{declr.extractor.extractToClassName} @{declr.identifier} = (@{declr.extractor.extractToClassName}) innerDeclarations[@{i0}].@{declr.extractor.nativeReadMethod.name}( (org.drools.common.InternalWorkingMemory) workingMemory, object );
+        @foreach{innerDeclarations as declr} @{declr.extractor.extractToClassName} @{declr.identifier} = (@{declr.extractor.extractToClassName}) innerDeclarations[@{i0}].@{declr.nativeReadMethod.name}( (org.drools.common.InternalWorkingMemory) workingMemory, object );
         @end{}
         @{actionCode}
     }
@@ -53,7 +53,7 @@
                            @foreach{declarationTypes, declarations as type, declr} @{type} @{declr.identifier}@end{","}@if{globals != empty}, at end{} 
                            @foreach{globalTypes, globals as type, identifier} @{type} @{identifier} @end{","}
                            ) throws Exception {
-        @foreach{innerDeclarations as declr} @{declr.extractor.extractToClassName} @{declr.identifier} = (@{declr.extractor.extractToClassName}) innerDeclarations[@{i0}].@{declr.extractor.nativeReadMethod.name}( (org.drools.common.InternalWorkingMemory) workingMemory, object );
+        @foreach{innerDeclarations as declr} @{declr.extractor.extractToClassName} @{declr.identifier} = (@{declr.extractor.extractToClassName}) innerDeclarations[@{i0}].@{declr.nativeReadMethod.name}( (org.drools.common.InternalWorkingMemory) workingMemory, object );
         @end{}
         @{reverseCode}
     }

Modified: labs/jbossrules/trunk/drools-compiler/src/test/java/org/drools/integrationtests/MiscTest.java
===================================================================
--- labs/jbossrules/trunk/drools-compiler/src/test/java/org/drools/integrationtests/MiscTest.java	2007-08-25 14:23:23 UTC (rev 14551)
+++ labs/jbossrules/trunk/drools-compiler/src/test/java/org/drools/integrationtests/MiscTest.java	2007-08-25 14:41:22 UTC (rev 14552)
@@ -66,7 +66,6 @@
 import org.drools.State;
 import org.drools.StatefulSession;
 import org.drools.StatelessSession;
-import org.drools.StatelessSessionResult;
 import org.drools.TestParam;
 import org.drools.WorkingMemory;
 import org.drools.Cheesery.Maturity;
@@ -3946,4 +3945,30 @@
                       results.size() );
     }
 
+    public void testShadowProxyOnCollections() throws Exception {
+        final PackageBuilder builder = new PackageBuilder();
+        builder.addPackageFromDrl( new InputStreamReader( getClass().getResourceAsStream( "test_ShadowProxyOnCollections.drl" ) ) );
+        final Package pkg = builder.getPackage();
+
+        final RuleBase ruleBase = getRuleBase();
+        ruleBase.addPackage( pkg );
+        final WorkingMemory workingMemory = ruleBase.newStatefulSession();
+
+        final List results = new ArrayList();
+        workingMemory.setGlobal( "results",
+                                 results );
+
+        final Cheesery cheesery = new Cheesery();
+        workingMemory.insert( cheesery );
+
+        workingMemory.fireAllRules();
+
+        assertEquals( 1,
+                      results.size() );
+        assertEquals( 1, 
+                      cheesery.getCheeses().size() );
+        assertEquals( results.get( 0 ), 
+                      cheesery.getCheeses().get( 0 ) );
+    }
+
 }
\ No newline at end of file

Added: labs/jbossrules/trunk/drools-compiler/src/test/resources/org/drools/integrationtests/test_ShadowProxyOnCollections.drl
===================================================================
--- labs/jbossrules/trunk/drools-compiler/src/test/resources/org/drools/integrationtests/test_ShadowProxyOnCollections.drl	                        (rev 0)
+++ labs/jbossrules/trunk/drools-compiler/src/test/resources/org/drools/integrationtests/test_ShadowProxyOnCollections.drl	2007-08-25 14:41:22 UTC (rev 14552)
@@ -0,0 +1,16 @@
+package org.drools;
+
+import java.util.ArrayList;
+
+global java.util.List results;
+
+rule "shadow proxy on collections"
+	when
+		$list : ArrayList() from collect( Cheesery() )
+	then
+	    Cheesery $c = (Cheesery) $list.get( 0 );
+	    Cheese cheese = new Cheese( "stilton", 10 );
+		$c.getCheeses().add( cheese );
+		results.add( cheese );
+end
+

Modified: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/base/ClassFieldExtractorCache.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/base/ClassFieldExtractorCache.java	2007-08-25 14:23:23 UTC (rev 14551)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/base/ClassFieldExtractorCache.java	2007-08-25 14:41:22 UTC (rev 14552)
@@ -20,9 +20,9 @@
         return instance;
     }
 
-    public static ClassFieldExtractor getExtractor(final Class clazz,
-                                                   final String fieldName,
-                                                   ClassLoader classLoader) {
+    public synchronized static ClassFieldExtractor getExtractor(final Class clazz,
+                                                                final String fieldName,
+                                                                ClassLoader classLoader) {
         if ( cache == null ) {
             cache = new HashMap();
         }

Modified: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/reteoo/builder/PatternBuilder.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/reteoo/builder/PatternBuilder.java	2007-08-25 14:23:23 UTC (rev 14551)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/reteoo/builder/PatternBuilder.java	2007-08-25 14:41:22 UTC (rev 14552)
@@ -29,7 +29,6 @@
 import org.drools.reteoo.Rete;
 import org.drools.reteoo.ReteooBuilder;
 import org.drools.reteoo.ReteooRuleBase;
-import org.drools.reteoo.ReteooBuilder.IdGenerator;
 import org.drools.rule.Declaration;
 import org.drools.rule.InvalidPatternException;
 import org.drools.rule.Pattern;

Modified: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/rule/Declaration.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/rule/Declaration.java	2007-08-25 14:23:23 UTC (rev 14551)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/rule/Declaration.java	2007-08-25 14:41:22 UTC (rev 14552)
@@ -41,7 +41,12 @@
  */
 
 import java.io.Serializable;
+import java.lang.reflect.Method;
+import java.util.Collection;
+import java.util.Iterator;
 
+import org.drools.RuntimeDroolsException;
+import org.drools.base.ShadowProxy;
 import org.drools.base.ValueType;
 import org.drools.common.InternalWorkingMemory;
 import org.drools.spi.Extractor;
@@ -85,8 +90,10 @@
 
     private final Extractor   extractor;
 
-    private Pattern            pattern;
-    
+    private Pattern           pattern;
+
+    private final boolean     internalFact;
+
     // ------------------------------------------------------------
     // Constructors
     // ------------------------------------------------------------
@@ -104,9 +111,33 @@
     public Declaration(final String identifier,
                        final Extractor extractor,
                        final Pattern pattern) {
+        this( identifier,
+              extractor,
+              pattern,
+              false );
+    }
+
+    /**
+     * Construct.
+     * 
+     * @param identifier
+     *            The name of the variable.
+     * @param objectType
+     *            The type of this variable declaration.
+     * @param order
+     *            The index within a rule.
+     * @param internalFact
+     *            True if this is an internal fact created by the engine, like a collection result
+     *            of a collect CE
+     */
+    public Declaration(final String identifier,
+                       final Extractor extractor,
+                       final Pattern pattern,
+                       final boolean internalFact) {
         this.identifier = identifier;
         this.extractor = extractor;
         this.pattern = pattern;
+        this.internalFact = internalFact;
     }
 
     // ------------------------------------------------------------
@@ -143,13 +174,13 @@
     public void setPattern(final Pattern pattern) {
         this.pattern = pattern;
     }
-    
+
     /**
      * Returns true if this declaration is a pattern declaration
      * @return
      */
     public boolean isPatternDeclaration() {
-        return this.pattern.getDeclaration() == this;
+        return this.pattern != null && this.pattern.getDeclaration() == this;
     }
 
     /**
@@ -161,50 +192,104 @@
         return this.extractor;
     }
 
-    public Object getValue(InternalWorkingMemory workingMemory, final Object object) {
-        return this.extractor.getValue( workingMemory, object );
+    public Object getValue(InternalWorkingMemory workingMemory,
+                           final Object object) {
+        return this.extractor.getValue( workingMemory,
+                                        object );
     }
 
-    public char getCharValue(InternalWorkingMemory workingMemory, final Object object) {
-        return this.extractor.getCharValue( workingMemory, object );
+    public Object getNonShadowedValue(InternalWorkingMemory workingMemory,
+                                      final Object object) {
+        Object result = this.extractor.getValue( workingMemory,
+                                                 object );
+        if ( this.isInternalFact() && result instanceof Collection ) {
+            try {
+                Collection newCol = (Collection) result.getClass().newInstance();
+                for ( Iterator it = ((Collection) result).iterator(); it.hasNext(); ) {
+                    Object element = it.next();
+                    newCol.add( (element instanceof ShadowProxy) ? ((ShadowProxy) element).getShadowedObject() : element );
+                }
+                return newCol;
+            } catch ( InstantiationException e ) {
+                // nothing we can do, so just return the resulting object
+            } catch ( IllegalAccessException e ) {
+                // TODO Auto-generated catch block
+            }
+        }
+        return result;
     }
 
-    public int getIntValue(InternalWorkingMemory workingMemory, final Object object) {
-        return this.extractor.getIntValue( workingMemory, object );
+    public char getCharValue(InternalWorkingMemory workingMemory,
+                             final Object object) {
+        return this.extractor.getCharValue( workingMemory,
+                                            object );
     }
 
-    public byte getByteValue(InternalWorkingMemory workingMemory, final Object object) {
-        return this.extractor.getByteValue( workingMemory, object );
+    public int getIntValue(InternalWorkingMemory workingMemory,
+                           final Object object) {
+        return this.extractor.getIntValue( workingMemory,
+                                           object );
     }
 
-    public short getShortValue(InternalWorkingMemory workingMemory, final Object object) {
-        return this.extractor.getShortValue( workingMemory, object );
+    public byte getByteValue(InternalWorkingMemory workingMemory,
+                             final Object object) {
+        return this.extractor.getByteValue( workingMemory,
+                                            object );
     }
 
-    public long getLongValue(InternalWorkingMemory workingMemory, final Object object) {
-        return this.extractor.getLongValue( workingMemory, object );
+    public short getShortValue(InternalWorkingMemory workingMemory,
+                               final Object object) {
+        return this.extractor.getShortValue( workingMemory,
+                                             object );
     }
 
-    public float getFloatValue(InternalWorkingMemory workingMemory, final Object object) {
-        return this.extractor.getFloatValue( workingMemory, object );
+    public long getLongValue(InternalWorkingMemory workingMemory,
+                             final Object object) {
+        return this.extractor.getLongValue( workingMemory,
+                                            object );
     }
 
-    public double getDoubleValue(InternalWorkingMemory workingMemory, final Object object) {
-        return this.extractor.getDoubleValue( workingMemory, object );
+    public float getFloatValue(InternalWorkingMemory workingMemory,
+                               final Object object) {
+        return this.extractor.getFloatValue( workingMemory,
+                                             object );
     }
 
-    public boolean getBooleanValue(InternalWorkingMemory workingMemory, final Object object) {
-        return this.extractor.getBooleanValue( workingMemory, object );
+    public double getDoubleValue(InternalWorkingMemory workingMemory,
+                                 final Object object) {
+        return this.extractor.getDoubleValue( workingMemory,
+                                              object );
     }
 
-    public int getHashCode(InternalWorkingMemory workingMemory, final Object object) {
-        return this.extractor.getHashCode( workingMemory, object );
+    public boolean getBooleanValue(InternalWorkingMemory workingMemory,
+                                   final Object object) {
+        return this.extractor.getBooleanValue( workingMemory,
+                                               object );
     }
-    
+
+    public int getHashCode(InternalWorkingMemory workingMemory,
+                           final Object object) {
+        return this.extractor.getHashCode( workingMemory,
+                                           object );
+    }
+
     public boolean isGlobal() {
         return this.extractor.isGlobal();
     }
 
+    public Method getNativeReadMethod() {
+        if ( this.isPatternDeclaration() && this.isInternalFact() ) {
+            try {
+                return this.getClass().getDeclaredMethod( "getNonShadowedValue",
+                                                          new Class[]{InternalWorkingMemory.class, Object.class} );
+            } catch ( final Exception e ) {
+                throw new RuntimeDroolsException( "This is a bug. Please report to development team: " + e.getMessage(),
+                                                  e );
+            }
+        } 
+        return this.extractor.getNativeReadMethod();
+    }
+
     // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 
     public String toString() {
@@ -234,4 +319,8 @@
         return this.pattern.getOffset() == other.pattern.getOffset() && this.identifier.equals( other.identifier ) && this.extractor.equals( other.extractor );
     }
 
+    protected boolean isInternalFact() {
+        return internalFact;
+    }
+
 }

Modified: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/rule/Pattern.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/rule/Pattern.java	2007-08-25 14:23:23 UTC (rev 14551)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/rule/Pattern.java	2007-08-25 14:41:22 UTC (rev 14552)
@@ -47,7 +47,7 @@
     private int               offset;
 
     public Pattern(final int index,
-                  final ObjectType objectType) {
+                   final ObjectType objectType) {
         this( index,
               index,
               objectType,
@@ -55,8 +55,8 @@
     }
 
     public Pattern(final int index,
-                  final ObjectType objectType,
-                  final String identifier) {
+                   final ObjectType objectType,
+                   final String identifier) {
         this( index,
               index,
               objectType,
@@ -64,16 +64,29 @@
     }
 
     public Pattern(final int index,
-                  final int offset,
-                  final ObjectType objectType,
-                  final String identifier) {
+                   final int offset,
+                   final ObjectType objectType,
+                   final String identifier) {
+        this( index,
+              offset,
+              objectType,
+              identifier,
+              false );
+    }
+
+    public Pattern(final int index,
+                   final int offset,
+                   final ObjectType objectType,
+                   final String identifier,
+                   final boolean isInternalFact) {
         this.index = index;
         this.offset = offset;
         this.objectType = objectType;
         if ( identifier != null && (!identifier.equals( "" )) ) {
             this.declaration = new Declaration( identifier,
                                                 new PatternExtractor( objectType ),
-                                                this );
+                                                this,
+                                                isInternalFact );
             this.declarations = new HashMap( 2 ); // default to avoid immediate resize
             this.declarations.put( this.declaration.getIdentifier(),
                                    this.declaration );
@@ -85,8 +98,8 @@
     public Object clone() {
         final String identifier = (this.declaration != null) ? this.declaration.getIdentifier() : null;
         final Pattern clone = new Pattern( this.index,
-                                   this.objectType,
-                                   identifier );
+                                           this.objectType,
+                                           identifier );
 
         for ( final Iterator it = this.constraints.iterator(); it.hasNext(); ) {
             final Object constr = it.next();
@@ -230,7 +243,7 @@
         if ( this.offset != other.offset ) {
             return false;
         }
-        return ( this.source == null ) ? other.source == null : this.source.equals( other.source ); 
+        return (this.source == null) ? other.source == null : this.source.equals( other.source );
     }
 
 }
\ No newline at end of file

Modified: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/spi/PatternExtractor.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/spi/PatternExtractor.java	2007-08-25 14:23:23 UTC (rev 14551)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/spi/PatternExtractor.java	2007-08-25 14:41:22 UTC (rev 14552)
@@ -1,6 +1,8 @@
 package org.drools.spi;
 
 import java.lang.reflect.Method;
+import java.util.Collection;
+import java.util.Iterator;
 
 import org.drools.RuntimeDroolsException;
 import org.drools.base.ClassObjectType;




More information about the jboss-svn-commits mailing list