juel计算引擎/阿里计算引擎/Groovy引擎
import java.util.Map;
import de.odysseus.el.ExpressionFactoryImpl;
import de.odysseus.el.util.SimpleContext;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.el.ExpressionFactory;
import javax.el.ValueExpression;
public class JuelUtil {
protected static final Logger LOGGER = LoggerFactory.getLogger(JuelUtil.class);
private JuelUtil() {
}
public static Object execute(String expression, Map<String, Object> dataMap) {
if (StringUtils.isBlank(expression)) {
LOGGER.warn("calculate: expression is empty");
return null;
}
ExpressionFactory factory = new ExpressionFactoryImpl();
SimpleContext context = new SimpleContext();
for (Object key : dataMap.keySet()) {
Object value = dataMap.get(key);
if (value != null) {
ValueExpression valueExpression = factory.createValueExpression(value, value.getClass());
context.setVariable(key.toString(), valueExpression);
}
}
ValueExpression ve = factory.createValueExpression(context, expression, Boolean.class);
return ve.getValue(context);
}
}
import com.ql.util.express.DefaultContext;
import com.ql.util.express.ExpressRunner;
import com.ql.util.express.IExpressContext;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.Map;
public class QLExpressUtil {
protected static final Logger LOGGER = LoggerFactory.getLogger(QLExpressUtil.class);
private QLExpressUtil() {
}
public static Object execute(String expression, Map<String, Object> dataMap) {
if (StringUtils.isBlank(expression)) {
LOGGER.warn("calculate: expression is empty");
return null;
}
ExpressRunner runner = new ExpressRunner();
IExpressContext<String, Object> expressContext = new DefaultContext<>();
for (Object key : dataMap.keySet()) {
Object value = dataMap.get(key);
if (value != null) {
expressContext.put(key.toString(), value);
}
}
try {
String el = expression.contains("${") ? expression.substring(2, expression.length() - 1) : expression;
return runner.execute(el, expressContext, null, true, false);
}catch (Exception e){
LOGGER.error(e.getMessage());
}
return false;
}
}
import com.didiglobal.turbo.engine.common.ErrorEnum;
import com.didiglobal.turbo.engine.exception.ProcessException;
import groovy.lang.Binding;
import groovy.lang.GroovyShell;
import groovy.lang.MissingPropertyException;
import groovy.lang.Script;
import org.apache.commons.lang3.StringUtils;
import org.codehaus.groovy.runtime.InvokerHelper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
public class GroovyUtil {
protected static final Logger LOGGER = LoggerFactory.getLogger(GroovyUtil.class);
private static final Map<String, Class> SCRIPT_CLASS_CACHE = new ConcurrentHashMap<String, Class>();
private GroovyUtil() {
}
public static Object execute(String expression, Map<String, Object> dataMap) throws Exception {
if (StringUtils.isBlank(expression)) {
LOGGER.warn("calculate: expression is empty");
return null;
}
try {
Binding binding = createBinding(dataMap);
Script shell = createScript(expression, binding);
Object resultObject = shell.run();
LOGGER.info("calculate.||expression={}||resultObject={}", expression, resultObject);
return resultObject;
} catch (MissingPropertyException mpe) {
LOGGER.warn("calculate MissingPropertyException.||expression={}||dataMap={}", expression, dataMap);
throw new ProcessException(ErrorEnum.MISSING_DATA.getErrNo(), mpe.getMessage());
}
}
private static Script createScript(String groovyExpression, Binding binding) {
Script script;
if (SCRIPT_CLASS_CACHE.containsKey(groovyExpression)) {
Class scriptClass = SCRIPT_CLASS_CACHE.get(groovyExpression);
script = InvokerHelper.createScript(scriptClass, binding);
} else {
script = new GroovyShell(binding).parse(groovyExpression);
SCRIPT_CLASS_CACHE.put(groovyExpression, script.getClass());
}
return script;
}
private static Binding createBinding(Map<String, Object> infos) {
Binding binding = new Binding();
if (!infos.isEmpty()) {
for (Map.Entry<String, Object> entry : infos.entrySet()) {
binding.setVariable(entry.getKey(), entry.getValue());
}
}
return binding;
}
}
<dependency>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy-all</artifactId>
<version>2.3.7</version>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>18.0</version>
</dependency>
<!-- JUEL begin-->
<dependency>
<groupId>de.odysseus.juel</groupId>
<artifactId>juel-api</artifactId>
<version>2.2.7</version>
</dependency>
<dependency>
<groupId>de.odysseus.juel</groupId>
<artifactId>juel-impl</artifactId>
<version>2.2.7</version>
</dependency>
<!--JUEL end-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>QLExpress</artifactId>
<version>3.2.2</version>
</dependency>