[jboss-svn-commits] JBL Code SVN: r25600 - in labs/jbossrules/branches/Branch_4_0_2_SOA_4_2: drools-core/src/main/java/org/drools/base/mvel and 1 other directory.

jboss-svn-commits at lists.jboss.org jboss-svn-commits at lists.jboss.org
Wed Mar 11 14:57:15 EDT 2009


Author: tirelli
Date: 2009-03-11 14:57:15 -0400 (Wed, 11 Mar 2009)
New Revision: 25600

Modified:
   labs/jbossrules/branches/Branch_4_0_2_SOA_4_2/drools-compiler/src/test/java/org/drools/rule/builder/dialect/mvel/MVELSalienceBuilderTest.java
   labs/jbossrules/branches/Branch_4_0_2_SOA_4_2/drools-core/src/main/java/org/drools/base/mvel/MVELSalienceExpression.java
Log:
JBRULES-1991: porting fix into the SOA-P branch as requested by customer issue: 274682

Modified: labs/jbossrules/branches/Branch_4_0_2_SOA_4_2/drools-compiler/src/test/java/org/drools/rule/builder/dialect/mvel/MVELSalienceBuilderTest.java
===================================================================
--- labs/jbossrules/branches/Branch_4_0_2_SOA_4_2/drools-compiler/src/test/java/org/drools/rule/builder/dialect/mvel/MVELSalienceBuilderTest.java	2009-03-11 18:29:25 UTC (rev 25599)
+++ labs/jbossrules/branches/Branch_4_0_2_SOA_4_2/drools-compiler/src/test/java/org/drools/rule/builder/dialect/mvel/MVELSalienceBuilderTest.java	2009-03-11 18:57:15 UTC (rev 25600)
@@ -11,8 +11,6 @@
 import org.drools.WorkingMemory;
 import org.drools.base.ClassObjectType;
 import org.drools.common.InternalFactHandle;
-import org.drools.compiler.DialectConfiguration;
-import org.drools.compiler.DialectRegistry;
 import org.drools.compiler.PackageBuilder;
 import org.drools.compiler.PackageBuilderConfiguration;
 import org.drools.lang.descr.RuleDescr;
@@ -23,9 +21,16 @@
 import org.drools.rule.builder.SalienceBuilder;
 import org.drools.spi.ObjectType;
 import org.drools.spi.PatternExtractor;
+import org.drools.spi.Salience;
 
 public class MVELSalienceBuilderTest extends TestCase {
-    public void testSimpleExpression() {        
+
+    private InstrumentedBuildContent context;
+    private RuleBase ruleBase;
+
+    @Override
+    protected void setUp() throws Exception {
+        super.setUp();
         final Package pkg = new Package( "pkg1" );
         final RuleDescr ruleDescr = new RuleDescr( "rule 1" );
         ruleDescr.setSalience( "(p.age + 20)/2" );
@@ -33,13 +38,13 @@
 
         PackageBuilder pkgBuilder = new PackageBuilder( pkg );
         final PackageBuilderConfiguration conf = pkgBuilder.getPackageBuilderConfiguration();
-        MVELDialect mvelDialect = ( MVELDialect ) pkgBuilder.getDialectRegistry().getDialect( "mvel" );
+        MVELDialect mvelDialect = (MVELDialect) pkgBuilder.getDialectRegistry().getDialect( "mvel" );
 
-        final InstrumentedBuildContent context = new InstrumentedBuildContent( conf,
-                                                                               pkg,
-                                                                               ruleDescr,
-                                                                               pkgBuilder.getDialectRegistry(),
-                                                                               mvelDialect );
+        context = new InstrumentedBuildContent( conf,
+                                                pkg,
+                                                ruleDescr,
+                                                pkgBuilder.getDialectRegistry(),
+                                                mvelDialect );
 
         final InstrumentedDeclarationScopeResolver declarationResolver = new InstrumentedDeclarationScopeResolver();
 
@@ -59,17 +64,102 @@
         declarationResolver.setDeclarations( map );
         context.setDeclarationResolver( declarationResolver );
 
-        final RuleBase ruleBase = RuleBaseFactory.newRuleBase();
-        final WorkingMemory wm = ruleBase.newStatefulSession();
+        ruleBase = RuleBaseFactory.newRuleBase();
 
-        final Person p = new Person("mark", "", 31);
+        SalienceBuilder salienceBuilder = new MVELSalienceBuilder();
+        salienceBuilder.build( context );
+    }
+
+    public void testSimpleExpression() {
+        WorkingMemory wm = ruleBase.newStatefulSession();
+
+        final Person p = new Person( "mark",
+                                     "",
+                                     31 );
         final InternalFactHandle f0 = (InternalFactHandle) wm.insert( p );
         final ReteTuple tuple = new ReteTuple( f0 );
 
-        SalienceBuilder salienceBuilder = new MVELSalienceBuilder();
-        salienceBuilder.build( context );
-                
-        assertEquals( 25, context.getRule().getSalience().getValue( tuple, wm ) );
-      
+        assertEquals( 25,
+                      context.getRule().getSalience().getValue( tuple,
+                                                                wm ) );
+
     }
+
+    public void testMultithreadSalienceExpression() {
+        final int tcount = 10;
+        final SalienceEvaluator[] evals = new SalienceEvaluator[tcount];
+        final Thread[] threads = new Thread[tcount];
+        for( int i = 0; i < evals.length; i++ ) {
+            evals[i] = new SalienceEvaluator(ruleBase, context.getRule().getSalience(), new Person( "bob"+i, 30+(i*3) ) );
+            threads[i] = new Thread( evals[i] );
+        }
+        for( int i = 0; i < threads.length ; i++ ) {
+            threads[i].start();
+        }
+        for( int i = 0; i < threads.length ; i++ ) {
+            try {
+                threads[i].join();
+            } catch ( InterruptedException e ) {
+                e.printStackTrace();
+            }
+        }
+        int errors = 0;
+        for( int i = 0; i < evals.length; i++ ) {
+            if( evals[i].isError() ) {
+                errors++;
+            }
+        }
+        assertEquals( "There shouldn't be any threads in error: ", 0, errors);
+
+    }
+    
+    public static class SalienceEvaluator implements Runnable {
+        public static final int iterations = 1000;
+
+        private Salience salience;
+        private ReteTuple tuple;
+        private WorkingMemory wm;
+        private final int result;
+        private transient boolean halt;
+
+        private boolean error;
+
+        public SalienceEvaluator( RuleBase ruleBase, Salience salience, Person person ) {
+            wm = ruleBase.newStatefulSession();
+            final InternalFactHandle f0 = (InternalFactHandle) wm.insert( person );
+            tuple = new ReteTuple( f0 );
+            this.salience = salience;
+            this.halt = false;
+            this.error = false;
+            this.result = (person.getAge() + 20)/2;
+        }
+
+        public void run() {
+            try {
+                Thread.sleep( 1000 );
+                for( int i = 0; i < iterations && !halt ; i++ ) {
+                    assertEquals( result,
+                                  salience.getValue( tuple,
+                                                     wm ) );
+                    Thread.currentThread().yield();
+                }
+            } catch ( Throwable e ) {
+                e.printStackTrace();
+                this.error = true;
+            }
+        }
+        
+        public void halt() {
+            this.halt = true;
+        }
+
+        public boolean isError() {
+            return error;
+        }
+
+        public void setError(boolean error) {
+            this.error = error;
+        }
+        
+    }
 }

Modified: labs/jbossrules/branches/Branch_4_0_2_SOA_4_2/drools-core/src/main/java/org/drools/base/mvel/MVELSalienceExpression.java
===================================================================
--- labs/jbossrules/branches/Branch_4_0_2_SOA_4_2/drools-core/src/main/java/org/drools/base/mvel/MVELSalienceExpression.java	2009-03-11 18:29:25 UTC (rev 25599)
+++ labs/jbossrules/branches/Branch_4_0_2_SOA_4_2/drools-core/src/main/java/org/drools/base/mvel/MVELSalienceExpression.java	2009-03-11 18:57:15 UTC (rev 25600)
@@ -15,23 +15,24 @@
     private static final long       serialVersionUID = 400L;
 
     private final Serializable      expr;
-    private final DroolsMVELFactory factory;
+    private final DroolsMVELFactory prototype;
 
     public MVELSalienceExpression(final Serializable expr,
                                   final DroolsMVELFactory factory) {
         this.expr = expr;
-        this.factory = factory;
+        this.prototype = factory;
     }
 
     public int getValue(final Tuple tuple,
                         final WorkingMemory workingMemory) {
-        this.factory.setContext( tuple,
+        DroolsMVELFactory factory = (DroolsMVELFactory) this.prototype.clone();
+        factory.setContext( tuple,
                                  null,
                                  null,
                                  workingMemory,
                                  null );
         return ((Number) MVEL.executeExpression( this.expr,
-                                                  this.factory )).intValue();
+                                                 factory )).intValue();
     }
 
 }




More information about the jboss-svn-commits mailing list