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

jboss-svn-commits at lists.jboss.org jboss-svn-commits at lists.jboss.org
Mon Jun 4 23:27:02 EDT 2007


Author: mark.proctor at jboss.com
Date: 2007-06-04 23:27:02 -0400 (Mon, 04 Jun 2007)
New Revision: 12327

Modified:
   labs/jbossrules/trunk/drools-compiler/.classpath
   labs/jbossrules/trunk/drools-compiler/src/main/java/org/drools/rule/builder/dialect/mvel/MVELConsequenceBuilder.java
   labs/jbossrules/trunk/drools-compiler/src/main/java/org/drools/rule/builder/dialect/mvel/MVELExprAnalyzer.java
   labs/jbossrules/trunk/drools-compiler/src/test/java/org/drools/integrationtests/MVELTest.java
   labs/jbossrules/trunk/drools-compiler/src/test/resources/org/drools/integrationtests/test_mvel.drl
Log:
JBRULES-905 Add 'modify' support using 'with' to MVEL
-Updated WorkingMemory with modifyRetract and modifyAssert objects.

Modified: labs/jbossrules/trunk/drools-compiler/.classpath
===================================================================
--- labs/jbossrules/trunk/drools-compiler/.classpath	2007-06-05 03:26:14 UTC (rev 12326)
+++ labs/jbossrules/trunk/drools-compiler/.classpath	2007-06-05 03:27:02 UTC (rev 12327)
@@ -5,14 +5,16 @@
   <classpathentry kind="src" path="src/test/resources" output="target/test-classes" excluding="**/*.java"/>
   <classpathentry kind="output" path="target/classes"/>
   <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
-  <classpathentry kind="var" path="M2_REPO/org/mvel/mvel14/1.2beta22/mvel14-1.2beta22.jar"/>
+  <classpathentry kind="var" path="M2_REPO/xstream/xstream/1.1.3/xstream-1.1.3.jar"/>
+  <classpathentry kind="var" path="M2_REPO/antlr/antlr/2.7.7/antlr-2.7.7.jar"/>
+  <classpathentry kind="var" path="M2_REPO/org/antlr/stringtemplate/3.0/stringtemplate-3.0.jar"/>
   <classpathentry kind="var" path="M2_REPO/org/eclipse/jdt/core/3.2.1.v_677_R32x/core-3.2.1.v_677_R32x.jar"/>
-  <classpathentry kind="var" path="M2_REPO/xstream/xstream/1.1.3/xstream-1.1.3.jar" sourcepath="M2_REPO/xstream/xstream/1.1.3/xstream-1.1.3-sources.jar"/>
-  <classpathentry kind="var" path="M2_REPO/xpp3/xpp3/1.1.3.4.O/xpp3-1.1.3.4.O.jar" sourcepath="M2_REPO/xpp3/xpp3/1.1.3.4.O/xpp3-1.1.3.4.O-sources.jar"/>
+  <classpathentry kind="var" path="M2_REPO/xml-apis/xml-apis/1.0.b2/xml-apis-1.0.b2.jar"/>
+  <classpathentry kind="var" path="M2_REPO/janino/janino/2.5.7/janino-2.5.7.jar"/>
+  <classpathentry kind="var" path="M2_REPO/xerces/xercesImpl/2.4.0/xercesImpl-2.4.0.jar"/>
+  <classpathentry kind="var" path="M2_REPO/xpp3/xpp3/1.1.3.4.O/xpp3-1.1.3.4.O.jar"/>
   <classpathentry kind="src" path="/drools-core"/>
-  <classpathentry kind="var" path="M2_REPO/junit/junit/3.8.1/junit-3.8.1.jar" sourcepath="M2_REPO/junit/junit/3.8.1/junit-3.8.1-sources.jar"/>
+  <classpathentry kind="var" path="M2_REPO/junit/junit/3.8.1/junit-3.8.1.jar"/>
+  <classpathentry kind="var" path="M2_REPO/org/mvel/mvel14/1.2beta23/mvel14-1.2beta23.jar"/>
   <classpathentry kind="var" path="M2_REPO/org/antlr/antlr/3.0/antlr-3.0.jar"/>
-  <classpathentry kind="var" path="M2_REPO/janino/janino/2.5.7/janino-2.5.7.jar"/>
-  <classpathentry kind="var" path="M2_REPO/xerces/xercesImpl/2.4.0/xercesImpl-2.4.0.jar" sourcepath="M2_REPO/xerces/xercesImpl/2.4.0/xercesImpl-2.4.0-sources.jar"/>
-  <classpathentry kind="var" path="M2_REPO/xml-apis/xml-apis/1.0.b2/xml-apis-1.0.b2.jar" sourcepath="M2_REPO/xml-apis/xml-apis/1.0.b2/xml-apis-1.0.b2-sources.jar"/>
 </classpath>
\ No newline at end of file

Modified: labs/jbossrules/trunk/drools-compiler/src/main/java/org/drools/rule/builder/dialect/mvel/MVELConsequenceBuilder.java
===================================================================
--- labs/jbossrules/trunk/drools-compiler/src/main/java/org/drools/rule/builder/dialect/mvel/MVELConsequenceBuilder.java	2007-06-05 03:26:14 UTC (rev 12326)
+++ labs/jbossrules/trunk/drools-compiler/src/main/java/org/drools/rule/builder/dialect/mvel/MVELConsequenceBuilder.java	2007-06-05 03:27:02 UTC (rev 12327)
@@ -1,19 +1,57 @@
 package org.drools.rule.builder.dialect.mvel;
 
 import java.io.Serializable;
+import java.util.HashMap;
+import java.util.Map;
 
+import org.drools.FactHandle;
 import org.drools.base.mvel.DroolsMVELFactory;
+import org.drools.base.mvel.DroolsMVELPreviousDeclarationVariable;
 import org.drools.base.mvel.MVELConsequence;
 import org.drools.compiler.RuleError;
 import org.drools.lang.descr.RuleDescr;
 import org.drools.rule.builder.ConsequenceBuilder;
 import org.drools.rule.builder.RuleBuildContext;
+import org.mvel.ASTNode;
 import org.mvel.MVEL;
+import org.mvel.Macro;
+import org.mvel.MacroProcessor;
+import org.mvel.ast.WithNode;
+import org.mvel.integration.Interceptor;
+import org.mvel.integration.VariableResolverFactory;
 
 public class MVELConsequenceBuilder
     implements
     ConsequenceBuilder {
 
+    //private final Interceptor assertInterceptor;
+    //private final Interceptor modifyInterceptor;
+    private final Map         interceptors;
+    private final Map         macros;
+
+    public MVELConsequenceBuilder() {
+        this.interceptors = new HashMap(1);
+        this.interceptors.put( "Modify", new ModifyInterceptor() );
+//        this.assertInterceptor = new AssertInterceptor();
+//        this.modifyInterceptor = new ModifyInterceptor();
+        this.macros = new HashMap(1);
+//        macros.put( "assert",
+//                    new Macro() {
+//                        public String doMacro() {
+//                            return "@Modify with";
+//                        }
+//                    } );   
+        
+        macros.put( "modify",
+                    new Macro() {
+                        public String doMacro() {
+                            return "@Modify with";
+                        }
+                    } );
+        
+     
+    }
+
     public void build(final RuleBuildContext context) {
         // pushing consequence LHS into the stack for variable resolution
         context.getBuildStack().push( context.getRule().getLhs() );
@@ -24,8 +62,10 @@
                                                                      context.getPkg().getGlobals() );
             factory.setNextFactory( ((MVELDialect) context.getDialect()).getClassImportResolverFactory() );
 
-            final Serializable expr = MVEL.compileExpression( delimitExpressions( (String) context.getRuleDescr().getConsequence() ),
-                                                              ((MVELDialect) context.getDialect()).getClassImportResolverFactory().getImportedClasses() );
+            
+            
+            final Serializable expr = MVEL.compileExpression( new MacroProcessor(delimitExpressions( (String) context.getRuleDescr().getConsequence() )).parse(this.macros),
+                                                              ((MVELDialect) context.getDialect()).getClassImportResolverFactory().getImportedClasses(), this.interceptors );
 
             context.getRule().setConsequence( new MVELConsequence( expr,
                                                                    factory ) );
@@ -90,4 +130,41 @@
         return result.toString();
     }
 
+    public static class AssertInterceptor
+        implements
+        Interceptor {
+        public int doBefore(ASTNode node,
+                            VariableResolverFactory factory) {
+            return 0;
+        }
+
+        public int doAfter(Object value,
+                           ASTNode node,
+                           VariableResolverFactory factory) {
+            ((DroolsMVELFactory) factory).getWorkingMemory().assertObject( value );
+            return 0;
+        }
+    }
+
+    public static class ModifyInterceptor
+        implements
+        Interceptor {
+        public int doBefore(ASTNode node,
+                            VariableResolverFactory factory) {
+            Object object = ((WithNode) node).getContext().getValue( null,
+                                                                     factory );
+            FactHandle handle = ((DroolsMVELFactory) factory).getWorkingMemory().getFactHandle( object );
+            ((DroolsMVELFactory) factory).getWorkingMemory().modifyRetract( handle, null, null );
+            return 0;
+        }
+
+        public int doAfter(Object value,
+                           ASTNode node,
+                           VariableResolverFactory factory) {
+            FactHandle handle = ((DroolsMVELFactory) factory).getWorkingMemory().getFactHandle( value );            
+            ((DroolsMVELFactory) factory).getWorkingMemory().modifyAssert( handle, value, null, null );
+            return 0;
+        }
+    }
+
 }

Modified: labs/jbossrules/trunk/drools-compiler/src/main/java/org/drools/rule/builder/dialect/mvel/MVELExprAnalyzer.java
===================================================================
--- labs/jbossrules/trunk/drools-compiler/src/main/java/org/drools/rule/builder/dialect/mvel/MVELExprAnalyzer.java	2007-06-05 03:26:14 UTC (rev 12326)
+++ labs/jbossrules/trunk/drools-compiler/src/main/java/org/drools/rule/builder/dialect/mvel/MVELExprAnalyzer.java	2007-06-05 03:27:02 UTC (rev 12327)
@@ -79,7 +79,7 @@
      * @throws RecognitionException
      *             If an error occurs in the parser.
      */
-    private List[] analyze(final List identifiers,
+    private List[] analyze(final Set identifiers,
                            final Set[] availableIdentifiers) throws RecognitionException {
         final Set notBound = new HashSet( identifiers );
         final List[] used = new List[availableIdentifiers.length + 1];

Modified: labs/jbossrules/trunk/drools-compiler/src/test/java/org/drools/integrationtests/MVELTest.java
===================================================================
--- labs/jbossrules/trunk/drools-compiler/src/test/java/org/drools/integrationtests/MVELTest.java	2007-06-05 03:26:14 UTC (rev 12326)
+++ labs/jbossrules/trunk/drools-compiler/src/test/java/org/drools/integrationtests/MVELTest.java	2007-06-05 03:27:02 UTC (rev 12327)
@@ -37,7 +37,9 @@
 
         workingMemory.assertObject( new Cheese("stilton", 10) );
         workingMemory.fireAllRules();
+        assertEquals( 2, list.size() );
         assertEquals( new Integer(30), list.get(0));
+        assertEquals( new Integer(22), list.get(1));
         
     }
     

Modified: labs/jbossrules/trunk/drools-compiler/src/test/resources/org/drools/integrationtests/test_mvel.drl
===================================================================
--- labs/jbossrules/trunk/drools-compiler/src/test/resources/org/drools/integrationtests/test_mvel.drl	2007-06-05 03:26:14 UTC (rev 12326)
+++ labs/jbossrules/trunk/drools-compiler/src/test/resources/org/drools/integrationtests/test_mvel.drl	2007-06-05 03:27:02 UTC (rev 12327)
@@ -4,24 +4,27 @@
 
 global java.util.List list;
 
-rule "Throw Eval Exception"
+rule "mvel1"
 	dialect "mvel"
     when
         $c : Cheese(type == "stilton", price == (new Integer( 5 ) + 5 ), eval(price == (new Integer( 5 ) + 5 ) ) )
         eval( $c.price == 10 && list == empty)
     then
-        Cheese cheese = new Cheese("cheddar", 15);
+        Cheese cheese = new Cheese("cheddar", 35);
+
         a = new java.math.BigInteger( "10" );
         b = new java.math.BigInteger( "10" );
-        c = a + b; //look ma, optional ';' - ooh ahh....
+        c = a + b; 
         list.add( c + $c.price);
-
-//    	#we have optional ";"
-//        a = new java.math.BigInteger( "10" ) 
-//        b = new java.math.BigInteger( "10" )
-//
-//		#use ";" to seperate on the one line        
-//      c = a + b; list.add( c + $c.price)
         
+        modify ( $c ) { price = c }      
+end
 
+
+rule "mvel2"
+	dialect "mvel"
+    when
+        $c : Cheese(type == "stilton", price == 20 )
+    then
+        list.add( $c.price + 2);       
 end
\ No newline at end of file




More information about the jboss-svn-commits mailing list