Ngôn ngữ lập trình Groovy – phần 1

Tram Ho

Groovy là ngôn ngữ lập trình chạy theo mô hình lập trình chức năng (functional programming) thực thi các câu lệnh trên JVM (Java Virtual Machine – máy ảo Java). Để tự động hóa các nghiệp vụ kinh doanh trong phần mềm ERP, như trong Apache Ofbiz, có rất nhiều đoạn chương trình viết bằng *.groovy để thực hiện các nghiệp vụ một cách tự động theo nhiều bước. Cú pháp viết file build.gradle là sử dụng cú pháp của Apache Groovy.

Môi trường phát triển

  • IntelliJ IDEA 2020.2.3 (Ultimate Edition)
  • Windows 10 x64 version 2004 – Enterprise edition
  • Groovy 3.0.6
  • JDK 1.8

Ví dụ: File ofbiz-frameworkapplicationsordergroovyScriptsallocationplanCreateAllocationPlan.groovy (Apache Ofbiz version 17.12.04, bản quyền Apache Software Foundation)

Đoạn code trên tạo kế hoạch phan bổ dựa trên Mã sản phẩm, Mã trạng thái, Mã kế hoạch; phân bổ đơn hàng vào các kênh bán hàng với số lượng cụ thể. Liên kết tải về https://dl.bintray.com/groovy/maven/apache-groovy-sdk-3.0.6.zip (71,2 MB), giải nén ra (thành khoảng 242 MB). Source code: https://github.com/apache/groovy Groovy 3.0.6 yêu cầu phải có JDK phiên bản từ 1.8 trở lên. Trong thư mục sau giải nén, tìm thư mục bin , giả sử D:toolsgroovy-3.0.6bin , khai báo đường dẫn này trong biến môi trường của Windows.

Chạy lệnh kiểm tra phiên bản của Groovy

Kết quả là

Khai báo Groovy SDK trong IntelliJ IDEA 2020

Viết chương trình Groovy đầu tiên, file Hello.groovy

Kết quả:

Xin chúc mừng, vậy là bạn đã viết chương trình Groovy đầu tiên.

Các từ khóa (keyword) trong Groovy là as, assert, break, case, catch, class, const, continue, def, default, do, else, enum, extends, false, finally, for, goto, if, implements, import, in, instanceof, interface, new, null, package, return, super, switch, this, throws, throw, trait, true, try, var, while .

Các định danh hợp lệ và không hợp lệ

Kiểu dữ liệu map

Quy định đặt tên định danh: Bắt đầu bởi chữ cái, ký hiệu dollar sign, hoặc dấu gạch dưới. Không được phép bắt đầu bởi số.

Chữ cái là các ký tự nằm trong khoảng

  • từ a đến z (ký tự ASCII viết thường)
  • từ A đến Z (ký tự ASCIII viết hoa)
  • từ u00c0 đến u00d6
  • từ u00d8 đến u00f6
  • từ u00f8 đến u00ff
  • từ u0100 đến ufffe

Một số định danh đặc biệt

Cách sử dụng map

File Hello.groovy có nội dung

Lưu ý với cách đặt tên key trong kiểu dữ liệu map, những thứ dưới đây là cho phép

Chạy lệnh

Kết quả là

Cái này gọi là Groovy GString, khả năng nội suy xâu ký tự của Groovy. Javadoc https://docs.groovy-lang.org/latest/html/api/groovy/lang/GString.html

Các ký tự đặc biệt cho phép trong string là

  • b
  • f
  • n
  • r
  • s
  • \
  • '
  • "

Ví dụ liên quan đến GString

Ví dụ liên quan đến eager GString và lazy GString

  • Mục 1. Định ngĩa biến number chứa giá trị 1 ở đó chúng ta nội suy bên trong 2 GString, như là một biểu thức trong eagerGString và một closure trong lazyGString .
  • Mục 2. Chúng ta kỳ vọng rằng xâu kết quả chứa cùng giá trị là 1 cho eagerGString
  • Mục 3. Chúng ta kỳ vọng rằng xâu kết quả chứa cùng giá trị là 1 cho lazyGString
  • Mục 4. Sau đó, chúng ta thay đổi giá trị của biến thành một giá trị khác.
  • Mục 5. Với biểu thức nội suy thuần, giá trị thực sự ràng buộc vào thời điểm khởi tạo GString.
  • Mục 6. Nhưng với biểu thức closure, closure được gọi dựa trên mỗi lần ép giá trị của GSTring vào String, kết quả là một chuỗi được cập nhật chưuá giá trị số mới.

Một số dạng đặc biệt của string

itemList = []
isPlanAlreadyExists = false
productId = parameters.productId
planName = parameters.planName

if (productId) {
orderedQuantityTotal = 0.0
orderedValueTotal = 0.0
reservedQuantityTotal = 0.0

ecl = EntityCondition.makeCondition([
EntityCondition.makeCondition(“productId”, EntityOperator.EQUALS, productId),
EntityCondition.makeCondition(“statusId”, EntityOperator.IN, [“ALLOC_PLAN_CREATED”, “ALLOC_PLAN_APPROVED”]),
EntityCondition.makeCondition(“planTypeId”, EntityOperator.EQUALS, “SALES_ORD_ALLOCATION”)],
EntityOperator.AND)
allocationPlanHeader = from(“AllocationPlanHeader”).where(ecl).queryFirst()
if (allocationPlanHeader == null) {
ecl = EntityCondition.makeCondition([
EntityCondition.makeCondition(“productId”, EntityOperator.EQUALS, productId),
EntityCondition.makeCondition(“orderStatusId”, EntityOperator.EQUALS, “ORDER_APPROVED”),
EntityCondition.makeCondition(“orderTypeId”, EntityOperator.EQUALS, “SALES_ORDER”)],
EntityOperator.AND)
orderAndItemList = from(“OrderHeaderAndItems”).where(ecl).queryList()
orderAndItemList.each { orderAndItem ->
itemMap =
salesChannelEnumId = orderAndItem.salesChannelEnumId
itemMap.salesChannelEnumId = salesChannelEnumId
salesChannel = from(“Enumeration”).where(“enumId”, salesChannelEnumId).queryOne()
if (salesChannel) {
itemMap.salesChannel = salesChannel.description
}

orh = new OrderReadHelper(delegator, orderAndItem.orderId)
placingParty = orh.getPlacingParty()
if (placingParty != null) {
itemMap.partyId = placingParty.partyId
itemMap.partyName = PartyHelper.getPartyName(placingParty)
}

itemMap.orderId = orderAndItem.orderId
itemMap.orderItemSeqId = orderAndItem.orderItemSeqId
itemMap.estimatedShipDate = orderAndItem.estimatedShipDate

unitPrice = orderAndItem.unitPrice
cancelQuantity = orderAndItem.cancelQuantity
quantity = orderAndItem.quantity
if (cancelQuantity != null) {
orderedQuantity = quantity.subtract(cancelQuantity)
} else {
orderedQuantity = quantity
}
orderedValue = orderedQuantity.multiply(unitPrice)
orderedQuantityTotal = orderedQuantityTotal.add(orderedQuantity)
orderedValueTotal = orderedValueTotal.add(orderedValue)
itemMap.orderedQuantity = orderedQuantity
itemMap.orderedValue = orderedValue

// Reserved quantity
reservedQuantity = 0.0
reservations = from(“OrderItemShipGrpInvRes”).where(“orderId”, orderAndItem.orderId, “orderItemSeqId”, orderAndItem.orderItemSeqId).queryList()
reservations.each { reservation ->
if (reservation.quantity) {
reservedQuantity += reservation.quantity
}
}
reservedQuantityTotal = reservedQuantityTotal.add(reservedQuantity)
itemMap.reservedQuantity = reservedQuantity
itemList.add(itemMap)
}
} else {
isPlanAlreadyExists = true
}
allocationPlanInfo.orderedQuantityTotal = orderedQuantityTotal
allocationPlanInfo.orderedValueTotal = orderedValueTotal
allocationPlanInfo.reservedQuantityTotal = reservedQuantityTotal
}
allocationPlanInfo.isPlanAlreadyExists = isPlanAlreadyExists
allocationPlanInfo.itemList = itemList
context.allocationPlanInfo = allocationPlanInfo

The above code creates a attribution plan based on Product Code, Status Code, Plan Code; allocate orders to sales channels with specific quantities. Download link https://dl.bintray.com/groovy/maven/apache-groovy-sdk-3.0.6.zip (71.2 MB), extract it (into about 242 MB). Source code: https://github.com/apache/groovy Groovy 3.0.6 requires JDK version 1.8 or higher. In the following folder unzip, find the bin directory, assuming D:toolsgroovy-3.0.6bin , declare this path in the Windows environment variable.

Run Groovy’s version check command

The result is

Declare the Groovy SDK in IntelliJ IDEA 2020

Write your first Groovy program, file Hello.groovy

Result:

Congratulations, you’ve written your first Groovy program.

The keywords (keyword) in Groovy are as , assert , break , case , catch , class , const , continue , def , default , do , else , enum , extends , false , finally , for , goto , if , implements , import , in , instanceof , interface , new , null , package , return , super , switch , this , throws , throw , trait , true , try , var , while .

Valid and invalid identifiers

Map data type

Identification naming rules: Start with a letter, a dollar sign, or an underscore. Not allowed to start with numbers.

Letters are the characters in the range

  • from a to z (lowercase ASCII characters)
  • from A to Z (uppercase ASCIII characters)
  • from u00c0 to u00d6
  • from u00d8 to u00f6
  • from u00f8 to u00ff
  • from u0100 to ufffe

Some special identifiers

How to use map

Hello.groovy file has content

Note with the naming of keys in map data type, the following is permissible

Run the command

The result is

This is called Groovy GString, Groovy’s ability to interpolate string of characters. Javadoc https://docs.groovy-lang.org/latest/html/api/groovy/lang/GString.html

Special characters allowed in string are

  • b
  • f
  • n
  • r
  • s
  • \
  • '
  • "

Example related to GString

The example involves eager GString and lazy GString

  • Item 1. Define the variable number containing the value 1 where we interpolate inside 2 GString, as an expression in eagerGString and a closure on lazyGString .
  • Item 2. We expect that the resulting string contains the same value 1 for eagerGString
  • Item 3. We expect that the resulting string contains the same value of 1 for lazyGString
  • Section 4. After that, we change the value of the variable to something else.
  • Section 5. For pure interpolation expression, value is actually constrained at GString initialization.
  • Section 6. But for the closure expression, the closure is called based on each force of GSTring’s value into String, the result is an updated string containing a new numeric value.

Some special form of string

Chia sẻ bài viết ngay

Nguồn bài viết : Viblo