A solution to implement user-defined fields based on Jep and configurable formulas

Source: Internet
Author: User
Tags file size mathematical functions

Background

With the popularization and development of IT technology, the user's information level is more and more high, besides satisfying the basic needs of the users, the software products must take more and more care of the personalized needs of the users and provide the users with a deep and personalized service. As an example of a product that contains a report presentation, the default renders to all users exactly the same report, that is, the field contents and labels for the same report are exactly the same for all users. In practice, we often encounter different users because of their different business requirements, for the same report, in addition to the basic data fields, but also require additional to meet the user-specific business meaning of the field, we call the user custom fields (custom Metric). Such requirements are more common in financial statements and data analysis reports. For user-defined fields, the calculation formula for different users given different calculation formulas, even for the same field of the same user, may change over time. An intuitive way is to store all the fields that are likely to be used by all users, and then implement different fields for different users, which not only wastes the storage space, but also makes the maintenance cost very expensive later. This article will introduce a solution based on Jep and configurable formulas, which can flexibly and quickly resolve the needs of users without adding additional storage space, and has good maintainability and scalability.

Jep Introduction

Considering that many people are still unfamiliar with Jep, before introducing the whole implementation plan, it is necessary to let you have a preliminary understanding of Jep.

Jep (Java Math Expression Parser) is a Third-party Java toolkit that provides a class library for parsing and calculating mathematical expressions, and its core function is to compute the parsing of formulas and the calculation of results. Prior to the JEP2.4.1 version of the open source free package to meet the GPLV3 agreement, you can download and use it on the SourceForge website. Using the API provided by Jep, you can calculate the results instantly based on the formula given by the user. Jep supports user-defined variables, constants, and functions. In Jep, a large number of commonly used general mathematical functions and constants have been included in advance to meet most of the day-to-day mathematical computing needs. Its official website is http://www.singularsys.com/jep/, you can download the trial version and related documents on the website.

Jep has the following characteristics:

Small file (jar archive file size below 300k)

Quick Value Evaluation

High precision, using BigDecimal in calculation

Contains common mathematical functions and operators

Supports Boolean expressions

Good scalability and Scalability

Supports strings, vectors, and complex values

Support for implicit multiplication

Allow declared or undeclared variable

Compatible JAVA1.5

Support for Unicode characters

A large number of development documents for reference

Contains the JAVACC parsing builder to automatically generate main class

Jep the calculation of an expression is divided into two steps. Jep first parses the expression, and after parsing it generates a tree structure that is then evaluated quickly based on the tree structure. Its work flow chart is as follows:

Figure 1. Jep Work Flow chart

As can be seen from the above picture, Jep's working process is very simple. Here is a simple example to further explain, let you have a more intuitive understanding of Jep.

Listing 1. Jep Simple Example

Jep Jep = new Jep (); 
   try { 
       int x = ten; 
      1. Sets the value of the variable
      jep.addvariable ("x", x); 
      2. Loading and parsing formula
      Jep.parse ("x+1"); 
      3. Computed result
      = Jep.evaluate (); 
      4. The output displays
      System.out.println ("x + 1 = + result +" (When x= "+x+"));  catch (Exception e) { 
      System.out.println ("An error occured:" + e.getmessage ()); 
  } 
Output Result: X + 1 = 11.0 (when x=10)

After the introduction, presumably you have a preliminary understanding of Jep, then you can begin to enter the topic of this 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.