[jbpm-dev] [Design of JBoss jBPM] - Re: Expression Evaluation in ActionHandler Properties

brittm do-not-reply at jboss.com
Mon Sep 22 11:01:48 EDT 2008


(I've posted this against the JIRA issue, but I'm also posting it here for any who might be searching/following the thread.)

I have a fear that automatic expression evaluation for ActionHandler configuration could interfere with the ability to solve some interesting problems.  I'll provide an example below.

A recommendation:

Rather than hard coding the expression handling logic, provide an example of a base class that users can extend if they want to take advantage of expression handling/syntax in any of their Handlers.  With a base class, users can take advantage of expression handling when they want to, customize it if they need to, or intentionally ignore it.  Below is a class that I use consistently--if you like it, just re-brand it, re-write it or whatever you like.  

As an example of customizing the handling of expressions, this particular class provides a specialized 'concatenateEL' method--for instance, if you want to assign swimlanes to a particular Sales queue based on Market, in your AssignmentHandler configuration you could write <group>Sales-#{market}</group> which would evaluate to something like "Sales-London".  The stock JbpmExpressionEvaluator doesn't provide for this type of evaluation (and I really don't know if it should.)

No, this is not completely automatic for the user, they have to call one of the methods from their Handler, but it does allow them much more flexibility.

  | /*
  |  * GenericHandler.java
  |  * Created on April 1, 2008, 7:06 PM
  |  * Copyright 2008, Britt Miner.
  |  */
  | package org.bminer.jbpm.pd;
  | 
  | import java.util.regex.*;
  | import org.jbpm.graph.exe.ExecutionContext;
  | import org.jbpm.jpdl.el.impl.JbpmExpressionEvaluator;
  | 
  | /**
  |  * @author bminer
  |  */
  | public abstract class GenericHandler {
  | 
  |     // Prepare to identify any EL expressions, #{...}, regex: "#\{.*?}"
  |     String elPatternStr = "#\\{.*?}";
  |     Pattern elPattern = Pattern.compile(elPatternStr);
  |     
  |     protected String conditionExpression = null;
  | 
  |     /* Evaluate the input as a possible EL expression. */
  |     protected Object evaluateEL(String inputStr, ExecutionContext ec) {
  |         if (inputStr == null) {
  |             return null;
  |         }
  | 
  |         Matcher matcher = elPattern.matcher(inputStr);
  |         if (matcher.matches()) { //input is one big EL expression
  |             return JbpmExpressionEvaluator.evaluate(inputStr, ec);
  |         } else {
  |             return inputStr;
  |         }
  |     }
  | 
  |     /* Treats input as a possible series of EL expressions and concatenates what
  |      * is found.
  |      */
  |     protected String concatenateEL(String inputStr, ExecutionContext ec) {
  |         if (inputStr == null) {
  |             return null;
  |         }
  | 
  |         Matcher matcher = elPattern.matcher(inputStr);
  |         StringBuffer buf = new StringBuffer();
  |         while (matcher.find()) {
  |             // Get the match result
  |             String elExpr = matcher.group();
  |             // Evaluate EL expression
  |             Object o = JbpmExpressionEvaluator.evaluate(elExpr, ec);
  |             String elValue = "";
  |             if (o != null) {
  |                 elValue = String.valueOf(JbpmExpressionEvaluator.evaluate(elExpr, ec));
  |             }
  |             // Insert the calculated value in place of the EL expression
  |             matcher.appendReplacement(buf, elValue);
  |         }
  |         matcher.appendTail(buf);
  | 
  |         // Deliver result
  |         if (buf.length() > 0) {
  |             return buf.toString();
  |         } else {
  |             return null;
  |         }
  |     }
  | 
  |     /* Returns true if the value is a String which contains the pattern delineating
  |      * an EL expression.
  |      */
  |     protected boolean hasEL(Object value) {
  |         if (value instanceof String) {
  |             Matcher matcher = elPattern.matcher((String) value);
  |             return matcher.find();
  |         }
  |         return false;
  |     }
  |     
  |     /* Returns true if the value is a String which in its entirety composes  
  |      * one EL expression.
  |      */
  |     protected boolean isEL(Object value) {
  |         if (value instanceof String) {
  |             Matcher matcher = elPattern.matcher((String) value);
  |             return matcher.matches();
  |         }
  |         return false;
  |     }
  |     
  |     public void setConditionExpression(String condition) {
  |         this.conditionExpression = condition;
  |     }
  |     
  |     public void setCondition(String condition) {
  |         this.conditionExpression = condition;
  |     }
  | }

View the original post : http://www.jboss.com/index.html?module=bb&op=viewtopic&p=4178088#4178088

Reply to the post : http://www.jboss.com/index.html?module=bb&op=posting&mode=reply&p=4178088



More information about the jbpm-dev mailing list