[jboss-svn-commits] JBL Code SVN: r13483 - in labs/jbossrules/trunk/drools-core/src: main/java/org/drools/reteoo and 1 other directories.

jboss-svn-commits at lists.jboss.org jboss-svn-commits at lists.jboss.org
Fri Jul 13 20:28:09 EDT 2007


Author: mark.proctor at jboss.com
Date: 2007-07-13 20:28:09 -0400 (Fri, 13 Jul 2007)
New Revision: 13483

Added:
   labs/jbossrules/trunk/drools-core/src/main/java/org/drools/base/mvel/LocalVariableResolver.java
Modified:
   labs/jbossrules/trunk/drools-core/src/main/java/org/drools/base/mvel/DroolsMVELFactory.java
   labs/jbossrules/trunk/drools-core/src/main/java/org/drools/reteoo/AccumulateNode.java
   labs/jbossrules/trunk/drools-core/src/test/java/org/drools/reteoo/AccumulateNodeTest.java
Log:
-MVEL now works ok with accumulate, implemented our own LocalVariableResolver which resolves from the DroolsMVELFactory.

Modified: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/base/mvel/DroolsMVELFactory.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/base/mvel/DroolsMVELFactory.java	2007-07-13 22:38:08 UTC (rev 13482)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/base/mvel/DroolsMVELFactory.java	2007-07-14 00:28:09 UTC (rev 13483)
@@ -11,7 +11,6 @@
 import org.mvel.CompileException;
 import org.mvel.integration.VariableResolver;
 import org.mvel.integration.impl.BaseVariableResolverFactory;
-import org.mvel.integration.impl.MapVariableResolver;
 
 public class DroolsMVELFactory extends BaseVariableResolverFactory
     implements
@@ -23,10 +22,6 @@
     /**
      * Holds the instance of the variables.
      */
-    //private Map           variables;
-    //    public DroolsMVELFactory(Map variables) {
-    //        this.variables = variables;
-    //    }
     private Tuple             tuple;
     private KnowledgeHelper   knowledgeHelper;
     private Object            object;
@@ -36,7 +31,7 @@
 
     private WorkingMemory     workingMemory;
 
-    private Map               variables;
+    private Map               localVariables;
 
     public DroolsMVELFactory(final Map previousDeclarations,
                              final Map localDeclarations,
@@ -63,7 +58,7 @@
         this.knowledgeHelper = knowledgeHelper;
         this.object = object;
         this.workingMemory = workingMemory;
-        this.variables = variables;
+        this.localVariables = variables;
     }
 
     public KnowledgeHelper getKnowledgeHelper() {
@@ -78,6 +73,16 @@
         return this.workingMemory.getGlobal( identifier );
     }
 
+    public Object getLocalValue(final String identifier) {
+        return this.localVariables.get( identifier );
+    }
+
+    public void setLocalValue(final String identifier,
+                              final Object value) {
+        this.localVariables.put( identifier,
+                                 value );
+    }
+
     public VariableResolver createVariable(String name,
                                            Object value) {
         VariableResolver vr = getVariableResolver( name );
@@ -85,12 +90,12 @@
             vr.setValue( value );
             return vr;
         } else {
-            if ( this.variables == null ) {
-                this.variables = new HashMap();
+            if ( this.localVariables == null ) {
+                this.localVariables = new HashMap();
             }
             addResolver( name,
-                         vr = new MapVariableResolver( this.variables,
-                                                       name ) );
+                         vr = new LocalVariableResolver( this,
+                                                         name ) );
             vr.setValue( value );
             return vr;
         }
@@ -103,13 +108,13 @@
         if ( vr != null && vr.getType() != null ) {
             throw new CompileException( "variable already defined within scope: " + vr.getType() + " " + name );
         } else {
-            if ( this.variables == null ) {
-                this.variables = new HashMap();
+            if ( this.localVariables == null ) {
+                this.localVariables = new HashMap();
             }
             addResolver( name,
-                         vr = new MapVariableResolver( this.variables,
-                                                       name,
-                                                       type ) );
+                         vr = new LocalVariableResolver( this,
+                                                         name,
+                                                         type ) );
             vr.setValue( value );
             return vr;
         }
@@ -141,8 +146,8 @@
             return true;
         } else if ( this.variableResolvers != null && this.variableResolvers.containsKey( name ) ) {
             addResolver( name,
-                         new MapVariableResolver( this.variableResolvers,
-                                                  name ) );
+                         new LocalVariableResolver( this,
+                                                    name ) );
             return true;
         } else if ( nextFactory != null ) {
             return nextFactory.isResolveable( name );

Added: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/base/mvel/LocalVariableResolver.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/base/mvel/LocalVariableResolver.java	                        (rev 0)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/base/mvel/LocalVariableResolver.java	2007-07-14 00:28:09 UTC (rev 13483)
@@ -0,0 +1,109 @@
+/**
+ * MVEL (The MVFLEX Expression Language)
+ *
+ * Copyright (C) 2007 Christopher Brock, MVFLEX/Valhalla Project and the Codehaus
+ *
+ * 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.
+ *
+ */
+package org.drools.base.mvel;
+
+import org.mvel.CompileException;
+import org.mvel.DataConversion;
+import org.mvel.integration.VariableResolver;
+
+import java.util.Map;
+
+public class LocalVariableResolver implements VariableResolver {
+    private String name;
+    private Class knownType;
+    private DroolsMVELFactory factory;
+
+    private boolean cache = false;
+
+    public LocalVariableResolver(DroolsMVELFactory factory, String name) {
+        this.factory = factory;
+        this.name = name;
+    }
+
+    public LocalVariableResolver(DroolsMVELFactory factory, String name, Class knownType) {
+        this.name = name;
+        this.knownType = knownType;
+        this.factory = factory;
+    }
+
+    public LocalVariableResolver(DroolsMVELFactory factory, String name, boolean cache) {
+        this.factory = factory;
+        this.name = name;
+        this.cache = cache;
+    }
+
+    public LocalVariableResolver(DroolsMVELFactory factory, String name, Class knownType, boolean cache) {
+        this.name = name;
+        this.knownType = knownType;
+        this.factory = factory;
+        this.cache = cache;
+    }
+
+
+    public void setName(String name) {
+        this.name = name;
+    }
+
+    public void setStaticType(Class knownType) {
+        this.knownType = knownType;
+    }
+
+    public String getName() {
+        return name;
+    }
+
+    public Class getType() {
+        return knownType;
+    }
+
+    public void setValue(Object value) {
+        if (knownType != null && value != null && value.getClass() != knownType) {
+            if (!DataConversion.canConvert(knownType, value.getClass())) {
+                throw new CompileException("cannot assign " + value.getClass().getName() + " to type: "
+                        + knownType.getName());
+            }
+            try {
+                value = DataConversion.convert(value, knownType);
+            }
+            catch (Exception e) {
+                throw new CompileException("cannot convert value of " + value.getClass().getName()
+                        + " to: " + knownType.getName());
+            }
+        }
+        
+        this.factory.setLocalValue( this.name, value );
+    }
+
+    public Object getValue() {
+        return this.factory.getLocalValue( this.name );
+    }
+
+    public int getFlags() {
+        return 0;
+    }
+
+
+    public boolean isCache() {
+        return cache;
+    }
+
+    public void setCache(boolean cache) {
+        this.cache = cache;
+    }
+}

Modified: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/reteoo/AccumulateNode.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/reteoo/AccumulateNode.java	2007-07-13 22:38:08 UTC (rev 13482)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/reteoo/AccumulateNode.java	2007-07-14 00:28:09 UTC (rev 13483)
@@ -455,7 +455,7 @@
         return memory;
     }
 
-    private static class AccumulateMemory {
+    public static class AccumulateMemory {
         private static final long serialVersionUID = -5487673715134696118L;
         
         public Object workingMemoryContext;

Modified: labs/jbossrules/trunk/drools-core/src/test/java/org/drools/reteoo/AccumulateNodeTest.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/test/java/org/drools/reteoo/AccumulateNodeTest.java	2007-07-13 22:38:08 UTC (rev 13482)
+++ labs/jbossrules/trunk/drools-core/src/test/java/org/drools/reteoo/AccumulateNodeTest.java	2007-07-14 00:28:09 UTC (rev 13483)
@@ -25,6 +25,7 @@
 import org.drools.common.InternalFactHandle;
 import org.drools.common.InternalWorkingMemory;
 import org.drools.common.PropagationContextImpl;
+import org.drools.reteoo.AccumulateNode.AccumulateMemory;
 import org.drools.rule.Accumulate;
 import org.drools.rule.Declaration;
 import org.drools.rule.Pattern;
@@ -86,7 +87,7 @@
 
         this.node.addTupleSink( this.sink );
 
-        this.memory = (BetaMemory) this.workingMemory.getNodeMemory( this.node );
+        this.memory = ((AccumulateMemory) this.workingMemory.getNodeMemory( this.node )).betaMemory;
 
         // check memories are empty
         assertEquals( 0,
@@ -372,7 +373,7 @@
                                                                   objectSource,
                                                                   this.accumulate );
 
-        final BetaMemory memory = (BetaMemory) workingMemory.getNodeMemory( accumulateNode );
+        final BetaMemory memory = ((AccumulateMemory) this.workingMemory.getNodeMemory( this.node )).betaMemory;
 
         assertNotNull( memory );
     }
@@ -389,7 +390,7 @@
         this.workingMemory = new ReteooWorkingMemory( 1,
                                                       (ReteooRuleBase) RuleBaseFactory.newRuleBase( conf ) );
         
-        this.memory = (BetaMemory) this.workingMemory.getNodeMemory( this.node );
+        this.memory = ((AccumulateMemory) this.workingMemory.getNodeMemory( this.node )).betaMemory;
 
         final DefaultFactHandle f0 = (DefaultFactHandle) this.workingMemory.getFactHandleFactory().newFactHandle( "cheese" );
         final DefaultFactHandle f1 = (DefaultFactHandle) this.workingMemory.getFactHandleFactory().newFactHandle( "other cheese" );




More information about the jboss-svn-commits mailing list