SoFunction
Updated on 2025-03-08

Application example of ExpressionUtil tool class

Application example of ExpressionUtil tool class

Updated: April 2, 2024 10:11:04 Author: Flying_Fish_roe
This article mainly introduces the application examples of ExpressionUtil tool class. There are many commonly used tool classes. This is one of them. Understanding the basic API can help us develop better. The article introduces the code in detail. Friends who need it can refer to it.

What is ExpressionUtil

ExpressionUtil is a tool class that handles expression-related operations. It provides some methods to facilitate users to process expression-related calculations, comparisons, conversions and other operations in the program. For example, you can use ExpressionUtil to calculate the result of a mathematical expression, compare whether the values ​​of two expressions are equal, convert a string expression into executable code, etc. ExpressionUtil simplifies the development process and improves the readability and maintainability of the code.

What can be done

ExpressionUtil is an expression processing tool class that can be used to parse, calculate and convert mathematical expressions.

  • Analytical expressions: Mathematical expression strings can be parsed into expression objects to facilitate subsequent calculations and operations.

  • Calculate expression: You can calculate the expression and get the result. Supports basic mathematical operators (addition, subtraction, multiplication and division) and functions (such as sin, cos, sqrt, etc.).

  • Expression conversion: You can convert an expression object to a string form, or convert an expression in a string form to an expression object. This makes it easy to use expressions in different scenarios.

  • Variable replacement: You can replace the variable in the expression to a specific numerical value, thereby realizing the calculation of expressions with variables.

  • Error handling: It can detect and handle illegal expressions, such as bracket mismatch, operator errors, etc.

What APIs are there

  • evaluate(String expression): evaluates the specified expression and returns the result.

  • evaluate(Expression expression): evaluates the passed expression object and returns the result.

  • parse(String expression): parse the specified string expression into an expression object.

  • isValid(String expression): Checks whether the specified expression is legal.

  • simplify(Expression expression): simplifies the process of the incoming expression object to minimize the complexity of the expression.

  • differentiate(Expression expression, String variable): performs differential calculations on the passed expression object to obtain the derivative of the specified variable.

  • integrate(Expression expression, String variable): performs integral calculation on the passed expression object to obtain the original function about the specified variable.

  • substitute(Expression expression, Map<String, Double> variables): Replace the variable in the passed expression object with the specified value and return the new expression object.

Example

Dependencies that need to be introduced

<dependency>
    <groupId></groupId>
    <artifactId>mvel2</artifactId>
    <version>2.4.</version>
</dependency>

Use the ExpressionUtil tool class to evaluate expressions:

  • Use multiple variables:
String ruleExpress = "age &gt; 18 &amp;&amp; gender == 'male' &amp;&amp; height &gt; 160";
Map&lt;String, Object&gt; map = new HashMap&lt;String, Object&gt;();
("age", 19);
("gender", "male");
("height", 170);
Boolean eval = (Boolean) (ruleExpress, map);
(eval); // Output: true
  • Comparison with strings
String ruleExpress = "('John') &amp;&amp; ('New York')";
Map&lt;String, Object&gt; map = new HashMap&lt;String, Object&gt;();
("name", "John");
("city", "new york");
Boolean eval = (Boolean) (ruleExpress, map);
(eval); // Output: true
  • Use the computed expression:
String ruleExpress = "score * weight &gt;= 80";
Map&lt;String, Object&gt; map = new HashMap&lt;String, Object&gt;();
("score", 90);
("weight", 0.9);
Boolean eval = (Boolean) (ruleExpress, map);
(eval); // Output: true

Summarize

  • Expression parsing:

    • parse(String expression): parses a string expression into oneExpressionObject. This method parses the expression based on the syntax rules and returns an object representing the expression.
  • Expression calculation:

    • evaluate(Expression expression): Calculate the given expression object and return the calculation result. This method will perform corresponding calculation operations based on the type of expression and operators, and return the calculation result.
    • evaluate(String expression): parses a string expression into oneExpressionObject, and calculate it, and return the calculation result.
  • Expression evaluation:

    • evaluateBoolean(Expression expression): Evaluate the given expression object and return an evaluation result of a Boolean type. This method converts the expression to a Boolean value based on the evaluation result, and returns the evaluation result.
    • evaluateBoolean(String expression): parses a string expression into oneExpressionObject, evaluate it, and return a Boolean type evaluation result.
  • Expression optimization:

    • optimize(Expression expression): Optimize the given expression object and return the optimized expression object. This method will optimize operations based on the structure and logic of the expression to improve the computational efficiency and performance of the expression.
  • Expression conversion:

    • convertToString(Expression expression): Convert the given expression object to a string expression and return the string result. This method converts the expression object into the corresponding string expression based on the type and structure of the expression object and returns the string result.
  • Expression verification:

    • validate(Expression expression): Verify the given expression object and check whether it complies with syntax rules and logical requirements. This method performs corresponding verification operations based on the type and operator of the expression and returns the verification result.

This is the end of this article about the ExpressionUtil tool application. For more related ExpressionUtil application content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!

  • expressionutil
  • Tools

Related Articles

  • Summary of attribute variables in Maven pom file

    This article mainly introduces a summary of attribute variables in the Maven pom file, which is of good reference value and hopes it will be helpful to everyone. If there are any mistakes or no complete considerations, I hope you will be very grateful for your advice
    2021-12-12
  • Java to learn about Collection single-column collection Set in an easy-to-understand way

    The Collection collection class is the root interface of a single column collection class, which is used to store a series of elements that comply with certain rules. List interface and Set interface are subinterfaces of Collection collection class. Among them, the commonly used implementation classes of List interface include ArrayList class, Vector class and LinkedList class; the commonly used implementation classes of Set interface include HashSet class and TreeSet class.
    2021-11-11
  • SpringBoot+Vue project deployment implements traditional methods

    When we are distributing front-end and back-end, we usually deploy the front-end project to nginx server and deploy it separately from the back-end project. This article mainly introduces relevant information about the traditional method of SpringBoot+Vue project deployment. Friends who need it can refer to it.
    2024-01-01
  • Summary of dynamic SQL query based on mybatis

    This article mainly introduces the summary of mybatis dynamic SQL query, which is of good reference value and hopes to be helpful to everyone. If there are any mistakes or no complete considerations, I hope you will be very grateful for your advice
    2021-07-07
  • Super easy to use lightweight JSON processing command jq (latest recommendation)

    jq is a lightweight command line tool that allows you to process JSON data very conveniently, such as segmentation, filtering, mapping, conversion, etc., just like the three musketeers of sed, awk, and grep text processing. This article mainly introduces the super easy-to-use lightweight JSON processing command jq. Friends who need it can refer to it.
    2023-01-01
  • Solve the problem that Lombok annotations do not work

    This article mainly introduces the problem of solving the problem that Lombok annotations do not work. It has good reference value and hopes it will be helpful to everyone. If there are any mistakes or no complete considerations, I hope you will be very grateful for your advice
    2021-08-08
  • Java automatically decompresses file instance code

    Java automatically decompresses file instance code, friends who need it can refer to it
    2013-04-04
  • Java development using OGEngine 2048

    As we all know, OGEngine is a game engine for Chinese people to improve AndEngine's improved Java programming. In addition to supporting the useless function of 3D games, AndEngine has OGEngine, and the multi-touch function that AndEngine lacks has also been improved by Chinese people. Today we will try to use OGEngine to create popular games 2048.
    2015-03-03
  • Java springboot Mongodb add, delete, modify and search code examples

    This article mainly introduces Java springboot Mongodb code addition, deletion, modification and search examples. The example code is introduced in this article in detail, which has certain reference value for everyone's learning or work. Friends who need it can refer to it.
    2020-07-07
  • Java network programming and multi-threading many-to-many chat system

    This article mainly introduces many-to-many chat system based on Java network programming and multi-threading. There are very detailed code examples in the article, which are very helpful to friends who are learning Java network programming. Friends who need it can refer to it.
    2021-04-04

Latest Comments