;; Jess code to keep track of the currently firing rule ;; ;; This code may be useful in building simple explanation systems ;; ;; This code installs a user function to act as a listener to ;; DEFRULE_FIRED JessEvents. It sets a global variable, ;; ?*current-rule-name*, to be the string representation of the firing ;; rule's name. ***This variable's value is only "current" within the ;; code of a defrule*** ;; ;; Written September 2002 by Michael Stiber, from example code in the ;; Jess documentation, section 4.10.1 ;; Example of use to automatically set creating rule for asserted facts: ;; (deftemplate afact ;; (slot a) ;; (slot support (default-dynamic (get-current-rule-name)))) (import jess.JessEvent) (import jess.JessEventAdapter) (defglobal ?*current-rule-name* = "") ; Sometimes, a function is needed (deffunction get-current-rule-name () ?*current-rule-name*) ; This is the function we want to install to handle DEFRULE_FIRED events. ; JessEvent.getObject() returns the object of interest in an event, which ; in this case is an Activation. Activation.getRule() returns the active ; Rule, and Rule.getName() returns the rule's name as a String. (deffunction set-current-rule-name (?evt) (if (eq (get-member JessEvent DEFRULE_FIRED) (get ?evt type)) then (bind ?*current-rule-name* (call (call (call ?evt getObject) getRule) getName)))) ; We add the function using JessEventAdapter (call (engine) addJessListener (new JessEventAdapter set-current-rule-name (engine))) ; We make sure that DEFRULE_FIRED events will be produced by setting that ; bit in Rete.eventMask (set (engine) eventMask (bit-or (get (engine) eventMask) (get-member JessEvent DEFRULE_FIRED)))