Spads Formula Parsing System

Source: Internet
Author: User

In many network applications, the specific evaluation logic involved in some internal operations cannot be determined at the development stage. These logics need to be used with the detection of user response and constantly corrected. A common condition is the calculation formula for Action gains in online games. The Spads development team has launched an analytical formula system for this situation.

The Formula Parsing System can calculate various arithmetic operations, logical operations, and comparison operations. It can connect local functions and support bracket classification. It allows you to set temporary variables in the formula step by step and provides branch operators, json data format operations are also supported. Many design methods are used in system writing, including concept extraction, inheritance, polymorphism, interface-oriented design, enumeration, recursion, and factory. The data structure also uses the easy-to-use but rare double-end columns. It is suitable for practical scenarios and learning Java design ideas and programming methods. This program is designed and written by Jon Loo LI. All the details focus on the running efficiency. In many details, we have made several times better than Java) system library methods; because you only need to process the formula, the syntax tree structure is single, and this ensures a higher execution speed than the code interpreter of other existing injection languages.

The following describes the usage of the Formula Parsing System.
First, the formula passes? = To assign values to variables. This assignment is not an operation, but an exclusive symbol that each line of the formula requires and can only appear once.
Result? = 38*6
The Formula Parsing System uses Result to represent the final Result. The formula above indicates that the final result is 228.

The system supports the following arithmetic operations:
Negative-
Add +
Minus-
Multiplication *
Division/
Remainder %
Multiplication ^
Factorial!
For example

Result? = (2 ^ 5-(-13) % 10 )!

120.

Next, let's take a look at the branch control of the formula system.

DayCount? = 31 DayCost? = 58.5 Limit? = 1500 Result? = DayCount * DayCost> Limit? "Overspending! "~ "No overspending"

We can see that the entire formula is divided into four rows. Each row declares a variable.
In fact, in this formula system, it is not strictly required that the above formula cannot use the variables declared by the following formula, but if nested is used, an exception is thrown.
The above formula will produce a string with the content "overspending ".

The supported operations are as follows:
Greater than>
Less than <
Equal to =
And &
Or |
Non-this is in the C-language series! )
Condition?
Branch ~ This is different from the C-language series)

The most important feature of this system is the ability to bind the functions in the formula to local functions.
You only need to create an implementation class of the Function interface and register it with FunctionEnum to obtain a Function that can be recognized in the formula.
I recently learned the idea that "conventions are better than express conventions" and will change the FunctionEnum configuration method to a certain extent in the near future.

For example, I provide the built-in function TOGETHER, which means that the first parameter is the name, the second parameter is the value, which forms a key-value pair, and the third parameter is the name, the fourth parameter is a value that forms a key-value pair. Finally, it is combined into a Json string. See the following formula.

Name? = "Shane" Level? = 16 Result? = TOGETHER (\ "name \", Name, "graduated", Level> 10? "Graduated "~ "Not graduated ")

The final result is {"name": "Shane", "graduated": "graduated "}


I have discussed the usage of so many Formula Parsing systems. Now we will introduce how to enable this system function from a programming perspective.
First, you must import the. jar package. See the following code.

 
 
  1. EvaluatorFactory factory = EvaluatorFactory.INST;

  2. Evaluator eva = factory.getEvaluator("Result ?= 28 + 2 / 10.0");

  3. ExpValue result = eva.evaluate();

  4. System.out.println(result);

Or

 
 
  1. String form = "Result ?= 28 + 2 / 10.0";

  2. ExpValue result = EvaluatorFactory.INST.getEvaluator(form).evaluate();

First, the calculator Factory is a Singleton, but if you want to expand the function library in the future, you can rewrite it into multiple factories. After obtaining the factory, input the formula string multiple rows) to obtain the calculator for this formula through the factory. Then, call the calculation method of the calculator to obtain the final result of the formula.

If you have any questions, you can contact Surmounting@gmail.com
Project package: http://download.csdn.net/detail/shanelooli/4726670

This article also published in my CSDN log: http://blog.csdn.net/shanelooli/article/details/8142726
ITeye: http://surmounting.iteye.com/blog/1714807
China open source community published: http://my.oschina.net/shane1984/blog/86812


This article is from the "Shane from Spads" blog, please be sure to keep this source http://shanelooli.blog.51cto.com/5523233/1048723

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.