Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to understand the MVEL used in the project

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article introduces you how to understand the MVEL used in the project, the content is very detailed, interested friends can refer to, hope to be helpful to you.

What is 1.MVEL? What can it do?

Simply put, it is a powerful expression parser. We can write some expressions ourselves, give them to mvel for parsing, and get the value of this expression. Play with the concept, I don't understand.

Let me give you an example. ?

For example, we are going to do an addition operation. In java, we write:

Int res = 1: 1; / / 2

If I use mvel, I will write:

Object res = MVEL.eval ("1x 1"); / / 2

Are you surprised? So if you want to calculate 1-1. You can directly pass on different expressions. Now it is time to calculate'(2'2) * 3'5 pound 2' or'2 > 1'1'1 purl 2'. Come on, why don't you hard-code these calculations? Is it necessary to write a few more lines of code, and it is inconvenient to expand.

You think that's all mvel can do? That would be too young. At present, mvel supports a large number of syntax, conditions, loops, and so on. Custom functions can also be supported, which means Yes. So what are we doing with this thing at work?

two。 Using ① in Custom data flow what is data flow

Data flow is the conversion of data between different objects. For example, an object data is transformed into b object data through some rules. Ca, is this about data cleaning? Yes, that's true, but data cleaning is just one of the specific items. Let's take a picture:

You can see from the figure that the two objects name and age are one-to-one mappings, but the target object does not need a sex field, but there is an extra field for the year of birth, and it is calculated by age. Let's simulate the conversion process with code, where my objects are defined by map.

HashMap srcMap = Maps.newHashMap (); srcMap.put ("name", "zs"); srcMap.put ("age", 10); srcMap.put ("sex", "female"); / / Field mapping HashMap mapping = Maps.newHashMap (); mapping.put ("name", "name"); mapping.put ("age", "age"); / / write the current year as 2019mapping.put ("birthYear", "2019-age"); / / Target object HashMap targetMap = Maps.newHashMap () / / k is the target table field, v is the transformation rule mapping.forEach ((kscorev)-> {Object reValue = MVEL.eval (vRecience srcMap); targetMap.put (krecoery revalue);}); System.out.println ("source object" + srcMap); / / source object {sex= female, name=zs, age=10} System.out.println ("target object" + targetMap); / / target object {birthYear=2009, name=zs, age=10}

Yes, it's that simple, but the current year in our calculation of the year of birth is dead. Details are not what we want. it's all right. let's take our time.

. Custom function

Define a function to get the current year

/ * the method to get the current year * @ return * / public static Object getCurrentYear () {Calendar date = Calendar.getInstance (); String year = String.valueOf (date.get (Calendar.YEAR)); return year;}

. Register custom functions

Go directly to the code

Static ParserContext context = new ParserContext (); static {/ / MvelTest is the class of getCurrentYear function Method [] declaredMethods = MvelTest.class.getDeclaredMethods (); for (Method method: declaredMethods) {context.addImport (method.getName (), method);}}

. Use

Directly replace Object reValue = MVEL.eval (vJournal srcMap) with

Object reValue = MVEL.execute_Expression (MVEL.compile_Expression (v, context), srcMap)

That's it. The function of compileExpression is to compile our rules into a process that mvel can recognize.

Replace the birthYear rule with mapping.put ("birthYear", "getCurrentYear ()-age"); execute to get the same result.

With these, we can customize more conversion rules, and we can also develop a set of user configuration tools to map resources according to the user's own configuration. Get the target data you want.

On how to understand the MVEL used in the project to share here, I hope the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report