RE: JESS: execute a function of the LHS of a rule

2004-10-26 Thread Jason Morris
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]




JESS: execute a function of the LHS of a rule

2004-10-25 Thread Giovanni Crudele
Hi, 

I am pretty new to jess and I would be very happy if
someone could explain: what I am trying to do is
syntactically wrong or jess is not design to do the
following.
I have used the example of the book 
DimmerSwitchDynamic.
I defined a new method getBrightness in the java bean
DimmerSwitchDynamic. What I am try to do is to fire
the rule if the returning value of the method of the
java bean getBrightness is equal to the slot value pv
of template previous-value.  
Does jess allow executing functions or methods on the
left hand side of a rule? And if that is the case,
how?
Thank you in advance for your help 


Best regards,
Giovanni


**
CODE*
(clear)
(watch all)
;Defining the class and link it to the 
DimmerSwitchDynamic java bean
(defclass dimmer DimmerSwitchDynamic)
(bind ?ds (new DimmerSwitchDynamic))
(definstance dimmer ?ds )


;Test the methods of the DimmerSwitchDynamic java bean

(call ?ds setBrightness 20)
(bind ?x (call ?ds getBrightness))
(printout t The getBrightness:  ?x crlf)
;(printout t The getBrightness  (bind ?x (call
?ds getBrightness))crlf)

; Define a template previous-value in order to compare
with Brigthtness value of the java bean 
(deftemplate previous-value 
(slot pv)) 

(assert (previous-value  (pv 20)))

(facts)

;This rule should only be triggered if the value of
the java bean DimmerSwitchDynamic is equal to the 
;value of the slot pv defined in the previous-value
template 
(defrule check-brightness
it triggers if the brightness is = 20
(previous-value (pv ?n:( eq (call ?ds getBrightness)
?n)))
=
(printout t The new value is equal to the previous
one   crlf))

(run)

** ERROR MESSAGE
**


Jess, the Rule Engine for the Java Platform
Copyright (C) 2004 Sandia Corporation
Jess Version 7.0a1 9/17/2004

This copy of Jess will expire in 11 day(s).
 == f-0 (MAIN::dimmer (brightness 0) (class
External-Address:java.lang.Class) (OBJECT
External-Address:DimmerSwitchDynamic))
 = f-0 (MAIN::dimmer (brightness 20) (class
External-Address:java.lang.Class) (OBJECT
External-Address:DimmerSwitchDynamic))
The getBrightness: 20
 == f-1 (MAIN::previous-value (pv 20))
f-0   (MAIN::dimmer (brightness 20) (class
External-Address:java.lang.Class) (OBJECT
External-Address:DimmerSwitchDynamic))
f-1   (MAIN::previous-value (pv 20))
For a total of 2 facts.
Jess reported an error in routine HasLHS.addPattern.
  Message: Variable used before definition: ds.
  Program text: ( defrule check-brightness it
triggers if the brightness is = 20 ( previous-value (
pv ?n  : ( eq ( call ?ds getBrightness ) ?n ) ) ) = 
( printout t The new value is equal to the previous
one  crlf ) )  at line 29.





___ 
Nuovo Yahoo! Messenger: E' molto piy divertente: Audibles, Avatar, Webcam, Giochi, 
Rubrica Scaricalo ora! 
http://it.messenger.yahoo.it



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]