admin管理员组文章数量:1289565
I use a global map in Drools to store lookup values to check against. The map is Java defined as:
Map<String,Set<String>>
and is passed to the engine as global, so that the rule (shortened to the essentials) looks like this:
import java.util.Set;
global java.util.Map lookupValues;
dialect "java"
rule "rule"
agenda-group "SYSTEM"
when
$lookup : Set() from lookupValues["KEY"]
//Alternative
//$lookup : Set() from lookupValues.get("KEY")
then
System.out.println("fail")
end
I want to use $lookup as a Set so that I can later check against other values using contains. But declaring it as a Set prevents the RHS part from being executed.
When i access die lookupValues in this way:
import java.util.*;
global java.util.Map lookupValues;
dialect "java"
rule "rule"
agenda-group "SYSTEM"
when
$rawLookup : Object() from lookupValues.get("KEY")
then
Set values = (Set)lookupValues.get("KEY");
System.out.println("Value: " + values+", "+values.getClass());
end
prints:
Value: [id3, id2, id1], class java.util.HashSet
as i expected. So, the definition of the lookup variable or the global is probably wrong. Any ideas? Thanks in advance.
I use a global map in Drools to store lookup values to check against. The map is Java defined as:
Map<String,Set<String>>
and is passed to the engine as global, so that the rule (shortened to the essentials) looks like this:
import java.util.Set;
global java.util.Map lookupValues;
dialect "java"
rule "rule"
agenda-group "SYSTEM"
when
$lookup : Set() from lookupValues["KEY"]
//Alternative
//$lookup : Set() from lookupValues.get("KEY")
then
System.out.println("fail")
end
I want to use $lookup as a Set so that I can later check against other values using contains. But declaring it as a Set prevents the RHS part from being executed.
When i access die lookupValues in this way:
import java.util.*;
global java.util.Map lookupValues;
dialect "java"
rule "rule"
agenda-group "SYSTEM"
when
$rawLookup : Object() from lookupValues.get("KEY")
then
Set values = (Set)lookupValues.get("KEY");
System.out.println("Value: " + values+", "+values.getClass());
end
prints:
Value: [id3, id2, id1], class java.util.HashSet
as i expected. So, the definition of the lookup variable or the global is probably wrong. Any ideas? Thanks in advance.
Share Improve this question asked Feb 19 at 18:12 ArtiArti 1 1 |1 Answer
Reset to default 0I'm not sure what you are trying to do with this rule. When looking at your rule there is little reason the condition would fail unless "KEY" doesn't exist. But because it's hardcoded to a literal the rule either runs or not 1 time.
But the assignment in the when
doesn't make much sense either. I think when
should be reserved for conditionals and not assignments. Besides once you decide your rule's conditions you can always perform the lookup in the then
section as you demonstrate. So I fail to see why it's important to get phrase $lookup : Set() from lookupValues["KEY"]
working. So I might rewrite it to this:
import java.util.*;
global java.util.Map lookupValues;
rule "globalLookup"
agenda-group "SYSTEM"
dialect "java"
when lookupValues.containsKey("KEY")
then
Set values = (Set)lookupValues.get("KEY")
System.out.println("Value: " + values+", "+values.getClass());
end
This rule would essentially do the same thing as the rule you wrote above, but it at least makes more sense because the when clause is a conditional, and protects the then clause from throwing a NPE. And maybe that is all you need? It's hard to give much more advice than that because your concrete problem has been abstracted by a general hypothetical situation.
本文标签: javaUsing MapsSets in DroolsStack Overflow
版权声明:本文标题:java - Using MapsSets in Drools - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741478623a2381032.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
eval($lookup contains $lookup)
seems like a no-op. How could that possibly return anything but true? So what are you trying to accomplish with that expression? Then$lookup : Set() from lookupValues["systematic"]
seems like you're only trying to assign a variable that's a Set. You probably don't need it. I could make some suggestions, but not until I understand what theeval()
expression is trying to do – chubbsondubs Commented Feb 24 at 16:36