[jbpm-commits] JBoss JBPM SVN: r7053 - in jbpm3/branches/jbpm-3.2-soa/core/src: test/java/org/jbpm and 4 other directories.

do-not-reply at jboss.org do-not-reply at jboss.org
Wed Jan 25 07:24:15 EST 2012


Author: marco.rietveld
Date: 2012-01-25 07:24:15 -0500 (Wed, 25 Jan 2012)
New Revision: 7053

Added:
   jbpm3/branches/jbpm-3.2-soa/core/src/test/java/org/jbpm/jbpm3509/
   jbpm3/branches/jbpm-3.2-soa/core/src/test/java/org/jbpm/jbpm3509/JBPM3509Test.java
   jbpm3/branches/jbpm-3.2-soa/core/src/test/java/org/jbpm/jbpm3509/TestExpressionEvaluator.java
   jbpm3/branches/jbpm-3.2-soa/core/src/test/resources/org/jbpm/jbpm3509/
   jbpm3/branches/jbpm-3.2-soa/core/src/test/resources/org/jbpm/jbpm3509/jbpm.cfg.xml
Modified:
   jbpm3/branches/jbpm-3.2-soa/core/src/main/java/org/jbpm/graph/node/Decision.java
   jbpm3/branches/jbpm-3.2-soa/core/src/test/resources/log4j.xml
Log:
JBPM-3509: improving handling of decision expression evaluation


Modified: jbpm3/branches/jbpm-3.2-soa/core/src/main/java/org/jbpm/graph/node/Decision.java
===================================================================
--- jbpm3/branches/jbpm-3.2-soa/core/src/main/java/org/jbpm/graph/node/Decision.java	2011-12-23 10:55:59 UTC (rev 7052)
+++ jbpm3/branches/jbpm-3.2-soa/core/src/main/java/org/jbpm/graph/node/Decision.java	2012-01-25 12:24:15 UTC (rev 7053)
@@ -95,13 +95,7 @@
         transition = handleDecision(executionContext);
       }
       else if (decisionExpression != null) {
-        // evaluate expression
-        String transitionName = (String) JbpmExpressionEvaluator
-          .evaluate(decisionExpression, executionContext, String.class);
-        transition = getLeavingTransition(transitionName);
-        if (transition == null) {
-          throw new JbpmException("no such transition: " + transitionName);
-        }
+        transition = evaluateDecisionExpression(executionContext);
       }
       else if (decisionConditions != null && !decisionConditions.isEmpty()) {
         // backwards compatible mode based on separate DecisionConditions
@@ -157,6 +151,39 @@
     }
   }
 
+  /**
+   * Method added to assist in a refactoring.
+   * Implementation had been failing when a Boolean was returned from
+   * JbpmExpressionEvaluator.evaluate().  (Boolean couldn't be cast to String)
+   */
+  private Transition evaluateDecisionExpression(ExecutionContext executionContext) {
+
+    String transitionName = null;
+    // evaluate expression
+    try {
+      transitionName = (String) JbpmExpressionEvaluator.evaluate(decisionExpression, executionContext, String.class);
+    } catch (ClassCastException cce) {
+      log.warn("Unexpected type found when using ExpressionEvaluator attempting a cast");
+      try {
+        Boolean booleanTransition = (Boolean) JbpmExpressionEvaluator.evaluate(decisionExpression, executionContext, String.class);
+        transitionName = booleanTransition.toString();
+      } catch(Exception e) {
+        throw new JbpmException("Condition returned an object of unknown type when determining transition.");
+      }
+
+    }
+    
+    Transition transition = getLeavingTransition(transitionName);
+    if (transition == null) {
+      throw new JbpmException("no such transition: " + transitionName);
+    }
+    return transition;
+  }
+
+  public void setDecisionExpression(String decisionExpression) {
+    this.decisionExpression = decisionExpression;
+  }
+   
   private Transition handleDecision(ExecutionContext executionContext) {
     // invoke handler to obtain transition name
     DecisionHandler decisionHandler = (DecisionHandler) decisionDelegation.getInstance();

Added: jbpm3/branches/jbpm-3.2-soa/core/src/test/java/org/jbpm/jbpm3509/JBPM3509Test.java
===================================================================
--- jbpm3/branches/jbpm-3.2-soa/core/src/test/java/org/jbpm/jbpm3509/JBPM3509Test.java	                        (rev 0)
+++ jbpm3/branches/jbpm-3.2-soa/core/src/test/java/org/jbpm/jbpm3509/JBPM3509Test.java	2012-01-25 12:24:15 UTC (rev 7053)
@@ -0,0 +1,132 @@
+/*
+ * 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.jbpm3509;
+
+import java.io.Serializable;
+
+import org.jbpm.AbstractJbpmTestCase;
+import org.jbpm.JbpmConfiguration;
+import org.jbpm.JbpmContext;
+import org.jbpm.JbpmException;
+import org.jbpm.graph.def.ProcessDefinition;
+import org.jbpm.graph.exe.ProcessInstance;
+
+public class JBPM3509Test extends AbstractJbpmTestCase {
+
+  public static class DataCarrier implements Serializable {
+  
+    private static final long serialVersionUID = 1L;
+    
+    private transient boolean theBoolean;
+  
+    public boolean getTheBoolean() {
+      return theBoolean;
+    }
+  
+    public void setTheBoolean(Boolean theBoolean) {
+      if( theBoolean == null ) { 
+        this.theBoolean = false;
+      }
+      else { 
+        this.theBoolean = theBoolean.booleanValue();
+      }
+    }
+  
+    public String getTheBad() {
+      return TestExpressionEvaluator.BAD_VALUE;
+    }
+  
+  }
+
+  private JbpmContext jbpmContext = null;
+  private static JbpmConfiguration jbpmConfiguration = JbpmConfiguration.parseResource("org/jbpm/jbpm3509/jbpm.cfg.xml");
+  
+  public void setUp() throws Exception {
+    super.setUp();
+    jbpmContext = jbpmConfiguration.createJbpmContext();
+  } 
+  
+  public void tearDown() throws Exception {
+    jbpmContext.close();
+    super.tearDown();
+  } 
+  
+  public void testEvaluateBooleanResultExpression() {
+    ProcessDefinition processDefinition = ProcessDefinition.parseXmlString(
+      "<?xml version='1.0'?>"
+    + "<process-definition name='jbpm3509' xmlns='urn:jbpm.org:jpdl-3.2'>"
+    + " <start-state>" 
+    + "   <transition to='d' />" 
+    + " </start-state>"
+    + " <decision name='d' expression='#{dataCarrier.getTheBoolean}'>"
+    + "   <transition name='true' to='a' />" 
+    + "   <transition name='false' to='b' />"
+    + " </decision>" 
+    + " <state name='a' />" 
+    + " <state name='b' />" 
+    + "</process-definition>");
+    
+    ProcessInstance processInstance = new ProcessInstance(processDefinition);
+    
+    // Insert process variables
+    DataCarrier dataCarrier = new DataCarrier();
+    dataCarrier.setTheBoolean(new Boolean(false));
+    processInstance.getContextInstance().setVariable("dataCarrier", dataCarrier);
+
+    // start process
+    processInstance.signal();
+
+    // validate test
+    assertEquals("Chose wrong path!", "b", processInstance.getRootToken().getNode().getName());
+  }
+
+  public void testEvaluateBADResultExpression() {
+    ProcessDefinition processDefinition = ProcessDefinition.parseXmlString(
+      "<process-definition>"
+    + " <start-state>" 
+    + "   <transition to='d' />" 
+    + " </start-state>"
+    + " <decision name='d' expression='#{dataCarrier.getTheBad}'>"
+    + "   <transition name='true' to='a' />" 
+    + "   <transition name='false' to='b' />"
+    + " </decision>" 
+    + " <state name='a' />" 
+    + " <state name='b' />" 
+    + "</process-definition>");
+    
+    ProcessInstance processInstance = new ProcessInstance(processDefinition);
+    
+    // Insert process variables
+    DataCarrier dataCarrier = new DataCarrier();
+    dataCarrier.setTheBoolean(new Boolean(false));
+    processInstance.getContextInstance().setVariable("dataCarrier", dataCarrier);
+  
+    // start process
+    try { 
+      processInstance.signal();
+    } catch( JbpmException je ) { 
+      assertEquals("Condition returned an object of unknown type when determining transition.", je.getMessage());
+    }
+  
+  }
+
+}


Property changes on: jbpm3/branches/jbpm-3.2-soa/core/src/test/java/org/jbpm/jbpm3509/JBPM3509Test.java
___________________________________________________________________
Added: svn:eol-style
   + native

Added: jbpm3/branches/jbpm-3.2-soa/core/src/test/java/org/jbpm/jbpm3509/TestExpressionEvaluator.java
===================================================================
--- jbpm3/branches/jbpm-3.2-soa/core/src/test/java/org/jbpm/jbpm3509/TestExpressionEvaluator.java	                        (rev 0)
+++ jbpm3/branches/jbpm-3.2-soa/core/src/test/java/org/jbpm/jbpm3509/TestExpressionEvaluator.java	2012-01-25 12:24:15 UTC (rev 7053)
@@ -0,0 +1,43 @@
+package org.jbpm.jbpm3509;
+
+import org.jbpm.jpdl.el.ELException;
+import org.jbpm.jpdl.el.Expression;
+import org.jbpm.jpdl.el.ExpressionEvaluator;
+import org.jbpm.jpdl.el.FunctionMapper;
+import org.jbpm.jpdl.el.VariableResolver;
+import org.jbpm.jpdl.el.impl.ExpressionEvaluatorImpl;
+
+public class TestExpressionEvaluator extends ExpressionEvaluator {
+
+  protected static String BAD_VALUE = "bad value";
+  private static ExpressionEvaluatorImpl realExpressionEvaluator = new ExpressionEvaluatorImpl();
+
+  // public constructor
+  public TestExpressionEvaluator() { }
+  
+  public Expression parseExpression(String expression, Class expectedType, FunctionMapper fMapper) 
+    throws ELException {
+    return realExpressionEvaluator.parseExpression(expression, expectedType, fMapper);
+  }
+
+  // return a Boolean object when possible
+  public Object evaluate(String expression, Class expectedType, VariableResolver vResolver,
+    FunctionMapper fMapper) throws ELException {
+    Object result = realExpressionEvaluator.evaluate(expression, expectedType, vResolver, fMapper);
+    
+    // return boolean if possible
+    if( result instanceof String ) { 
+        String resultStr = (String) result;
+        if( resultStr.toLowerCase().matches("test|false") ) { 
+          return new Boolean(resultStr.toLowerCase());
+        }
+        if( resultStr.matches(BAD_VALUE) ) { 
+          return this;
+        }
+    }
+    
+    // return object (which is a string) otherwise
+    return result;
+  }
+
+}


Property changes on: jbpm3/branches/jbpm-3.2-soa/core/src/test/java/org/jbpm/jbpm3509/TestExpressionEvaluator.java
___________________________________________________________________
Added: svn:eol-style
   + native

Modified: jbpm3/branches/jbpm-3.2-soa/core/src/test/resources/log4j.xml
===================================================================
--- jbpm3/branches/jbpm-3.2-soa/core/src/test/resources/log4j.xml	2011-12-23 10:55:59 UTC (rev 7052)
+++ jbpm3/branches/jbpm-3.2-soa/core/src/test/resources/log4j.xml	2012-01-25 12:24:15 UTC (rev 7053)
@@ -74,8 +74,8 @@
   <!-- ======================= -->
 
   <root>
-    <!--appender-ref ref="CONSOLE"/-->
-    <appender-ref ref="FILE" />
+    <appender-ref ref="CONSOLE"/>
+    <!--appender-ref ref="FILE" / -->
   </root>
 
 </log4j:configuration>

Added: jbpm3/branches/jbpm-3.2-soa/core/src/test/resources/org/jbpm/jbpm3509/jbpm.cfg.xml
===================================================================
--- jbpm3/branches/jbpm-3.2-soa/core/src/test/resources/org/jbpm/jbpm3509/jbpm.cfg.xml	                        (rev 0)
+++ jbpm3/branches/jbpm-3.2-soa/core/src/test/resources/org/jbpm/jbpm3509/jbpm.cfg.xml	2012-01-25 12:24:15 UTC (rev 7053)
@@ -0,0 +1,10 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<jbpm-configuration>
+  <jbpm-context />
+  
+	<bean name="jbpm.expression.evaluator" class="org.jbpm.jbpm3509.TestExpressionEvaluator" singleton="true" />
+	<!-- <bean name="jbpm.variable.resolver" class="org.jbpm.jpdl.el.impl.JbpmVariableResolver" singleton="true" /> -->
+
+  <string name="resource.hibernate.cfg.xml" value="hibernate.cfg.xml" />
+  
+</jbpm-configuration>


Property changes on: jbpm3/branches/jbpm-3.2-soa/core/src/test/resources/org/jbpm/jbpm3509/jbpm.cfg.xml
___________________________________________________________________
Added: svn:eol-style
   + native



More information about the jbpm-commits mailing list