今天写了一段drools规则脚本,一直出错,多次调试终于找到原因,是因为drools不能在{}体内是用“//”写注释,如:
/*
计算10:
If CURRENT_DATE in (当年的0101-0430含) then
Corp_quar_date_1=上年最后一天
Corp_quar_date_2=上上年最后一天
*/
int monthDay = monthDay($a.data_date);
log.debug("monthDay = {}", monthDay);
if (monthDay <= 430) {
// If CURRENT_DATE in (当年的0101-0430含) then Corp_quar_date_1=上年最后一天 Corp_quar_date_2=上上年最后一天
$a.corp_quar_date_1 = (Integer.parseInt($a.data_date.substring(0,4)) -1) + "1231";
$a.corp_quar_date_2 = $a.corp_quar_date_1;
} else {
//CURRENT_DATE in (当年的1101-1231含) then Corp_quar_date_1=当年930 Corp_quar_date_2=当年930
$a.corp_quar_date_1 = $a.data_date.substring(0,4) + "0930";
$a.corp_quar_date_2 = $a.corp_quar_date_1;
}
log.debug("$a.corp_quar_date_1 = {}, corp_quar_date_2={}", $a.corp_quar_date_1, $a.corp_quar_date_2);
加载这个规则会一直报错:
log.trace("6) ��˾Ͷ�ʼ��ж� -----");:
[Error: unbalanced braces { ... }]
[Near : {... f (monthDay <= 430) {// If CURRENT_DATE ....}]
^
[Line: 22, Column: 0] : [Rule name='ExposurePre_COMP']
意思是说,花括号“{ }” 不对称。
研究了半天才发现,在花括号“{ }”语句块里面不能使用"//"写注释,改成“/* */” 就好了。修改成下面示例就好了:
/*
计算10:
If CURRENT_DATE in (当年的0101-0430含) then
Corp_quar_date_1=上年最后一天
Corp_quar_date_2=上上年最后一天
*/
int monthDay = monthDay($a.data_date);
log.debug("monthDay = {}", monthDay);
if (monthDay <= 430) {
/*If CURRENT_DATE in (当年的0101-0430含) then Corp_quar_date_1=上年最后一天 Corp_quar_date_2=上上年最后一天*/
$a.corp_quar_date_1 = (Integer.parseInt($a.data_date.substring(0,4)) -1) + "1231";
$a.corp_quar_date_2 = $a.corp_quar_date_1;
} else {
/*CURRENT_DATE in (当年的1101-1231含) then Corp_quar_date_1=当年930 Corp_quar_date_2=当年930*/
$a.corp_quar_date_1 = $a.data_date.substring(0,4) + "0930";
$a.corp_quar_date_2 = $a.corp_quar_date_1;
}
log.debug("$a.corp_quar_date_1 = {}, corp_quar_date_2={}", $a.corp_quar_date_1, $a.corp_quar_date_2);
具体点,错误的写法:
//这个可以
if (a < 1) {
//这里不行
log.debug("true);
}
正确的写法:
//这个可以
if (a < 1) {
/*这样注释就对了*/
log.debug("true);
}