【发布时间】:2015-10-15 23:36:33
【问题描述】:
我对 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 个),并将每个事实之一添加到会话中,并通过断点和调试查看 EvaluationResults.getExclusiveRuleApplied() getter 仅被评估一次(而 setter 被调用四次,所以所有规则都适用)。
我正在使用有状态会话,下面是我的 EvaluationResults 类(遵循 drools 示例中的“状态”示例)。 我不确定我错过了什么,我没有看到任何地方添加了任何 propertyChangeListener(也没有在示例中),所以不确定这应该如何在 java 方言中工作? 也许我缺少一些规则引擎设置,我使用有状态会话和流作为事件处理模式(虽然不知道它是什么)。
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 );
}
}
很高兴听到任何想法。 谢谢!
【问题讨论】:
-
您必须
update。 stackoverflow.com/questions/14507633/… -
哦,伙计..这非常简单(尽管我确实阅读了一些手册,但一定忽略了这一点)。谢谢@MarkoTopolnik!如果您添加答案,我会接受。
标签: java drools rule-engine