简体   繁体   中英

Drools Rule depending on Knowledge from JDK Map (not within non-JDK Class)

I'm trying to write the below Rule, which depends on Knowledge provided in the below main that uses just a JDK [library] Map (instead of a Map within a non-library Class). But it doesn't seem to be working...

Main method:

Map<String, Float> mapa = new HashMap<String, Float>();
mapa.put("Height", (float)1.73);
mapa.put("Weight", (float)79.0);
mapa.put("BMI", mapa.get("Weight") /
                (mapa.get("Height") * mapa.get("Height")));
ksession.insert(mapa);

rule.drl:

rule "Low BMI"
    when
        $map : (Map(values("BMI")) < 18.0)
    then 
        System.out.println("You have a low BMI");
end

I want to compare the value of BMI inside of rule precondition, if this condition is true, so I want to show the message below.

What is wrong?

You should write something like:

when
    $map: Map(this["BMI"] < 18)
then

It should work :)

In general, see Drools doc 7.8.3.3.10. List and Map access section...

FWIW: Even though this below more-simplified syntax is not mentioned in that reference, I have anecdotally found [at least with Drools 6.2.0 ] that this works (at least for Map<String, Boolean> , within the ObjectUnderSpecification ):

rule "you rule name"
when
  ObjectUnderSpecification(mapKey == true)
then
  // RHS code
end

I have tested that to work. Apparently the name of the Map that key is within doesn't even need to be referenced... I guess as long as some Map within ObjectUnderSpecification has the key, then that is what gets further evalutated. Please comment or edit in any reference if you find one that reflects what I've anecdotally found above.

(I don't know if this could work similarly with nested Maps).

I would imagine that this type of syntax might also work with the Map<String, Float> from the Question here, something like this (but I haven't tested this one):

when
  $map: Map(BMI < 18)
then

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM