如何在DMN中访问列表元素?决策节点评估报错求助
DMN列表元素校验错误排查与解决
问题场景
现有DMN包含三个列表变量:taxablePremium、taxRate、taxAmount,数据示例如下:
{ "taxablePremium": [ 1000, 1500, 2000 ], "taxRate": [ 5.000, 5.000, 5.000 ], "taxAmount": [ 100, 150, 200 ] }
需求为校验用户输入的taxAmount是否等于计算值,使用的FEEL表达式如下:
for X in 1..count(taxablePremium) return if taxAmount[X] != ((taxRate[X] * taxablePremium[X]) / 100) then "Tax Amount must equal the calculated value" else "Valid"
执行时触发错误:
"dmn-evaluation-result" : { "messages" : [ { "dmn-message-severity" : "ERROR", "message" : "Error evaluating Decision node 'Check Taxable Amount': null", "message-type" : "ERROR_EVAL_NODE" } ] }
解决步骤
1. 修正列表索引起始值
DMN的FEEL语言中,列表索引默认从0开始,而非1。原表达式中1..count(taxablePremium)会生成1、2、3三个索引值,但示例列表仅包含3个元素(索引0、1、2),访问索引3时会返回null,导致表达式报错。
修正后的表达式(调整为0起始索引):
for X in 0..count(taxablePremium)-1 return if taxAmount[X] != ((taxRate[X] * taxablePremium[X]) / 100) then "Tax Amount must equal the calculated value" else "Valid"
更推荐使用FEEL的for each带位置的语法,可读性更强:
for each (premium, index) in taxablePremium return if taxAmount[index] != ((taxRate[index] * premium) / 100) then "Tax Amount must equal the calculated value" else "Valid"
2. 校验列表长度一致性
若三个列表的长度不一致,访问对应索引时也会出现null值。建议在决策节点中增加前置校验:
if count(taxablePremium) != count(taxRate) or count(taxRate) != count(taxAmount) then "All lists must have the same length" else ( for each (premium, index) in taxablePremium return if taxAmount[index] != ((taxRate[index] * premium) / 100) then "Tax Amount must equal the calculated value" else "Valid" )
3. 确认变量名拼写匹配
检查DMN中变量名是否与表达式中的完全一致:比如原描述中提到的taxPremium与数据中的taxablePremium是否为同一变量,拼写错误会导致变量引用返回null。
内容的提问来源于stack exchange,提问作者technocraft
相关产品推荐
相关产品推荐

