Drools规则在Sping Boot 控制器中不起作用,但在Junit测试中起作用

j8ag8udp  于 2022-11-11  发布在  其他
关注(0)|答案(4)|浏览(128)

我第一天学流口水的时候。我有个奇怪的问题。
drl中的规则“Hello World”无法在控制器中运行,但在Junit测试用例中运行良好。规则“另一个规则”始终在控制器和Junit测试中运行。
控制器和junit测试器中的代码完全相同。
任何有想法的人都是受欢迎的。谢谢。

  • 示例.drl:*
package com.happylifeplat.checkin

import com.happylifeplat.checkin.managerorder.beans.RaBean1;

rule "Hello World"
   when
       $h : RaBean1( id == 1)
   then
       $h.setContent("from drl content");
       System.out.println("-----Hello World rule called  id == 1");
end

rule "Another rule"
   when
   then
       System.out.println("-----Another rule called");
end

kmodule.xml:

<?xml version="1.0" encoding="UTF-8"?>
<kmodule xmlns="http://jboss.org/kie/6.0.0/kmodule">
    <kbase name="rules" packages="rules">
        <ksession name="ksession-rules"/>
    </kbase>
</kmodule>

RaBean1.java:

package com.happylifeplat.checkin.managerorder.beans;

public class RaBean1 {
    private int id;
    private String content;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }
}

HelloController.java:

@RestController
@RequestMapping("/hello")
public class HelloController {

    private static KieContainer kieContainer;
    private KieSession sessionStatefull = null;

    @RequestMapping(value = "/helloworld", method = RequestMethod.GET)
    @ApiOperation(value = "hello")
    public Result metadata() {
        try {
            if (kieContainer == null) {
                kieContainer = KnowledgeSessionHelper.createRuleBase();
            }
            sessionStatefull = KnowledgeSessionHelper.getStatefulKnowledgeSessionWithCallback(kieContainer, "ksession-rules");
            RaBean1 bean1 = new RaBean1();
            bean1.setId(1);
            bean1.setContent("default content");
            sessionStatefull.insert(bean1);
            sessionStatefull.fireAllRules();
            return new Result(CommonCode.sussess, bean1.getContent());
        } catch (Exception e) {
            return new Result(CommonCode.fail, null);
        }
    }
}

HelloControllerTest.java:

@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = Application.class)
@ImportResource({"classpath:spring/applicationContext.xml"})
@IntegrationTest("server.port=0")
@WebAppConfiguration
public class HelloControllerTest {
    private static final Logger log = LoggerFactory.getLogger(HelloControllerTest.class);
    private MockMvc mockMvc;
    private static KieContainer kieContainer;
    private KieSession sessionStatefull = null;

    @Before
    public void setUp() throws Exception {
//        mockMvc = MockMvcBuilders.standaloneSetup(managerOrderController).build();
    }

    @Test
    public void helloTest() throws Exception {
        if (kieContainer == null) {
            kieContainer = KnowledgeSessionHelper.createRuleBase();
        }
        sessionStatefull = KnowledgeSessionHelper.getStatefulKnowledgeSessionWithCallback(kieContainer, "ksession-rules");
        RaBean1 bean1 = new RaBean1();
        bean1.setId(1);
        bean1.setContent("default content");
        sessionStatefull.insert(bean1);
        sessionStatefull.fireAllRules();
        System.out.println("rabean.getContent---->"+bean1.getContent());
    }
}
qlfbtfca

qlfbtfca1#

最后一个朋友帮了我。就是因为“热部署”。关闭它,问题就解决了。
它位于pom.xml中:

<dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-devtools</artifactId>
        <optional>true</optional>
    </dependency>
fnvucqvd

fnvucqvd2#

我也遇到了同样的问题,我的原因是dev-tools依赖项的冲突,在删除冲突的依赖项后,它开始为我工作了。

zc0qhyus

zc0qhyus3#

我们仍然可以通过在resources文件夹中的META-INF/ www.example.com文件中添加restart.include.dools=/(drools|kie)\-.*\.jar这一行来使用dev-toolsspring-devtools.properties。

cgvd09ve

cgvd09ve4#

我删除了此依赖项,它正在工作

<dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-devtools</artifactId>
        <scope>runtime</scope>
        <optional>true</optional>
    </dependency>

相关问题