I think that Giovanni Crudele wrote:
>> Does jess allow executing functions or methods on the
>> left hand side of a rule? And if that is the case, how?

Hi Giovanni,

One of the basic principles of Jess is that the LHS of rules is reserved for
fact patterns and the RHS is where methods and functions belong.  Jess's
pattern matching capability is very robust, so any time that you find
yourself wanting to write a function to evaluate some part of a pattern, ask
yourself if you are missing a much simpler constraint or conditional
expression.

For what you want to do, it seems a whole lot simpler to look for both
patterns on the LHS -- recall that Jess places an implicit logical AND
between all LHS patterns.  Try this code and tell me if it works for you.

// DimmerSwitch.java
// Almost verbatim from JIA pp.92-93
// I placed it in C:/Jess70a2 and compiled it there for simplicity
import java.io.Serializable;
import java.beans.*;

public class DimmerSwitch implements Serializable {

  private PropertyChangeSupport pcs = new PropertyChangeSupport(this);
  private int brightness = 0;

  public DimmerSwitch() {
  }

  public void setBrightness(int b) {
    int old = brightness;
    brightness = b;
    pcs.firePropertyChange("brightness", new Integer(old), new Integer(b));
  }

  public int getBrightness() {
    return this.brightness;
  }

  public void addPropertyChangeListener(PropertyChangeListener p) {
    pcs.addPropertyChangeListener(p);
  }

  public void removePropertyChangeListener(PropertyChangeListener p) {
    pcs.removePropertyChangeListener(p);
  }

}


;; ========= dimmer.clp ==========
(clear)
(watch all)
;; Deftemplates
(deftemplate previous-value
  (slot pv))

;; Defclasses
(defclass dimmer DimmerSwitch)
(ppdeftemplate dimmer)


;; Defrules
(defrule notify-on-brightness-change
"Notifies a user on change of brightness"
  ;; Notice the use of patterns here not function calls
  (declare (auto-focus TRUE))
  (previous-value (pv ?pv))
  ;; The shadow fact acts just like a regular Jess fact!  :-D
  (dimmer (brightness ?b&:(neq ?pv ?b)))
  =>
  (printout t "A change in brightness has occurred!" crlf)
  (printout t "New Brightness = " ?b crlf))

;; Program
(reset)
(bind ?ds (new DimmerSwitch))
(definstance dimmer ?ds)
(call ?ds setBrightness 20)
(bind ?b2 (call ?ds getBrightness))
(assert (previous-value (pv ?b2)))
(call ?ds setBrightness 35)
(run)
;; ================================

Jess prints out...

Jess> (batch ds.clp)
MAIN::notify-on-brightness-change: +1+1+1+2+t
 ==> Focus MAIN
 ==> f-0 (MAIN::initial-fact)
 ==> f-1 (MAIN::dimmer (brightness 0)
        (class <External-Address:java.lang.Class>)
        (OBJECT <External-Address:DimmerSwitch>))
 <=> f-1 (MAIN::dimmer (brightness 20)
        (class <External-Address:java.lang.Class>)
        (OBJECT <External-Address:DimmerSwitch>))
 ==> f-2 (MAIN::previous-value (pv 20))
 <=> f-1 (MAIN::dimmer (brightness 35)
        (class <External-Address:java.lang.Class>)
        (OBJECT <External-Address:DimmerSwitch>))
==> Activation: MAIN::notify-on-brightness-change :  f-2, f-1
FIRE 1 MAIN::notify-on-brightness-change f-2, f-1
A change in brightness has occurred!
New Brightness = 35
 <== Focus MAIN
1
Jess>

Let me know if this helps!
Cheers, Jason
----------------------------------------------
 Jason Morris - Assistant Moderator
 Jess Listserver - [EMAIL PROTECTED]
 email: [EMAIL PROTECTED]
 www.morristechnicalsolutions.com
 fax/phone: 503.692.1088





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