是否可以 运行 Drools 动态规则?

is it possible to run Drools rules dynamically?

我的 drl 文件中有以下内容

rule 1
  when
    ...
  then
    ...
end

rule 2
  when
    ...
  then
    ...
end

....

rule 40
  when
    ...
  then
    ...
end

我想做的是按照我想要的顺序执行规则。例如 首先是 40,然后是 39 第二……等等。 我读过有关显着性的内容,但显着性的价值很难确定。我怎样才能实现按我想要的顺序阅读规则?

Drools 支持动态显着性。您可以根据传入数据中的任何表达式来定义它:

勾选 Role Attributes doc

但基本上您可以定义一个全局变量(数组或映射),您将其注入到上下文中,然后基于该变量定义显着性。例如:

rule 1
  salience( ruleOrders[1] )
  when
    ...
  then
    ...
end

rule 2
  salience( ruleOrders[2] )
  when
    ...
  then
    ...
end

....

rule 40
  salience( ruleOrders[40] )
  when
    ...
  then
    ...
end

如果您以某种方式从那里获得每条规则的顺序,您也可以使用绑定变量:

rule 1
  salience( $order )
  when
    Element( $order : order )
  then
    ...
end