Method for processing string Formula operations using java _ MySQL

Source: Internet
Author: User
Method for processing string Formula operations using java bitsCN.com

When improving a contract project, there is a need because the calculation formula for non-data items in the contract will be changed according to the year, and the formula was previously hard coded into the system, as long as the time changes, the system will not be able to use it. Therefore, all non-basic data items in the contract must be able to customize the formula and generate reports and data in the contract according to the set formula.

Obviously, all the defined formulas are stored in the database as strings, but java does not have such tools or classes to execute the string formula, and an intermediate formula can be nested in the formula. For example, the basic data dddd is 56, while a formula depends on dddd. eeee = dddd * 20, and the final formula may be: eeee *-12 + 13-dddd + 24. We can see that eeee is an intermediate formula, so the calculation of a formula requires the intermediate formula and basic data.

This seems to be able to be solved using an interpreter mode, but I have not succeeded, because the priority of parentheses is a tricky problem, later I thought that I could use a template engine similar to freemarker or the ScriptEngine script engine provided after Java 6. I did an experiment and the script engine could solve the problem, however, this restricts the use of Java 6 and later versions. Finally, I found the perfect solution, that is, the suffix expression. The formula we usually write is called an infix expression, which is difficult for computers to process. Therefore, we need to convert the infix expression into a suffix expression that is easier to process on computers.

Convert an infix expression to a suffix expression. specific algorithm rules: see suffix expressions.


A. If it is '(', inbound stack;

B. If it is ')', add the operators in the stack to the suffix expression in sequence until '(' appears, and delete '(' from the stack '(';

C. If it is an operator other than parentheses, when its priority is higher than the top operator of the stack, it is directly written into the stack. Otherwise, from the top of the stack, an operator with a higher priority and a higher priority than the operator currently processed will pop up until one operator with a lower priority or a left bracket is encountered.

When the infix expression of the scan ends, all operators in the stack exit the stack;

Our requirements are as follows:

Public class FormulaTest {
@ Test
Public void testFormula (){
// Basic data
Map Values = new HashMap ();
Values. put ("dddd", BigDecimal. valueOf (56d ));

// Other formulas to be dependent
Map Formulas = new HashMap ();
Formulas. put ("eeee", "# {dddd} * 20 ");

// Formula to be calculated
String expression = "# {eeee} *-12 + 13-# {dddd} + 24 ";

BigDecimal result = FormulaParser. parse (expression, formulas, values );
Assert. assertEquals (result, BigDecimal. valueOf (-13459.0 ));
}
}

The following are the steps to solve the problem:

1. replace all the intermediate variables with the basic data.

The finalExpression method of FormulaParser replaces all the intermediate variables with the basic data, which is a recursive method.

Public class FormulaParser {
/**
* Regular expressions matching placeholder variables
*/
Private static Pattern pattern = Pattern. compile ("// # // {(. + ?) /

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.