迭代时,在HashMap内部调用访问对象

Dil*_*ama 5 java oop drools hashmap

我已经将用户定义的类中的对象添加到HashMap中.当插入Drools代码时,我可以遍历HashMap并获取键和值对.但我无法访问用户类中的属性,这是HashMap的值部分.

这是用于保存数据的POJO文件.此POJO将使用单独的键插入LinkedHashMap.目前,这个密钥只是使用一个简单的for循环生成.

package com.sample.pojos;

import java.util.Date;

public class DateSet {

public DateSet() {
    // TODO Auto-generated constructor stub
    super();
}

public DateSet(String trainingType, Date completedDate, Date expirationDate) {
    super();
    this.trainingType = trainingType;
    this.completedDate = completedDate;
    this.expirationDate = expirationDate;
}

private String trainingType;
private Date completedDate;
private Date expirationDate;



public String getTrainingType() {
    return trainingType;
}
public void setTrainingType(String trainingType) {
    this.trainingType = trainingType;
}
public Date getCompletedDate() {
    return completedDate;
}
public void setCompletedDate(Date completedDate) {
    this.completedDate = completedDate;
}
public Date getExpirationDate() {
    return expirationDate;
}
public void setExpirationDate(Date expirationDate) {
    this.expirationDate = expirationDate;
}




}
Run Code Online (Sandbox Code Playgroud)

这是用于向LinkedHashMap添加值的Java代码.我使用了LinkedHashMap,因为我需要以正确的顺序访问项目.HashMap的键是一个int,而值将是一个DateSet对象.

outHash.put(incrementedId, new DateSet(training.getTrainingType(), training.getCompletionDate(),
                    training.getExpirationDate()));
Run Code Online (Sandbox Code Playgroud)

这是我用来处理HashMap的Drools规则.代码中的注释部分是我想在Drools中使用对象的方式."entry.getValue()"打印DateSet对象,但我无法访问其中的属性.

rule "Validate test"
agenda-group "validate_init"
    when
        someClass: SomeClass($tMap : outHash)                       
        entry : Entry($valueV : value) from $tMap.entrySet()  

        //Boolean(booleanValue == true) from ($valueV.getTrainingType() == "NEW")       

    then
    //System.out.println($valueV.getTrainingType());
    System.out.println(entry.getKey() + "-" + entry.getValue());
 end
Run Code Online (Sandbox Code Playgroud)

lau*_*une 3

这条规则做了我认为你想要的(版本 6.3.0):

rule "Validate test"
when
    SomeClass($tMap : outHash)                       
    e: Map.Entry(k:key, v:value) from $tMap.entrySet()
    DateSet( tt: trainingType == "NEW" ) from v 
then
    System.out.println(e.getKey() + "-" + tt);
end
Run Code Online (Sandbox Code Playgroud)

然而,有人想知道为什么不将 DateSet 对象作为单独的事实插入,这使得访问和过滤变得非常容易。人工编号(incrementedId)不是数据的一部分,那么它的目的是什么?

编辑 如果您需要将一个 DateSet 对象与下一个进行比较(按日期排序),您应该向 DateSet 添加一个序数属性并插入 DateSet 对象。然后:

rule "check for overdue training"
when
    $ds1: DateSet( $ord1: ordinal, $exp: expirationDate )
    $ds2: DateSet( ordinal == $ord1+1, completedDate > $exp )
then
    // ds2 is too late
end
Run Code Online (Sandbox Code Playgroud)

她的技巧在于,该属性ordinal使您能够选择连续的 DateSet 对象对(因为它们是按日期创建和编号的):它将第一个与第二个进行比较,第二个与第三个进行比较,依此类推。- 您可以添加trainingType == "NEW"或类似以供进一步选择。