按日期选择前任

时间:2019-04-09 09:36:08

标签: drools

我是Drools的新手,我正在使用Drools 7.12.0尝试验证一组看起来像的电表读数

public class MeterReading() {
    private long id;
    private LocalDate readDate;
    private int value;
    private String meterId
    private boolean valid;

/* Getters & Setters omitted */

}

作为验证的一部分,我需要通过readDate将每个MeterReading的值与其前身进行比较。

我首先尝试使用“累计”

when $mr: MeterReading()
  $previousDate: LocalDate() from accumulate(MeterReading($pdate: readDate < $mr.readDate ), max($pdate))

then
  System.out.println($mr.getId() + ":" + $previousDate);
end 

,但是随后发现它仅返回上一次抄表的日期,而不返回包含该抄表的对象的日期。然后,我尝试使用自定义

when
$mr: MeterReading()

$previous: MeterReading() from accumulate(
      $p: MeterReading(id != $mr.id),
      init( MeterReading prev = null; ),

      action( if( prev == null ||  $p.readDate < prev.readDate) {
         prev = $p;
      }),
      result(prev))
 then
  System.out.println($mr.getId() + ":" + $previous.getId() + ":" + $previous.getReadDate());

 end

但这会选择仪表读数集中最早的读数,而不是直接的前身。有人可以针对我应该做什么或要为我指示正确的方向,以便能够为每个单独的抄表选择直接的前身。

致谢

1 个答案:

答案 0 :(得分:0)

经过进一步研究,我发现这篇文章http://planet.jboss.org/post/how_to_implement_accumulate_functions曾用来编写自己的累加函数; \

public class PreviousReadFinder  implements AccumulateFunction {
@Override

public Serializable createContext() {
  return new PreviousReadFinderContext();
}

@Override
public void init(Serializable context) throws Exception {
  PreviousReadFinderContext prfc = (PreviousReadFinderContext) context;
  prfc.list.clear();
}

@Override
public void accumulate(Serializable context, Object value) {
  PreviousReadFinderContext prfc = (PreviousReadFinderContext) context;
  prfc.list.add((MeterReading) value);
}

@Override
public void reverse(Serializable context, Object value) throws Exception {
  PreviousReadFinderContext prfc = (PreviousReadFinderContext) context;
  prfc.list.remove((MeterReading) value);
}

@Override
public Object getResult(Serializable context) throws Exception {
  PreviousReadFinderContext prfc = (PreviousReadFinderContext) context;
  return prfc.findLatestReadDate();
}

@Override
public boolean supportsReverse() {
  return true;
}

@Override
public Class<?> getResultType() {
  return MeterReading.class;
}

@Override
public void writeExternal(ObjectOutput out) throws IOException {

}

@Override
public void readExternal(ObjectInput in) throws IOException,  ClassNotFoundException {

}

private static class PreviousReadFinderContext implements Serializable {
    List<MeterReading> list = new ArrayList<>();

public Object findLatestReadDate() {

Optional<MeterReading> optional =  list.stream().max(Comparator.comparing(MeterReading::getReadDate));
if (optional.isPresent()) {
  MeterReading to = optional.get();
  return to;
}
  return  null;
}
 }
}

现在我的规则是

rule "Opening Read With Previous"

  dialect "mvel"

  when $mr: MeterReading()
       $pmr: MeterReading() from accumulate($p: MeterReading(readDate < $mr.readDate ), previousReading($p))

then
  System.out.println($mr.getId() + ":" + $pmr.getMeterReadDate());
end

如何编写规则以选择集合中没有先前读数的最脏的抄表?