juel计算引擎/阿里计算引擎/Groovy引擎

    1. import java.util.Map;
    2. import de.odysseus.el.ExpressionFactoryImpl;
    3. import de.odysseus.el.util.SimpleContext;
    4. import org.apache.commons.lang3.StringUtils;
    5. import org.slf4j.Logger;
    6. import org.slf4j.LoggerFactory;
    7. import javax.el.ExpressionFactory;
    8. import javax.el.ValueExpression;
    9. public class JuelUtil {
    10. protected static final Logger LOGGER = LoggerFactory.getLogger(JuelUtil.class);
    11. private JuelUtil() {
    12. }
    13. public static Object execute(String expression, Map<String, Object> dataMap) {
    14. if (StringUtils.isBlank(expression)) {
    15. LOGGER.warn("calculate: expression is empty");
    16. return null;
    17. }
    18. ExpressionFactory factory = new ExpressionFactoryImpl();
    19. SimpleContext context = new SimpleContext();
    20. for (Object key : dataMap.keySet()) {
    21. Object value = dataMap.get(key);
    22. if (value != null) {
    23. ValueExpression valueExpression = factory.createValueExpression(value, value.getClass());
    24. context.setVariable(key.toString(), valueExpression);
    25. }
    26. }
    27. ValueExpression ve = factory.createValueExpression(context, expression, Boolean.class);
    28. return ve.getValue(context);
    29. }
    30. }
    1. import com.ql.util.express.DefaultContext;
    2. import com.ql.util.express.ExpressRunner;
    3. import com.ql.util.express.IExpressContext;
    4. import org.apache.commons.lang3.StringUtils;
    5. import org.slf4j.Logger;
    6. import org.slf4j.LoggerFactory;
    7. import java.util.Map;
    8. public class QLExpressUtil {
    9. protected static final Logger LOGGER = LoggerFactory.getLogger(QLExpressUtil.class);
    10. private QLExpressUtil() {
    11. }
    12. public static Object execute(String expression, Map<String, Object> dataMap) {
    13. if (StringUtils.isBlank(expression)) {
    14. LOGGER.warn("calculate: expression is empty");
    15. return null;
    16. }
    17. ExpressRunner runner = new ExpressRunner();
    18. IExpressContext<String, Object> expressContext = new DefaultContext<>();
    19. for (Object key : dataMap.keySet()) {
    20. Object value = dataMap.get(key);
    21. if (value != null) {
    22. expressContext.put(key.toString(), value);
    23. }
    24. }
    25. try {
    26. String el = expression.contains("${") ? expression.substring(2, expression.length() - 1) : expression;
    27. return runner.execute(el, expressContext, null, true, false);
    28. }catch (Exception e){
    29. LOGGER.error(e.getMessage());
    30. }
    31. return false;
    32. }
    33. }
    1. import com.didiglobal.turbo.engine.common.ErrorEnum;
    2. import com.didiglobal.turbo.engine.exception.ProcessException;
    3. import groovy.lang.Binding;
    4. import groovy.lang.GroovyShell;
    5. import groovy.lang.MissingPropertyException;
    6. import groovy.lang.Script;
    7. import org.apache.commons.lang3.StringUtils;
    8. import org.codehaus.groovy.runtime.InvokerHelper;
    9. import org.slf4j.Logger;
    10. import org.slf4j.LoggerFactory;
    11. import java.util.Map;
    12. import java.util.concurrent.ConcurrentHashMap;
    13. public class GroovyUtil {
    14. protected static final Logger LOGGER = LoggerFactory.getLogger(GroovyUtil.class);
    15. private static final Map<String, Class> SCRIPT_CLASS_CACHE = new ConcurrentHashMap<String, Class>();
    16. private GroovyUtil() {
    17. }
    18. public static Object execute(String expression, Map<String, Object> dataMap) throws Exception {
    19. if (StringUtils.isBlank(expression)) {
    20. LOGGER.warn("calculate: expression is empty");
    21. return null;
    22. }
    23. try {
    24. Binding binding = createBinding(dataMap);
    25. Script shell = createScript(expression, binding);
    26. Object resultObject = shell.run();
    27. LOGGER.info("calculate.||expression={}||resultObject={}", expression, resultObject);
    28. return resultObject;
    29. } catch (MissingPropertyException mpe) {
    30. LOGGER.warn("calculate MissingPropertyException.||expression={}||dataMap={}", expression, dataMap);
    31. throw new ProcessException(ErrorEnum.MISSING_DATA.getErrNo(), mpe.getMessage());
    32. }
    33. }
    34. private static Script createScript(String groovyExpression, Binding binding) {
    35. Script script;
    36. if (SCRIPT_CLASS_CACHE.containsKey(groovyExpression)) {
    37. Class scriptClass = SCRIPT_CLASS_CACHE.get(groovyExpression);
    38. script = InvokerHelper.createScript(scriptClass, binding);
    39. } else {
    40. script = new GroovyShell(binding).parse(groovyExpression);
    41. SCRIPT_CLASS_CACHE.put(groovyExpression, script.getClass());
    42. }
    43. return script;
    44. }
    45. private static Binding createBinding(Map<String, Object> infos) {
    46. Binding binding = new Binding();
    47. if (!infos.isEmpty()) {
    48. for (Map.Entry<String, Object> entry : infos.entrySet()) {
    49. binding.setVariable(entry.getKey(), entry.getValue());
    50. }
    51. }
    52. return binding;
    53. }
    54. }
    1. <dependency>
    2. <groupId>org.codehaus.groovy</groupId>
    3. <artifactId>groovy-all</artifactId>
    4. <version>2.3.7</version>
    5. </dependency>
    6. <dependency>
    7. <groupId>com.google.guava</groupId>
    8. <artifactId>guava</artifactId>
    9. <version>18.0</version>
    10. </dependency>
    1. <!-- JUEL begin-->
    2. <dependency>
    3. <groupId>de.odysseus.juel</groupId>
    4. <artifactId>juel-api</artifactId>
    5. <version>2.2.7</version>
    6. </dependency>
    7. <dependency>
    8. <groupId>de.odysseus.juel</groupId>
    9. <artifactId>juel-impl</artifactId>
    10. <version>2.2.7</version>
    11. </dependency>
    1. <!--JUEL end-->
    2. <dependency>
    3. <groupId>com.alibaba</groupId>
    4. <artifactId>QLExpress</artifactId>
    5. <version>3.2.2</version>
    6. </dependency>