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

jboss-svn-commits at lists.jboss.org jboss-svn-commits at lists.jboss.org
Thu May 24 12:43:33 EDT 2007


Author: mark.proctor at jboss.com
Date: 2007-05-24 12:43:32 -0400 (Thu, 24 May 2007)
New Revision: 12123

Added:
   labs/jbossrules/trunk/drools-compiler/src/main/java/org/drools/rule/builder/dialect/mvel/MVELExprAnalyzer.java
Modified:
   labs/jbossrules/trunk/drools-compiler/.classpath
   labs/jbossrules/trunk/drools-compiler/src/main/java/org/drools/rule/builder/dialect/java/JavaExprAnalyzer.java
   labs/jbossrules/trunk/drools-compiler/src/main/java/org/drools/rule/builder/dialect/mvel/MVELDialect.java
   labs/jbossrules/trunk/drools-compiler/src/main/java/org/drools/rule/builder/dialect/mvel/MVELEvalBuilder.java
   labs/jbossrules/trunk/drools-compiler/src/main/java/org/drools/rule/builder/dialect/mvel/MVELPredicateBuilder.java
   labs/jbossrules/trunk/drools-compiler/src/test/java/org/drools/integrationtests/MVELTest.java
Log:
JBRULES-708 MVEL Integration
JBRULES-713 Make Dialects Pluggeable

Modified: labs/jbossrules/trunk/drools-compiler/.classpath
===================================================================
--- labs/jbossrules/trunk/drools-compiler/.classpath	2007-05-24 16:39:05 UTC (rev 12122)
+++ labs/jbossrules/trunk/drools-compiler/.classpath	2007-05-24 16:43:32 UTC (rev 12123)
@@ -16,4 +16,4 @@
 	<classpathentry kind="var" path="M2_REPO/org/mvel/mvel14/1.2beta18/mvel14-1.2beta18.jar"/>
 	<classpathentry kind="var" path="M2_REPO/org/antlr/antlr/3.0/antlr-3.0.jar"/>
 	<classpathentry kind="output" path="target/classes"/>
-</classpath>
+</classpath>

Modified: labs/jbossrules/trunk/drools-compiler/src/main/java/org/drools/rule/builder/dialect/java/JavaExprAnalyzer.java
===================================================================
--- labs/jbossrules/trunk/drools-compiler/src/main/java/org/drools/rule/builder/dialect/java/JavaExprAnalyzer.java	2007-05-24 16:39:05 UTC (rev 12122)
+++ labs/jbossrules/trunk/drools-compiler/src/main/java/org/drools/rule/builder/dialect/java/JavaExprAnalyzer.java	2007-05-24 16:43:32 UTC (rev 12123)
@@ -122,20 +122,6 @@
         }
         used[used.length - 1] = new ArrayList( notBound );
 
-        return used;
-        //--------------------------------------------------------        
-        //        List decls = new ArrayList();
-        //        
-        //        for ( Iterator declIter = availDecls.iterator(); declIter.hasNext(); )
-        //        {
-        //            String eachDecl = (String) declIter.next();
-        //
-        //            if ( identifiers.contains( eachDecl ) )
-        //            {
-        //                decls.add( eachDecl );
-        //            }
-        //        }
-        //
-        //        return decls;        
+        return used;    
     }
 }
\ No newline at end of file

Modified: labs/jbossrules/trunk/drools-compiler/src/main/java/org/drools/rule/builder/dialect/mvel/MVELDialect.java
===================================================================
--- labs/jbossrules/trunk/drools-compiler/src/main/java/org/drools/rule/builder/dialect/mvel/MVELDialect.java	2007-05-24 16:39:05 UTC (rev 12122)
+++ labs/jbossrules/trunk/drools-compiler/src/main/java/org/drools/rule/builder/dialect/mvel/MVELDialect.java	2007-05-24 16:43:32 UTC (rev 12123)
@@ -5,10 +5,12 @@
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
+import java.util.Set;
 
 import org.drools.base.ClassFieldExtractorCache;
 import org.drools.base.TypeResolver;
 import org.drools.compiler.PackageBuilderConfiguration;
+import org.drools.compiler.RuleError;
 import org.drools.lang.descr.AccumulateDescr;
 import org.drools.lang.descr.AndDescr;
 import org.drools.lang.descr.BaseDescr;
@@ -68,6 +70,8 @@
     private final TypeResolver             typeResolver;
     private final ClassFieldExtractorCache classFieldExtractorCache;
 
+    private final MVELExprAnalyzer         analyzer;    
+    
     public void addFunction(FunctionDescr functionDescr,
                             TypeResolver typeResolver) {
         throw new UnsupportedOperationException( "MVEL does not support functions" );
@@ -88,6 +92,8 @@
         this.typeResolver = typeResolver;
         this.classFieldExtractorCache = classFieldExtractorCache;
 
+        this.analyzer = new MVELExprAnalyzer();
+        
         if ( pkg != null ) {
             init( pkg );
         }
@@ -143,11 +149,37 @@
 
     public void compileAll() {
     }
+    
+    public List[] getExpressionIdentifiers(RuleBuildContext context,
+                                           BaseDescr descr,
+                                           Object content) {
+        List[] usedIdentifiers = null;
+        try {
+            usedIdentifiers = this.analyzer.analyzeExpression( (String) content,
+                                                               new Set[]{context.getDeclarationResolver().getDeclarations().keySet(), context.getPkg().getGlobals().keySet()} );
+        } catch ( final Exception e ) {
+            context.getErrors().add( new RuleError( context.getRule(),
+                                                    descr,
+                                                    null,
+                                                    "Unable to determine the used declarations" ) );
+        }
+        return usedIdentifiers;        
+    }    
 
     public List[] getBlockIdentifiers(RuleBuildContext context,
                                       BaseDescr descr,
                                       String text) {
-        return null;
+        List[] usedIdentifiers = null;
+        try {
+            usedIdentifiers = this.analyzer.analyzeExpression( text,
+                                                               new Set[]{context.getDeclarationResolver().getDeclarations().keySet(), context.getPkg().getGlobals().keySet()} );
+        } catch ( final Exception e ) {
+            context.getErrors().add( new RuleError( context.getRule(),
+                                                    descr,
+                                                    null,
+                                                    "Unable to determine the used declarations" ) );
+        }
+        return usedIdentifiers;   
     }
 
     public Object getBuilder(final Class clazz) {
@@ -178,12 +210,6 @@
         return this.eval;
     }
 
-    public List[] getExpressionIdentifiers(RuleBuildContext context,
-                                           BaseDescr descr,
-                                           Object content) {
-        return null;
-    }
-
     public FromBuilder getFromBuilder() {
         return this.from;
     }

Modified: labs/jbossrules/trunk/drools-compiler/src/main/java/org/drools/rule/builder/dialect/mvel/MVELEvalBuilder.java
===================================================================
--- labs/jbossrules/trunk/drools-compiler/src/main/java/org/drools/rule/builder/dialect/mvel/MVELEvalBuilder.java	2007-05-24 16:39:05 UTC (rev 12122)
+++ labs/jbossrules/trunk/drools-compiler/src/main/java/org/drools/rule/builder/dialect/mvel/MVELEvalBuilder.java	2007-05-24 16:43:32 UTC (rev 12123)
@@ -17,6 +17,9 @@
 package org.drools.rule.builder.dialect.mvel;
 
 import java.io.Serializable;
+import java.util.Iterator;
+import java.util.List;
+
 import org.drools.base.mvel.DroolsMVELFactory;
 import org.drools.base.mvel.MVELEvalExpression;
 import org.drools.lang.descr.BaseDescr;
@@ -27,6 +30,7 @@
 import org.drools.rule.builder.RuleBuildContext;
 import org.drools.rule.builder.PatternBuilder;
 import org.drools.rule.builder.ConditionalElementBuilder;
+import org.mvel.ExpressionCompiler;
 import org.mvel.MVEL;
 
 /**
@@ -52,7 +56,7 @@
         // it must be an EvalDescr
         final EvalDescr evalDescr = (EvalDescr) descr;
 
-        final Declaration[] declarations = new Declaration[0];
+        //final Declaration[] declarations = new Declaration[0];
         //        final List[] usedIdentifiers = utils.getUsedIdentifiers( context,
         //                                                                 evalDescr,
         //                                                                 evalDescr.getText() );
@@ -64,8 +68,18 @@
 
         final DroolsMVELFactory factory = new DroolsMVELFactory(context.getDeclarationResolver().getDeclarations(), null,  context.getPkg().getGlobals() );
 
+        final List[] usedIdentifiers = context.getDialect().getExpressionIdentifiers( context,
+                                                                                      evalDescr,
+                                                                                      evalDescr.getContent() );
+
+        final Declaration[] declarations = new Declaration[usedIdentifiers[0].size()];
+        for ( int i = 0, size = usedIdentifiers[0].size(); i < size; i++ ) {
+            declarations[i] = context.getDeclarationResolver().getDeclaration( (String) usedIdentifiers[0].get( i ) );
+        }
+        
+        final EvalCondition eval = new EvalCondition( declarations );
         final Serializable expr = MVEL.compileExpression( (String) evalDescr.getContent() );
-        final EvalCondition eval = new EvalCondition( declarations );
+        
         eval.setEvalExpression( new MVELEvalExpression( expr,
                                                         factory ) );
 

Added: 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	                        (rev 0)
+++ labs/jbossrules/trunk/drools-compiler/src/main/java/org/drools/rule/builder/dialect/mvel/MVELExprAnalyzer.java	2007-05-24 16:43:32 UTC (rev 12123)
@@ -0,0 +1,104 @@
+package org.drools.rule.builder.dialect.mvel;
+
+/*
+ * Copyright 2005 JBoss Inc
+ * 
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ * 
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+import java.util.ArrayList;
+import java.util.HashSet;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Set;
+
+import org.antlr.runtime.ANTLRStringStream;
+import org.antlr.runtime.CharStream;
+import org.antlr.runtime.CommonTokenStream;
+import org.antlr.runtime.RecognitionException;
+import org.antlr.runtime.TokenStream;
+import org.drools.rule.builder.dialect.java.parser.JavaParserLexer;
+import org.drools.rule.builder.dialect.java.parser.JavaParserParser;
+import org.mvel.ExpressionCompiler;
+
+/**
+ * Expression analyzer.
+ * 
+ */
+public class MVELExprAnalyzer {
+    
+    public MVELExprAnalyzer() {
+        // intentionally left blank.
+    }
+
+    // ------------------------------------------------------------
+    // Instance methods
+    // ------------------------------------------------------------
+
+    /**
+     * Analyze an expression.
+     * 
+     * @param expr
+     *            The expression to analyze.
+     * @param availDecls
+     *            Total set of declarations available.
+     * 
+     * @return The <code>Set</code> of declarations used by the expression.
+     * @throws RecognitionException 
+     *             If an error occurs in the parser.
+     */
+    public List[] analyzeExpression(final String expr,
+                                    final Set[] availableIdentifiers) throws RecognitionException {
+        ExpressionCompiler compiler = new ExpressionCompiler( expr);
+        compiler.compile();        
+
+        return analyze( compiler.getInputs(),
+                        availableIdentifiers );
+    }
+
+    /**
+     * Analyze an expression.
+     * 
+     * @param availDecls
+     *            Total set of declarations available.
+     * @param ast
+     *            The AST for the expression.
+     * 
+     * @return The <code>Set</code> of declarations used by the expression.
+     * 
+     * @throws RecognitionException
+     *             If an error occurs in the parser.
+     */
+    private List[] analyze(final List identifiers,
+                           final Set[] availableIdentifiers) throws RecognitionException {
+        final Set notBound = new HashSet( identifiers );
+        final List[] used = new List[availableIdentifiers.length + 1];
+        for ( int i = 0, length = used.length; i < length - 1; i++ ) {
+            used[i] = new ArrayList();
+        }
+
+        for ( int i = 0, length = availableIdentifiers.length; i < length; i++ ) {
+            final Set set = availableIdentifiers[i];
+            for ( final Iterator it = set.iterator(); it.hasNext(); ) {
+                final String eachDecl = (String) it.next();
+                if ( identifiers.contains( eachDecl ) ) {
+                    used[i].add( eachDecl );
+                    notBound.remove( eachDecl );
+                }
+            }
+        }
+        used[used.length - 1] = new ArrayList( notBound );
+
+        return used;      
+    }
+}
\ No newline at end of file

Modified: labs/jbossrules/trunk/drools-compiler/src/main/java/org/drools/rule/builder/dialect/mvel/MVELPredicateBuilder.java
===================================================================
--- labs/jbossrules/trunk/drools-compiler/src/main/java/org/drools/rule/builder/dialect/mvel/MVELPredicateBuilder.java	2007-05-24 16:39:05 UTC (rev 12122)
+++ labs/jbossrules/trunk/drools-compiler/src/main/java/org/drools/rule/builder/dialect/mvel/MVELPredicateBuilder.java	2007-05-24 16:43:32 UTC (rev 12123)
@@ -60,17 +60,19 @@
         Map previousMap = new HashMap();
         for ( int i = 0, length = previousDeclarations.length; i < length; i++ ) {
             previousMap.put( previousDeclarations[i].getIdentifier(),
-                     previousDeclarations[i] );
+                             previousDeclarations[i] );
         }
 
         Map localMap = new HashMap();
         for ( int i = 0, length = localDeclarations.length; i < length; i++ ) {
             localMap.put( localDeclarations[i].getIdentifier(),
-                     localDeclarations[i] );
+                          localDeclarations[i] );
         }
-        
-        final DroolsMVELFactory factory = new DroolsMVELFactory(previousMap, localMap,  context.getPkg().getGlobals() );
 
+        final DroolsMVELFactory factory = new DroolsMVELFactory( previousMap,
+                                                                 localMap,
+                                                                 context.getPkg().getGlobals() );
+
         final Serializable expr = MVEL.compileExpression( (String) predicateDescr.getContent() );
         predicate.setPredicateExpression( new MVELPredicateExpression( expr,
                                                                        factory ) );

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-05-24 16:39:05 UTC (rev 12122)
+++ labs/jbossrules/trunk/drools-compiler/src/test/java/org/drools/integrationtests/MVELTest.java	2007-05-24 16:43:32 UTC (rev 12123)
@@ -3,7 +3,9 @@
 import java.io.IOException;
 import java.io.InputStreamReader;
 import java.io.Reader;
+import java.io.Serializable;
 import java.util.ArrayList;
+import java.util.HashMap;
 import java.util.List;
 
 import org.drools.Cheese;
@@ -16,13 +18,13 @@
 import org.drools.integrationtests.helloworld.Message;
 import org.drools.lang.descr.PackageDescr;
 import org.drools.rule.Package;
+import org.mvel.MVEL;
 
 import junit.framework.Assert;
 import junit.framework.TestCase;
 
 public class MVELTest extends TestCase {
-    public void testHelloWorld() throws Exception {
-
+    public void testHelloWorld() throws Exception {                 
         // read in the source
         final Reader reader = new InputStreamReader( getClass().getResourceAsStream( "test_mvel.drl" ) );
         final RuleBase ruleBase = loadRuleBase( reader );
@@ -37,6 +39,11 @@
         workingMemory.fireAllRules();
         assertEquals( new Integer(30), list.get(0));
     }
+    
+    public Object compiledExecute(String ex) {
+        Serializable compiled = MVEL.compileExpression(ex);
+        return MVEL.executeExpression(compiled, new Object(), new HashMap());
+    }    
 
     private RuleBase loadRuleBase(final Reader reader) throws IOException,
                                                       DroolsParserException,




More information about the jboss-svn-commits mailing list