Drools 6.0.3规则 - 为什么这段代码在无限循环中运行?

时间:2016-04-16 21:28:09

标签: java eclipse drools rules expert-system

我正试图在病人诊断时显示一些消息。显示这个的规则是“Imprime diagnosticos” 这里的问题是这段代码没有显示任何打印消息......并且在无限循环中运行。

package com.sample

rule "sem sintomas 1"
salience 100
when
    $p : Paciente()
    forall($s : Sintomas(idPessoa != $p.getId()))
then
    $p.setDiagnostico("sem sintomas definidos");
    update($p);
end

rule "sem sintomas 2"
salience 90
when
    $s1 : Sintomas(sintoma == "dores")
    $s2 : Sintomas(sintoma == "manchas")
    $p : Paciente(id == $s1.getIdPessoa(), id == $s2.getIdPessoa(), diagnostico == "")
then
    $p.setDiagnostico("sem sintomas definidos");
    update($p);
end

rule "gripe"
salience 60
when
    $s1 : Sintomas(sintoma == "febre")
    $s2 : Sintomas(sintoma == "dores")
    $p : Paciente(id == $s1.getIdPessoa(), id == $s2.getIdPessoa(), diagnostico == "")
then
    $p.setDiagnostico("gripe");
    update($p);
end

rule "enxatema 1"
salience 80
when
    $s1 : Sintomas(sintoma == "febre")
    $s2 : Sintomas(sintoma == "dores")
    $s3 : Sintomas(sintoma == "manchas")
    $p : Paciente(id == $s1.getIdPessoa(), id == $s2.getIdPessoa(), id == $s3.getIdPessoa(), diagnostico == "", idade >= 15)
then
    $p.setDiagnostico("enxatema");
    update($p);
end

rule "enxatema 2"
salience 70
when
    $s1 : Sintomas(sintoma == "febre")
    $s2 : Sintomas(sintoma == "manchas")
    $p : Paciente(id == $s1.getIdPessoa(), id == $s2.getIdPessoa(), diagnostico == "", idade < 15)
then
    $p.setDiagnostico("enxatema");
    update($p);
end

rule "resfriado"
salience 50
when
    $s1 : Sintomas(sintoma == "febre")
    $p : Paciente(id == $s1.getIdPessoa(), diagnostico == "")
then
    $p.setDiagnostico("resfriado");
    update($p);
end

rule "Imprime diagnosticos"
salience 40
when
    $s : Sintomas()
    $p : Paciente(id == $s.getIdPessoa(), diagnostico != "")
then
    System.out.println("Paciente " + $p.getNome() + " com diagnóstico: " + $p.getDiagnostico());
end

这是插入事实的Java程序:

package com.sample;

import org.kie.api.KieServices;
import org.kie.api.runtime.KieContainer;
import org.kie.api.runtime.KieSession;

public class DroolsTest {

public static final void main(String[] args) {
    try {
        // load up the knowledge base
        KieServices ks = KieServices.Factory.get();
        KieContainer kContainer = ks.getKieClasspathContainer();
        KieSession kSession = kContainer.newKieSession("ksession-rules");

        // go !
        Paciente p1 = new Paciente("001", "Ana Melo", "", 33);
        Paciente p2 = new Paciente("002", "Rui Costa", "", 13);
        Paciente p3 = new Paciente("003", "Joana Martins", "", 85);
        Paciente p4 = new Paciente("004", "Pedro Torres","", 53);
        Paciente p5 = new Paciente("005", "Ana Gomes", "", 93);
        Paciente p6 = new Paciente("006", "Jorge Costa", "", 56);

        Sintomas s1 = new Sintomas("001", "febre");
        Sintomas s2 = new Sintomas("001", "dores");
        Sintomas s3 = new Sintomas("001", "manchas");
        Sintomas s4 = new Sintomas("002", "febre");
        Sintomas s5 = new Sintomas("002", "manchas");
        Sintomas s6 = new Sintomas("003", "febre");
        Sintomas s7 = new Sintomas("003", "dores");
        Sintomas s8 = new Sintomas("004", "febre");
        Sintomas s9 = new Sintomas("006", "manchas");
        Sintomas s10 = new Sintomas("006", "dores");

        kSession.insert(p1);
        kSession.insert(p2);
        kSession.insert(p3);
        kSession.insert(p4);
        kSession.insert(p5);
        kSession.insert(p6);

        kSession.insert(s1);
        kSession.insert(s2);
        kSession.insert(s3);
        kSession.insert(s4);
        kSession.insert(s5);
        kSession.insert(s6);
        kSession.insert(s7);
        kSession.insert(s8);
        kSession.insert(s9);
        kSession.insert(s10);
        kSession.fireAllRules();
    } catch (Throwable t) {
        t.printStackTrace();
    }
}
}

public class Paciente {
  private String id, nome, diagnostico;
  private int idade;
  //...
}

public class Sintomas {
  private String idPessoa, sintoma;
  //...
}

感谢所有帮助,

1 个答案:

答案 0 :(得分:0)

您必须使用约束

来避免循环
$p : Paciente(diagnostico == "")

in rule&#34; sem sintomas 1&#34;同样。

建议:您应该使用

modify( $p ){ setDiagnostico("...") }

而不是更新。

这些id值(&#34; 001&#34;,...)很奇怪:他们没有识别症状,他们也没有识别患者,因为他们不会与症状一起存储。通常,用钥匙识别症状(例如ICD-10),并且患者将具有症状键列表。然后,规则可以根据症状的存在和不存在来确定疾病。

我不确定你的ID是什么意思。整个过程中大量使用显着性表明某些事情是不对的。您应该只能在最后一条规则中以(低)显着性来执行此操作,以便为每位患者打印结果。