[JAVA] [Introduction to MVEL] Aiming to be the best MVELer in the world

Note: We cannot guarantee the accuracy of the content. Please note that this is written with the intention of alleviating the psychological anxiety of MVEL beginners who feel lonely due to lack of information in Japanese.

What kind of language is MVEL?

In short, Java is a scripting language. It's Java, not JavaScript. I dared to say it because it is important.

I think it's a fairly minor language, but it's actually open source. For the time being, it seems that issues are raised regularly and maintenance is also being done.

Basic syntax

greet = "Hello "; //String type
name = "MVEL"; //String type
return greet + name; //result:"Hello MVEL"

--The line delimiter is a semicolon (;) --Dynamic typing eliminates the need to declare variable data types

MVEL has the property of returning the last evaluated value as a return value. So, return can be omitted, but I think it's kinder to add it for readability.

operator

Most of the following common operators can be used.

--Comparison operators (==,! =,>,> =, <, <=) --Arithmetic operators (+,-, \ *, /,%, + =,-=, * =, / =,% =) -Logical operator (&&、||)

Others include contains, is instanceof, ~ =.

//Determine if the character string contains "n"
"Hello" contains "Hmm" //Result: true

//Judgment whether it is a numerical value
value = "100" //String type
value is instanceof Integer //Result: false

//Judge whether it is half-width alphanumeric characters
"Hello" ~= '^[A-Za-z0-9]+$' //Result: true
operator Description
contains True if the value on the left side contains the value on the right side
is instanceof True if the data type of the variable on the left side is the data type on the right side
˜= True if the value on the left side matches the regular expression on the right side

About empty value (null literal)

hoge == null;
hoge == nil; //Synonymous with null

MVEL treats null and nil as synonymous as null values. You can use either one, but it may be safer to use null for Java.

Conditional branch

//Change usage fee depending on age
if (age >= 60) {
  System.out.println("Senior discount rate: 1,200 yen");
}
else if (age >= 20) {
  System.out.println("General charge: 1,800 yen");
}
else {
  System.out.println("Student discount fee: 1,000 Yen");
}

When the conditional expression is true, the processing inside {} is executed. If you want to set multiple conditions, connect them with else if ().

Although it is a small detail, the else statement is written on the next lower line with a line break according to the way of writing the official document.

age >= 15 ? "Yes" : "No";

In this way, conditional branching with the ternary operator is also possible.

Iterative processing

For iterative processing, in addition to the basic loop syntax (for, while, until, do while, do until), you can use foreach, which is similar to the extended for statement added in Java 1.5.

//FizzBuzz problem
for (i = 0; i < 100; i++) {
  if (i % 15 == 0) {
    System.out.println("FizzBuzz");
  }
  else if (i % 3 == 0) {
    System.out.println("Fizz");
  }
  else if (i % 5 == 0) {
    System.out.println("Buzz");
  }
}

Like i ++, increment can be used without any problem.

//Extract the elements of the list fruits one by one and output them to the console
fruits = ["Apple", "Grape", "pineapple"];
count = 0;
foreach (item : fruits) {
  count++;
  System.out.println(count + ". " + item);
}

If you want to display the index with foreach, it seems that you have to prepare the variable for the index yourself.

Function definition

//Output a character string to the console (when the output destination is the standard setting)
def hello() {
  System.out.println("Hello, world"); //Result: Hello, world
}

//Calculates the area of a triangle and returns a message
function calcTriangleArea(bottom, height) {
  area = bottom * height / 2; //Int type
  message = "The bottom is" + bottom + "The height is cm" + height + "The area of the cm triangle is" + area + "It is ㎠."; //String type (variable is automatically Int type=>Convert to String type)
  return message;
}

You can use def and function for MVEL function definition. There is no difference between the two, but since def is used in the official document, let's unify it to def.

As an aside, when I first saw the MVEL source, I misunderstood it as Ruby.

Practical tips

Let's deal with dates

Use Java libraries to handle dates in MVEL. There are Date and Calendar in the date library, but this time we will use Calendar because we want to perform date and time calculation processing.

import java.util.Calendar; //Import Calendar class

//Create a calendar instance
Calendar calendar = Calendar.getInstance();
today = calendar.getTime(); //Get the current date and time

calendar.set(2020, 4, 23, 23, 59, 59); //Set sale end date (April 23, 2020 23:59:59)
endOfSaleDay = calendar.getTime();

if (today >= endOfSaleDay) {
  System.out.println("20%OFF sale has ended.");
}

The above is the process of determining whether the discount sale has ended. Since the sale end date setting is a destructive change, if you then getTime (), it will be the current date and time at the set date and time.

Definition of MVELer

What is MVELer in the first place! I would like to define it as the responsibility of the statement. That said, it feels like PHPer for PHP users and Rubyist for Ruby users, but strictly speaking, it refers to MVEL users who meet the following conditions.

-~~ Those who are fully aware of the range of influence ~~ ―― ~~ Those who can actively communicate so as not to cause degreasing ~~ --Those who love and enjoy the language MVEL

MVEL users who meet these requirements should be prepared to be ** MVEler ** and be proud to call themselves MVELER.

Summary

--You can use most of the methods and libraries in Java ――The more you use it, the better it becomes.

reference

Recommended Posts

[Introduction to MVEL] Aiming to be the best MVELer in the world
Understand the characteristics of Scala in 5 minutes (Introduction to Scala)
I want to set the conditions to be displayed in collection_check_boxes
Introduction to Rakefile that can be done in about 10 minutes
[Rails] Where to be careful in the description of validation
Determine if the strings to be compared are the same in Java
About the language to be learned
Pass the i18n locale to JavaScript
Introduction to algorithms in java-cumulative sum
Best Gaming Products To Buy In 2021
Reason to add L to the number to be put in Java long type
I thought about the best way to create a ValueObject in Ruby
Introduction to Slay the Spire Mod Development (1) Introduction
Introduction to java for the first time # 2
Hello, World! In the bootstrap loader area
How to get the date in java
Introduction to Ratpack (3) --hello world detailed explanation
Output of the book "Introduction to Java"
Just skip Masakari to "programming languages that will be needed in the future"
Summarize the life cycle of Java objects to be aware of in Android development