Pattern-matching is driven by changes to working memory. If no working memory elements relevant to a rule are changed, then the rule won't be evaluated and can't be activated. Global variables are not working memory elements; changes to a global variable don't cause rules to be reevaluated.

On Apr 9, 2009, at 5:52 PM, ANA TANASESCU wrote:

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



---------------------------------------------------------
Ernest Friedman-Hill
Informatics & Decision Sciences          Phone: (925) 294-2154
Sandia National Labs
PO Box 969, MS 9012                            [email protected]
Livermore, CA 94550                             http://www.jessrules.com





--------------------------------------------------------------------
To unsubscribe, send the words 'unsubscribe jess-users [email protected]'
in the BODY of a message to [email protected], NOT to the list
(use your own address!) List problems? Notify [email protected].
--------------------------------------------------------------------

Reply via email to