简体   繁体   English

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

[英]Drools access object inside HashMap while iterating

I have added an object from a user defined class into a HashMap. 我已经将用户定义的类中的对象添加到HashMap中。 When inserted into Drools code, I can iterate through the HashMap and get the key and value pair. 当插入Drools代码时,我可以遍历HashMap并获取键和值对。 But I cannot access the attributes inside the user class which is the value section of the HashMap. 但我无法访问用户类中的属性,这是HashMap的值部分。

This is the POJO file used to hold data. 这是用于保存数据的POJO文件。 This POJO will be inserted to the LinkedHashMap with a separate key. 此POJO将使用单独的键插入LinkedHashMap。 Currently, this key is just being generated using a simple for loop. 目前,这个密钥只是使用一个简单的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;
}




}

This is the Java code used to add values to the LinkedHashMap. 这是用于向LinkedHashMap添加值的Java代码。 I have used LinkedHashMap because I need to access the items in the correct order. 我使用了LinkedHashMap,因为我需要以正确的顺序访问项目。 The key of the HashMap is an int while the value will be a DateSet object. HashMap的键是一个int,而值将是一个DateSet对象。

outHash.put(incrementedId, new DateSet(training.getTrainingType(), training.getCompletionDate(),
                    training.getExpirationDate()));

This is the Drools rule that I'm using to handle the HashMap. 这是我用来处理HashMap的Drools规则。 The commented part in the code is how I would like to use the object inside Drools. 代码中的注释部分是我想在Drools中使用对象的方式。 " entry.getValue()" prints the DateSet object but I cannot access attributes inside it. “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

This rule does what I think you want (version 6.3.0): 此规则执行我认为您想要的(版本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

However, one wonders why you didn't insert the DateSet object as individual facts, which makes accessing and filtering quite easy. 但是,有人想知道为什么你没有将DateSet对象作为单独的事实插入,这使得访问和过滤非常容易。 The artificial numbering (incrementedId) isn't part of the data, so what's its purpose? 人工编号(incrementedId)不是数据的一部分,那么它的目的是什么?

Edit If you need to compare one DateSet object with the next (in sort order by date) you should add an ordinal attribute to DateSet and insert the DateSet objects. 编辑如果需要将一个DateSet对象与下一个DateSet对象进行比较(按日期排序),则应该向DateSet添加一个序数属性并插入DateSet对象。 Then: 然后:

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

The trick her is that the attribute ordinal enables you to select successive pairs of DateSet objects (as they were created and numbered ordered by date): it compares the first with the second, the second with the third, and so on. 她的诀窍是,属性ordinal使您能够选择连续的DateSet对象对(因为它们是按日期排序创建和编号的):它将第一个与第二个进行比较,第二个与第三个进行比较,依此类推。 - You can add trainingType == "NEW" or similar for further selection. - 您可以添加trainingType == "NEW"或类似内容以供进一步选择。

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

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