qlexpress 另外一個表達式引擎


qlexpress 是阿里開源的一個基於java 的腳本引擎,使用起來還是比較靈活的,以下是一個簡單的使用

環境准備

  • pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.dalong.ex</groupId>
    <artifactId>qlex-learnint</artifactId>
    <version>1.0-SNAPSHOT</version>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <encoding>UTF-8</encoding>
        <java.version>1.8</java.version>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>
    <dependencies>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>QLExpress</artifactId>
            <version>3.2.0</version>
        </dependency>
    </dependencies>
</project>
  • Application.java
import com.ql.util.express.DefaultContext;
import com.ql.util.express.ExpressRunner;
import com.ql.util.express.IExpressContext;
/**
 @author dalong
*/
public class Application {
    public static void main(String[] args) throws Exception {
        UserService userService =new UserService();
        ExpressRunner runner = new ExpressRunner();
        runner.addMacro("計算平均成績", "(語文+數學+英語)/3.0");
        runner.addMacro("是否優秀", "計算平均成績>90");
        IExpressContext<String, Object> context =new DefaultContext<String, Object>();
        context.put("語文", 88);
        context.put("數學", 99);
        context.put("英語", 95);
        runner.addFunctionOfServiceMethod("logout",userService,"logOut",new String[]{"String"},null);
        runner.addFunctionOfClassMethod("取絕對值", Math.class.getName(), "abs",
                new String[] { "double" }, null);
        runner.addFunctionOfClassMethod("轉換為大寫", UserService.class.getName(),
                "upper", new String[] { "String" }, null);
        runner.addFunctionOfServiceMethod("打印", System.out, "println",new String[] { "String" }, null);
        runner.addFunctionOfServiceMethod("contains", new UserService(), "anyContains",
                new Class[] { String.class, String.class }, null);
        String exp = "取絕對值(-100);轉換為大寫(\"hello world\");打印(\"你好嗎?\");contains(\"helloworld\",\"aeiou\")";
        Object result = runner.execute("是否優秀", context, null, false, false);
        Object result2 = runner.execute(exp, context, null, false, false);
        Object result3 = runner.execute("logout(\"demoapp\")", context, null, false, false);
        System.out.println(result);
        System.out.println(result2);
        System.out.println(result3);
    }
}
  • UserService.java 
/**
 @author dalong
*/
public class UserService {
    public static String token(String name,String password) {
        return String.format("%s----%s",name,password);
    }
    public static String upper(String abc) {
        return abc.toUpperCase();
    }
    public boolean anyContains(String str, String searchStr) {
        char[] s = str.toCharArray();
        for (char c : s) {
            if (searchStr.contains(c+"")) {
                return true;
            }
        }
        return false;
    }
}
 

說明

qlexpress 是一個不錯的選擇,但是從目前好多jvm 語言選擇的腳本引擎來說js 越來越多了(可以的有nashorn ,rhino以及graalvm(更好)),個人比較推薦的
是js 引擎,因為很多時候提供類似的功能更多是為了系統的擴展,但是js 更加通用學習成本低,好多東西可以復用(集成起來typescript 會更加強大)

參考資料

https://github.com/alibaba/QLExpress


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM