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
  • I saw you tried to reply to my answer. Update your question with some of those details. But, after seeing that I still have more questions about what you're trying to go for here. 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 the eval() expression is trying to do – chubbsondubs Commented Feb 24 at 16:36
Add a comment  | 

1 Answer 1

Reset to default 0

I'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