如果没有其他规则触发,如何在Drools撤回事件中创建“最后”规则?

时间:2018-07-19 19:25:24

标签: drools rule-engine kie

我有两个规则:一个规则生成警报消息,并将其附加到事件的字符串属性列表中(将来将有多个此类规则),另一个规则则在未生成警报的情况下撤消事件。

我使用salience设置规则优先级(“最后一个”规则应从字面上最后触发)。但是由于某些原因,规则顺序是错误的,因此警报列表仍然为空,所有事件都被撤消。请在下面查看我的示例。

MainHello.scala

import org.json4s._
import org.json4s.jackson.Serialization.write
import org.kie.api.KieServices
import org.kie.api.builder.Message
import org.kie.internal.io.ResourceFactory

object MainHello {

  implicit val formats = DefaultFormats

  val events = List(
    new Event(123L, null, null),
    new Event(456L, null, null),
    new Event(789L, null, null),
    new Event(688723L, null, null),
    new Event(12L, null, null),
    new Event(13L, null, null))


  def main(args: Array[String]): Unit = {
    val kieService = KieServices.get()
    val repository = kieService.getRepository
    val kfs = kieService.newKieFileSystem

    kfs.write(ResourceFactory.newClassPathResource("hello.drl", this.getClass))

    val kb = kieService.newKieBuilder(kfs)

    kb.buildAll

    if (kb.getResults.hasMessages(Message.Level.ERROR)) {
      throw new RuntimeException("Build Errors:\n" + kb.getResults.toString)
    }

    val kContainer = kieService.newKieContainer(repository.getDefaultReleaseId)
    val session = kContainer.newKieSession()

    events.foreach { item =>
      println("INPUT OBJECT: " + write(item))
      val facthandle = session.insert(item)
      session.fireAllRules()
      println("result object:  " + write(session.getObject(facthandle)))
    }

  }
}

Event.java

import java.util.ArrayList;
import java.util.List;

public class Event implements java.io.Serializable {

    static final long serialVersionUID = 1L;

    private java.lang.Long event_id;
    private java.lang.String alert;
    private java.util.List<java.lang.String> alerts;

    public Event() {
    }

    public java.lang.Long getEvent_id() {
        return this.event_id;
    }

    public void setEvent_id(java.lang.Long event_id) {
        this.event_id = event_id;
    }

    public java.lang.String getAlert() {
        return this.alert;
    }

    public void setAlert(java.lang.String alert) {
        if (alerts == null) {
            alerts = new java.util.ArrayList<java.lang.String>();
        }
        alerts.add(alert);
        this.alert = alert;
    }

    public java.util.List<java.lang.String> getAlerts() {
        return this.alerts;
    }

    public void setAlerts(java.util.List<java.lang.String> alerts) {
        this.alerts = alerts;
    }

    public Event(java.lang.Long event_id, java.lang.String alert,
                 java.util.List<java.lang.String> alerts) {
        this.event_id = event_id;
        this.alert = alert;
        this.alerts = alerts;
    }

}

Hello.drl

rule "alert"
when
  e: ru.test.Event(event_id == 123)
then
  modify(e) {
    setAlert("alert")
  }
end

rule "Retract" salience -1
when
  e: ru.test.Event (alerts == null || alerts.empty == true)
then
  retract(e);
end

控制台输出

INPUT OBJECT: {"event_id":12,"alert":null,"alerts":null}
result object:  null
INPUT OBJECT: {"event_id":13,"alert":null,"alerts":null}
result object:  null
INPUT OBJECT: {"event_id":123,"alert":null,"alerts":null}  // This object should not be retracted because "123" event_id generates alert
result object:  null

如您所见,所有事件都变成null

Drools版本为7.8.0.Final

我在做什么错?谢谢!

0 个答案:

没有答案