Read this in other languages: 简体中文
Calculate an open-source Maven project that parses and evaluates arithmetic expressions using an Abstract Syntax Tree (AST) and generates optimized bytecode. Originally designed to work with double
for computations, after version will be refactored to use BigDecimal
to ensure high-precision arithmetic, making it especially suitable for financial and scientific calculations.
Note:
This project is a work in progress. The current version supports interpretation mode with BigDecimal-based evaluation. Future improvements will include extending the compile mode (bytecode generation) to support BigDecimal operations and further performance optimizations.
-
AST-Based Parsing & Evaluation
Parses arithmetic expressions (supporting addition, subtraction, multiplication, division, and basic function calls) into an AST, and evaluates them usingBigDecimal
to maintain high precision. -
Function Support
Built-in support for basic functions such assqrt
,pow
,max
,min
, andnvl
through a centralFunctionRegistry
. Custom functions can be easily registered. -
Variable Resolution
Variables are resolved from a provided context (Map<String, BigDecimal>
). If a variable is not defined, it defaults to zero. -
Extensible Architecture
The AST module is pluggable, enabling the choice between interpretation and (in future) compilation modes. -
Future Compile Mode (Planned)
Although the current release only supports interpretation mode, future releases will implement a compile mode with full BigDecimal support for even higher performance.
Clone the repository and build the project using Maven:
git clone https://github.com/akuowen/calculate
cd calculate
mvn clean install
Below is an example demonstrating how to evaluate arithmetic expressions using the engine:
package io.ouka.demo;
import io.ouka.demo.ex.CalculationException;
import io.ouka.demo.graph.DependencyGraph;
public class DAGMetricCalculationExample {
public static void main(String[] args) {
DependencyGraph dependencyGraph = new DependencyGraph();
MetricValueManager valueManager = new MetricValueManager(dependencyGraph);
CalculationEngine engine = new CalculationEngine(dependencyGraph, valueManager);
HistoryRecorder history = new HistoryRecorder(valueManager);
try {
engine.addExpression("profit", "revenue - cost");
engine.addExpression("margin", "(profit / revenue) * 100");
engine.addExpression("test", "profit * margin");
valueManager.setValue("revenue", 150000.0);
valueManager.setValue("cost", 150000.0);
System.out.println("利润: " + valueManager.getValue("profit"));
System.out.println("利润率: " + valueManager.getValue("margin") + "%");
System.out.println("test: " + valueManager.getValue("test"));
valueManager.setValue("revenue", 200000.0);
System.out.println("利润: " + valueManager.getValue("profit"));
System.out.println("利润率: " + valueManager.getValue("margin") + "%");
System.out.println("test: " + valueManager.getValue("test"));
history.getHistory().forEach(System.out::println);
} catch (CalculationException e) {
System.err.println("系统错误: " + e.getMessage());
}
}
}
We plan to implement the following enhancements in future releases:
- Extend AST Compilation for BigDecimal Operations
Modify the existing Abstract Syntax Tree (AST) compilation process to fully supportBigDecimal
operations, transitioning from interpretation to compilation for improved performance.:contentReference[oaicite:0]{index=0}
-
Incremental Computation Mechanism
Implement strategies to update computations efficiently by reusing previously computed results when inputs change slightly, reducing processing time. -
Caching Strategies
Develop and integrate caching mechanisms to store and reuse results of expensive computations, minimizing redundant processing and enhancing overall performance. -
Dynamic Method Dispatch Optimization
Explore the use ofinvokedynamic
to optimize dynamic method dispatch, potentially improving execution speed. -
AST Processing Pipeline Optimization
Optimize the AST processing pipeline to reduce overhead and enhance efficiency.
-
Plugin-Based Function System
Design a plugin architecture for the function system, allowing users to add or modify functions without altering the core engine, facilitating customization and scalability. -
DSL Extension Support
Introduce support for Domain-Specific Languages (DSLs) to enable users to define and parse custom expressions tailored to specific problem domains, enhancing flexibility and usability. -
Support for Additional Arithmetic Operators and Advanced Mathematical Functions
Expand the range of supported operations to include more arithmetic operators and advanced mathematical functions. -
User-Defined Functions
Allow users to define custom functions and register them dynamically within theFunctionRegistry
, enhancing the engine's flexibility.
-
Isolation of Computational Resources
Implement mechanisms to isolate computational resources, ensuring that individual calculations do not interfere with each other, thereby enhancing system stability. -
Transactional Updates
Incorporate transactional processing to ensure that updates to computations are atomic, consistent, isolated, and durable (ACID), maintaining data integrity.
-
Computation Process Tracing
Develop tools to trace and monitor the computation process, providing insights into the execution flow and facilitating debugging and optimization. -
Performance Metrics Collection
Implement systems to collect and analyze performance metrics, enabling continuous monitoring and identification of potential bottlenecks. -
Enhanced Error Handling
Improve error handling by providing more detailed and user-friendly parsing error messages.
-
Version Difference Analysis
Introduce capabilities to analyze differences between various versions of computations or expressions, aiding in tracking changes and understanding their impacts. -
Predictive Computation Functionality
Develop features that allow the engine to predict and pre-compute potential future computations based on patterns, reducing latency for anticipated tasks.
-
Dynamic Class Loading Mechanism
Implement a dynamic class loading system to allow the engine to load and execute classes at runtime, enhancing flexibility and adaptability in a distributed environment. -
Version Control Design in Compile Mode
Design a robust version control system for the compile mode to manage and track different versions of compiled expressions or functions, ensuring consistency across distributed systems. -
Network Communication Optimization
Optimize network communication protocols to enhance data transfer efficiency and reduce latency in distributed computing scenarios.
-
Precision and Rounding Mode Configuration
Add runtime configuration options for specifying precision and rounding modes, allowing users to tailor calculations to their specific requirements. -
Integration with Logging and Debugging Frameworks
Improve integration with existing logging and debugging frameworks to facilitate monitoring and troubleshooting.
- Project Modularization
:contentReference[oaicite:1]{index=1}:contentReference[oaicite:2]{index=2}
- Support for Mixed-Type Operations
:contentReference[oaicite:3]{index=3}:contentReference[oaicite:4]{index=4}
These enhancements aim to improve the performance, extensibility, stability, observability, and scalability of the BigDecimal AST Expression Engine, ensuring it meets evolving user needs and technological advancements.
Contributions are welcome! Please fork the repository, submit pull requests, or open issues for any bug reports or feature requests. For major changes, please open an issue first to discuss your ideas.
This project is licensed under the MIT License. See the LICENSE file for more details.