[jboss-svn-commits] JBL Code SVN: r24472 - in labs/jbossrules/trunk: drools-core/src/main/java/org/drools/common and 2 other directories.
jboss-svn-commits at lists.jboss.org
jboss-svn-commits at lists.jboss.org
Mon Dec 22 21:07:11 EST 2008
Author: KrisVerlaenen
Date: 2008-12-22 21:07:11 -0500 (Mon, 22 Dec 2008)
New Revision: 24472
Added:
labs/jbossrules/trunk/drools-compiler/src/test/java/org/drools/integrationtests/ProcessSplitTest.java
Modified:
labs/jbossrules/trunk/drools-core/src/main/java/org/drools/common/DefaultAgenda.java
labs/jbossrules/trunk/drools-core/src/main/java/org/drools/common/InternalAgenda.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
Log:
JBRULES-1895: Rule constraints can use processInstance variable to refer to current process instance
- fixed issue with multiple activations
Added: labs/jbossrules/trunk/drools-compiler/src/test/java/org/drools/integrationtests/ProcessSplitTest.java
===================================================================
--- labs/jbossrules/trunk/drools-compiler/src/test/java/org/drools/integrationtests/ProcessSplitTest.java (rev 0)
+++ labs/jbossrules/trunk/drools-compiler/src/test/java/org/drools/integrationtests/ProcessSplitTest.java 2008-12-23 02:07:11 UTC (rev 24472)
@@ -0,0 +1,120 @@
+package org.drools.integrationtests;
+
+import java.io.Reader;
+import java.io.StringReader;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+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.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 ProcessSplitTest extends TestCase {
+
+ public void testSplitWithProcessInstanceConstraint() {
+ PackageBuilder builder = new PackageBuilder();
+ Reader source = new StringReader(
+ "<process xmlns=\"http://drools.org/drools-5.0/process\"" +
+ " xmlns:xs=\"http://www.w3.org/2001/XMLSchema-instance\"" +
+ " xs:schemaLocation=\"http://drools.org/drools-5.0/process drools-processes-5.0.xsd\"" +
+ " type=\"RuleFlow\" name=\"ruleflow\" id=\"org.drools.process-split\" package-name=\"org.drools\" >" +
+ "" +
+ " <header>" +
+ " <imports>" +
+ " <import name=\"org.drools.Person\" />" +
+ " <import name=\"org.drools.integrationtests.ProcessSplitTest.ProcessUtils\" />" +
+ " </imports>" +
+ " <globals>" +
+ " <global identifier=\"list\" type=\"java.util.List\" />" +
+ " </globals>" +
+ " <variables>\n" +
+ " <variable name=\"name\" >\n" +
+ " <type name=\"org.drools.process.core.datatype.impl.type.StringDataType\" />\n" +
+ " </variable>\n" +
+ " </variables>\n" +
+ " </header>" +
+ "" +
+ " <nodes>" +
+ " <actionNode id=\"2\" name=\"Action\" >" +
+ " <action type=\"expression\" dialect=\"mvel\" >insert(context.getProcessInstance());</action>" +
+ " </actionNode>" +
+ " <split id=\"4\" name=\"Split\" type=\"2\" >" +
+ " <constraints>" +
+ " <constraint toNodeId=\"8\" toType=\"DROOLS_DEFAULT\" priority=\"2\" type=\"rule\" dialect=\"mvel\" >eval(true)</constraint>" +
+ " <constraint toNodeId=\"6\" toType=\"DROOLS_DEFAULT\" name=\"constraint\" priority=\"1\" type=\"rule\" dialect=\"mvel\" >processInstance: org.drools.ruleflow.instance.RuleFlowProcessInstance()" +
+ "Person( name == (ProcessUtils.getValue(processInstance, \"name\")) )</constraint>" +
+ " </constraints>" +
+ " </split>" +
+ " <end id=\"8\" name=\"End\" />" +
+ " <actionNode id=\"6\" name=\"Action\" >" +
+ " <action type=\"expression\" dialect=\"mvel\" >list.add(context.getProcessInstance().getId());</action>" +
+ " </actionNode>" +
+ " <start id=\"1\" name=\"Start\" />" +
+ " <end id=\"3\" name=\"End\" />" +
+ " </nodes>" +
+ " <connections>" +
+ " <connection from=\"1\" to=\"2\" />" +
+ " <connection from=\"2\" to=\"4\" />" +
+ " <connection from=\"4\" to=\"8\" />" +
+ " <connection from=\"4\" to=\"6\" />" +
+ " <connection from=\"6\" to=\"3\" />" +
+ " </connections>" +
+ "" +
+ "</process>");
+ builder.addRuleFlow(source);
+ Package pkg = builder.getPackage();
+ RuleBase ruleBase = RuleBaseFactory.newRuleBase();
+ ruleBase.addPackage( pkg );
+ WorkingMemory workingMemory = ruleBase.newStatefulSession();
+ List<Long> list = new ArrayList<Long>();
+ workingMemory.setGlobal("list", list);
+
+ Person john = new Person("John Doe", 20);
+ Person jane = new Person("Jane Doe", 20);
+ Person julie = new Person("Julie Doe", 20);
+ workingMemory.insert(john);
+ workingMemory.insert(jane);
+
+ Map<String, Object> params = new HashMap<String, Object>();
+ params.put("name", john.getName());
+ ProcessInstance processInstance1 = ( ProcessInstance )
+ workingMemory.startProcess("org.drools.process-split", params);
+
+ params = new HashMap<String, Object>();
+ params.put("name", jane.getName());
+ ProcessInstance processInstance2 = ( ProcessInstance )
+ workingMemory.startProcess("org.drools.process-split", params);
+
+ params = new HashMap<String, Object>();
+ params.put("name", julie.getName());
+ ProcessInstance processInstance3 = ( ProcessInstance )
+ workingMemory.startProcess("org.drools.process-split", params);
+
+ assertEquals(ProcessInstance.STATE_COMPLETED, processInstance1.getState());
+ assertEquals(ProcessInstance.STATE_COMPLETED, processInstance2.getState());
+ assertEquals(ProcessInstance.STATE_COMPLETED, processInstance3.getState());
+ assertEquals(2, list.size());
+ }
+
+ public static class ProcessUtils {
+
+ public static Object getValue(RuleFlowProcessInstance processInstance, String name) {
+ VariableScopeInstance scope = (VariableScopeInstance)
+ processInstance.getContextInstance(VariableScope.VARIABLE_SCOPE);
+ return scope.getVariable(name);
+ }
+
+ }
+
+}
Modified: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/common/DefaultAgenda.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/common/DefaultAgenda.java 2008-12-23 01:29:50 UTC (rev 24471)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/common/DefaultAgenda.java 2008-12-23 02:07:11 UTC (rev 24472)
@@ -34,8 +34,10 @@
import org.drools.base.SequentialKnowledgeHelper;
import org.drools.common.RuleFlowGroupImpl.DeactivateCallback;
import org.drools.impl.StatefulKnowledgeSessionImpl;
+import org.drools.process.instance.ProcessInstance;
import org.drools.reteoo.LeftTuple;
import org.drools.reteoo.ReteooWorkingMemory;
+import org.drools.rule.Declaration;
import org.drools.spi.Activation;
import org.drools.spi.ActivationGroup;
import org.drools.spi.AgendaFilter;
@@ -974,20 +976,39 @@
/**
* @inheritDoc
*/
- public Activation isRuleActiveInRuleFlowGroup(String ruleflowGroupName,
- String ruleName) {
+ public boolean isRuleActiveInRuleFlowGroup(String ruleflowGroupName,
+ String ruleName,
+ long processInstanceId) {
RuleFlowGroup systemRuleFlowGroup = this.getRuleFlowGroup( ruleflowGroupName );
for ( Iterator<RuleFlowGroupNode> activations = systemRuleFlowGroup.iterator(); activations.hasNext(); ) {
Activation activation = activations.next().getActivation();
if ( ruleName.equals( activation.getRule().getName() ) ) {
- return activation;
+ if (checkProcessInstance(activation, processInstanceId)) {
+ return true;
+ }
}
}
- return null;
+ return false;
}
+ private boolean checkProcessInstance(Activation activation, long processInstanceId) {
+ 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(
+ workingMemory,
+ ((InternalFactHandle) activation.getTuple().get(declaration)).getObject());
+ if (value instanceof ProcessInstance) {
+ return ((ProcessInstance) value).getId() == processInstanceId;
+ }
+ }
+ }
+ return true;
+ }
+
public void addRuleFlowGroupListener(String ruleFlowGroup,
RuleFlowGroupListener listener) {
InternalRuleFlowGroup rfg = (InternalRuleFlowGroup) this.getRuleFlowGroup( ruleFlowGroup );
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-23 01:29:50 UTC (rev 24471)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/common/InternalAgenda.java 2008-12-23 02:07:11 UTC (rev 24472)
@@ -56,8 +56,9 @@
*
* @return
*/
- public Activation isRuleActiveInRuleFlowGroup(String ruleflowGroupName,
- String ruleName);
+ public boolean isRuleActiveInRuleFlowGroup(String ruleflowGroupName,
+ String ruleName,
+ long processInstanceId);
/**
* Adds a RuleFlowGroupListerner to the named RuleFlowGroup
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-23 01:29:50 UTC (rev 24471)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/workflow/instance/impl/RuleConstraintEvaluator.java 2008-12-23 02:07:11 UTC (rev 24472)
@@ -17,17 +17,11 @@
*/
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;
@@ -98,26 +92,10 @@
InternalAgenda agenda = (InternalAgenda) ((ProcessInstance) processInstance).getAgenda();
String rule = "RuleFlow-Split-" + processInstance.getProcessId() + "-" + instance.getNode().getId() + "-" + connection.getTo().getId();
- Activation activation = agenda.isRuleActiveInRuleFlowGroup( "DROOLS_SYSTEM", rule );
- return activation != null && checkProcessInstance(activation, instance.getProcessInstance());
+ boolean isActive = agenda.isRuleActiveInRuleFlowGroup( "DROOLS_SYSTEM", rule, processInstance.getId() );
+ return isActive;
}
- 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-23 01:29:50 UTC (rev 24471)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/workflow/instance/node/MilestoneNodeInstance.java 2008-12-23 02:07:11 UTC (rev 24472)
@@ -57,12 +57,10 @@
}
String rule = "RuleFlow-Milestone-" + getProcessInstance().getProcessId()
+ "-" + getNode().getId();
- Activation activation = ((InternalAgenda) getProcessInstance().getAgenda())
- .isRuleActiveInRuleFlowGroup("DROOLS_SYSTEM", rule);
- if (activation != null) {
- if (checkProcessInstance(activation)) {
- triggerCompleted();
- }
+ boolean isActive = ((InternalAgenda) getProcessInstance().getAgenda())
+ .isRuleActiveInRuleFlowGroup("DROOLS_SYSTEM", rule, getProcessInstance().getId());
+ if (isActive) {
+ triggerCompleted();
} else {
addActivationListener();
}
More information about the jboss-svn-commits
mailing list