Why AviatorScript Is the Lightweight JVM Rule Engine You Need

This article introduces AviatorScript, a high‑performance, lightweight JVM‑based scripting language, covering its origins, key features, quick‑start setup, core syntax for numbers, strings, booleans, control flow, functions, and real‑world use cases such as client version control, marketing rules, and order risk management.

Su San Talks Tech
Su San Talks Tech
Su San Talks Tech
Why AviatorScript Is the Lightweight JVM Rule Engine You Need

Introduction

AviatorScript is a high‑performance, lightweight scripting language that runs on the JVM (including Android). It originated in 2010 as a customized subset of Groovy.

Key Features

Supports numbers, strings, regular expressions, booleans and all Java operators.

Built‑in bigint and decimal types for large integer and high‑precision calculations, with operator overloading.

Full syntax for multi‑line data, conditionals, loops, lexical scopes and exception handling.

Sequence abstraction for functional‑style collection processing.

Lightweight module system for organizing code.

Easy Java method invocation and a complete script API for calling scripts from Java.

High performance: ASM mode compiles scripts to JVM bytecode; an interpreter mode works on non‑standard platforms such as Android.

Quick Start

Add the Maven dependency:

<dependency>
    <groupId>com.googlecode.aviator</groupId>
    <artifactId>aviator</artifactId>
    <version>5.3.3</version>
</dependency>

Create hello.av with println("Hello, AviatorScript!"); and run it via a JUnit test:

@Test
void testHello() throws Exception {
    ClassPathResource resource = new ClassPathResource("script/hello.av");
    String scriptPath = resource.getPath();
    Expression exp = AviatorEvaluator.getInstance().compileScript(scriptPath);
    exp.execute();
}

You can also compile a script string directly:

@Test
void testHelloStr() throws Exception {
    String script = "println(\"Hello, AviatorScript!\");";
    Expression exp = AviatorEvaluator.getInstance().compile(script);
    exp.execute();
}

The execution consists of two steps: compile and execute.

Basic Syntax

Numbers

Supports integers, floating‑point numbers and high‑precision Decimal (suffix M). Example:

let a = 99;
let b = 0xFF;
let c = -99;
println(a + b); // 270
println(a / b); // 0
println(a - b + c); // -156
println(a + b * c); // -9801
println(a - (b - c)); // 198
println(a / b * b + a % b); // 99

Type conversion follows the order long → bigint → decimal → double.

Floating‑Point Numbers

let a = 1.34159265;
let b = 0.33333;
let c = 1e-2;
println(a + b); // 1.67492265
println(a - b); // 1.00826265
println(a * b); // 0.4471865500145
println(a / b); // 4.0257402772554
println(a + c); // 1.35159265

High‑Precision Decimal

let a = 1.34M;
let b = 0.333M;
let c = 2e-3M;
println(a + b); // 1.673M
println(a - b); // 1.007M
println(a * b); // 0.44622M
println(a / b); // 4.022022022M
println(a + c); // 1.342M

Strings

Strings are enclosed in single or double quotes and can be concatenated with +. Length can be obtained via string.length.

let a = "hello world";
println(a); // hello world
println(string.length(a)); // 11
println(a + ", " + "AviatorScript" + "!" + 5); // hello world, AviatorScript!5

Booleans and Logic

println("3 > 1 is " + (3 > 1)); // true
println("3 <= 3 is " + (3 <= 3)); // true
println("3 == 1 is " + (3 == 1)); // false

Control Flow

Conditional statements:

if (true) {
    println("in if body");
} else {
    println("in else body");
}

Loop statements:

for i in range(0, 10) {
    println(i);
}
while sum < 1000 {
    sum = sum + sum;
}

Functions

Define functions with fn:

fn add(x, y) {
    return x + y;
}
three = add(1, 2);
println(three); // 3
s = add('hello', ' world');
println(s); // hello world

Custom Java functions can be added by extending AbstractFunction and registering them with the evaluator.

Real‑World Use Cases

Client Version Control

Store version‑comparison logic in a script and inject device and version values via the execution context, allowing dynamic compatibility handling without code changes.

Marketing Activity Rules

Dynamic discount rules can be expressed as scripts, enabling rapid updates for promotions such as tiered discounts, first‑order reductions, or random offers.

Order Risk Control

Risk level determination based on order amount, customer rating, and address can be scripted, providing flexible configuration for high, medium, or low risk classifications.

Conclusion

AviatorScript offers a rich yet lightweight syntax, high performance, and easy extensibility, making it suitable for rule engines, formula calculations, dynamic scripting, and ELT‑style data processing.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

JavaJVMrule engineBackend DevelopmentScripting LanguageAviatorScript
Su San Talks Tech
Written by

Su San Talks Tech

Su San, former staff at several leading tech companies, is a top creator on Juejin and a premium creator on CSDN, and runs the free coding practice site www.susan.net.cn.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.