Hello everyone,
I have a problem with a value coming from a text field. This value is numerical
and must be stored in a global variable because I have to use it in the entire
program.
My simplified version of the program tries to display the symbol of the product
depending on the category of the product.
I know that when I read a number using a text field I have to convert it after
I insert it using the integer function.
My question is why the rule 1 won’t fire and the rule 2 fires?
Simplified program version:
(clear *)
(import javax.swing.*)
(import java.awt.*)
(import java.awt.event.*)
(deftemplate MAIN::product(slot symbol)(slot category))
(deftemplate MAIN::insert(declare(ordered TRUE)))
(set-reset-globals FALSE)
(defglobal ?*category* = 0)
(defglobal ?*frame* = (new JFrame "Testing"))
(defglobal ?*panel* =(new JPanel))
(defglobal ?*label* = (new JLabel "Category:"))
(defglobal ?*textfield* =(new JTextField 4))
(?*panel* add ?*label*)
(?*panel* add ?*textfield*)
((?*frame* getContentPane) add ?*panel*)
(?*frame* setSize 400 100)
(?*frame* setVisible TRUE)
(?*frame* validate)
(?*frame* repaint)
(bind ?l (new jess.awt.ActionListener read-category(engine)))
(?*textfield* addActionListener ?l)
(deffunction read-category(?EVENT)
(bind ?*category* (integer(?*textfield* getText)))
(assert(insert(integer(?*textfield* getText)))))
(defrule assert
=>
(assert(MAIN::product(symbol "A")(category 1))
(MAIN::product(symbol "B")(category 2))))
(defrule rule1
(declare(auto-focus TRUE))
(MAIN::product(symbol ?s&"A")(category ?*category*))
=>
(printout t "The symbol is " ?s crlf)
(halt))
(defrule rule2
(declare(auto-focus TRUE))
(MAIN::insert 1)
=>
(printout t "Inserare 1" crlf)
(halt))
(reset)
(run-until-halt)
(facts)
If I use a simple function like this:
(deffunction read-number()
(printout t "Read the number")
(bind ?n (read))
(return ?n))
(defglobal ?*category* = (read-number))
My problem disappeared but I have to use a GUI.
I looking forward for your answer,
Ana