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

jboss-svn-commits at lists.jboss.org jboss-svn-commits at lists.jboss.org
Mon Dec 15 18:48:45 EST 2008


Author: KrisVerlaenen
Date: 2008-12-15 18:48:45 -0500 (Mon, 15 Dec 2008)
New Revision: 24387

Added:
   labs/jbossrules/trunk/drools-compiler/src/test/java/org/drools/integrationtests/ProcessMilestoneTest.java
   labs/jbossrules/trunk/drools-compiler/src/test/resources/org/drools/integrationtests/test_KnowledgeContextJava.drl
   labs/jbossrules/trunk/drools-compiler/src/test/resources/org/drools/integrationtests/test_KnowledgeContextMVEL.drl
Modified:
   labs/jbossrules/trunk/drools-compiler/src/main/java/org/drools/rule/builder/dialect/mvel/MVELActionBuilder.java
   labs/jbossrules/trunk/drools-compiler/src/main/java/org/drools/rule/builder/dialect/mvel/MVELDialect.java
   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-compiler/src/test/java/org/drools/integrationtests/ProcessActionTest.java
   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/DroolsMVELKnowledgeHelper.java
   labs/jbossrules/trunk/drools-core/src/main/java/org/drools/base/mvel/MVELAction.java
   labs/jbossrules/trunk/drools-core/src/main/java/org/drools/base/mvel/MVELCompilationUnit.java
   labs/jbossrules/trunk/drools-core/src/main/java/org/drools/common/InternalAgenda.java
   labs/jbossrules/trunk/drools-core/src/main/java/org/drools/spi/ProcessContext.java
   labs/jbossrules/trunk/drools-core/src/main/java/org/drools/workflow/instance/impl/RuleConstraintEvaluator.java
   labs/jbossrules/trunk/drools-core/src/main/java/org/drools/workflow/instance/node/MilestoneNodeInstance.java
   labs/jbossrules/trunk/drools-core/src/main/java/org/drools/workflow/instance/node/TimerNodeInstance.java
Log:
JBRULES-1895: Rule constraints can use processInstance variable to refer to current process instance
 - binding process instance to current process instance when evaluating constraint
JBRULES-1896: Support kcontext variable in rules and processes
 - adding support in core and compiler for kcontext variable

Modified: labs/jbossrules/trunk/drools-compiler/src/main/java/org/drools/rule/builder/dialect/mvel/MVELActionBuilder.java
===================================================================
--- labs/jbossrules/trunk/drools-compiler/src/main/java/org/drools/rule/builder/dialect/mvel/MVELActionBuilder.java	2008-12-15 23:00:29 UTC (rev 24386)
+++ labs/jbossrules/trunk/drools-compiler/src/main/java/org/drools/rule/builder/dialect/mvel/MVELActionBuilder.java	2008-12-15 23:48:45 UTC (rev 24387)
@@ -75,6 +75,7 @@
 
             Set<String> variables = new HashSet<String>();
             variables.add("context");
+            variables.add("kcontext");
             Dialect.AnalysisResult analysis = dialect.analyzeBlock( context,
                                                                     actionDescr,
                                                                     dialect.getInterceptors(),
@@ -84,6 +85,7 @@
             
             Map<String, Class> variableClasses = new HashMap<String, Class>();
             variableClasses.put("context", ProcessContext.class);
+            variableClasses.put("kcontext", org.drools.runtime.process.ProcessContext.class);
             MVELCompilationUnit unit = dialect.getMVELCompilationUnit( text,
                                                                        analysis,
                                                                        null,

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	2008-12-15 23:00:29 UTC (rev 24386)
+++ labs/jbossrules/trunk/drools-compiler/src/main/java/org/drools/rule/builder/dialect/mvel/MVELDialect.java	2008-12-15 23:48:45 UTC (rev 24387)
@@ -14,7 +14,6 @@
 import java.util.Map;
 import java.util.Set;
 import java.util.Map.Entry;
-import java.util.concurrent.atomic.AtomicInteger;
 
 import org.drools.base.ModifyInterceptor;
 import org.drools.base.TypeResolver;
@@ -69,6 +68,7 @@
 import org.drools.rule.builder.SalienceBuilder;
 import org.drools.rule.builder.dialect.java.JavaDialect;
 import org.drools.rule.builder.dialect.java.JavaFunctionBuilder;
+import org.drools.runtime.rule.RuleContext;
 import org.drools.spi.DeclarationScopeResolver;
 import org.drools.spi.KnowledgeHelper;
 import org.drools.util.StringUtils;
@@ -734,6 +734,10 @@
 
             parserContext.addInput( "drools",
                                     KnowledgeHelper.class );
+            if ( parserContext.getInputs().get( "kcontext" ) == null ) {
+            	parserContext.addInput( "kcontext",
+            							RuleContext.class );
+            }
         }
 
         return parserContext;

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	2008-12-15 23:00:29 UTC (rev 24386)
+++ labs/jbossrules/trunk/drools-compiler/src/main/resources/org/drools/rule/builder/dialect/java/javaRule.mvel	2008-12-15 23:48:45 UTC (rev 24387)
@@ -101,12 +101,14 @@
 
 @declare{"consequenceMethod"}
 public static void @{methodName}(org.drools.spi.KnowledgeHelper drools at if{declarations != empty}, at end{} @foreach{type : declarationTypes, declr : declarations} @{type} @{declr.identifier}, org.drools.FactHandle @{declr.identifier}__Handle__ @end{","}@if{globals != empty}, at end{} @foreach{type : globalTypes, identifier : globals}  @{type} @{identifier} @end{","} ) throws Exception {
+    org.drools.runtime.rule.RuleContext kcontext = drools;
     @{text}
 }
 @end{}
 
 @declare{"actionMethod"}
 public static void @{methodName}(org.drools.spi.KnowledgeHelper drools at if{globals != empty}, at end{} @foreach{type :globalTypes, identifier : globals}  @{type} @{identifier} @end{","}, org.drools.spi.ProcessContext context ) throws Exception {
+    org.drools.runtime.process.ProcessContext kcontext = context;
     @{text}
 }
 @end{}

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	2008-12-15 23:00:29 UTC (rev 24386)
+++ labs/jbossrules/trunk/drools-compiler/src/test/java/org/drools/integrationtests/MiscTest.java	2008-12-15 23:48:45 UTC (rev 24387)
@@ -53,6 +53,8 @@
 import org.drools.Guess;
 import org.drools.IndexedNumber;
 import org.drools.InsertedObject;
+import org.drools.KnowledgeBase;
+import org.drools.KnowledgeBaseFactory;
 import org.drools.Message;
 import org.drools.MockPersistentSet;
 import org.drools.ObjectWithSet;
@@ -82,6 +84,11 @@
 import org.drools.Cheesery.Maturity;
 import org.drools.audit.WorkingMemoryFileLogger;
 import org.drools.audit.WorkingMemoryInMemoryLogger;
+import org.drools.builder.KnowledgeBuilder;
+import org.drools.builder.KnowledgeBuilderError;
+import org.drools.builder.KnowledgeBuilderErrors;
+import org.drools.builder.KnowledgeBuilderFactory;
+import org.drools.builder.ResourceType;
 import org.drools.common.AbstractWorkingMemory;
 import org.drools.common.DefaultAgenda;
 import org.drools.common.InternalFactHandle;
@@ -107,16 +114,16 @@
 import org.drools.event.WorkingMemoryEventListener;
 import org.drools.facttemplates.Fact;
 import org.drools.facttemplates.FactTemplate;
+import org.drools.io.ResourceFactory;
 import org.drools.lang.DrlDumper;
 import org.drools.lang.descr.AttributeDescr;
 import org.drools.lang.descr.PackageDescr;
 import org.drools.lang.descr.RuleDescr;
-import org.drools.reteoo.ReteooRuleBase;
 import org.drools.rule.FactType;
 import org.drools.rule.InvalidRulePackage;
 import org.drools.rule.Package;
 import org.drools.rule.builder.dialect.java.JavaDialectConfiguration;
-import org.drools.spi.Activation;
+import org.drools.runtime.StatefulKnowledgeSession;
 import org.drools.spi.ConsequenceExceptionHandler;
 import org.drools.spi.GlobalResolver;
 import org.drools.xml.XmlDumper;
@@ -5739,6 +5746,47 @@
                       ((List) session.getGlobal( "results" )).size() );
 
     }
+    
+    public void testKnowledgeContextJava() {
+    	KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
+		kbuilder.add(ResourceFactory.newClassPathResource("test_KnowledgeContextJava.drl", getClass()), ResourceType.DRL);
+		KnowledgeBuilderErrors errors = kbuilder.getErrors();
+		if (errors.size() > 0) {
+			for (KnowledgeBuilderError error: errors) {
+				System.err.println(error);
+			}
+			throw new IllegalArgumentException("Could not parse knowledge.");
+		}
+		KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
+		kbase.addKnowledgePackages(kbuilder.getKnowledgePackages());
+		StatefulKnowledgeSession ksession = kbase.newStatefulKnowledgeSession();
+		List<String> list = new ArrayList<String>();
+		ksession.setGlobal("list", list);
+		ksession.insert(new Message());
+		ksession.fireAllRules();
+		assertEquals(1, list.size());
+		assertEquals("Hello World", list.get(0));
+    }
 
+    public void testKnowledgeContextMVEL() {
+    	KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
+		kbuilder.add(ResourceFactory.newClassPathResource("test_KnowledgeContextMVEL.drl", getClass()), ResourceType.DRL);
+		KnowledgeBuilderErrors errors = kbuilder.getErrors();
+		if (errors.size() > 0) {
+			for (KnowledgeBuilderError error: errors) {
+				System.err.println(error);
+			}
+			throw new IllegalArgumentException("Could not parse knowledge.");
+		}
+		KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
+		kbase.addKnowledgePackages(kbuilder.getKnowledgePackages());
+		StatefulKnowledgeSession ksession = kbase.newStatefulKnowledgeSession();
+		List<String> list = new ArrayList<String>();
+		ksession.setGlobal("list", list);
+		ksession.insert(new Message());
+		ksession.fireAllRules();
+		assertEquals(1, list.size());
+		assertEquals("Hello World", list.get(0));
+    }
 
 }

Modified: labs/jbossrules/trunk/drools-compiler/src/test/java/org/drools/integrationtests/ProcessActionTest.java
===================================================================
--- labs/jbossrules/trunk/drools-compiler/src/test/java/org/drools/integrationtests/ProcessActionTest.java	2008-12-15 23:00:29 UTC (rev 24386)
+++ labs/jbossrules/trunk/drools-compiler/src/test/java/org/drools/integrationtests/ProcessActionTest.java	2008-12-15 23:48:45 UTC (rev 24387)
@@ -128,6 +128,8 @@
 			"list.add(variable);\n" +
 			"String nodeName = context.getNodeInstance().getNodeName();\n" +
 			"list.add(nodeName);\n" +
+			"nodeName = kcontext.getNodeInstance().getNodeName();\n" +
+			"list.add(nodeName);\n" +
 			"insert( new Message() );\n" +
 			"</action>\n" +
 			"    </actionNode>\n" + 
@@ -156,9 +158,10 @@
         workingMemory.setGlobal("list", list);
         ProcessInstance processInstance = ( ProcessInstance )
             workingMemory.startProcess("org.drools.actions");
-        assertEquals(2, list.size());
+        assertEquals(3, list.size());
         assertEquals("SomeText", list.get(0));
         assertEquals("MyActionNode", list.get(1));
+        assertEquals("MyActionNode", list.get(2));
         Iterator<?> iterator = workingMemory.iterateObjects(new ObjectFilter() {
 			public boolean accept(Object object) {
 				return object instanceof Message;
@@ -197,14 +200,12 @@
 			"    <actionNode id=\"2\" name=\"MyActionNode\" >\n" +
 			"      <action type=\"expression\" dialect=\"mvel\" >System.out.println(\"Triggered\");\n" +
 			"System.out.println(drools.getWorkingMemory());\n" +
-			// TODO: Cannot put "String myVariable = ..." here because this generates a runtime exception
-			// stating that myVariable could not be resolved
-			"myVariable = (String) context.getVariable(\"variable\");\n" +
+			"String myVariable = (String) context.getVariable(\"variable\");\n" +
 			"list.add(myVariable);\n" +
-			// TODO: Cannot put "String nodeName = ..." here because this generates a runtime exception
-			// stating that nodeName could not be resolved
-			"nodeName = context.getNodeInstance().getNodeName();\n" +
+			"String nodeName = context.getNodeInstance().getNodeName();\n" +
 			"list.add(nodeName);\n" +
+			"nodeName = kcontext.getNodeInstance().getNodeName();\n" +
+			"list.add(nodeName);\n" +
 			"insert( new Message() );\n" +
 			"</action>\n" +
 			"    </actionNode>\n" + 
@@ -233,9 +234,10 @@
         workingMemory.setGlobal("list", list);
         ProcessInstance processInstance = ( ProcessInstance )
             workingMemory.startProcess("org.drools.actions");
-        assertEquals(2, list.size());
+        assertEquals(3, list.size());
         assertEquals("SomeText", list.get(0));
         assertEquals("MyActionNode", list.get(1));
+        assertEquals("MyActionNode", list.get(2));
         Iterator<?> iterator = workingMemory.iterateObjects(new ObjectFilter() {
 			public boolean accept(Object object) {
 				return object instanceof Message;

Added: labs/jbossrules/trunk/drools-compiler/src/test/java/org/drools/integrationtests/ProcessMilestoneTest.java
===================================================================
--- labs/jbossrules/trunk/drools-compiler/src/test/java/org/drools/integrationtests/ProcessMilestoneTest.java	                        (rev 0)
+++ labs/jbossrules/trunk/drools-compiler/src/test/java/org/drools/integrationtests/ProcessMilestoneTest.java	2008-12-15 23:48:45 UTC (rev 24387)
@@ -0,0 +1,148 @@
+package org.drools.integrationtests;
+
+import java.io.Reader;
+import java.io.StringReader;
+import java.util.HashMap;
+import java.util.Map;
+
+import junit.framework.TestCase;
+
+import org.drools.Person;
+import org.drools.RuleBase;
+import org.drools.RuleBaseFactory;
+import org.drools.WorkingMemory;
+import org.drools.compiler.DroolsError;
+import org.drools.compiler.PackageBuilder;
+import org.drools.process.core.context.variable.VariableScope;
+import org.drools.process.instance.ProcessInstance;
+import org.drools.process.instance.context.variable.VariableScopeInstance;
+import org.drools.rule.Package;
+import org.drools.ruleflow.instance.RuleFlowProcessInstance;
+
+public class ProcessMilestoneTest extends TestCase {
+    
+    public void testMilestone() {
+        PackageBuilder builder = new PackageBuilder();
+        Reader source = new StringReader(
+            "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
+            "<process xmlns=\"http://drools.org/drools-5.0/process\"\n" +
+            "         xmlns:xs=\"http://www.w3.org/2001/XMLSchema-instance\"\n" +
+            "         xs:schemaLocation=\"http://drools.org/drools-5.0/process drools-processes-5.0.xsd\"\n" +
+            "         type=\"RuleFlow\" name=\"flow\" id=\"org.drools.milestone\" package-name=\"org.drools\" version=\"1\" >\n" +
+            "\n" +
+            "  <header>\n" +
+            "    <imports>\n" +
+            "      <import name=\"org.drools.Person\" />\n" +
+            "    </imports>\n" +
+            "  </header>\n" +
+            "\n" +
+            "  <nodes>\n" +
+            "    <start id=\"1\" name=\"Start\" />\n" +
+            "    <milestone id=\"2\" name=\"Milestone\" >\n" +
+            "      <constraint type=\"rule\" dialect=\"mvel\" >Person( name == \"John Doe\" )</constraint>" +
+            "    </milestone>\n" +
+            "    <end id=\"3\" name=\"End\" />\n" +
+            "  </nodes>\n" +
+            "\n" +
+            "  <connections>\n" +
+            "    <connection from=\"1\" to=\"2\" />\n" +
+            "    <connection from=\"2\" to=\"3\" />\n" +
+            "  </connections>\n" +
+            "\n" +
+            "</process>");
+        builder.addRuleFlow(source);
+        Package pkg = builder.getPackage();
+        RuleBase ruleBase = RuleBaseFactory.newRuleBase();
+        ruleBase.addPackage( pkg );
+        WorkingMemory workingMemory = ruleBase.newStatefulSession();
+        
+        ProcessInstance processInstance = ( ProcessInstance )
+            workingMemory.startProcess("org.drools.milestone");
+        assertEquals(ProcessInstance.STATE_ACTIVE, processInstance.getState());
+        workingMemory.insert(new Person("Jane Doe", 20));
+        assertEquals(ProcessInstance.STATE_ACTIVE, processInstance.getState());
+        workingMemory.insert(new Person("John Doe", 50));
+        assertEquals(ProcessInstance.STATE_COMPLETED, processInstance.getState());
+    }
+    
+    public void testMilestoneWithProcessInstanceConstraint() {
+        PackageBuilder builder = new PackageBuilder();
+        Reader source = new StringReader(
+            "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
+            "<process xmlns=\"http://drools.org/drools-5.0/process\"\n" +
+            "         xmlns:xs=\"http://www.w3.org/2001/XMLSchema-instance\"\n" +
+            "         xs:schemaLocation=\"http://drools.org/drools-5.0/process drools-processes-5.0.xsd\"\n" +
+            "         type=\"RuleFlow\" name=\"flow\" id=\"org.drools.milestone\" package-name=\"org.drools\" version=\"1\" >\n" +
+            "\n" +
+            "  <header>\n" +
+            "    <imports>\n" +
+            "      <import name=\"org.drools.Person\" />\n" +
+            "      <import name=\"org.drools.integrationtests.ProcessMilestoneTest.ProcessUtils\" />\n" +
+            "    </imports>\n" +
+            "    <variables>\n" +
+            "      <variable name=\"name\" >\n" +
+            "        <type name=\"org.drools.process.core.datatype.impl.type.StringDataType\" />\n" +
+            "      </variable>\n" +
+            "    </variables>\n" +
+            "  </header>\n" +
+            "\n" +
+            "  <nodes>\n" +
+            "    <start id=\"1\" name=\"Start\" />\n" +
+            "    <milestone id=\"2\" name=\"Milestone\" >\n" +
+            "      <constraint type=\"rule\" dialect=\"mvel\" >processInstance: org.drools.ruleflow.instance.RuleFlowProcessInstance()\n" +
+            "Person( name == (ProcessUtils.getValue(processInstance, \"name\")) )</constraint>" +
+            "    </milestone>\n" +
+            "    <end id=\"3\" name=\"End\" />\n" +
+            "  </nodes>\n" +
+            "\n" +
+            "  <connections>\n" +
+            "    <connection from=\"1\" to=\"2\" />\n" +
+            "    <connection from=\"2\" to=\"3\" />\n" +
+            "  </connections>\n" +
+            "\n" +
+            "</process>");
+        builder.addRuleFlow(source);
+        Package pkg = builder.getPackage();
+        for (DroolsError error: builder.getErrors().getErrors()) {
+        	System.err.println(error);
+        }
+        RuleBase ruleBase = RuleBaseFactory.newRuleBase();
+        ruleBase.addPackage( pkg );
+        WorkingMemory workingMemory = ruleBase.newStatefulSession();
+        
+        Person john = new Person("John Doe", 20);
+        Person jane = new Person("Jane Doe", 20);
+        
+        Map<String, Object> params = new HashMap<String, Object>();
+        params.put("name", john.getName());
+        ProcessInstance processInstanceJohn = ( ProcessInstance )
+            workingMemory.startProcess("org.drools.milestone", params);
+        workingMemory.insert(processInstanceJohn);
+        assertEquals(ProcessInstance.STATE_ACTIVE, processInstanceJohn.getState());
+
+        params = new HashMap<String, Object>();
+        params.put("name", jane.getName());
+        ProcessInstance processInstanceJane = ( ProcessInstance )
+            workingMemory.startProcess("org.drools.milestone", params);
+        workingMemory.insert(processInstanceJane);
+        assertEquals(ProcessInstance.STATE_ACTIVE, processInstanceJane.getState());
+        
+        workingMemory.insert(jane);
+        assertEquals(ProcessInstance.STATE_ACTIVE, processInstanceJohn.getState());
+        assertEquals(ProcessInstance.STATE_COMPLETED, processInstanceJane.getState());
+        
+        workingMemory.insert(john);
+        assertEquals(ProcessInstance.STATE_COMPLETED, processInstanceJohn.getState());
+    }
+    
+    public static class ProcessUtils {
+    	
+    	public static Object getValue(RuleFlowProcessInstance processInstance, String name) {
+    		VariableScopeInstance scope = (VariableScopeInstance)
+    			processInstance.getContextInstance(VariableScope.VARIABLE_SCOPE);
+    		return scope.getVariable(name);
+    	}
+    	
+    }
+
+}

Added: labs/jbossrules/trunk/drools-compiler/src/test/resources/org/drools/integrationtests/test_KnowledgeContextJava.drl
===================================================================
--- labs/jbossrules/trunk/drools-compiler/src/test/resources/org/drools/integrationtests/test_KnowledgeContextJava.drl	                        (rev 0)
+++ labs/jbossrules/trunk/drools-compiler/src/test/resources/org/drools/integrationtests/test_KnowledgeContextJava.drl	2008-12-15 23:48:45 UTC (rev 24387)
@@ -0,0 +1,13 @@
+package HelloWorld
+ 
+import org.drools.Message
+import java.util.List
+
+global List list;
+
+rule "Hello World"
+	when
+		Message()
+	then
+	    list.add(kcontext.getRule().getName()); 
+end

Added: labs/jbossrules/trunk/drools-compiler/src/test/resources/org/drools/integrationtests/test_KnowledgeContextMVEL.drl
===================================================================
--- labs/jbossrules/trunk/drools-compiler/src/test/resources/org/drools/integrationtests/test_KnowledgeContextMVEL.drl	                        (rev 0)
+++ labs/jbossrules/trunk/drools-compiler/src/test/resources/org/drools/integrationtests/test_KnowledgeContextMVEL.drl	2008-12-15 23:48:45 UTC (rev 24387)
@@ -0,0 +1,15 @@
+package HelloWorld
+ 
+import org.drools.Message
+import java.util.List
+
+dialect "mvel"
+
+global List list;
+
+rule "Hello World"
+	when
+		Message()
+	then
+	    list.add(kcontext.getRule().getName()); 
+end

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	2008-12-15 23:00:29 UTC (rev 24386)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/base/mvel/DroolsMVELFactory.java	2008-12-15 23:48:45 UTC (rev 24387)
@@ -258,6 +258,11 @@
         else if (this.variableResolvers != null && this.variableResolvers.containsKey(name)) {
             return true;
         }
+        else if (DroolsMVELKnowledgeHelper.CONTEXT.equals(name)) {
+            addResolver(DroolsMVELKnowledgeHelper.CONTEXT,
+                    new DroolsMVELKnowledgeHelper(this));
+            return true;
+        }
         else if (this.previousDeclarations != null && this.previousDeclarations.containsKey(name)) {
             addResolver(name,
                     new DroolsMVELPreviousDeclarationVariable((Declaration) this.previousDeclarations.get(name),

Modified: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/base/mvel/DroolsMVELKnowledgeHelper.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/base/mvel/DroolsMVELKnowledgeHelper.java	2008-12-15 23:00:29 UTC (rev 24386)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/base/mvel/DroolsMVELKnowledgeHelper.java	2008-12-15 23:48:45 UTC (rev 24387)
@@ -17,6 +17,7 @@
     
     private DroolsMVELFactory factory;
     public static final String DROOLS = "drools";
+    public static final String CONTEXT = "kcontext";
 
     public DroolsMVELKnowledgeHelper() {
     }

Modified: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/base/mvel/MVELAction.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/base/mvel/MVELAction.java	2008-12-15 23:00:29 UTC (rev 24386)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/base/mvel/MVELAction.java	2008-12-15 23:48:45 UTC (rev 24387)
@@ -75,6 +75,7 @@
         DroolsMVELFactory factory = (DroolsMVELFactory) this.prototype.clone();
         
         factory.addResolver("context", new SimpleValueResolver(context));
+        factory.addResolver("kcontext", new SimpleValueResolver(context));
         if (variableNames != null) {
         	for (String variableName: variableNames) {
         		factory.addResolver(

Modified: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/base/mvel/MVELCompilationUnit.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/base/mvel/MVELCompilationUnit.java	2008-12-15 23:00:29 UTC (rev 24386)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/base/mvel/MVELCompilationUnit.java	2008-12-15 23:48:45 UTC (rev 24387)
@@ -13,11 +13,11 @@
 import java.util.HashSet;
 import java.util.Map;
 import java.util.Set;
-import java.util.concurrent.atomic.AtomicInteger;
 
 import org.drools.RuntimeDroolsException;
 import org.drools.base.ModifyInterceptor;
 import org.drools.rule.Declaration;
+import org.drools.runtime.rule.RuleContext;
 import org.drools.spi.KnowledgeHelper;
 import org.mvel2.DataConversion;
 import org.mvel2.ParserContext;
@@ -244,7 +244,14 @@
         parserContext.setStrictTypeEnforcement( strictMode );
 
         resolvedInputs = new HashMap<String, Class>( inputIdentifiers.length );
-        String lastIdentifier = null;
+
+        parserContext.addInput( "drools",
+                                KnowledgeHelper.class );
+
+		resolvedInputs.put( "drools",
+		                    KnowledgeHelper.class );
+		
+		String lastIdentifier = null;
         String lastType = null;
         try {
             for ( int i = 0, length = inputIdentifiers.length; i < length; i++ ) {
@@ -262,12 +269,16 @@
             throw new RuntimeDroolsException( "Unable to resolve class '" + lastType + "' for identifier '" + lastIdentifier );
         }
 
-        parserContext.addInput( "drools",
-                                KnowledgeHelper.class );
+        if ( parserContext.getInputs().get( "kcontext" ) == null)  {
 
-        resolvedInputs.put( "drools",
-                            KnowledgeHelper.class );
+        	parserContext.addInput( "kcontext",
+	                                RuleContext.class );
+	
+	        resolvedInputs.put( "kcontext",
+	                            RuleContext.class );
 
+        }
+        
         return compile( expression,
                         classLoader,
                         parserContext,

Modified: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/common/InternalAgenda.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/common/InternalAgenda.java	2008-12-15 23:00:29 UTC (rev 24386)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/common/InternalAgenda.java	2008-12-15 23:48:45 UTC (rev 24387)
@@ -56,7 +56,8 @@
      * 
      * @return 
      */
-	public boolean isRuleActiveInRuleFlowGroup(String ruleflowGroupName, String ruleName);
+	public Activation isRuleActiveInRuleFlowGroup(String ruleflowGroupName,
+		                                          String ruleName);
 
 	/**
 	 * Adds a RuleFlowGroupListerner to the named RuleFlowGroup

Modified: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/spi/ProcessContext.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/spi/ProcessContext.java	2008-12-15 23:00:29 UTC (rev 24386)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/spi/ProcessContext.java	2008-12-15 23:48:45 UTC (rev 24387)
@@ -1,11 +1,14 @@
 package org.drools.spi;
 
+import org.drools.impl.StatefulKnowledgeSessionImpl;
 import org.drools.process.core.context.variable.VariableScope;
 import org.drools.process.instance.ProcessInstance;
 import org.drools.process.instance.context.variable.VariableScopeInstance;
+import org.drools.reteoo.ReteooStatefulSession;
+import org.drools.runtime.KnowledgeRuntime;
 import org.drools.runtime.process.NodeInstance;
 
-public class ProcessContext {
+public class ProcessContext implements org.drools.runtime.process.ProcessContext {
     
 	private ProcessInstance processInstance;
     private NodeInstance nodeInstance;
@@ -65,5 +68,10 @@
     	}
     	variableScope.setVariable(variableName, value);
     }
+
+	public KnowledgeRuntime getKnowledgeRuntime() {
+		return new StatefulKnowledgeSessionImpl(
+			(ReteooStatefulSession) getProcessInstance().getWorkingMemory() );
+	}
     
 }

Modified: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/workflow/instance/impl/RuleConstraintEvaluator.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/workflow/instance/impl/RuleConstraintEvaluator.java	2008-12-15 23:00:29 UTC (rev 24386)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/workflow/instance/impl/RuleConstraintEvaluator.java	2008-12-15 23:48:45 UTC (rev 24387)
@@ -17,11 +17,17 @@
  */
 
 import java.io.Serializable;
+import java.util.Iterator;
+import java.util.Map;
 
 import org.drools.common.InternalAgenda;
+import org.drools.common.InternalFactHandle;
+import org.drools.common.InternalWorkingMemory;
 import org.drools.definition.process.Connection;
 import org.drools.process.instance.ProcessInstance;
+import org.drools.rule.Declaration;
 import org.drools.runtime.process.WorkflowProcessInstance;
+import org.drools.spi.Activation;
 import org.drools.workflow.core.Constraint;
 import org.drools.workflow.instance.node.SplitInstance;
 
@@ -92,9 +98,26 @@
         InternalAgenda agenda = (InternalAgenda) ((ProcessInstance) processInstance).getAgenda();
         String rule = "RuleFlow-Split-" + processInstance.getProcessId() + "-" + instance.getNode().getId() + "-" + connection.getTo().getId();
 
-        return agenda.isRuleActiveInRuleFlowGroup( "DROOLS_SYSTEM", rule );
+        Activation activation = agenda.isRuleActiveInRuleFlowGroup( "DROOLS_SYSTEM", rule );
+        return activation != null && checkProcessInstance(activation, instance.getProcessInstance());
     }
 
+    private boolean checkProcessInstance(Activation activation, ProcessInstance processInstance) {
+    	final Map<?, ?> declarations = activation.getSubRule().getOuterDeclarations();
+        for ( Iterator<?> it = declarations.values().iterator(); it.hasNext(); ) {
+            Declaration declaration = (Declaration) it.next();
+            if ("processInstance".equals(declaration.getIdentifier())) {
+            	Object value = declaration.getValue(
+        			(InternalWorkingMemory) processInstance.getWorkingMemory(),
+        			((InternalFactHandle) activation.getTuple().get(declaration)).getObject());
+            	if (value instanceof ProcessInstance) {
+            		return ((ProcessInstance) value).getId() == processInstance.getId();
+            	}
+        	}
+        }
+        return true;
+    }
+    
 	public Object getMetaData(String name) {
 		return null;
 	}

Modified: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/workflow/instance/node/MilestoneNodeInstance.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/workflow/instance/node/MilestoneNodeInstance.java	2008-12-15 23:00:29 UTC (rev 24386)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/workflow/instance/node/MilestoneNodeInstance.java	2008-12-15 23:48:45 UTC (rev 24387)
@@ -16,8 +16,13 @@
  * limitations under the License.
  */
 
+import java.util.Iterator;
+import java.util.Map;
+
 import org.drools.WorkingMemory;
 import org.drools.common.InternalAgenda;
+import org.drools.common.InternalFactHandle;
+import org.drools.common.InternalWorkingMemory;
 import org.drools.event.ActivationCancelledEvent;
 import org.drools.event.ActivationCreatedEvent;
 import org.drools.event.AfterActivationFiredEvent;
@@ -26,7 +31,9 @@
 import org.drools.event.AgendaGroupPushedEvent;
 import org.drools.event.BeforeActivationFiredEvent;
 import org.drools.process.instance.ProcessInstance;
+import org.drools.rule.Declaration;
 import org.drools.runtime.process.NodeInstance;
+import org.drools.spi.Activation;
 import org.drools.workflow.core.node.MilestoneNode;
 
 /**
@@ -49,16 +56,34 @@
                 "A MilestoneNode only accepts default incoming connections!");
         }
         String rule = "RuleFlow-Milestone-" + getProcessInstance().getProcessId()
-        + "-" + getNode().getId();
-
-        if( ((InternalAgenda) ((ProcessInstance) getProcessInstance())
-        		.getAgenda()).isRuleActiveInRuleFlowGroup( "DROOLS_SYSTEM", rule ) ) {
-            triggerCompleted();
+        	+ "-" + getNode().getId();
+        Activation activation = ((InternalAgenda) getProcessInstance().getAgenda())
+			.isRuleActiveInRuleFlowGroup("DROOLS_SYSTEM", rule);
+        if (activation != null) {
+        	if (checkProcessInstance(activation)) {
+        		triggerCompleted();
+        	}
         } else {
             addActivationListener();
         }
     }
     
+    private boolean checkProcessInstance(Activation activation) {
+    	final Map<?, ?> declarations = activation.getSubRule().getOuterDeclarations();
+        for ( Iterator<?> it = declarations.values().iterator(); it.hasNext(); ) {
+            Declaration declaration = (Declaration) it.next();
+            if ("processInstance".equals(declaration.getIdentifier())) {
+            	Object value = declaration.getValue(
+        			(InternalWorkingMemory) getProcessInstance().getWorkingMemory(),
+        			((InternalFactHandle) activation.getTuple().get(declaration)).getObject());
+            	if (value instanceof ProcessInstance) {
+            		return ((ProcessInstance) value).getId() == getProcessInstance().getId();
+            	}
+        	}
+        }
+        return true;
+    }
+    
     public void addEventListeners() {
         super.addEventListeners();
         addActivationListener();
@@ -87,7 +112,7 @@
             // trigger node instances of that milestone node
             String ruleName = event.getActivation().getRule().getName();
             String milestoneName = "RuleFlow-Milestone-" + getProcessInstance().getProcessId() + "-" + getNodeId();
-            if (milestoneName.equals(ruleName)) {
+            if (milestoneName.equals(ruleName) && checkProcessInstance(event.getActivation())) {
             	synchronized(getProcessInstance()) {
 	                removeEventListeners();
 	                triggerCompleted();

Modified: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/workflow/instance/node/TimerNodeInstance.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/workflow/instance/node/TimerNodeInstance.java	2008-12-15 23:00:29 UTC (rev 24386)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/workflow/instance/node/TimerNodeInstance.java	2008-12-15 23:48:45 UTC (rev 24387)
@@ -73,7 +73,7 @@
     public void addEventListeners() {
         super.addEventListeners();
         if (getTimerInstances() == null) {
-        	((ProcessInstance) getProcessInstance()).addEventListener("timerTriggered", this, false);
+        	addTimerListener();
         }
     }
     




More information about the jboss-svn-commits mailing list