Java常见规则引擎 Jexl,Groovy,Aviator,Jeval 等使用示例

Java常见规则引擎使用示例

    • 1. Jexl
    • 2. Groovy
    • 3. Aviator
    • 4. Jeval

1. Jexl

  • 需要引入的依赖包

	org.apache.commons
	commons-jexl
	2.1.1

  • 使用示例
import org.apache.commons.jexl2.Expression;
import org.apache.commons.jexl2.JexlContext;
import org.apache.commons.jexl2.JexlEngine;
import org.apache.commons.jexl2.MapContext;

public class Jexl {

    public static void main(String[] args) {
        Object result1 = parse("line.contains(\"test\")", "9123rhewtestdfks");
        Object result2 = parse("line.contains(\"test\")", "123");
        System.out.println(result1);
        System.out.println(result2);
    }

    public static Object parse(String re, String line) {
        JexlEngine jexlEngine = new JexlEngine();
        Expression expression = jexlEngine.createExpression(re);

        JexlContext jexlContext = new MapContext();
        jexlContext.set("line", line);

        return expression.evaluate(jexlContext);
    }
}
  • 执行结果
true
false

Process finished with exit code 0

2. Groovy

  • 需要引入的依赖包

	org.codehaus.groovy
	groovy
	 3.0.3

  • 使用示例
import groovy.lang.Binding;
import groovy.lang.GroovyShell;
import groovy.lang.Script;

import java.util.HashMap;
import java.util.Map;

public class Groovy {

    public static void main(String[] args) {
        Object result1 = parse("line.contains(\"test\")", "9123rhewtestdfks");
        Object result2 = parse("line.contains(\"test\")", "123");
        System.out.println(result1);
        System.out.println(result2);
    }

    public static Object parse(String re, String line) {

        Map map = new HashMap(1);
        map.put("line", line);

        Script script = new GroovyShell().parse(re);
        script.setBinding(new Binding(map));
        return script.run();
    }
}

3. Aviator

  • 需要引入的依赖包

	com.googlecode.aviator
	aviator
	5.0.0
 
  • 使用示例
import com.googlecode.aviator.AviatorEvaluator;

public class Aviator{

    public static void main(String[] args) {
        System.out.println(parse("string.contains(\"test\", \"es\")"));
    }

    public static Object parse(String expression) {

        return AviatorEvaluator.execute(expression);
    }
    
}
  • 执行结果
true

Process finished with exit code 0

4. Jeval

  • 需要引入的依赖包

	net.sourceforge.jeval
	jeval
	 0.9.4

  • 使用示例
import net.sourceforge.jeval.EvaluationException;
import net.sourceforge.jeval.Evaluator;

public class Jeval {

    public static void main(String[] args) {
        Object result1 = parse("#{a}+#{b}", "3", "5");
        Object result2 = parse("7+6", "", "");
        
        System.out.println(result1);
        System.out.println(result2);
    }

    public static Object parse(String re, String a, String b) {
        Evaluator evaluator = new Evaluator();
        evaluator.putVariable("a", a);
        evaluator.putVariable("b",b);

        try {
            return evaluator.evaluate(re);
        } catch (EvaluationException e) {
            e.printStackTrace();
        }
      return "";
    }

}
  • 执行结果
8.0
13.0

Process finished with exit code 0

你可能感兴趣的:(开源项目使用)