简体   繁体   English

Drools 规则取决于来自 JDK Map 的知识(不在非 JDK 类中)

[英]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).我正在尝试编写以下规则,这取决于以下main中提供的知识,该知识仅使用 JDK [库] 映射(而不是非库类中的映射)。 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:规则.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.我想比较规则前提条件中 BMI 的值,如果这个条件为真,那么我想显示下面的消息。

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.通常,请参阅 Drools 文档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 ): FWIW:尽管该参考文献中没有提到下面更简化的语法,但我偶然发现 [至少在 Drools 6.2.0 ] 这是有效的(至少对于Map<String, Boolean> ,在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.显然,键所在的 Map 的名称甚至不需要被引用......我想只要ObjectUnderSpecification中的某些Map 具有键,那么这就是进一步评估的内容。 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):我想这种类型的语法也可能适用于问题here中的Map<String, Float> ,类似这样(但我还没有测试过这个):

when
  $map: Map(BMI < 18)
then

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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