ofbiz学习笔记--Tips & Tricks while working with Groovy

本文对比了Beanshell与Groovy的语法特点,包括文件命名规范、空值检查、列表及映射操作等,并列举了Groovy在OFBiz框架中的关键应用文件。
Groovy Goodies
1.Start the name of groovy file from capital letter and then follow the camel case pattern.For example "PendingCommunications.groovy".
The reason behind is shown below (Comments from Joe on Developer Mailing List) :-
•The main reason for this is that when a script is run without a class declaration, the filename is used to create the class name and you can experience problems with untyped variables. For example, a script named product.groovybecomes class "product", and if the script contains an untyped variable "product", it assumes you're trying to access the class "product" instead of a new variable "product".
•A secondary reason would be consistency, as some scripts have already been named this way. (EditProductFeatures.groovy, etc.
2.Primary Key Definition
Beanshell :-
-productAssoc = delegator.findByPrimaryKey("ProductAssoc", UtilMisc.toMap("productId", productId, "productIdTo", productIdTo, "productAssocTypeId", productAssocTypeId, "fromDate", fromDate));
Groovy :-
+productAssoc = delegator.findByPrimaryKey("ProductAssoc", ['productId' : productId, 'productIdTo' : productIdTo, 'productAssocTypeId' : productAssocTypeId, 'fromDate' : fromDate]);
Note :- Removed the usage of UtilMis.toMap(). Instead of findByPrimaryKey try to use findOne().
3.Null check
Beanshell :-
- if (payment == null) continue;
Groovy :-
+ if (!payment) continue;
4.Null + Size greater then Zero Check
Beanshell :-
- if (glAccounts != null && glAccounts.size() > 0) {
Groovy :-
+ if (glAccounts) {
5.One line assignment
Beanshell :-
-nowDate = UtilDateTime.nowDate();
-context.put("nowDate", nowDate);
Groovy :-
+context.nowDate = UtilDateTime.nowDate();
Note :- Sentence can be kept in single line with the usage of DOT (.) for putting some values in context.
6.Beanshell :-
- String nowTimestampString = UtilDateTime.nowTimestamp().toString();
Groovy :-
+ context.nowTimestampString = UtilDateTime.nowTimestamp().toString();
Note :- No need to specify the object type.
7.No need to specify semicolon
Beanshell :-
-import org.ofbiz.product.inventory.InventoryWorker;
Groovy :-
+import org.ofbiz.product.inventory.InventoryWorker
Note :- We can remove the semicolon in groovy import syntax. Always import the files that are being used in Groovy files instead of importing all the files from the package.

8.Beanshell :-
List allTypes = new LinkedList();
- i = invoiceItemTypes.iterator();
- while ( i ) {
- GenericValue invoiceItemType = i.next();
Groovy :-
+invoiceItemTypes.each {
+ GenericValue invoiceItemType = it;
or
+invoiceItemTypes.each { invoiceItemType ->
9.Beanshell :-
- invoiceAppls = delegator.findByAnd("PaymentApplication", UtilMisc.toMap("invoiceId", invoiceId, "invoiceItemSeqId", null));
Groovy :-
+ invoiceAppls = delegator.findByAnd("PaymentApplication", [invoiceId : invoiceId, invoiceItemSeqId : null]);
Note :- Instead of findByAnd() use findList().
10.Empty Map example
Beanshell :-
product = new HashMap(); // Empty map
Groovy :-
product = [:] ;
11.Empty List example
Beanshell :-
products = new ArrayList(); // Empty list
Groovy :-
products = [] ;

Important Note
This is pretty cool, groovy coerces objects into booleans:
An empty string,list,map = false otherwise true
An iterator with no more elements = false otherwise true
null = false
12.Beanshell :-
-while (iter.hasNext()) {
Groovy :-
+while (iter) {
Note :- Another alternate of while statement is the usage of "each" on the list values.
13.List example
Beanshell :-
- if (UtilValidate.isNotEmpty(invoiceItemTypeOrgs)) {
Groovy :-
+ if (invoiceItemTypeOrgs) {
Note :- Not empty list returns true.
14.Map Example.
Beanshell :-
- if (UtilValidate.isNotEmpty(invoiceItemTypeOrgs)) {
Groovy :-
+ if (invoiceItemTypeOrgs) {
Note :- Not empty Map returns true.
15.String Example
Beanshell :-
-if (paymentId != null) {
Groovy :-
+if (paymentId) {
Note :- Not empty String returns true.
16.Beanshell :-
invoiceId = parameters.get("invoiceId");
Groovy :-
invoiceId = parameters.invoiceId;
Note :- The value coming from parameters map should be considered as String.Other types should be explicitly specified.
17.Elvis Operator :- If any string return empty or null value then we can put Default value with the help of Elvis Operator ( ?: ) .Its short form of Java Ternary Operator.
Beanshell :-
- invoiceType = parameters.get("invoiceTypeId");
- if (invoiceType == null) invoiceType = "ANY";
Groovy :-
- invoiceType = parameters.invoiceTypeId ?: "ANY" ;
18.Some important files that are responsible for Groovy handling in OFBiz.
a) GroovyUtil.java
b) GroovyServiceTest.groovy
c) GroovyEngine.java
d) ModelFormAction.java
e) Some *.jar files that are responsible to run the Groovy scripts are shown below.
ofbiz/trunk/framework/base/lib/scripting/antlr-2.7.6.jar (with props)
ofbiz/trunk/framework/base/lib/scripting/asm-2.2.jar (with props)
ofbiz/trunk/framework/base/lib/scripting/asm-analysis-2.2.jar (with props)
ofbiz/trunk/framework/base/lib/scripting/asm-tree-2.2.jar (with props)
ofbiz/trunk/framework/base/lib/scripting/asm-util-2.2.jar (with props)
ofbiz/trunk/framework/base/lib/scripting/groovy-1.5.6.jar (with props)
【评估多目标跟踪方法】9个高度敏捷目标在编队中的轨迹和测量研究(Matlab代码实现)内容概要:本文围绕“评估多目标跟踪方法”,重点研究9个高度敏捷目标在编队飞行中的轨迹生成与测量过程,并提供完整的Matlab代码实现。文中详细模拟了目标的动态行为、运动约束及编队结构,通过仿真获取目标的状态信息与观测数据,用于验证和比较不同多目标跟踪算法的性能。研究内容涵盖轨迹建模、噪声处理、传感器测量模拟以及数据可视化等关键技术环节,旨在为雷达、无人机编队、自动驾驶等领域的多目标跟踪系统提供可复现的测试基准。; 适合人群:具备一定Matlab编程基础,从事控制工程、自动化、航空航天、智能交通或人工智能等相关领域的研究生、科研人员及工程技术人员。; 使用场景及目标:①用于多目标跟踪算法(如卡尔曼滤波、粒子滤波、GM-CPHD等)的性能评估与对比实验;②作为无人机编队、空中交通监控等应用场景下的轨迹仿真与传感器数据分析的教学与研究平台;③支持对高度机动目标在复杂编队下的可观测性与跟踪精度进行深入分析。; 阅读建议:建议读者结合提供的Matlab代码进行实践操作,重点关注轨迹生成逻辑与测量模型构建部分,可通过修改目标数量、运动参数或噪声水平来拓展实验场景,进一步提升对多目标跟踪系统设计与评估的理解。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值