Unlock High‑Performance Rule Engine with AviatorScript: A Complete Guide
This article introduces AviatorScript, a lightweight high‑performance JVM‑based scripting language, explains its core features, provides step‑by‑step setup and syntax examples, demonstrates function creation and custom extensions, and showcases real‑world use cases such as client version control, marketing rules, and order risk assessment.
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, offering a more compact and faster alternative to traditional rule engines like Drools, Jess, and JRules.
Key Features
Supports basic types (numbers, strings, regex, booleans) and all Java operators.
Built‑in bigint and decimal types for large integer and high‑precision calculations, with operator overloading for +-*/.
Comprehensive syntax for multi‑line data, conditionals, loops, lexical scoping, 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.
Exceptional performance via ASM mode (direct bytecode generation) and an interpreter mode for non‑standard JVM platforms.
Quick Start
Add the Aviator dependency to a SpringBoot project:
<dependency>
<groupId>com.googlecode.aviator</groupId>
<artifactId>aviator</artifactId>
<version>5.3.3</version>
</dependency>Create a script file hello.av in src/main/resources/script: println("Hello, AviatorScript!"); Run it with a unit 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();
}Or compile a script string directly:
@Test
void testHelloStr() throws Exception {
String script = "println(\"Hello, AviatorScript!\");";
Expression exp = AviatorEvaluator.getInstance().compile(script);
exp.execute();
}The official IDEA plugin allows direct script compilation and execution, though it is only maintained up to IDEA 2021.
Basic Syntax
AviatorScript’s syntax resembles mathematical expressions and includes the following constructs:
Data Types and Operations
Numbers: integers (Java long range), floating‑point ( double), and high‑precision decimal (BigDecimal) using the M suffix.
Strings: single or double quotes, concatenation with +, length via string.length, and functions like substring under the string namespace.
Booleans: true and false, with standard comparison operators ( >, >=, <, <=, ==, !=).
Control Flow
Conditional statements:
if (true) {
println("in if body");
}
if (false) {
println("in if body");
} else {
println("in else body");
}
let a = rand(1100);
if (a > 1000) {
println("a is greater than 1000.");
} elsif (a > 100) {
println("a is greater than 100.");
} else {
println("a is less than 10");
}Loop statements:
for i in range(0, 10) {
println(i);
}
for i in range(0, 10, 2) {
println(i);
}
let sum = 1;
while sum < 1000 {
sum = sum + sum;
}
println(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 worldCustom Java functions can be injected by extending AbstractFunction and registering them with an AviatorEvaluatorInstance. Example:
public class AddFunction extends AbstractFunction {
@Override
public AviatorObject call(Map<String, Object> env, AviatorObject arg1, AviatorObject arg2) {
Number left = FunctionUtils.getNumberValue(arg1, env);
Number right = FunctionUtils.getNumberValue(arg2, env);
return new AviatorDouble(left.doubleValue() + right.doubleValue());
}
@Override
public String getName() { return "add"; }
}Real‑World Use Cases
Client Version Control
Custom version‑comparison functions can be written in AviatorScript and registered, allowing dynamic rule updates without code changes.
Marketing Activity Rules
Business rules such as tiered discounts can be expressed as scripts and evaluated at runtime, simplifying frequent promotional changes.
Order Risk Assessment
Risk‑level determination based on order amount, customer rating, and other attributes can be encapsulated in scripts, enabling rapid adjustments to fraud‑prevention policies.
Conclusion
AviatorScript provides a rich yet lightweight rule‑engine language that runs on the JVM, supports custom extensions, and can be seamlessly integrated into Java projects to increase flexibility and reduce development overhead.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
macrozheng
Dedicated to Java tech sharing and dissecting top open-source projects. Topics include Spring Boot, Spring Cloud, Docker, Kubernetes and more. Author’s GitHub project “mall” has 50K+ stars.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
