java qlExpress demo

22 篇文章 0 订阅
QlExpressUtils.java:
import com.ql.util.express.DefaultContext;
import com.ql.util.express.ExpressRunner;

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

public class QlExpressUtils {

    private static final ExpressRunner runner = new ExpressRunner();

    static {
        try {
            runner.addFunctionOfServiceMethod("AND", new FunctionClass(), "and", new Class[]{Boolean.class, Boolean.class}, null);
            runner.addFunctionOfServiceMethod("CONTAINS", new FunctionClass(), "contains", new Class[]{String.class, String.class}, null);
            runner.addFunctionOfServiceMethod("REPLACE_ALL", new FunctionClass(), "replaceAll", new Class[]{String.class,String.class, String.class}, null);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static class FunctionClass {

        /**
         * @param condition1  条件1
         * @param condition2  条件2
         * @return
         */
        public boolean and(Boolean condition1, Boolean condition2) {
            return  condition1 && condition2;
        }

        /**
         * @param srcText
         * @param targetText
         * @return
         */
        public boolean contains( String srcText,String targetText ){
            return srcText.contains( targetText );
        }

        /**
         * @param srcText 原始文本
         * @param oldStr  需要替换的旧文本
         * @param newStr  需要替换成的新文本
         * @return
         */
        public String replaceAll( String srcText,String oldStr,String newStr ){
            return srcText.replaceAll( oldStr,newStr );
        }
    }

    /**
     * @param ifFunctionExpress 形如:IF( a > 200,IF( b > 200,true,false ),true )
     * @param paramsMap
     * @return
     */
    public static Object calculateIfFunction(String ifFunctionExpress, Map<String,Integer> paramsMap){
        try {
            String express = "function IF( boolean condition,Object result_conditionIsTrue,Object result_conditionIsFalse ){" +
                             "  if( condition ){" +
                             "      return result_conditionIsTrue;" +
                             "  }else{" +
                             "      return result_conditionIsFalse;" +
                             "  }" +
                             "}" +
                             "result = " + ifFunctionExpress + ";" +
                             "return result;";
            DefaultContext<String, Object> context = new DefaultContext<>();
            Set<String> keys = paramsMap.keySet();
            for( String key:keys ){
                Integer value = paramsMap.get(key);
                context.put( key,value );
            }
            // 执行脚本
            Object result = runner.execute(express, context, null, true, false);
            System.out.println( result );
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    public static Object doExpress(String express, Map<String,Object> paramsMap){
        try {
            DefaultContext<String, Object> context = new DefaultContext<>();
            if( paramsMap != null && paramsMap.size() > 0 ){
                Set<String> keys = paramsMap.keySet();
                for( String key:keys ){
                    Object value = paramsMap.get(key);
                    context.put( key,value );
                }
            }
            // 执行脚本
            Object result = runner.execute(express, context, null, true, false);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public static void test1() {
        String express = "IF( age < 18,true,IF( age > 55,true,false ) )";
        Integer age = 16;

        Map<String, Integer> paramsMap = new HashMap<>();
        paramsMap.put( "age",age );

        boolean hasRisk = (boolean) calculateIfFunction(express, paramsMap);
        System.out.println(  );
        if( hasRisk ){
            System.err.println( "年龄 " + age + " 有风险" );
        }else {
            System.out.println( "年龄 " + age + " 无风险" );
        }
    }

    public static void test2() {
        String express = "IF( age < 18,10,IF( age > 55,9,100 ) )";
        Integer age = 90;

        Map<String, Integer> paramsMap = new HashMap<>();
        paramsMap.put( "age",age );

        Integer score = (Integer) calculateIfFunction(express, paramsMap);
        System.out.println( "age = " + age + " 的 score is " + score );
    }

    public static void test3() {
        String express = "IF( age < 18,'未成年人',IF( age > 55,'老年人','中年人' ) )";
        Integer age = 11;

        Map<String, Integer> paramsMap = new HashMap<>();
        paramsMap.put( "age",age );

        String agePeriod = (String) calculateIfFunction(express, paramsMap);
        System.out.println( "age = " + age + " 的 年龄段属于 " + agePeriod );
    }

    /**
     * IF(
     * 	age < 15,
     * 	'少年',
     * 	IF(
     * 		age < 25,
     * 		'青年',
     * 		IF(
     * 			age < 35,
     * 			'青壮年',
     * 			IF(
     * 				age < 45,
     * 				'壮年',
     * 				IF(
     * 					age < 55,
     * 					'中老人',
     * 					IF(
     * 						age < 65,
     * 						'老年',
     * 						'高寿'
     * 					)
     * 				)
     * 			)
     * 		)
     * 	)
     * )
     */
    public static String getAgePeriodByAge( Integer age ){
        String express = "IF( \n" +
                        "\tage < 15,\n" +
                        "\t'少年',\n" +
                        "\tIF( \n" +
                        "\t\tage < 25,\n" +
                        "\t\t'青年',\n" +
                        "\t\tIF(\n" +
                        "\t\t\tage < 35,\n" +
                        "\t\t\t'青壮年',\n" +
                        "\t\t\tIF(\n" +
                        "\t\t\t\tage < 45,\n" +
                        "\t\t\t\t'壮年',\n" +
                        "\t\t\t\tIF(\n" +
                        "\t\t\t\t\tage < 55,\n" +
                        "\t\t\t\t\t'中老人',\n" +
                        "\t\t\t\t\tIF(\n" +
                        "\t\t\t\t\t\tage < 65,\n" +
                        "\t\t\t\t\t\t'老年',\n" +
                        "\t\t\t\t\t\t'高寿'\n" +
                        "\t\t\t\t\t)\n" +
                        "\t\t\t\t)\n" +
                        "\t\t\t)\n" +
                        "\t\t)\n" +
                        "\t)\n" +
                        ")";
        Map<String, Integer> paramsMap = new HashMap<>();
        paramsMap.put( "age",age );
        String agePeriod = (String) calculateIfFunction(express, paramsMap);
        return agePeriod;
    }

    public static void main(String[] args) {
        // test1();
        // test2();
        // test3();

       /* for (int age = 1; age < 100; age++) {
            String agePeriod = getAgePeriodByAge(age);
            System.out.println( "age = " + age + " 属于 " + agePeriod );
        }*/
        // String express = "REPLACE_ALL( \"党和国家不会亏待你们的\",\"党\",'***' )";
        String express = "REPLACE_ALL( REPLACE_ALL( \"请dang和zhengfu放心,我们一定会完成任务的\",\"dang\",\"***\" ),\"zhengfu\",\"***\" )";

        Map<String, Object> paramsMap = new HashMap<>();
        // paramsMap.put( "srcText",srcText );

        String result = (String) doExpress( express, paramsMap );
        System.out.println( result );
    }
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值