我对 drools 还很陌生(使用 6.2),但被一个看似简单的问题难住了。
问题: 我有多个规则,最多应应用其中一个。换句话说,一旦应用了其他规则,就不应再将其“when”子句评估为 true。
package xyz
import xyz.EvaluationResults;
import xyz.ExclusivenessTestFact;
rule "ExclusivenessTest Rule 1"
salience 10
when
$exclusivenessFact : ExclusivenessTestFact()
$evaluationResults : EvaluationResults(exclusiveRuleApplied == false)
then
$evaluationResults.addResult("TR1","TR1");
$evaluationResults.setExclusiveRuleApplied(true);
end
该规则的想法是,在应用第一个规则后,它将 ExclusiveRuleApplied 标志设置为 true,因此正在评估的其他规则不应再评估为 true。 (不确定这是否是最好的方法,但这是我能想到的最好的方法)
我有多个这些规则(现在有 4 个),并将每个事实之一添加到 session 中,并通过断点和调试查看EvaluationResults.getExclusiveRuleApplied() getter 只评估一次(而 setter 被调用四次,因此所有规则均适用)。
我正在使用有状态 session ,下面是我的EvaluationResults 类(遵循 drools 示例中的“State”示例)。 我不确定我错过了什么,我没有看到任何 propertyChangeListener 被添加到任何地方(也没有在示例中),所以不确定这在 java 方言中应该如何工作? 也许我缺少一些规则引擎设置,我使用有状态 session 和流作为事件处理模式(但并不真正知道它是什么)。
package xyz
import java.beans.PropertyChangeSupport;
import java.beans.PropertyChangeListener;
import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
import java.util.Map;
public class EvaluationResults
implements java.io.Serializable
{
private Boolean exclusiveRuleApplied;
private Map<String,Object> results;
private final PropertyChangeSupport changes = new PropertyChangeSupport(this);
public EvaluationResults()
{
// default constructor
}
public Object addResult(final String key, final Object result)
{
return results.put(key, result);
}
public void setExclusiveRuleApplied(Boolean exclusiveRuleApplied)
{
Boolean oldState = this.exclusiveRuleApplied;
this.exclusiveRuleApplied = exclusiveRuleApplied;
// each fact change gets propagated via Java Beans PropertyChangeEvents
this.changes.firePropertyChange( "exclusiveRuleApplied",
oldState,
exclusiveRuleApplied );
}
public Boolean getExclusiveRuleApplied()
{
return exclusiveRuleApplied;
}
public void setResults(Map<String,Object> results)
{
Map<String,Object> oldState = this.results;
this.results = results;
// each fact change gets propagated via Java Beans PropertyChangeEvents
this.changes.firePropertyChange( "results",
oldState,
results );
}
public Map<String,Object> getResults()
{
return results;
}
@Override
public boolean equals(Object obj)
{
return EqualsBuilder.reflectionEquals(this, obj);
}
@Override
public int hashCode()
{
return HashCodeBuilder.reflectionHashCode(this);
}
public void addPropertyChangeListener(final PropertyChangeListener l) {
this.changes.addPropertyChangeListener( l );
}
public void removePropertyChangeListener(final PropertyChangeListener l) {
this.changes.removePropertyChangeListener( l );
}
}
很高兴听到任何想法。 谢谢!
最佳答案
Drools 引擎不会自动检测您何时更改了事实。因此,您必须显式调用 update($evaluationResults)
来通知它并使其重新评估所有其他规则的条件。
关于java - drools (6.2) 事实不会在有状态 session 中重新评估,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31625056/