Building a powerful, flexible formula management system using Java script

xiaoxiao2021-03-06  18

Implement formula management with beanshell

content:

Preface Beanshell Introduction JDM Introduction The Target Formula Management System of the Formula Management System When Applying Beanshell Better Understanding the System Conclusion Reference About the author

There is also a Java area:

Teaching tools and product code and components all articles practical skills

Building a powerful, flexible formula management system using Java script

Yang Tiejun (Yattie@163.com or helloyattie@hotmail.com) Java technology enthusiast, system designer July 2003

In many large applications, such as SCM (Supply Chain Management), CRM (Customer Relationship Management), and ERP (Enterprise Resource Plan), etc., users often have to change some parameter values ​​based on their own needs. The results calculated in accordance with a fixed formula are in accordance with the current situation. If the discount rate of commodity prices in different periods needs to be adjusted according to the actual situation, or the percentage of staff of the staff should be determined according to the company's performance. This requires a powerful formula management mechanism to flexibly adjust some parameters. This article uses Beanshell (a Java interpreter) to achieve such a formula management system. From this system, we can learn that Beanshell brings us a flexible Java script mechanism; and we can customize your own formula management system based on the system.

The demand for foreword customers is constantly changing. Although they said that their company's staff bonuses should be calculated according to that formula, but they will tell you that this formula is not very reasonable, but also need to add some parameters. You may say that this is no problem, we can change the program. But when such changes are not a one, but frequent, when you appear, you may think that there is a formula management system to do these trivial but important changes. Yes, it has been done in many systems. Here, a simple, easy-to-expand formula management system will be introduced, which uses a simple and flexible Beanshell script mechanism and is implemented in conjunction with JDOM technology. Before reading this article, you need to know what Beanshell and JDOM. Introduction to Beanshell Beanshell is a Java interpreter that is included in the scripting language. It is compatible with Java language, has a small size, simple, compliant with a Java style. This article does not introduce the grammatical and usage of Beanshell, but based on Beanshell scripts implement a formula manager to illustrate the powerful scripting function of Beanshell, simplifying the programmers of Java programmers, making them more in-depth understanding of technology will make build The system is more flexible. You can read the reference materials to learn more about Beanshell's knowledge. Similar Java scripts technology also have Dynamicj, etc., you want to know more about them in detail, please refer to the references later. (Note: The Java script here is not javascript.) JDM Introduction JDOM makes it easier to use the Java action XML file. Here, use the XML file format to store the user's custom formula library, which is simple and easy to manage. With JDOM technology, you can easily and quickly complete this task. If you want to learn more about JDOM, please check the references later in the article. The main goal of the target formula management system implementation of the formula management system is that the user can customize the formula according to its own needs, including the addition, modification, and delete the parameters contained in the formula; provide the interface to make the user or other system can utilize the formula library The formula is calculated. From the main functions of the above system, you can know that the system mainly contains two use cases: custom formulas and calculation expressions. Custom formula is the user pre-defined parameters (including parameter names, parameter types, etc.), and then combines these parameters to combine the required formula in accordance with certain rules. After the formula is defined, it will be saved to the formula library, which is called when the user or other system is calculated later. This is the system feature of the manager to flexibly changing the parameters contained in the formula or formula according to its own needs; the calculation expression is assigned to the corresponding parameter, and then specifies the formula to follow to calculate the calculation. This is the external interface provided to the user. In general, users only need to provide the formula ID and related parameter values ​​to follow, and can call the interface for calculation. The example of this system is as follows: Figure 1. The design of the formula management system is designated by the example of the example formula management system, which is the first step to do to find the core class of the system. First, we need a formula manager, which is responsible for converting the user-defined formula into system format and saves to the formula library. It provides an external interface for external needs to be used according to the formula; we also need a calculation Calculator, its work is calculated. It contains all the types of operations, such as plus, minus, multiply or division, of course, can also include large (small), equal to other operations, can be expanded as needed; Finally, we also need a formula (Formula). As the name suggests, the formula object represents a formula.

The Formula Manager is a manager, which is to accept the user's custom formula, but also provide an interface to the external system, call the corresponding formula and calculate with a calculator. The core class of the system is shown below. Figure 2. The core class is clear, such a class design is conducive to Beanshell's implementation (the author has always believed that at least one language expert is included in the designer, so that it can be more flexible, simpler, to the coding phase) . So, which part of Beanshell is the most appropriate? When to apply Beanshell answers, it is necessary to determine according to actual conditions. You may say this is nonsense, but this is the truth of the truth. Generally, when a portion of the system varies, and needs to call some methods and parameters according to the actual situation, then you can try to implement Beanshell into your system. If you find that the problem is simple and the flexibility is greatly improved, then Beanshell is worth trying to try; the opposite, if you find that the problem is not just simplified, even more complicated, then you have to consider whether your method is Suitable, or find some Beanshell application experts to communicate. Note that we are not pursuing new technologies, but pursue a new way to improve our production efficiency. So, how do we apply Beanshell technology in this formula management system? From the above analysis, we know that users need to save custom formulas to be called when the external system is calculated. Imagine that when the external system calls the interface, it should be at least two parameters: one is the formula ID that needs to be followed; the other is a HashTable object, store the "Parameter Name-Parameter Value" list. After the FormulaParser class receives these parameters, load the specified formula from the formula library according to the formula ID, and then assign the parameter value to the corresponding parameters contained in the formula, and finally calculate the calculation according to the defined formula expression. Before there is no Beanshell, we generally complete custom formulas to the transformation of the system format format, and the parameter assignment, expression evaluation, etc. The level is very many times. The author has seen a formula parser for 1000 lines of code, and the function is simple. The reason is that most of its code is in parsing of characters. So, you should catch the problem. Let's try to complete these parsing works. First you have to know the syntax of the Beanshell script, and you should be familiar with several examples in its document. Then we will focus on the place of Beanshell app - External interface Caculatebyformula (Formula Formula, HashTable Parameters). The two parameters, formula is the formula for computing, Parameters is the "Parameter-Parameter Value" list, and its return value is the result of the calculation. Assuming that we have defined two formulas in the formula library, the price formula and staff bonus calculation formula after commercial discount. The formula library XML file is as follows: Listing 1.