【问题标题】:how to display many questions in a GUI jess application如何在 GUI jess 应用程序中显示许多问题
【发布时间】:2018-11-03 02:08:04
【问题描述】:

我想制作一个诊断 GUI 应用程序;我是杰斯的新手。我写了 Jess in action 的书,但是当我运行代码时,我只有第一个问题和答案,就像它没有注册答案一样。我在我的代码中发现了错误,但我没有看到。这是我的最终代码,我提出了一些问题,以便不要在此处添加更多代码。我没有代码错误,只是它没有像我在规则中指定的那样显示所有问题。我的错误可能是什么?

   (import javax.swing.*)
    (import java.awt.*)
    (import java.awt.event.*)

    ;; Don't clear defglobals on (reset)
    (set-reset-globals FALSE)

    (defglobal ?*crlf* = "
    ")

    ;; Question and answer templates

    (deftemplate questions
        (slot ident)
        (slot type)
        (slot texte)
        (multislot valid))

    (deftemplate reponses
        (slot texte)
        (slot ident))
    ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
    ;; Module app-rules

    (defmodule interview) 

    (defrule MAIN::questions_basique_1
        (declare (auto-focus TRUE))
     =>
     (assert (ask age)))

    (defrule MAIN::questions_basique_2
        (declare (auto-focus TRUE))
     =>
     (assert (ask poids)))

    (defrule MAIN::questions_basique_3
        (declare (auto-focus TRUE))
     =>
     (assert (ask symptome_majeur_1))) 

    (defrule MAIN::questions_basique_4
        (declare (auto-focus TRUE))
     =>
     (assert (ask symptome_majeur_1)))

    ;; Engine rules
    ;;questions pour le corps etrangé
    (defrule MAIN::request-dyspné_oui
     ;; si le patient a une dyspnée
     (reponses (ident symptome_majeur_1) (texte ?i&:(eq (str-compare ?i yes)0)))
     =>
     ;;lui posée la question si elle est brute
     (assert (ask dypnée_brute)))

     (defrule MAIN::request-dyspné_brute_oui
      (declare (auto-focus TRUE))
     ;; si le patient a une dyspnée brute
     (reponses (ident dypnée_brute) (texte ?i&:(eq (str-compare ?i yes)0)))
     =>
     ;;poser la question sur syndrome de penetration
     (assert (ask syndrome_penetration)))

    (defrule MAIN::request-syndrome_penetration_oui
     (declare (auto-focus TRUE))
     ;; si le patient a eu syndrome de pénétration
     (reponses (ident syndrome_penetration) (texte ?i&:(eq (str-compare ?i yes)0)))
     =>
     ;;poser la quetion sur des rales sibilants unilatérauxé
     (assert (ask rales_sibilant_uni)))

    (defrule MAIN::request-rales_sibilant_uni_oui
     (declare (auto-focus TRUE))
     ;; s'il a des rales sibilants unilatéreaux
     (reponses (ident rales_sibilant_uni) (texte ?i&:(eq (str-compare ?i yes)0)))
     =>
     ;;faire une radio du thorax pour définir s'il ya difference de clarté
     (assert (ask Rx_diff_clarte)))

    (defrule MAIN::request-Rx_diff_clarte_oui
     (declare (auto-focus TRUE))
     ;; s'il a des differences de clarté
     (reponses (ident Rx_diff_clarte) (texte ?i&:(eq (str-compare ?i yes)0)))
     =>
     ;;faire une bronchoscopie
     (assert (ask Bronchioscopie)))

    ;; fin de l'interview

    ;;début du diagnostique    
    (defrule MAIN::Corps-etrange
       (declare (auto-focus TRUE))
       (MAIN::reponses(ident dypnée_brute)(texte yes))
       (MAIN::reponses(ident syndrome_penetration)(texte yes))
       (MAIN::reponses(ident rales_sibilant_uni)(texte yes))
       (MAIN::reponses(ident Rx_diff_clarte)(texte yes))
       (MAIN::reponses(ident Bronchioscopie)(texte yes))
        => 
        (assert recommend-action "Corps etrangé")(halt))

    ;; Results output

    (deffunction recommend-action (?action)
      "Give final instructions to the user"
      (call JOptionPane showMessageDialog ?*frame*
            (str-cat "I recommend that you " ?action)
            "Recommendation"
            (get-member JOptionPane INFORMATION_MESSAGE)))

    (defadvice before halt (?*qfield* setText "Close window to exit"))

    ;; Module ask

    (defmodule ask)
    ;;poser une question et retourner une reponse
    (deffunction ask-patient (?question ?type ?valid)
     "Set up the GUI to ask a question"
     (?*qfield* setText ?question)
     (?*apanel* removeAll)
     (if (eq ?type multi) then
     (?*apanel* add ?*acombo*)
     (?*apanel* add ?*acombo-ok*)
     (?*acombo* removeAllItems)
     (foreach ?item ?valid
     (?*acombo* addItem ?item))
     else
     (?*apanel* add ?*afield*)
     (?*apanel* add ?*afield-ok*)
     (?*afield* setText ""))
     (?*frame* validate)
     (?*frame* repaint))

    ;;vérifier si la reponse est bien oui ou non
    (deffunction is-of-type (?answer ?type ?valid)
      "Check that the answer has the right form"
      (if (eq ?type multi) then
        (foreach ?item ?valid
                 (if (eq (sym-cat ?answer) (sym-cat ?item)) then
                   (return TRUE)))
        (return FALSE))

      (if (eq ?type number) then
        (return (is-a-number ?answer)))

      ;; plain text
      (return (> (str-length ?answer) 0)))

    (deffunction is-a-number (?value)
      (try
       (integer ?value)
       (return TRUE)
       catch 
       (return FALSE)))

    (defrule ask::ask-question-by-id
     "Given the identifier of a question, ask it"
     (declare (auto-focus TRUE))
     (MAIN::questions (ident ?id) (texte ?text)
                      (type ?type)(valid $?valid))
     (not (MAIN::reponses (ident ?id)))
     (MAIN::ask ?id)
     =>
     (ask-patient ?text ?type ?valid)
     ((engine) waitForActivations))

    (defrule ask::collect-user-input
     "Check and optionally return an answer from the GUI"
     (declare (auto-focus TRUE))
     (MAIN::questions (ident ?id) (texte ?text) (type ?type)(valid ?valid))
     (not (MAIN::reponses (ident ?id)))
     ?user <- (user-input ?input)
     ?ask <- (MAIN::ask ?id)
     =>
     (if (is-of-type ?input ?type ?valid) then
     (assert (MAIN::reponses (ident ?id) (texte ?input)))
     (retract ?ask ?user)
     (return)
     else
     (retract ?ask ?user)
     (assert (MAIN::ask ?id))))

    ;; Main window
    (defglobal ?*frame* = (new JFrame "Assistant Diagnostique en pneumonie pédiatrique"))
    (?*frame* setDefaultCloseOperation (get-member JFrame EXIT_ON_CLOSE))
    (?*frame* setSize 700 700)
    (?*frame* setVisible TRUE)

    ;; Question field
    (defglobal ?*qfield* = (new JTextArea 5 40))
    (bind ?scroll (new JScrollPane ?*qfield*))
    ((?*frame* getContentPane) add ?scroll)
    (?*qfield* setText "Please wait...")

    ;; Answer area
    (defglobal ?*apanel* = (new JPanel))
    (defglobal ?*afield* = (new JTextField 40))
    (defglobal ?*afield-ok* = (new JButton OK))

    (defglobal ?*acombo* = (new JComboBox (create$ "yes" "no")))
    (defglobal ?*acombo-ok* = (new JButton OK))

    (?*apanel* add ?*afield*)
    (?*apanel* add ?*afield-ok*)
    ((?*frame* getContentPane) add ?*apanel* (get-member BorderLayout SOUTH))
    (?*frame* validate)
    (?*frame* repaint)

    (deffunction read-input (?EVENT)
      "An event handler for the user input field"
      (assert (ask::user-input (sym-cat (?*afield* getText)))))

    (bind ?handler (new jess.awt.ActionListener read-input (engine)))
    (?*afield* addActionListener ?handler)
    (?*afield-ok* addActionListener ?handler)

    (deffunction combo-input (?EVENT)
      "An event handler for the combo box"
      (assert (ask::user-input (sym-cat (?*acombo* getSelectedItem)))))

    (bind ?handler (new jess.awt.ActionListener combo-input (engine)))
    (?*acombo-ok* addActionListener ?handler)

    (deffacts MAIN::question-data

     "questions posée par le systeme"
     (questions (ident poids) (type number)(texte "quel poids a-t-il?")(valid))
     (questions (ident age) (type number)(texte "quelle age a l'enfant?")(valid))
     (questions (ident symptome_majeur_1) (type multi)(texte "presente t-il des dyspnées sifflantes?")(valid yes no))
     (questions (ident symptome_majeur_2) (type multi)(texte "presente t-il une toux chronique?")(valid yes no))
     (questions (ident dypnée_brute) (type multi)(texte "la dyspnée est-elle brusque?")(valid yes no))
     (questions (ident rale_sibilant) (type multi)(texte "a-t-il des rales sibilants?")(valid yes no))
     (questions (ident syndrome_penetration) (type multi)(texte "y a t-il eu syndrome de pénétration?")(valid yes no))
     (questions (ident rales_sibilant_uni) (type multi)(texte "a t-il/elle des rales sibilants unilatéraux?")(valid yes no)) 
     (questions (ident Rx_diff_clarte) (type multi)(texte "la radio du thorax presente t-elle une difference de clarté des deux champs pulmonaires?")(valid yes no))
     (questions (ident Bronchioscopie) (type multi)(texte "la Bronchioscopie montre t-elle un corps étrangé?")(valid yes no))
        (ask poids))

    (reset)
    (run-until-halt)

【问题讨论】:

    标签: expert-system jess


    【解决方案1】:
    1. 扔掉所有(defmodule ...)
    2. 删除所有MAIN::ask::
    3. 删除所有(declare (auto-focus TRUE))
    4. 请勿在任何右侧代码中使用(return)

    简而言之,不要使用模块和(自动)聚焦,直到你确定你理解它在做什么。我认为这个相当小的 clp 文件没有必要。

    主要问题可能是这样的:确保使用

    (问题...(有效的 $?valid))

    始终在 $?valid 或类似名称前面加上 $ - 这是一个多槽。

    我认为你需要

    (defrule questions_basique_4
     =>
    (assert (ask symptome_majeur_2)))   ;; not _1
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-09-30
      • 2013-07-25
      • 2016-04-16
      • 1970-01-01
      • 2011-01-12
      相关资源
      最近更新 更多