All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.ow2.bonita.definition.activity.ConditionDefinition Maven / Gradle / Ivy

/**
 * Copyright (C) 2007  Bull S. A. S.
 * Bull, Rue Jean Jaures, B.P.68, 78340, Les Clayes-sous-Bois
 * This library 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
 * version 2.1 of the License.
 * This library 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
 * program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
 * Floor, Boston, MA  02110-1301, USA.
 **/
package org.ow2.bonita.definition.activity;

import bsh.EvalError;
import bsh.Interpreter;

import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;

import org.ow2.bonita.pvm.listener.EventListenerExecution;
import org.ow2.bonita.pvm.model.Condition;
import org.ow2.bonita.facade.exception.BonitaWrapperException;
import org.ow2.bonita.facade.exception.ExpressionEvaluationException;
import org.ow2.bonita.facade.runtime.var.Enumeration;
import org.ow2.bonita.util.BonitaRuntimeException;
import org.ow2.bonita.util.Misc;

/**
 * @author Marc Blachon, Guillaume Porcher, Charles Souillard, Miguel Valdes,
 *         Pierre Vigneras
 */
public class ConditionDefinition implements Condition {

  private static final long serialVersionUID = 1L;
  private static final Logger LOG = Logger.getLogger(ConditionDefinition.class.getName());
  /**
   * contains the condition set onto the transition; for instance:
   * "!str1.equals("toto")" where str1 is the name of a runtime dataField
   */
  protected String value;
  protected String scriptType;

  protected ConditionDefinition() {
  }

  public ConditionDefinition(final String value, final String scriptType) {
    this.value = value;
    this.scriptType = scriptType;
  }

  public boolean evaluate(final EventListenerExecution xpdlExecution) {
    if ("bsh".equals(this.scriptType)) {
      return this.evaluateBsh(xpdlExecution);
    }
    throw new BonitaRuntimeException("Unsupported scriptType: " + this.scriptType);
  }

  private boolean evaluateBsh(final EventListenerExecution xpdlExecution) {
    final Set nullVariables = new HashSet();
    try {
      boolean bool = false;
      final Interpreter i = new Interpreter();
      // to set all process variables to the interpreter
      final Set setVar = xpdlExecution.getVariableKeys();
      final Iterator it = setVar.iterator();

      if (LOG.isLoggable(Level.FINEST)) {
        LOG.finest("Adding variables to expressionEvaluator : " + setVar);
      }
      
      while (it.hasNext()) {
        final String key = it.next();
        final Object var = xpdlExecution.getVariable(key);
        if (var != null)  {
          if (LOG.isLoggable(Level.FINEST)) {
            LOG.finest("Addign variable : " + key + " of type : " + var.getClass() + " in expression evaluator");
          }
        }
        if (var == null) {
          nullVariables.add(key);
          i.set(key, null);
          if (LOG.isLoggable(Level.FINEST)) {
            LOG.finest("Variable " + var + " added to expression evaluator is null");
          }
        } else if (var instanceof Enumeration) {
          final Enumeration enu = (Enumeration) var;
          i.set(key, enu.getSelectedValue());
        } else  {
          i.set(key, var);
        }
      }
      // set result with the value evaluation
      i.eval("result = " + this.value);
      if (i.get("result") != null) {
        bool = ((Boolean) i.get("result")).booleanValue();
        return bool;
      }
      throw new BonitaWrapperException(
        new ExpressionEvaluationException("Evaluation of the condition by the interpreter is null!"));
    } catch (final EvalError e) {
      String end = "";
      if (!nullVariables.isEmpty()) {
        end += "The following variables were null when evaluating the condition, " 
          + "maybe one of those is used in the expression: ";
        for (final String nullVariable : nullVariables) {
          end += "\n- " + nullVariable;
        }
      }
      // bsh.CallStack is not Serializable
      final String stacktrace = Misc.getStackTraceFrom(e);
      final String errorMsg = "Error during evaluation of condition: " + this.value + ". " + end + "\n " + stacktrace;
      if (LOG.isLoggable(Level.INFO)) {
        LOG.info(errorMsg);
      }

      throw new BonitaWrapperException(new ExpressionEvaluationException(errorMsg));
    }
  }

  public String getValue() {
    return this.value;
  }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy