Drools 循环触发

标签 drools

如果执行,简单示例无限循环。
如果我添加“no-loop true”,它会起作用。但为什么?没有循环孔...

package spikes;

import org.springframework.roo.addon.javabean.RooJavaBean;

@RooJavaBean
public class Applicant {
private final String string;
private final int age;
public boolean valid=true;

public Applicant(String string, int i) {
    this.string = string;
    this.age = i;
}
}

DroolsSpikeTest
package spikes;

import static org.junit.Assert.*;

import org.drools.KnowledgeBase;
import org.drools.runtime.StatelessKnowledgeSession;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"classpath:**/applicationContext-drools.xml"})
public class DroolsSpikeTest {

    @Autowired
    private KnowledgeBase kbase;

    @Test
    public void testspikeDroolRule() {
        StatelessKnowledgeSession ksession = kbase.newStatelessKnowledgeSession();
        Applicant a = new Applicant("Mr John Smith", 16);
        assertTrue(a.isValid());
        ksession.execute(a);
        assertFalse(a.isValid());
    }

}

applicationContext-drools.xml
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!--INFO: http://architects.dzone.com/articles/drools-51-expands-spring -->
<beans xmlns="http://www.springframework.org/schema/beans" 
xmlns:aop="http://www.springframework.org/schema/aop" 
xmlns:context="http://www.springframework.org/schema/context" 
xmlns:jee="http://www.springframework.org/schema/jee" 
xmlns:tx="http://www.springframework.org/schema/tx" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns:drools="http://drools.org/schema/drools-spring" 
xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd         http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd         http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.1.xsd         http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd http://drools.org/schema/drools-spring http://anonsvn.jboss.org/repos/labs/labs/jbossrules/trunk/drools-container/drools-spring/src/main/resources/org/drools/container/spring/drools-spring-1.0.0.xsd">

<drools:kbase id="kbase1">
   <drools:resources>
    <drools:resource id="licenseRule" type="DRL" source="classpath:spikes/licenseApplication.drl"/>
   </drools:resources>
   <drools:configuration>
       <drools:mbeans enabled="true" />
   </drools:configuration>
</drools:kbase>
<drools:ksession kbase="kbase1" type="stateless" id="ksessionStateless" name="stateless1" >
</drools:ksession>
</beans>

许可证申请.drl
import spikes.Applicant
rule "Is of valid age" when
    $a : Applicant( age < 18 )
then
    modify( $a ) { valid = false };
end

最佳答案

modify($a) 开头的行告诉引擎申请人事实已更新。这会导致引擎重新评估所有规则条件。由于申请人的年龄仍未满 18 岁,这将导致此规则再次激活。冲洗并重复,永远。

添加 no-loop告诉引擎规则的结果不应导致相同的规则重新激活。请注意 no-loop不是对抗无限循环的 Elixir ;如果规则 A 的结果导致规则 B 激活,而规则 B 的结果导致规则 A 激活,no-loop不会阻止无限循环。

个人认为no-loop是绕过编写更好条件的作弊。对此更好的解决方案是以这样一种方式编写您的条件,即它们在不应该重新激活时不会重新激活。有时这涉及插入您的条件引用的“控制事实”,但您通常可以简单地更新模式以使其更具体。在您的情况下,解决方案很简单:

rule "Is of valid age" when
    $a : Applicant( age < 18, valid != false )
then
    modify( $a ) { valid = false };
end

通过添加 valid != false对于您的模式,事实的修改不会导致此规则重新激活。

关于Drools 循环触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8942533/

相关文章:

web-applications - 在Web应用程序中使用Drools Expert/Flow的最佳实践

java - 在 drools 中运行 helloworld 时出现空指针异常

java - 流口水规则中的特殊字符

java - forall 总是评估为 true [Drools]

java - Drools 集成与传统方式

Drools : Tool for generating . drl 文件或基于 XML 的规则内容

drools - Drools 规则可以有多重继承吗

java - Drools 6.0.1 - 未从 JAR 中选取规则

artificial-intelligence - 用启发式方法解决数独问题 : a good idea?

java - Drools 规则中的全局变量