当前位置: 首页 > 工具软件 > Commons JEXL > 使用案例 >

commons-jexl3 用法示例

祖波光
2023-12-01

这个库可以用来做强大无比的规则引擎,懂的都懂,不理解的多说也无用。

jexl版本信息:

 <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-jexl3</artifactId>
            <version>3.1</version>
        </dependency>

具体使用

jexl的用法是我自己使用、体会后总结出来的,会配以简要的说明,只代表个人理解。
下方代码片段的都可直接运行不报错。
本机环境 JDK8

建立几个test文件。

文件1: expression 用法

JexlExpressionTest.java

package cn.xuetian.jexl;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Callable;
import org.apache.commons.jexl3.JexlContext;
import org.apache.commons.jexl3.JexlEngine;
import org.apache.commons.jexl3.JexlExpression;
import org.apache.commons.jexl3.MapContext;
import org.apache.commons.jexl3.internal.Engine;

/**
 * @author zss
 * @date 2022/3/21 9:28
 * @Email:451682479@qq.com
 */
public class JexlExpressionTest {

    public static void main(String[] args) throws Exception {

        JexlEngine engine = new Engine();

        JexlContext context = new MapContext();
        String expStr = "array.size()";

        List<Object> arr = new ArrayList<>();
        arr.add("this is an array");
        arr.add(new Integer(2));

        context.set("array",arr);

        JexlExpression expression = engine.createExpression(expStr);

        Object evaluate = expression.evaluate(context);

        Callable<Object> callable = expression.callable(context);
        Object call = callable.call();


    }
}

文件2: script 用法

JexlScriptTest.java

package cn.xuetian.jexl;

import org.apache.commons.jexl3.JexlContext;
import org.apache.commons.jexl3.JexlEngine;
import org.apache.commons.jexl3.JexlScript;
import org.apache.commons.jexl3.MapContext;
import org.apache.commons.jexl3.internal.Engine;

/**
 * @author zss
 * @date 2022/3/21 10:28
 * @Email:451682479@qq.com
 */
public class JexlScriptTest {

    public static void main(String[] args) {

        JexlEngine engine = new Engine();

        JexlContext context = new MapContext();

        String expStr = "if(a>b){c=a;}else{c=b;}";

        context.set("a", 1);
        context.set("b", 2);
        context.set("c", 0);

        JexlScript script = engine.createScript(expStr);
        Object execute = script.execute(context);
        String s = execute.toString();

        expStr = "while(a<b){a=a+b;}";
        JexlScript script1  = engine.createScript(expStr);
        Object execute1 = script1.execute(context);
        String s1 = execute1.toString();


    }
}

文件3:java语法用法

JavaSynxTest.java

package cn.xuetian.jexl;

import org.apache.commons.jexl3.JexlContext;
import org.apache.commons.jexl3.JexlEngine;
import org.apache.commons.jexl3.JexlExpression;
import org.apache.commons.jexl3.MapContext;
import org.apache.commons.jexl3.internal.Engine;

/**
 * @author zss
 * @date 2022/3/21 10:56
 * @Email:451682479@qq.com
 */
public class JavaSynxTest {

    public static void main(String[] args) {
        JavaSynxTest test = new JavaSynxTest();
        test.test1();
    }

    public void test1() {

        JexlEngine engine = new Engine();

        JexlContext context = new MapContext();

        Person p = new Person("zhangsan", 11);
        context.set("person", p);

        String str = "person.getName()";
        JexlExpression expression = engine.createExpression(str);

        Object evaluate = expression.evaluate(context);

        String s = evaluate.toString();
    }


    /**
     * inner class
     */
    public class Person {

        private String name;

        private int age;

        public Person(String name, int age) {
            this.name = name;
            this.age = age;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public int getAge() {
            return age;
        }

        public void setAge(int age) {
            this.age = age;
        }
    }
}

文件4: Arithmetic用法

ArithmeticTest.java

package cn.xuetian.jexl;

import java.math.BigDecimal;
import org.apache.commons.jexl3.JexlArithmetic;
import org.apache.commons.jexl3.JexlEngine;
import org.apache.commons.jexl3.internal.Engine;

/**
 * @author zss
 * @date 2022/3/18 17:30
 * @Email:451682479@qq.com
 */
public class ArithmeticTest {

    public static void main(String[] args) {

        JexlArithmetic arithmetic = new JexlArithmetic(true);

        Object add = arithmetic.add(1, 2);
        System.out.println(add);

        BigDecimal a = new BigDecimal("3.1256");
        BigDecimal b = new BigDecimal("3.1256");

        Object multiply = arithmetic.multiply(a, b);
        System.out.println(multiply);


        JexlEngine engine = new Engine();
        JexlArithmetic arithmetic1 = engine.getArithmetic();
        Object divide = arithmetic1.divide(a, b);
        divide.toString();


    }
}


根据对上述片段的理解,进而可以包装出规则引擎,具体有机会再说。

 类似资料: