### Eclipse Workspace Patch 1.0 #P jbpm4 Index: modules/bpmn/src/main/java/org/jbpm/bpmn/flownodes/BusinessRuleTaskBinding.java =================================================================== --- modules/bpmn/src/main/java/org/jbpm/bpmn/flownodes/BusinessRuleTaskBinding.java (revision 0) +++ modules/bpmn/src/main/java/org/jbpm/bpmn/flownodes/BusinessRuleTaskBinding.java (revision 0) @@ -0,0 +1,69 @@ +package org.jbpm.bpmn.flownodes; + +import java.util.List; + +import org.jbpm.bpmn.common.RulesFact; +import org.jbpm.bpmn.model.BpmnProcessDefinition; +import org.jbpm.bpmn.parser.BpmnParser; +import org.jbpm.pvm.internal.el.Expression; +import org.jbpm.pvm.internal.model.ScopeElementImpl; +import org.jbpm.pvm.internal.task.TaskDefinitionImpl; +import org.jbpm.pvm.internal.util.XmlUtil; +import org.jbpm.pvm.internal.xml.Parse; +import org.w3c.dom.Element; + + +public class BusinessRuleTaskBinding extends AbstractTaskBinding { + + public BusinessRuleTaskBinding() { + super("businessRuleTask"); + } + + public Object parse(Element element, Parse parse, BpmnParser bpmnParser) { + BusinessRuleTaskActivity taskActivity = new BusinessRuleTaskActivity(); + + ScopeElementImpl scopeElement = parse.contextStackFind(ScopeElementImpl.class); + TaskDefinitionImpl taskDefinition = bpmnParser.parseTaskDefinition(element, parse, scopeElement); + + taskActivity.setTaskDefinition(taskDefinition); + taskActivity.setDefault(getDefault()); + + Element ioSpec = XmlUtil.element(element, "ioSpecification"); + + if (ioSpec != null) { + + BpmnProcessDefinition bpmnProcessDefinition = parse.contextStackFind(BpmnProcessDefinition.class); + + List inputSet = XmlUtil.elements(ioSpec, "inputSet"); + + if (inputSet != null) { + + for (Element dataInputRef : XmlUtil.elements(inputSet.get(0), "dataInputRefs")) { + + RulesFact rulesFact = new RulesFact(); + + String idRef = dataInputRef.getTextContent(); + + Element itemDef = bpmnProcessDefinition.getItemDefinitions().get(idRef); + Element variable = XmlUtil.element(itemDef, "var"); + Element object = XmlUtil.element(itemDef, "object"); + + if (variable != null) { + String variableName = XmlUtil.attribute(variable, "name"); + rulesFact.setVariableName(variableName); + } else if (object != null) { + String expression = XmlUtil.attribute(object, "expr"); + rulesFact.setExpression(Expression.create(expression)); + } + + taskActivity.addRuleFact(rulesFact); + } + + + } + } + + return taskActivity; + } + +} Index: modules/bpmn/src/main/java/org/jbpm/bpmn/common/RulesFact.java =================================================================== --- modules/bpmn/src/main/java/org/jbpm/bpmn/common/RulesFact.java (revision 0) +++ modules/bpmn/src/main/java/org/jbpm/bpmn/common/RulesFact.java (revision 0) @@ -0,0 +1,74 @@ +/* + * JBoss, Home of Professional Open Source + * Copyright 2005, JBoss Inc., and individual contributors as indicated + * by the @authors tag. See the copyright.txt in the distribution for a + * full listing of individual contributors. + * + * This is free software; you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation; either version 2.1 of + * the License, or (at your option) any later version. + * + * This software is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this software; if not, write to the Free + * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA + * 02110-1301 USA, or see the FSF site: http://www.fsf.org. + */ +package org.jbpm.bpmn.common; + +import java.io.Serializable; + +import org.jbpm.api.activity.ActivityExecution; +import org.jbpm.pvm.internal.el.Expression; + +/** + * @author Tom Baeyens + */ +public class RulesFact implements Serializable { + + private static final long serialVersionUID = 1L; + + protected String variableName; + protected Expression expression; + protected String language; + + public String getVariableName() { + return variableName; + } + + public void setVariableName(String variableName) { + this.variableName = variableName; + } + + public Expression getExpression() { + return expression; + } + + public void setExpression(Expression expression) { + this.expression = expression; + } + + public String getLanguage() { + return language; + } + + public void setLanguage(String language) { + this.language = language; + } + + public Object getObject(ActivityExecution execution) { + if (variableName!=null) { + return execution.getVariable(variableName); + + } else if (expression!=null) { + return expression.evaluate(execution); + } + + return null; + } +} Index: modules/test-db/src/test/java/org/jbpm/bpmn/test/task/Room.java =================================================================== --- modules/test-db/src/test/java/org/jbpm/bpmn/test/task/Room.java (revision 0) +++ modules/test-db/src/test/java/org/jbpm/bpmn/test/task/Room.java (revision 0) @@ -0,0 +1,50 @@ +package org.jbpm.bpmn.test.task; + +import java.io.Serializable; + + +public class Room implements Serializable { + + private static final long serialVersionUID = 1L; + + int temperature = 21; + boolean smoke = false; + boolean isOnFire = false; + + public Room(int temperature, boolean smoke) { + this.temperature = temperature; + this.smoke = smoke; + } + + + public int getTemperature() { + return temperature; + } + + + public void setTemperature(int temperature) { + this.temperature = temperature; + } + + + public boolean isSmoke() { + return smoke; + } + + + public void setSmoke(boolean smoke) { + this.smoke = smoke; + } + + + public boolean isOnFire() { + return isOnFire; + } + + + public void setOnFire(boolean isOnFire) { + this.isOnFire = isOnFire; + } + + +} Index: modules/bpmn/src/main/java/org/jbpm/bpmn/flownodes/BusinessRuleTaskActivity.java =================================================================== --- modules/bpmn/src/main/java/org/jbpm/bpmn/flownodes/BusinessRuleTaskActivity.java (revision 0) +++ modules/bpmn/src/main/java/org/jbpm/bpmn/flownodes/BusinessRuleTaskActivity.java (revision 0) @@ -0,0 +1,61 @@ +package org.jbpm.bpmn.flownodes; + +import java.util.ArrayList; +import java.util.List; + +import org.drools.KnowledgeBase; +import org.drools.runtime.StatefulKnowledgeSession; +import org.jbpm.api.activity.ActivityExecution; +import org.jbpm.api.model.OpenExecution; +import org.jbpm.bpmn.common.ExecutionGlobals; +import org.jbpm.bpmn.common.RulesFact; +import org.jbpm.internal.log.Log; +import org.jbpm.pvm.internal.model.ExecutionImpl; +import org.jbpm.pvm.internal.repository.RulesDeployer; +import org.jbpm.pvm.internal.task.TaskDefinitionImpl; + + +public class BusinessRuleTaskActivity extends BpmnAutomaticActivity { + + private static final long serialVersionUID = 1L; + + private static final Log log = Log.getLog(BusinessRuleTaskActivity.class.getName()); + + protected TaskDefinitionImpl taskDefinition; + protected List facts = new ArrayList(); + + @Override + void perform(OpenExecution execution) { + + ExecutionImpl executionImpl = (ExecutionImpl)execution; + String deploymentId = executionImpl.getProcessDefinition().getDeploymentId(); + KnowledgeBase knowledgeBase = RulesDeployer.getKnowledgeBase(deploymentId); + + StatefulKnowledgeSession knowledgeSession = + knowledgeBase.newStatefulKnowledgeSession(); + + ExecutionGlobals executionGlobals = new ExecutionGlobals(execution); + knowledgeSession.getGlobals().setDelegate(executionGlobals); + + for (RulesFact rulesFact: facts) { + Object fact = rulesFact.getObject((ActivityExecution) execution); + knowledgeSession.insert(fact); + } + + int count = knowledgeSession.fireAllRules(); + log.debug("Number of fired rules is " + count); + + proceed((ExecutionImpl) execution, findOutgoingSequenceFlow((ExecutionImpl) execution, CONDITIONS_CHECKED)); + } + + public TaskDefinitionImpl getTaskDefinition() { + return taskDefinition; + } + public void setTaskDefinition(TaskDefinitionImpl taskDefinition) { + this.taskDefinition = taskDefinition; + } + + public void addRuleFact(RulesFact fact) { + this.facts.add(fact); + } +} Index: modules/test-db/src/test/resources/org/jbpm/bpmn/task/rule.drl =================================================================== --- modules/test-db/src/test/resources/org/jbpm/bpmn/task/rule.drl (revision 0) +++ modules/test-db/src/test/resources/org/jbpm/bpmn/task/rule.drl (revision 0) @@ -0,0 +1,6 @@ +rule "CheckRoomOnFire" + when + room : org.jbpm.bpmn.test.task.Room( temperature > 30, smoke == true ) + then + room.setOnFire( true ); +end \ No newline at end of file Index: modules/bpmn/src/main/java/org/jbpm/bpmn/common/ExecutionGlobals.java =================================================================== --- modules/bpmn/src/main/java/org/jbpm/bpmn/common/ExecutionGlobals.java (revision 0) +++ modules/bpmn/src/main/java/org/jbpm/bpmn/common/ExecutionGlobals.java (revision 0) @@ -0,0 +1,43 @@ +package org.jbpm.bpmn.common; + +import org.drools.runtime.Globals; +import org.jbpm.api.Execution; +import org.jbpm.internal.log.Log; +import org.jbpm.pvm.internal.model.ExecutionImpl; + +public class ExecutionGlobals implements Globals { + + private static final Log log = Log.getLog(ExecutionGlobals.class.getName()); + + ExecutionImpl execution; + Outcome outcome = new Outcome(); + + public ExecutionGlobals(Execution execution) { + this.execution = (ExecutionImpl) execution; + } + + public Object get(String variableName) { + if ("execution".equals(variableName)) { + log.info("returning execution"); + return execution; + } + if ("outcome".equals(variableName)) { + log.info("returning outcome"); + return outcome; + } + Object variableValue = execution.getVariable(variableName); + log.info("returning variable "+variableName+": "+variableValue); + return variableValue; + } + + public void set(String variableName, Object value) { + throw new UnsupportedOperationException(); + } + + public void setDelegate(Globals globals) { + throw new UnsupportedOperationException(); + } + public Outcome getOutcome() { + return outcome; + } +} Index: modules/bpmn/pom.xml =================================================================== --- modules/bpmn/pom.xml (revision 6518) +++ modules/bpmn/pom.xml (working copy) @@ -40,6 +40,11 @@ + org.drools + drools-core + provided + + junit junit Index: modules/test-db/src/test/resources/org/jbpm/bpmn/businessRuleTask.bpmn.xml =================================================================== --- modules/test-db/src/test/resources/org/jbpm/bpmn/businessRuleTask.bpmn.xml (revision 0) +++ modules/test-db/src/test/resources/org/jbpm/bpmn/businessRuleTask.bpmn.xml (revision 0) @@ -0,0 +1,34 @@ + + + + + + + + + + + + + + + itemDefinition1 + + + + + + + + + + + + + + \ No newline at end of file Index: modules/test-db/src/test/java/org/jbpm/bpmn/test/task/BusinessRuleTaskTest.java =================================================================== --- modules/test-db/src/test/java/org/jbpm/bpmn/test/task/BusinessRuleTaskTest.java (revision 0) +++ modules/test-db/src/test/java/org/jbpm/bpmn/test/task/BusinessRuleTaskTest.java (revision 0) @@ -0,0 +1,45 @@ +package org.jbpm.bpmn.test.task; + +import java.util.HashMap; +import java.util.Map; + +import org.jbpm.api.NewDeployment; +import org.jbpm.api.ProcessInstance; +import org.jbpm.test.JbpmTestCase; + + +public class BusinessRuleTaskTest extends JbpmTestCase { + + @Override + protected void setUp() throws Exception { + super.setUp(); + NewDeployment deployment = repositoryService.createDeployment(); + deployment.addResourceFromClasspath("org/jbpm/bpmn/businessRuleTask.bpmn.xml"); + deployment.addResourceFromClasspath("org/jbpm/bpmn/task/rule.drl"); + registerDeployment(deployment.deploy()); + } + + @Override + protected void tearDown() throws Exception { + super.tearDown(); + } + + public void testJavaServiceTaskCall() { + + Room room = new Room(350, true); + Map variables = new HashMap(); + variables.put("fact", room); + + assertFalse(room.isOnFire()); + ProcessInstance pi = executionService.startProcessInstanceByKey("businessRuleTaskProcess", variables); + + assertNotNull(pi.getId()); + + room = (Room) executionService.getVariable(pi.getId(), "fact"); + assertTrue(room.isOnFire()); + pi = executionService.signalExecutionById(pi.getId()); + + assertProcessInstanceEnded(pi); + } + +} Index: modules/bpmn/src/main/resources/jbpm.bpmn.flownodes.xml =================================================================== --- modules/bpmn/src/main/resources/jbpm.bpmn.flownodes.xml (revision 6285) +++ modules/bpmn/src/main/resources/jbpm.bpmn.flownodes.xml (working copy) @@ -18,10 +18,11 @@ - - + + + Index: modules/bpmn/src/main/java/org/jbpm/bpmn/common/Outcome.java =================================================================== --- modules/bpmn/src/main/java/org/jbpm/bpmn/common/Outcome.java (revision 0) +++ modules/bpmn/src/main/java/org/jbpm/bpmn/common/Outcome.java (revision 0) @@ -0,0 +1,24 @@ +package org.jbpm.bpmn.common; + +import org.jbpm.internal.log.Log; + +public class Outcome { + + private static final Log log = Log.getLog(Outcome.class.getName()); + + String outcome = null; + + public void set(String outcome) { + log.info("outcome is being set to "+outcome); + this.outcome = outcome; + } + + public boolean isDefined() { + return (outcome!=null); + } + + public String get() { + log.info("outcome "+outcome+" is being fetched"); + return outcome; + } +}