Drools 如何让规则只开火一次?

Drools 如何让规则只开火一次?,drools,rules,rule-engine,kie,Drools,Rules,Rule Engine,Kie,假设以下规则侦听具有特定标识符的事件。每个标识符只能处理一个事件 rule "SampleRule" dialect "mvel" no-loop true when Event( $identifier : identifier) not(EventHandler( identifier == $identifier )) then EventHandler $h = new EventHandler(); $h.setIdentifier( $identif

假设以下规则侦听具有特定标识符的事件。每个标识符只能处理一个事件

rule "SampleRule"
dialect "mvel"
no-loop true
when
    Event( $identifier : identifier)
    not(EventHandler( identifier == $identifier )) 
then
    EventHandler $h = new EventHandler();
    $h.setIdentifier( $identifier );
    insert( $h );
end
事件类别:

...

@javax.persistence.Entity
@org.kie.api.definition.type.Role(org.kie.api.definition.type.
                         Role.Type.EVENT) // line break just for SO
public class Event implements java.io.Serializable {

  static final long serialVersionUID = 1L;

  @javax.persistence.GeneratedValue(generator = "STATUSNOTIFICATION_ID_GENERATOR", strategy = javax.persistence.GenerationType.AUTO)
  @javax.persistence.SequenceGenerator(name = "STATUSNOTIFICATION_ID_GENERATOR", sequenceName = "STATUSNOTIFICATION_ID_SEQ")
  private java.lang.Long id;

  private java.lang.String identifier;

  public Event() {
  }

  public java.lang.String getIdentifier() {
    return this.identifier;
  }

  public void setIdentifier(java.lang.String identifier) {
    this.identifier = identifier;
  }
}
通过Drools Workbench配置事件类。除了将类声明为事件外,我没有更改任何其他内容

事件插入规则引擎的方式:

KieServicesConfiguration conf = KieServicesFactory.newRestConfiguration(
            url, user, pass);
conf.setMarshallingFormat(FORMAT);   
KieServicesClient kieServicesClient = KieServicesFactory.newKieServicesClient(conf);
RuleServicesClient rulesClient = kieServicesClient.getServicesClient(RuleServicesClient.class); 
KieCommands commandsFactory = KieServices.Factory.get().getCommands();

while (hasEvent) {
  String identifier = ... // get the identifier of the event, events with same identifier possible, sometimes within same seconds, sometimes several minutes/hours in between

  Event event = new Event();
  event.setIdentifier(identifier);

  Command<?> insert = commandsFactory.newInsert(event, "notificationIdentifier");
  Command<?> fireAllRules = commandsFactory.newFireAllRules();
  Command<?> batchCommand = commandsFactory.newBatchExecution(Arrays.asList(insert, fireAllRules));

  ServiceResponse<ExecutionResults> executeResponse = rulesClient.executeCommandsWithResults("name_of_rules_package_incl_version", batchCommand);
  if (executeResponse.getType() == ResponseType.SUCCESS) {
    ...
  } else {
    ...
  }  
}                                                   
实际上,不应该要求“清除”规则来防止重复。我只是为了完整起见才在这里提到它


我希望“SampleRule”规则在每个标识符的每个事件中只触发一次。不幸的是,即使事件间隔很大,它也会多次触发。

这是
KieBase
中唯一的规则吗?您能否与我们分享事件类的定义(它是Drools事件?)以及将对象插入会话的方式?@EstebanAliverti我添加了更多详细信息。谢谢你的时间!
rule "SampleRuleClear"
dialect "mvel"
no-loop true
when
    $E : Event( $identifier : identifier)
    EventHandler( identifier == $identifier )
then
    retract( $E ); 
end