[英]How to compare and set exact collection in java and drools?
I have a java class as below 我有一个如下的Java类
public class Ball
{
private int size;
private String brand;
private ArrayList<String> colour;
private HashMap<String, String> price;
//getter and setters
}
I want to create Drools rule (java dialect) to read the size, brand and colour and set price map accordingly. 我想创建Drools规则(Java方言)以读取大小,品牌和颜色并相应地设置价格图。
colour ArrayList should match exact eg if a ball has Red, Black & Blue colour together, then a particular rule should apply. color ArrayList应该精确匹配,例如,如果一个球同时具有红色,黑色和蓝色 ,则应遵循特定规则。
price map eg [{"US", "1.79"},{"UK", "1.48"},{"UAE", "11.37"}] . 价格地图,例如[{“ US”,“ 1.79”},{“ UK”,“ 1.48”},{“ UAE”,“ 11.37”}] 。 Don't confuse this with json, it is a java HashMap.
不要将此与json混淆,它是一个Java HashMap。
rule "rule 1"
when
ball : Ball
(
size == 14,
brand == "NIVIA",
colour == //here i want to compare exact match of the arraylist.
)
then
ball.setPrice(//here i want to set price map);
end
Please help to create the .drl file 请帮助创建.drl文件
First of all, we should question the use of a List<String>
for holding a number of colours. 首先,我们应该质疑使用
List<String>
来容纳多种颜色。 Is order really important? 订单真的很重要吗? It's possible that you want to distinguish a red ball with green and blue dots from a white ball with purple stripes, but even so "order" alone won't be able to express all subtleties.
您可能希望将带有绿色和蓝色点的红色球与带有紫色条纹的白色球区分开,但是即使如此,仅凭“顺序”也无法表达所有的微妙之处。 Point is,
List.equals
(and also ==
in Drools) is defined to return true if and only if elements are equal, one by one . 点是,
List.equals
(以及==
在Drools中)被定义为返回true当且仅当元素是相等的, 一个接一个 。
The same problem arises if you use a String[]
and call Arrays.equals()
. 如果使用
String[]
并调用Arrays.equals()
,则会出现相同的问题。
I think the best solution would be to use a 我认为最好的解决方案是使用
private Set<String> colour;
and write the rule as 并将规则写为
rule "rule 1"
when
ball: Ball( size == 14, brand == "NIVIA",
colour == new HashSet( Arrays.asList("Red", "Black", "Blue") ) )
then
ball.setPrice(...);
end
Your Java application might provide a Ball method: 您的Java应用程序可能提供Ball方法:
public boolean hasColours( String... colours )
which would simplify the notation in the rule: 这将简化规则中的表示法:
ball: Ball( size == 14, brand == "NIVIA",
ball.hasColours("Red", "Black", "Blue") )
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.