как отобразить много вопросов в приложении с графическим интерфейсом jess - PullRequest
0 голосов
/ 24 мая 2018

Я хочу сделать диагностическое приложение с графическим интерфейсом;Я новичок в Джесс.я написал книгу Jess в действии , но когда я запускаю код, у меня появляется только первый вопрос и ответ, как будто он не регистрирует ответ.я нашел ошибку в своем коде, но я не видел. Вот мой окончательный код, я переместил некоторые вопросы, чтобы здесь не было больше кода.У меня нет ошибки кода, просто она не отображает все вопросы, как я указал в правилах. Что может быть моей ошибкой?

   (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)

1 Ответ

0 голосов
/ 24 мая 2018
  1. Выкинуть все (defmodule ...)
  2. Удалить все MAIN:: и ask::.
  3. Удалить все (declare (auto-focus TRUE)).
  4. Не использовать(return) в любом правом коде.

Короче говоря, не используйте модули и (авто) фокусировку, пока не убедитесь, что понимаете, что они делают.Я не думаю, что это необходимо с этим довольно маленьким файлом clp.

Основная проблема могла бы заключаться в следующем: убедитесь, что вы используете

(вопросы ... (valid $? Valid))

всегда с $ перед $?valid или аналогичным - это мультислот.

И я думаю, вам нужно

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