JESS: Fact modified and JessListener

View: New views
2 Messages — Rating Filter:   Alert me  

JESS: Fact modified and JessListener

by Nguyen, Son :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Fact modified and JessListener

Hi,

My goal is to capture fact modification events for shadow facts.
I have a Java class which I use to create a def template:

(import FactChanged)

(deftemplate FactChanged
        (declared (from-class FactChanged)
                (include-variables TRUE)))

I define the following to capture fact modification events:

(deffunction display-fact-modified-from-event (?evt)
    (bind ?ets (?evt toString))
    (bind ?et (?evt getType))
    (bind ?et1 (get ?evt type))
    (if (eq ((bit-or (JessEvent.FACT) (JessEvent.MODIFIED)) (get ?evt type)) then
    (printout t "modified fact:" (call (call ?evt getObject) getName) crlf))))

; install the above function using a JessEventAdapter
(call (engine) addJessListener
    (new JessEventAdapter display-fact-modified-from-event (engine)))

; add FACT|MODIFIED to the event mask
(set (engine) eventMask
(bit-or (get (engine) eventMask) (JessEvent.MODIFIED) (JessEvent.FACT)))

I add a new Java object

; create a new instance of FactChanged
(bind ?data (new FactChanged))
(add ?data)

I then get the shadow fact and modify it

(bind ?datafact (call (engine) getShadowFactForObject ?data))
(modify ?datafact (data "Once Upon A Time"))

I expected to receive an event for fact modified but did not receive.
Using JessDE to look at the event type I saw 16, which is JessEvent.Fact.

I can capture fact assertions but not fact modifications.
This seems to be related to the fact being a shadow fact instead of a regular fact.

The java class is simple:

public class FactChanged implements Serializable {

        private PropertyChangeSupport pcs = new PropertyChangeSupport(this);
        public String data;

        public FactChanged () {
                data = "original";
        }
        public String getData() {
                return data;
        }

        public void setData(String data) {
                String oldData = this.data;
                this.data = data;
                pcs.firePropertyChange("FactChanged ", oldData, data);
                System.out.println("DataChanged occurred");
        }

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

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


Any help will be appreciated.

Son Nguyen.



Re: JESS: Fact modified and JessListener

by Ernest Friedman-Hill :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

For beans that support PropertyChangeListeners, when you call "modify"  
Jess sets the property directly in the JavaBean. The corresponding  
fact is modified when the PropertyChangeEvent comes in. What's  
happening here is that Jess is calling setData(), which then (wrongly)  
sends out a property change event for a property named "FactChanged ".  
There's no such property, so Jess just ignores the event. The  
acceptable values for the property name are "data" (the actual name of  
the property) or null (in which case Jess will check all properties.)  
But if you use anything else, your event will be ignored, your shadow  
fact will never be updated, and then of course you'll never get the  
JessEvent.



On Oct 1, 2009, at 2:37 PM, Nguyen, Son wrote:

>
> Hi,
>
> My goal is to capture fact modification events for shadow facts.
> I have a Java class which I use to create a def template:
>
> (import FactChanged)
>
> (deftemplate FactChanged
>         (declared (from-class FactChanged)
>                 (include-variables TRUE)))
>
> I define the following to capture fact modification events:
>
> (deffunction display-fact-modified-from-event (?evt)
>     (bind ?ets (?evt toString))
>     (bind ?et (?evt getType))
>     (bind ?et1 (get ?evt type))
>     (if (eq ((bit-or (JessEvent.FACT) (JessEvent.MODIFIED)) (get ?
> evt type)) then
>     (printout t "modified fact:" (call (call ?evt getObject)  
> getName) crlf))))
>
> ; install the above function using a JessEventAdapter
> (call (engine) addJessListener
>     (new JessEventAdapter display-fact-modified-from-event (engine)))
>
> ; add FACT|MODIFIED to the event mask
> (set (engine) eventMask
> (bit-or (get (engine) eventMask) (JessEvent.MODIFIED)  
> (JessEvent.FACT)))
>
> I add a new Java object
>
> ; create a new instance of FactChanged
> (bind ?data (new FactChanged))
> (add ?data)
>
> I then get the shadow fact and modify it
>
> (bind ?datafact (call (engine) getShadowFactForObject ?data))
> (modify ?datafact (data "Once Upon A Time"))
>
> I expected to receive an event for fact modified but did not receive.
> Using JessDE to look at the event type I saw 16, which is  
> JessEvent.Fact.
>
> I can capture fact assertions but not fact modifications.
> This seems to be related to the fact being a shadow fact instead of  
> a regular fact.
>
> The java class is simple:
>
> public class FactChanged implements Serializable {
>
>         private PropertyChangeSupport pcs = new  
> PropertyChangeSupport(this);
>         public String data;
>
>         public FactChanged () {
>                 data = "original";
>         }
>         public String getData() {
>                 return data;
>         }
>
>         public void setData(String data) {
>                 String oldData = this.data;
>                 this.data = data;
>                 pcs.firePropertyChange("FactChanged ", oldData, data);
>                 System.out.println("DataChanged occurred");
>         }
>
>         public void addPropertyChangeListener(PropertyChangeListener  
> pcl) {
>                 pcs.addPropertyChangeListener(pcl);
>         }
>
>         public void  
> removePropertyChangeListener(PropertyChangeListener pcl) {
>                 pcs.removePropertyChangeListener(pcl);
>         }
> }
>
>
> Any help will be appreciated.
>
> Son Nguyen.
>
>

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





--------------------------------------------------------------------
To unsubscribe, send the words 'unsubscribe jess-users you@...'
in the BODY of a message to majordomo@..., NOT to the list
(use your own address!) List problems? Notify owner-jess-users@....
--------------------------------------------------------------------